Class结构
为了描述 class文件格式,Java虚拟机规范定义了u1,u2,u4三种数据类型来表示1、2和4字节无符号整数。Java虚拟机规范使用一种类似C语言的结构体来描述class文件格式,整个class文件被描述为如下结构体。
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count]
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count]
}
magic(魔数)
魔数用于标志文件格式,class文件的魔数是"0xCAFEBABE",固定在开头的4个字节。
minor/major version
用于标志class文件版本, 特定的Java虚拟机实现只支持版本号在特定范围内的class文件,比如Oracle java SE 8支持版本号为45.0~52.0的class文件。
constant pool
主要存储类、接口、字段、方法、属性的元数据,包括名称,描述。
常量池结构如下:
cp_info {
u1 tag;
u1 info[];
}
Constant Type | Value |
---|---|
CONSTANT_Class | 7 |
CONSTANT_Fieldref | 9 |
CONSTANT_Methodref | 10 |
CONSTANT_InterfaceMethodref | 11 |
CONSTANT_String | 8 |
CONSTANT_Integer | 3 |
CONSTANT_Float | 4 |
CONSTANT_Long | 5 |
CONSTANT_Double | 6 |
CONSTANT_NameAndType | 12 |
CONSTANT_Utf8 | 1 |
CONSTANT_MethodHandle | 15 |
CONSTANT_MethodType | 16 |
CONSTANT_InvokeDynamic | 18 |
Java虚拟机一共定义了14种常量。
Constant Type | Value |
---|---|
CONSTANT_Class | 7 |
CONSTANT_Fieldref | 9 |
CONSTANT_Methodref | 10 |
CONSTANT_InterfaceMethodref | 11 |
CONSTANT_String | 8 |
CONSTANT_Integer | 3 |
CONSTANT_Float | 4 |
CONSTANT_Long | 5 |
CONSTANT_Double | 6 |
CONSTANT_NameAndType | 12 |
CONSTANT_Utf8 | 1 |
CONSTANT_MethodHandle | 15 |
CONSTANT_MethodType | 16 |
CONSTANT_InvokeDynamic | 18 |
CONSTANT_Class
CONSTANT_Class_info {
u1 tag;
u2 name_index;
}
CONSTANT_Fieldref
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_Methodref
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_InterfaceMethodref
CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_String
CONSTANT_String_info {
u1 tag;
u2 string_index;
}
CONSTANT_Integer
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
}
CONSTANT_Float
CONSTANT_Float_info {
u1 tag;
u4 bytes;
}
CONSTANT_Long
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
CONSTANT_Double
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
CONSTANT_NameAndType
CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
}
CONSTANT_Utf8
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
CONSTANT_MethodHandle
CONSTANT_MethodHandle_info {
u1 tag;
u1 reference_kind;
u2 reference_index;
}
CONSTANT_MethodType
CONSTANT_MethodType_info {
u1 tag;
u2 descriptor_index;
}
CONSTANT_InvokeDynamic
CONSTANT_InvokeDynamic_info {
u1 tag;
u2 bootstrap_method_attr_index;
u2 name_and_type_index;
}
access flag
存储访问标志,比如private,public,static等。1bit对应一个标志。
this class
存储当前class文件对应的文件名在当前常量池中的索引
super class
存储父类名在当前常量池中的索引
interface
存储接口名在当前常量池中的索引
field
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
method
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
attribute
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
[继续完善中]