1. spring-core 包概况
概述:
ASM: a very small and fast Java bytecode manipulation framework
ASM: 一款非常小且快的java字节码操作框架
Class-1. AnnotationVisitor
(1).Class简介:
A visitor to visit a Java annotation. The methods of this class must be called in the following order: ( visit | visitEnum | visitAnnotation | visitArray )visitEnd.
翻译:
访问Java注释的访问者。 这个类的方法必须是按以下顺序调用:(visit| visitEnum |visitAnnotation | visitArray)visitEnd。
(2). Class域:
protected final int api;
protected AnnotationVisitor av;
/**
* The ASM API version implemented by this visitor. The value of this field
* must be one of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
*/
/*该访问者实施的ASM API版本。此字段的值必须是Opcodes.ASM4或Opcodes.ASM5之一。*/
protected final int api;
/**
* The annotation visitor to which this visitor must delegate method calls.
* May be null.
*/
/*该访问者必须委托方法调用的注释访问者。
可能为null。*/
protected AnnotationVisitor av;
public AnnotationVisitor(final int api) {
this(api, null);
}
public AnnotationVisitor(final int api, final AnnotationVisitor av) {
if (api != Opcodes.ASM4 && api != Opcodes.ASM5) {
throw new IllegalArgumentException();
}
this.api = api;
this.av = av;
}
//唯二的构造函数,给两个成员赋值,且api必须。
Visits a primitive value of the annotation.//访问注释的原始值
Visits an enumeration value of the annotation.//访问注释的枚举值
Visits a nested annotation value of the annotation.//访问嵌套的注释值
/** Visits an array value of the annotation. Note that arrays of primitive
* types (such as byte, boolean, short, char, int, long, float or double)
* can be passed as value to {@link #visit visit}. This is what
* {@link ClassReader} does.
*/
//访问注释的数组值,注意原始类型可以作为值传递给visit方法。这是ClassReader类的实现方式。
visitEnd:Visits the end of the annotation.//访问annotation的结尾
(4).类总结
该类是抽象类,并且是访问注释的类的祖先,定义了一套规则,用于访问注释。
Class-2. AnnotationWriter
(1).Class简介:
An {@link AnnotationVisitor} that generates annotations in bytecode form
//一个{@link AnnotationVisitor}的子类,以字节码形式生成注释
/**
* The class writer to which this annotation must be added.
* 翻译:必须添加此注释的ClassWriter
*/
private final ClassWriter cw;
/**
* The number of values in this annotation.
* 翻译:此注释中值的数量
*/
private int size;
/**
* true if values are named, false otherwise. Annotation writers used for annotation default and annotation arrays use unnamed values.
* 翻译:如果values被命名为true,否则为false。 用于注释缺省和注解数组的注释写入器使用未命名的值。
*/
private final boolean named;
/**
* The annotation values in bytecode form. This byte vector only contains the values themselves, i.e. the number of values must be stored as a unsigned short just before these bytes.
* 翻译:注释值以字节码形式显示。 该字节向量仅包含值本身,即值的数目必须在这些字节之前存储为无符号short类型的值。
*/
private final ByteVector bv;
/**
* The byte vector to be used to store the number of values of this annotation. See {@link #bv}.
* 翻译:用于存储此注释值的数量的字节向量。
*/
private final ByteVector parent;
/**
* Where the number of values of this annotation must be stored in {@link #parent}.
* 翻译:该注释的值的数量必须存储在哪里
*/
private final int offset;
/**
* Next annotation writer. This field is used to store annotation lists.
* 翻译:下一个AnnotationWriter。 此字段用于存储注释列表。
*/
AnnotationWriter next;
/**
* Previous annotation writer. This field is used to store annotation lists.
* 翻译:上一个AnnotationWriter。 此字段用于存储注释列表。
*/
AnnotationWriter prev;
(3).Class方法
AnnotationWriter(final ClassWriter cw, final boolean named,
final ByteVector bv, final ByteVector parent, final int offset) {
super(Opcodes.ASM5);
this.cw = cw;
this.named = named;
this.bv = bv;
this.parent = parent;
this.offset = offset;
}
//唯一的构造函数,单纯的给成员变量赋值,并调用父构造函数,使父成员变量asm = ASM5, av = null;
@Override
public void visit(final String name, final Object value) {
++size;
if (named) {
bv.putShort(cw.newUTF8(name));
}
if (value instanceof String) {
bv.put12('s', cw.newUTF8((String) value));
} else if (value instanceof Byte) {
bv.put12('B', cw.newInteger(((Byte) value).byteValue()).index);
} else if (value instanceof Boolean) {
int v = ((Boolean) value).booleanValue() ? 1 : 0;
bv.put12('Z', cw.newInteger(v).index);
} else if (value instanceof Character) {
bv.put12('C', cw.newInteger(((Character) value).charValue()).index);
} else if (value instanceof Short) {
bv.put12('S', cw.newInteger(((Short) value).shortValue()).index);
} else if (value instanceof Type) {
bv.put12('c', cw.newUTF8(((Type) value).getDescriptor()));
} else if (value instanceof byte[]) {
byte[] v = (byte[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('B', cw.newInteger(v[i]).index);
}
} else if (value instanceof boolean[]) {
boolean[] v = (boolean[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('Z', cw.newInteger(v[i] ? 1 : 0).index);
}
} else if (value instanceof short[]) {
short[] v = (short[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('S', cw.newInteger(v[i]).index);
}
} else if (value instanceof char[]) {
char[] v = (char[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('C', cw.newInteger(v[i]).index);
}
} else if (value instanceof int[]) {
int[] v = (int[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('I', cw.newInteger(v[i]).index);
}
} else if (value instanceof long[]) {
long[] v = (long[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('J', cw.newLong(v[i]).index);
}
} else if (value instanceof float[]) {
float[] v = (float[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('F', cw.newFloat(v[i]).index);
}
} else if (value instanceof double[]) {
double[] v = (double[]) value;
bv.put12('[', v.length);
for (int i = 0; i < v.length; i++) {
bv.put12('D', cw.newDouble(v[i]).index);
}
} else {
Item i = cw.newConstItem(value);
bv.put12(".s.IFJDCS".charAt(i.type), i.index);
}
}
(4).类总结
Class-4. ByteVector
(1).Class简介:
/**
* A dynamically extensible vector of bytes. This class is roughly equivalent to a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient.
* 翻译:一个动态可扩展的字节向量。 此类大致相当于ByteArrayOutputStream之上的DataOutputStream,但效率更高
*/
/**
* The content of this vector.
* 翻译:向量的内容
*/
byte[] data;
/**
* Actual number of bytes in this vector.
* 翻译:byte的真实长度
*/
int length;
(3).Class方法
- 构造函数
/**
* Constructs a new {@link ByteVector ByteVector} with a default initial size.
* 翻译:用默认的大小构造一个ByteVector类
*/
public ByteVector() {
data = new byte[64];
}
/**
* Constructs a new {@link ByteVector ByteVector} with the given initial size.
* 翻译:使用给与的初始化大小构造一个新的ByteVector
* @param initialSize
* the initial size of the byte vector to be constructed.
*/
public ByteVector(final int initialSize) {
data = new byte[initialSize];
}
Puts a byte into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:把byte放入该向量。如果必要,向量会自动增大
public ByteVector putByte(final int b) {
int length = this.length;
if (length + 1 > data.length) {
enlarge(1);
}
data[length++] = (byte) b;
this.length = length;
return this;
}
Puts two bytes into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将两个byte放入向量。
Puts a short into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将short类型放入向量。
Puts a byte and a short into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将一个byte和一个short类型放入向量
Puts an int into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将一个int类型放入向量
Puts a long into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将一个long类型放入向量
Puts an UTF8 string into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将UTF8字符串放入向量
Puts an UTF8 string into this byte vector. The byte vector is automatically enlarged if necessary. The string length is encoded in two bytes before the encoded characters, if there is space for that (i.e. if this.length - i - 2 >= 0).
翻译:将UTF8字符串插入此字节向量。 如有必要,字节向量将自动放大。 字符串长度在编码字符之前的两个字节中编码,如果有空格(即如果this.length - i - 2> = 0)
Puts an array of bytes into this byte vector. The byte vector is automatically enlarged if necessary.
翻译:将byte数组放入向量
enlarge:Enlarge this byte vector so that it can receive n more bytes.
翻译:将向量扩展至能容纳更多byte
方法总结
负责将一些东西传入内置byte数组
(4).类总结
该类提供存储byte数据的功能
ClassReader
(1).Class简介:
(2). Class域:
(3).Class方法
(4).类总结
(1).Class简介:
/**
* A visitor to visit a Java class. The methods of this class must be called in the following order: visit [ visitSource ] [ visitOuterClass ] ( visitAnnotation | visitTypeAnnotation | visitAttribute ) ( visitInnerClass | visitField | visitMethod ) visitEnd.
*
* 翻译:一个访问java类的访问者。该类方法的使用顺序必须如下:visit->[visitSource]->[visitOuterClass]->(visitAnnotation|visitTyeAnnotation|visitAttribute)->(visitInnerClass|visitField|visitMethod)->visitEnd
*/
/**
* The ASM API version implemented by this visitor. The value of this field must be one of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
* 翻译:该visitor实现的ASM API版本。该值必须为Opcodes.ASM4或Opcodes.ASM5
*/
protected final int api;
/**
* The class visitor to which this visitor must delegate method calls. May be null.
* 翻译:该类方法必须委托的visitor对象。可以为null。
*/
protected ClassVisitor cv;
(3).Class方法
/**
* Constructs a new {@link ClassVisitor}.
* 翻译:构造一个新的ClassVisitor对象
*/
public ClassVisitor(final int api) {
this(api, null);
}
/**
* Constructs a new {@link ClassVisitor}.
* 翻译:构造一个新的ClassVisitor对象
*/
public ClassVisitor(final int api, final ClassVisitor cv) {
if (api != Opcodes.ASM4 && api != Opcodes.ASM5) {
throw new IllegalArgumentException();
}
this.api = api;
this.cv = cv;
}
/**
* Visits the header of the class.
* 翻译:访问对象头
* @param version
* the class version.
* @param access
* the class's access flags (see {@link Opcodes}). This parameter
* also indicates if the class is deprecated.
* @param name
* the internal name of the class (see
* {@link Type#getInternalName() getInternalName}).
* @param signature
* the signature of this class. May be null if the class
* is not a generic one, and does not extend or implement generic
* classes or interfaces.
* @param superName
* the internal of name of the super class (see
* {@link Type#getInternalName() getInternalName}). For
* interfaces, the super class is {@link Object}. May be
* null, but only for the {@link Object} class.
* @param interfaces
* the internal names of the class's interfaces (see
* {@link Type#getInternalName() getInternalName}). May be
* null.
*/
/**
* Visits the source of the class.
* 翻译:访问类的源(文件、流等)
* @param source
* the name of the source file from which the class was compiled.
* May be null.
* @param debug
* additional debug information to compute the correspondance
* between source and compiled elements of the class. May be
* null.
*/
/**
* Visits the enclosing class of the class. This method must be called only if the class has an enclosing class.
* 翻译:访问该类的外部类。该方法的前置条件是该类有外部类
* @param owner
* internal name of the enclosing class of the class.
* @param name
* the name of the method that contains the class, or
* null if the class is not enclosed in a method of its
* enclosing class.
* @param desc
* the descriptor of the method that contains the class, or
* null if the class is not enclosed in a method of its
* enclosing class.
*/
/**
* Visits an annotation of the class.
* 翻译:访问该类的一个注释
* @param desc
* the class descriptor of the annotation class.
* @param visible
* true if the annotation is visible at runtime.
* @return a visitor to visit the annotation values, or null if this visitor is not interested in visiting this annotation.
*/
/**
* Visits an annotation on a type in the class signature.
* 翻译:访问该类签名中类的注释
* @param typeRef a reference to the annotated type. The sort of this type reference must be {@link TypeReference#CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER}, {@link TypeReference#CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or {@link TypeReference#CLASS_EXTENDS CLASS_EXTENDS}. See {@link TypeReference}.
* @param typePath the path to the annotated type argument, wildcard bound, array element type, or static inner type within 'typeRef'. May be null if the annotation targets 'typeRef' as a whole.
* @param desc the class descriptor of the annotation class.
* @param visible true if the annotation is visible at runtime.
* @return a visitor to visit the annotation values, or null if this visitor is not interested in visiting this annotation.
*/
(4).类总结
(2). Class域:
(3).Class方法
(4).类总结
(2). Class域:
(3).Class方法
(4).类总结
(2). Class域:
(3).Class方法
(4).类总结
(2). Class域:
(3).Class方法
(4).类总结