class文件结构
字节码文件结构,使用idea的插件jclasslib打开,如图:

二进制文本,使用Hex Friend打开,如图:

官网文件结构说明:
1 | ClassFile { |
常规信息
魔术(u4) cafe babe
版本信息(u4) 次版本 主版本
常量池
常量池数量(u2)
常量池明细
1 | cp_info { |
JDK19
| Constant Kind | Tag | class file format |
Java SE | 结构明细 |
|---|---|---|---|---|
| CONSTANT_Utf8 | 1 | 45.3 | 1.0.2 | CONSTANT_Utf8_info { u1 tag; //Ox01 u2 length; //对应的字节数量 u1 bytes[length]; //对应的ASCII的值} |
| CONSTANT_Integer | 3 | 45.3 | 1.0.2 | CONSTANT_Integer_info { u1 tag; //0x03 u4 bytes; //4字节(所以是-2^31~2^31-1)} |
| CONSTANT_Float | 4 | 45.3 | 1.0.2 | CONSTANT_Float_info { u1 tag; //0x04 u4 bytes; //4字节(所以是-2^31~2^31-1)} |
| CONSTANT_Long | 5 | 45.3 | 1.0.2 | CONSTANT_Long_info { u1 tag; u4 high_bytes; u4 low_bytes;} |
| CONSTANT_Double | 6 | 45.3 | 1.0.2 | CONSTANT_Double_info { u1 tag; u4 high_bytes; u4 low_bytes;} |
| CONSTANT_Class | 7 | 45.3 | 1.0.2 | CONSTANT_Class_info { u1 tag; //0x07 u2 name_index; //类名称在常量池的索引值} |
| CONSTANT_String | 8 | 45.3 | 1.0.2 | CONSTANT_String_info { u1 tag; //0x08 u2 string_index; //字符串名称在常量池的索引值} |
| CONSTANT_Fieldref | 9 | 45.3 | 1.0.2 | CONSTANT_Fieldref_info { u1 tag; //0x09 u2 class_index; //对应的常量池中类的索引值(CONSTANT_Class) u2 name_and_type_index; //对应的常量池中的名称和类型的索引值(CONSTANT_NameAndType)} |
| CONSTANT_Methodref | 10 | 45.3 | 1.0.2 | CONSTANT_Methodref_info { u1 tag; //0x0a u2 class_index; //对应的常量池中类的索引值(CONSTANT_Class) u2 name_and_type_index;//对应的常量池中的名称和类型的索引值(CONSTANT_NameAndType)} |
| CONSTANT_InterfaceMethodref | 11 | 45.3 | 1.0.2 | CONSTANT_Methodref_info { u1 tag; //0x0b u2 class_index; //对应的常量池中类的索引值(CONSTANT_Class) u2 name_and_type_index;//对应的常量池中的名称和类型的索引值(CONSTANT_NameAndType)} |
| CONSTANT_NameAndType | 12 | 45.3 | 1.0.2 | CONSTANT_NameAndType_info { u1 tag; //0x0c u2 name_index; //对应的常量池中的字符串名称的索引的索引 u2 descriptor_index; //对应的常量池中的字符串类的索引} |
| CONSTANT_MethodHandle | 15 | 51.0 | 7 | CONSTANT_MethodHandle_info { u1 tag; //0x0e u1 reference_kind; // u2 reference_index;} |
| CONSTANT_MethodType | 16 | 51.0 | 7 | CONSTANT_MethodType_info { u1 tag; //0x10 u2 descriptor_index; //对应的常量池中的字符串类的索引} |
| CONSTANT_Dynamic | 17 | 55.0 | 11 | 暂不描述,jdk11 |
| CONSTANT_InvokeDynamic | 18 | 51.0 | 7 | CONSTANT_InvokeDynamic_info { u1 tag; //0x12 u2 bootstrap_method_attr_index; // u2 name_and_type_index; //对应的常量池中的名称和类型的索引值(CONSTANT_NameAndType)} |
| CONSTANT_Module | 19 | 53.0 | 9 | 暂不描述,jdk9 |
| CONSTANT_Package | 20 | 53.0 | 9 | 暂不描述,jdk9 |
类访问修饰符
| 类访问修饰名称 | 对应的码值 | 描述 |
|---|---|---|
| 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. |
当前类
占2字节,指向常量池的(CONSTANT_Class)索引值,如图:

当前类的父类
占2字节,指向常量池的(CONSTANT_Class)索引值
当前类的接口
1 | u2 interfaces_count; //接口数量 |
字段
1 | u2 fields_count; //字段数量 |
Table 4.5-A. Field access and property flags
| 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 (JLS §17.5). |
| 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 . |
info
Table 4.7-A. Predefined class file attributes (by section)
| 属性名 | 链接 | class file |
Java SE |
|---|---|---|---|
| ConstantValue | §4.7.2 | 45.3 | 1.0.2 |
| Code | §4.7.3 | 45.3 | 1.0.2 |
| StackMapTable | §4.7.4 | 50.0 | 6 |
| Exceptions | §4.7.5 | 45.3 | 1.0.2 |
| InnerClasses | §4.7.6 | 45.3 | 1.1 |
| EnclosingMethod | §4.7.7 | 49.0 | 5.0 |
| Synthetic | §4.7.8 | 45.3 | 1.1 |
| Signature | §4.7.9 | 49.0 | 5.0 |
| SourceFile | §4.7.10 | 45.3 | 1.0.2 |
| SourceDebugExtension | §4.7.11 | 49.0 | 5.0 |
| LineNumberTable | §4.7.12 | 45.3 | 1.0.2 |
| LocalVariableTable | §4.7.13 | 45.3 | 1.0.2 |
| LocalVariableTypeTable | §4.7.14 | 49.0 | 5.0 |
| Deprecated | §4.7.15 | 45.3 | 1.1 |
| RuntimeVisibleAnnotations | §4.7.16 | 49.0 | 5.0 |
| RuntimeInvisibleAnnotations | §4.7.17 | 49.0 | 5.0 |
| RuntimeVisibleParameterAnnotations | §4.7.18 | 49.0 | 5.0 |
| RuntimeInvisibleParameterAnnotations | §4.7.19 | 49.0 | 5.0 |
| RuntimeVisibleTypeAnnotations | §4.7.20 | 52.0 | 8 |
| RuntimeInvisibleTypeAnnotations | §4.7.21 | 52.0 | 8 |
| AnnotationDefault | §4.7.22 | 49.0 | 5.0 |
| BootstrapMethods | §4.7.23 | 51.0 | 7 |
| MethodParameters | §4.7.24 | 52.0 | 8 |
Table 4.7-B. Predefined class file attributes (by class file version)
| Attribute | class file |
Java SE | Section |
|---|---|---|---|
| ConstantValue | 45.3 | 1.0.2 | §4.7.2 |
| Code | 45.3 | 1.0.2 | §4.7.3 |
| Exceptions | 45.3 | 1.0.2 | §4.7.5 |
| SourceFile | 45.3 | 1.0.2 | §4.7.10 |
| LineNumberTable | 45.3 | 1.0.2 | §4.7.12 |
| LocalVariableTable | 45.3 | 1.0.2 | §4.7.13 |
| InnerClasses | 45.3 | 1.1 | §4.7.6 |
| Synthetic | 45.3 | 1.1 | §4.7.8 |
| Deprecated | 45.3 | 1.1 | §4.7.15 |
| EnclosingMethod | 49.0 | 5.0 | §4.7.7 |
| Signature | 49.0 | 5.0 | §4.7.9 |
| SourceDebugExtension | 49.0 | 5.0 | §4.7.11 |
| LocalVariableTypeTable | 49.0 | 5.0 | §4.7.14 |
| RuntimeVisibleAnnotations | 49.0 | 5.0 | §4.7.16 |
| RuntimeInvisibleAnnotations | 49.0 | 5.0 | §4.7.17 |
| RuntimeVisibleParameterAnnotations | 49.0 | 5.0 | §4.7.18 |
| RuntimeInvisibleParameterAnnotations | 49.0 | 5.0 | §4.7.19 |
| AnnotationDefault | 49.0 | 5.0 | §4.7.22 |
| StackMapTable | 50.0 | 6 | §4.7.4 |
| BootstrapMethods | 51.0 | 7 | §4.7.23 |
| RuntimeVisibleTypeAnnotations | 52.0 | 8 | §4.7.20 |
| RuntimeInvisibleTypeAnnotations | 52.0 | 8 | §4.7.21 |
| MethodParameters | 52.0 | 8 | §4.7.24 |
Table 4.7-C. Predefined class file attributes (by location)
| Attribute | Location | class file |
|---|---|---|
| SourceFile | ClassFile | 45.3 |
| InnerClasses | ClassFile | 45.3 |
| EnclosingMethod | ClassFile | 49.0 |
| SourceDebugExtension | ClassFile | 49.0 |
| BootstrapMethods | ClassFile | 51.0 |
| ConstantValue | field_info | 45.3 |
| Code | method_info | 45.3 |
| Exceptions | method_info | 45.3 |
| RuntimeVisibleParameterAnnotations , RuntimeInvisibleParameterAnnotations |
method_info | 49.0 |
| AnnotationDefault | method_info | 49.0 |
| MethodParameters | method_info | 52.0 |
| Synthetic | ClassFile , field_info , method_info |
45.3 |
| Deprecated | ClassFile , field_info , method_info |
45.3 |
| Signature | ClassFile , field_info , method_info |
49.0 |
| RuntimeVisibleAnnotations , RuntimeInvisibleAnnotations |
ClassFile , field_info , method_info |
49.0 |
| LineNumberTable | Code | 45.3 |
| LocalVariableTable | Code | 45.3 |
| LocalVariableTypeTable | Code | 49.0 |
| StackMapTable | Code | 50.0 |
| RuntimeVisibleTypeAnnotations , RuntimeInvisibleTypeAnnotations |
ClassFile , field_info , method_info , Code |
52.0 |
方法
1 | u2 methods_count; //方法数量 |
access_flags
Table 4.6-A. Method access and property flags
| Flag Name | 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 ( §5.4.5 ). |
| 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. |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小天的博客!
评论
