ClassFile 结构
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];
}
总结一下,之后按着这个来
字节数 | 名称 | 描述 |
---|---|---|
u4 | magic; | 1 魔数 |
u2 | minor_version; | |
u2 | major_version; | 2 Class文件版本 |
u2 | constant_pool_count; | 3 常量池 |
cp_info | constant_pool[constant_pool_count-1]; | |
u2 | access_flags; | 4 访问标志 |
u2 | this_class; | 5 类索引 |
u2 | super_class; | |
u2 | interfaces_count; | |
u2 | interfaces[interfaces_count]; | |
u2 | fields_count; | 6 字段表集合 |
field_info | fields[fields_count]; | |
u2 | methods_count; | 7 方法表集合 |
method_info | methods[methods_count]; | |
u2 | attributes_count; | 8 属性表集合 |
attribute_info | attributes[attributes_count]; |
1. 魔数
0xCAFEBABE
2. Class 文件版本
0x0034 = 52 = JDK8
3. 常量池
查看命令: javap -verbose TestClass.class
开头为常量池容量,u2 类型
常量池项目类型
类型 | tag | 描述 |
---|---|---|
CONSTANT_utf8_info | 1 | UTF-8 编码的字符串 |
CONSTANT_Integer_info | 3 | 整型字面量 |
CONSTANT_Float_info | 4 | 浮点型字面量 |
CONSTANT_Long_info | 5 | 长整型字面量 |
CONSTANT_Double_info | 6 | 双精度浮点型字面量 |
CONSTANT_Class_info | 7 | 类或接口的符号引用 |
CONSTANT_String_info | 8 | 字符串类型字面量 |
CONSTANT_Fieldref_info | 9 | 字段的符号引用 |
CONSTANT_Methodref_info | 10 | 类中方法的符号引用 |
CONSTANT_InterfaceMethodref_info | 11 | 接口中方法的符号引用 |
CONSTANT_NameAndType_info | 12 | 字段或方法的符号引用 |
CONSTANT_MethodHandle_info | 15 | 表示方法句柄 |
CONSTANT_MethodType_info | 16 | 标识方法类型 |
CONSTANT_InvokeDynamic_info | 18 | 表示一个动态方法调用点 |
4. 访问标志
访问标识:表示类或接口的访问信息
在 16 进制文件中,对应常量池结束后的 .!
类、接口访问标志
Flag Name | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public ; may be accessed from outside its package. |
ACC_FINAL | 0x0010 | Declared final ; no subclasses allowed. |
ACC_SUPER | 0x0020 | Treat superclass methods specially when invoked by the invokespecial instruction. |
ACC_INTERFACE | 0x0200 | Is an interface, not a class. |
ACC_ABSTRACT | 0x0400 | Declared abstract ; must not be instantiated. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic; not present in the source code. |
ACC_ANNOTATION | 0x2000 | Declared as an annotation type. |
ACC_ENUM | 0x4000 | Declared as an enum type. |
5. 类索引、父类索引、接口索引集合
接口索引集合:第一项为接口的数量
6. 字段表集合
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
字段访问标志
Flag Name | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public ; may be accessed from outside its package. |
ACC_PRIVATE | 0x0002 | Declared private ; usable only within the defining class. |
ACC_PROTECTED | 0x0004 | Declared protected ; may be accessed within subclasses. |
ACC_STATIC | 0x0008 | Declared static . |
ACC_FINAL | 0x0010 | Declared final ; never directly assigned to after object construction. |
ACC_VOLATILE | 0x0040 | Declared volatile ; cannot be cached. |
ACC_TRANSIENT | 0x0080 | Declared transient ; not written or read by a persistent object manager. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic; not present in the source code. |
ACC_ENUM | 0x4000 | Declared as an element of an enum . |
基本数据类型 - 描述符标志符
标识字符 | 含义 |
---|---|
B | byte |
S | short |
I | int |
⭐️J | long |
F | float |
D | double |
C | char |
⭐️Z | boolean |
V | void |
L | 对象类型,如Ljava/lang/Object |
7. 方法表集合
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
方法访问标志
public、private、protected
只能选其一
接口字段必须有 public static final
FlagName | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public ; may be accessed from outside its package. |
ACC_PRIVATE | 0x0002 | Declared private ; accessible only within the defining class. |
ACC_PROTECTED | 0x0004 | Declared protected ; may be accessed within subclasses. |
ACC_STATIC | 0x0008 | Declared static . |
ACC_FINAL | 0x0010 | Declared final ; must not be overridden . |
ACC_SYNCHRONIZED | 0x0020 | Declared synchronized ; invocation is wrapped by a monitor use. |
ACC_BRIDGE | 0x0040 | A bridge method, generated by the compiler. |
ACC_VARARGS | 0x0080 | 方法是否接收不定参数 Declared with variable number of arguments . |
ACC_NATIVE | 0x0100 | Declared native ; implemented in a language other than Java. |
ACC_ABSTRACT | 0x0400 | Declared abstract ; no implementation is provided. |
ACC_STRICT | 0x0800 | Declared strictfp ; floating-point mode is FP-strict. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic ; not present in the source code. |
8. 属性表集合
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
code 属性
Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack;
u2 max_locals;
u4 code_length;
u1 code[code_length];
u2 exception_table_length;
{ u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
} exception_table[exception_table_length];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
参考
- 深入理解Java虚拟机 第六章
- The class File Format
- Jvm笔记总结(七):Class文件结构