本贴主要详解field_info结构和attribute_info,而attribute_info在后续的menthod_info也有涉及。
11、字段表
fields[],描述当前类或接口声明的所有字段,但不包括从父类或父接口继承的部分。field_info接口如下:
field_info {
u2 access_flags; #定义字段被访问权限和基础属性的掩码标志
u2 name_index; #对应常量池索引值所属的项为CONSTANT_Utf8_info结构
u2 descriptor_index; #对应常量池索引值所属的项为CONSTANT_Utf8_info结构
u2 attributes_count; #表示当前字段的附加属性的数量
attribute_info attributes[attributes_count]; #attribute结构
}
其中,attribute_info结构为
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
同一个Class文件中不会有两个字段(field)同时具有相同的字段名和描述符。
属性(Attributes)在Class文件格式中的ClassFile结构、field_info结构,method_info结构和Code_attribute结构中都有使用。
任意属性对常量池的索引项必须是CONSTANT_Class_info结构,表示当前属性的名字,attribute_length项的值给出了跟随其后的字节的长度,有些属性因Class文件格式规范所需,已被预先定义好,如下表格,详细属性名的介绍参考:属性名详解
属性名 | Java SE | Class 文件 |
---|---|---|
ConstantValue | 1.0.2 | 45.3 |
Code | 1.0.2 | 45.3 |
StackMapTable | 6 | 50.0 |
Exceptions | 1.0.2 | 45.3 |
InnerClasses | 1.1 | 45.3 |
EnclosingMethod | 5.0 | 49.0 |
Synthetic | 1.1 | 45.3 |
Signature | 5.0 | 49.0 |
SourceFile | 1.0.2 | 45.3 |
SourceDebugExtension | 5.0 | 49.0 |
LineNumberTable | 1.0.2 | 45.3 |
LocalVariableTable | 1.0.2 | 45.3 |
LocalVariableTypeTable | 5.0 | 49.0 |
Deprecated | 1.1 | 45.3 |
RuntimeVisibleAnnotations | 5.0 | 49.0 |
RuntimeInvisibleAnnotations | 5.0 | 49.0 |
RuntimeVisibleParameterAnnotations | 5.0 | 49.0 |
RuntimeInvisibleParameterAnnotations | 5.0 | 49.0 |
AnnotationDefault | 5.0 | 49.0 |
BootstrapMethods | 7 | 51.0 |
12、方法计数器
methods_count,表示当前class文件methods[]数组的成员个数,每一项都是method_info结构。
13、方法表
methods[],表示当前类或接口中某方法的完整描述,如果method_info的access_flag项既没有设置ACC_NATIVE标志也没有设置ACC_ABSTRACT标志,那么它对应的方法体就应当可以被java虚拟机直接从当前类加载,而不需要引用其他类。只描述当前类或接口中声明的方法,不包括父类或父接口继承的方法。
14、属性计数器
attributes_count,表示attributes表的成员个数,每一项都是attribute_info结构。
15、属性表
attributes[],在JAVA SE7中,Class文件结构中的attributes表的项包含下列定义的属性。
InnerClasses、EnclosingMethod、Synthetic、Signature、SourceFile、SourceDebugExtension、Deprecated、RuntimeVisibleAnnotations、RuntimeInvisibleAnnotations、BootstrapMethods。对于支持Class文件格式版本号为49.0或更高的java虚拟机实现,必须正确识别并读取attributes表中的Signature、RuntimeVisibleAnnotations和RuntimeInvisibleAnnotations属性。对于51.0或更高的java虚拟机实现,必须正确识别并读取BootstrapMethods属性。