ASM源码学习之ClassReader、ClassVisitor与ClassWriter详解
ASM
ASM是Java中⽐较流⾏的⽤来读写字节码的类库,⽤来基于字节码层⾯对代码进⾏分析和转换。在读写的过程中可以加⼊⾃定义的逻辑以增强或修改原来已编译好的字节码,⽐如CGLIB⽤它来实现动态代理。ASM被设计⽤于在运⾏时对Java类进⾏⽣成和转换,当然也包括离线处理。ASM短⼩精悍、且速度很快,从⽽避免在运⾏时动态⽣成字节码或转换时对程序速度的影响,⼜因为它体积⼩巧,可以在很多内存受限的环境中使⽤。
ASM的主要优势包括如下⼏个⽅⾯:
1. 它⼜⼀个很⼩,但设计良好并且模块化的API,且易于使⽤。
2. 它具有很好的⽂档,并且还有eclipse插件。
3. 它⽀持最新的Java版本。
4. 它短⼩精悍、快速、健壮。
5. 它⼜⼀个很⼤的⽤户社区,可以给新⽤户提供⽀持。
6. 它的开源许可允许你⼏乎以任何⽅式来使⽤它。
关于ASM的详细介绍可以参考:
ASM Core设计⼀览
在ASM的核⼼实现中,它主要有以下⼏个类、接⼝(在org.objectweb.asm包中):
ClassReader类:字节码的读取与分析引擎。它采⽤类似SAX的事件读取机制,每当有事件发⽣时,调⽤注册的ClassVisitor、AnnotationVisitor、FieldVisitor、MethodVisitor做相应的处理。
ClassVisitor接⼝:定义在读取Class字节码时会触发的事件,如类头解析完成、注解解析、字段解析、⽅法解析等。
AnnotationVisitor接⼝:定义在解析注解时会触发的事件,如解析到⼀个基本值类型的注解、enum值类型的注解、Array值类型的注解、注解值类型的注解等。FieldVisitor接⼝:定义在解析字段时触发的事件,如解析到字段上的注解、解析到字段相关的属性等。
MethodVisitor接⼝:定义在解析⽅法时触发的事件,如⽅法上的注解、属性、代码等。
ClassWriter类:它实现了ClassVisitor接⼝,⽤于拼接字节码。
AnnotationWriter类:它实现了AnnotationVisitor接⼝,⽤于拼接注解相关字节码。
FieldWriter类:它实现了FieldVisitor接⼝,⽤于拼接字段相关字节码。
MethodWriter类:它实现了MethodVisitor接⼝,⽤于拼接⽅法相关字节码。
SignatureReader类:对类定义、字段定义、⽅法定义、本地变量定义的签名的解析。Signature因范型引⼊,⽤于存储范型定义时的元数据(因为这些元数据在运⾏时会被擦除)。
SignatureVisitor接⼝:定义在解析Signature时会触发的事件,如正常的Type参数、类或接⼝的边界等。
SignatureWriter类:它实现了SignatureVisitor接⼝,⽤于拼接范型相关字节码。
Attribute类:字节码中属性的类抽象。
ByteVector类:字节码⼆进制存储的容器。
Opcodes接⼝:字节码指令的⼀些常量定义。
Type类:类型相关的常量定义以及⼀些基于其上的操作。
他们之间的类图关系如下:
ClassReader是ASM中最核⼼的实现,它⽤于读取并解析Class字节码。
在构建ClassReader实例时,它⾸先保存字节码⼆进制数组b,然后创建items数组,数组的长度在字节码数组的第8、9个字节指定(最前⾯4个字节是魔数CAFEBABE,之后2个字节是次版本号,再后2个字节是主版本号),每个item表⽰常量池项在字节码数组的偏移量加1(常量池中每个项由1个字节的type和紧跟的字节数组表⽰,常量池项有12种类型,其中CONSTANT_FieldRef_Info、CONSTANT_MethodRef_Info、CONSTANT_InterfaceMethodRef_Info、CONSTANT_NameAndType_Info包括其类型字节占⽤5个字节,另外4个字节每2个字节为字段、⽅法等所在的类、其名称、描述符在当前常量池中CONSTANT_Utf8_Info类型的引⽤;CONSTANT_Integer_Info、CONSTANT_Float_Info包括其类型字节占⽤5个字节,另外四个字节为其对应的值;CONSTANT_Class_Info、CONSTANT_String_Info包括其类型字节占⽤3个字节,另外两个字节为在当前常量池CONSTANT_Utf8_Info项的索引;CONSTANT_Utf8_Info类型第1个字节表⽰类型,第2、3个字节为该项所表⽰的字符串的长度);CONSTANT_Double_Info、CONSTANT_Long_Info加类型字节为9个字;maxStringLength表⽰最长的UTF8类型的常量池项的值,⽤于决定在解析CONSTANT_Utf8_Info 类型项时最⼤需要的字符数组;header表⽰常量池之后的字节码的第⼀个字节。
在调⽤ClassReader的accept⽅法时,它解析字节码中常量池之后的所有元素。紧接着常量池的2个字节是该类的access标签:ACC_PUBLIC、ACC_FINAL等;之后2个字节为当前类名在常量池CONSTANT_Utf8_Info类型的索引;之后2个字节为其⽗类名在常量池CONSTANT_Utf8_Info类型的索引(索引值0表⽰⽗类为null,即直接继承⾃Object类);再之后为其实现的接⼝数长度和对应各个接⼝名在常量池中CONSTANT_Utf8_Info类型的索引值;暂时先跳过Field和Method定义信息,解析类的attribute表,它⽤两个字节表达attribute数组的长度,每个attribute项中最前⾯2个字节是attribute名称:SourceFile(读取sourceFile值)、InnerClasses(暂时纪录起始索引)、EnclosingMethod(纪录当前匿名类、本地类包含者类名以及包含者的⽅法名和描述符)、Signature(类的签名信息,⽤于范型)、RuntimeVisibleAnnotations(暂时纪录起始索引)、Deprecated(表识属性)、Synthetic(标识属性)、SourceDebugExtension(为调试器提供的⾃定义扩展信息,读取成⼀个字符串)、RuntimeInvisibleAnnotations(暂时纪录起始索引),对其他不识别的属性,纪录成Attribute链,如果attribute名称符合在accept中attribute数组中指定的attribute名,则替换传⼊的attribute数组对应的项;根据解析出来的信息调⽤以下visit⽅法:
void visit(int version, int access, String name, String signature, String superName, String[] interfaces);
// sourceFile, sourceDebug
void visitSource(String source, String debug);
// EnclosingMethod attribute: enclosingOwner, enclosingName, enclosingDesc.
// Note: only when the class has EnclosingMethod attribute, meaning the class is a local class or an anonymous class
void visitOuterClass(String owner, String name, String desc);
依次解析RuntimeVisibleAnnotations和RuntimeInvisibleAnnotations属性,⾸先解析定义的Annotation的描述符以及运⾏时可见flag,返回⽤户⾃定义的AnnotationVisitor:
AnnotationVisitor visitAnnotation(String desc, boolean visible);
对每个定义的Annotation,解析其键值对,并根据不同的Annotation字段值调⽤AnnotationVisitor中的⽅法,在所有解析结束后,调⽤AnnotationVisitor.visitEnd⽅法:
public interface AnnotationVisitor {
// 对基本类型的数组,依然采⽤该⽅法,visitArray只是在⾮基本类型时调⽤。
void visit(String name, Object value);
void visitEnum(String name, String desc, String value);
AnnotationVisitor visitAnnotation(String name, String desc);
AnnotationVisitor visitArray(String name);
void visitEnd();
}
之前解析出的attribute链表(⾮标准的Attribute定义),对每个Attribute实例,调⽤ClassVisitor中的visitAttribute⽅法:
void visitAttribute(Attribute attr);
Attribute类包含type字段和⼀个字节数组:
public class Attribute {
public final String type;
byte[] value;
Attribute next;
}
对每个InnerClasses属性,解析并调⽤ClassVisitor的visitInnerClass⽅法(该属性事实上保存了所有其直接内部类以及它本⾝到最顶层类的路径):
void visitInnerClass(String name, String outerName, String innerName, int access);
解析字段,它紧跟接⼝数组定义之后,最前⾯的2个字节为字段数组的长度,对每个字段,前⾯2个字节为访问flag定义,再后2个字节为Name索引,以及2个字节的描述符索引,然后解析其Attribute信息:ConstantValue、Signature、Deprecated、Synthetic、RuntimeVisibleAnnotations、RuntimeInvisibleAnnotations以及⾮标准定义的Attribute链,⽽后调⽤ClassVisitor的visitField⽅法,返回FieldVisitor实例:
// 其中value为静态字段的初始化值(对⾮静态字段,它的初始化必须由构造函数实现),如果没有初始化值,该值为null。
FieldVisitor visitField(int access, String name, String desc, String signature, Object value);
对返回的FieldVisitor依次对其Annotation以及⾮标准Attribute解析,调⽤其visit⽅法,并在完成后调⽤它的visitEnd⽅法:
public interface FieldVisitor {
AnnotationVisitor visitAnnotation(String desc, boolean visible);
void visitAttribute(Attribute attr);
void visitEnd();
}
解析⽅法定义,它紧跟字段定义之后,最前⾯的2个字节为⽅法数组长度,对每个⽅法,前⾯2个字节为访问flag定义,再后2个字节为Name索引,以及2个字节的⽅法描述符索引,然后解析其Attribute信息:Code、Exceptions、Signature、Deprecated、RuntimeVisibleAnnotations、AnnotationDefault、Synthetic、RuntimeInvisibleAnnotations、RuntimeVisibleParameterAnnotations、RuntimeInvisibleParameterAnnotations以及⾮标准定义的Attribute链,如果存在Exceptions属性,解析其异常类数组,之后调⽤ClassVisitor的visitMethod⽅法,返回MethodVisitor实例:
MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions);
AnnotationDefault为对Annotation定义时指定默认值的解析;然后依次解析RuntimeVisibleAnnotations、RuntimeInvisibleAnnotations、RuntimeVisibleParameterAnnotations、RuntimeInvisibleParameterAnnotations等属性,调⽤相关AnnotationVisitor的visit⽅法;对⾮标准定义的Attribute链,依次调⽤MethodVisitor的visitAttribute⽅法:
public interface MethodVisitor {
AnnotationVisitor visitAnnotationDefault();
AnnotationVisitor visitAnnotation(String desc, boolean visible);
AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible);
void visitAttribute(Attribute attr);
}
对Code属性解析,读取2个字节的最深栈⼤⼩、最⼤local变量数、code占⽤字节数,调⽤MethodVisitor的visitCode()⽅法表⽰开始解析Code属性,对每条指令,创建⼀个Label实例并构成Label数组,解析Code属性中的异常表,对每个异常项,调⽤visitTryCatchBlock⽅法:
void visitTryCatchBlock(Label start, Label end, Label handler, String type);
Label包含以下信息:
/**
* A label represents a position in the bytecode of a method. Labels are used
* for jump, goto, and switch instructions, and for try catch blocks.
*
* @author Eric Bruneton
*/
public class Label {
public Object info;
int status;
int line;
int position;
private int referenceCount;
private int[] srcAndRefPositions;
int inputStackTop;
int outputStackMax;
Frame frame;
Label successor;
Edge successors;
Label next;
}
解析Code属性中的内部属性信息:LocalVariableTable、LocalVariableTypeTable、LineNumberTable、StackMapTable、StackMap以及⾮标准定义的Attribute链,对每个Label调⽤其visitLineNumber⽅法以及对每个Frame调⽤visitFrame⽅法,并且对相应的指令调⽤相应的⽅法:
void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack);
// Visits a zero operand instruction.
void visitInsn(int opcode);
// Visits an instruction with a single int operand.
void visitIntInsn(int opcode, int operand);
// Visits a local variable instruction. A local variable instruction is an instruction that loads or stores the value of a local variable.
void visitVarInsn(int opcode, int var);
// Visits a type instruction. A type instruction is an instruction that takes the internal name of a class as parameter.
void visitTypeInsn(int opcode, String type);
// Visits a field instruction. A field instruction is an instruction that loads or stores the value of a field of an object.
void visitFieldInsn(int opcode, String owner, String name, String desc);
// Visits a method instruction. A method instruction is an instruction that invokes a method.
void visitMethodInsn(int opcode, String owner, String name, String desc);
// Visits a jump instruction. A jump instruction is an instruction that may jump to another instruction.
void visitJumpInsn(int opcode, Label label);
// Visits a label. A label designates the instruction that will be visited just after it.
void visitLabel(Label label);
// Visits a LDC instruction.
void visitLdcInsn(Object cst);
// Visits an IINC instruction.
void visitIincInsn(int var, int increment);
// Visits a TABLESWITCH instruction.
void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels);
// Visits a LOOKUPSWITCH instruction.
void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels);
// Visits a MULTIANEWARRAY instruction.
字符常量池是什么意思void visitMultiANewArrayInsn(String desc, int dims);
// Visits a try catch block.
void visitTryCatchBlock(Label start, Label end, Label handler, String type);
void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index);
/
/ Visits a line number declaration.
void visitLineNumber(int line, Label start);
// Visits the maximum stack size and the maximum number of local variables of the method.
void visitMaxs(int maxStack, int maxLocals);
最后调⽤ClassVisitor的visitEnd⽅法:
void visitEnd();
ClassWriter实现
ClassWriter继承⾃ClassVisitor接⼝,可以使⽤它调⽤其相应的visit⽅法动态的构造⼀个字节码类。它包含以下字段信息:
public class ClassWriter implements ClassVisitor {
//The class reader from which this class writer was constructed, if any.
ClassReader cr;
//Minor and major version numbers of the class to be generated.
int version;
//Index of the next item to be added in the constant pool.
int index;
//The constant pool of this class.
final ByteVector pool;
//The constant pool's hash table data.
Item[] items;
//The threshold of the constant pool's hash table.
int threshold;
//A reusable key used to look for items in the {@link #items} hash table.
final Item key;
//A reusable key used to look for items in the {@link #items} hash table.
final Item key2;
//A reusable key used to look for items in the {@link #items} hash table.
final Item key3;
//A type table used to temporarily store internal names that will not necessarily be stored in the constant pool.
Item[] typeTable;
//Number of elements in the {@link #typeTable} array.
private short typeCount;
//The access flags of this class.
private int access;
//The constant pool item that contains the internal name of this class.
private int name;
//The internal name of this class.
String thisName;
//The constant pool item that contains the signature of this class.
private int signature;
//The constant pool item that contains the internal name of the super class of this class.
private int superName;
// Number of interfaces implemented or extended by this class or interface.
private int interfaceCount;
//The interfaces implemented or extended by this class or interface.
private int[] interfaces;
//The index of the constant pool item that contains the name of the source file from which this class was compiled.
private int sourceFile;
//The SourceDebug attribute of this class.
private ByteVector sourceDebug;
//The constant pool item that contains the name of the enclosing class of this class.
private int enclosingMethodOwner;
//The constant pool item that contains the name and descriptor of the enclosing method of this class.
private int enclosingMethod;
//The runtime visible annotations of this class.
private AnnotationWriter anns;
//The runtime invisible annotations of this class.
private AnnotationWriter ianns;
//The non standard attributes of this class.
private Attribute attrs;
//The number of entries in the InnerClasses attribute.
private int innerClassesCount;
//The InnerClasses attribute.
private ByteVector innerClasses;
//The fields of this class. These fields are stored in a linked list of {@link FieldWriter} objects, linked to each other by their {@link FieldWriter#next} field. This field stores the first element of this list.
FieldWriter firstField;
//This field stores the last element of this list.
FieldWriter lastField;
//The methods of this class. These methods are stored in a linked list of {@link MethodWriter} objects, linked to each other by their {@link MethodWriter#next} field. This field stores the first element of this list. MethodWriter firstMethod;
//This field stores the last element of this list.
MethodWriter lastMethod;
//true if the maximum stack size and number of local variables must be automatically computed.
private final boolean computeMaxs;
//true if the stack map frames must be recomputed from scratch.
private final boolean computeFrames;
//true if the stack map tables of this class are invalid.
boolean invalidFrames;
}
总结
以上就是这篇⽂章的全部内容,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。