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

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

官网文件结构说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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) cafe babe

版本信息(u4) 次版本 主版本

常量池

常量池数量(u2)

常量池明细

1
2
3
4
cp_info {
u1 tag; //对应常量种类表的Tag
u1 info[]; //每种Tag有具体不同的明细
}

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
2
u2             interfaces_count;                             //接口数量
u2 interfaces[interfaces_count]; //接口在常量池的对应索引值

字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
u2             fields_count;                                 //字段数量
field_info fields[fields_count]; //字段信息明细

field_info {
u2 access_flags; //Table 4.5-A(sum(Value)))
u2 name_index; //常量池对应字段名的索引
u2 descriptor_index; //常量池对应字段类型的索引
u2 attributes_count; //字段属性数量
attribute_info attributes[attributes_count];
}

attribute_info {
u2 attribute_name_index; //常量池对应属性名称的索引
u4 attribute_length; //属性长度
u1 info[attribute_length];//Table 4.7-A
}

//例子
ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
}

Deprecated_attribute {
u2 attribute_name_index;
u4 attribute_length;
}

RuntimeVisibleAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
annotation annotations[num_annotations];
}

annotation {
u2 type_index;
u2 num_element_value_pairs;
{ u2 element_name_index;
element_value value;
} element_value_pairs[num_element_value_pairs];
}

0005(this) 0006(object) 0002(接口数量2) 0007(Seriable) 0008(Conable)
0001(字段数量1) 0012(访问标识public final) 0009(num) 000A(String) 0003(字段属性数量3)
000B(属性类型ConstantValue) 0000 0002(2个属性长度) 0002(字符串值("123234")) 000C(属性索引Deprecated)
0000 0000(数量为0) 000D(属性索引RuntimeVisibleAnnotations) 0000 0006(6个属性长度)
0001(注解数量为1)) 000E(注解索引Ljava/lang/Deprecated;) 0000(元素值对为0)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
u2             methods_count;                                //方法数量
method_info methods[methods_count]; //方法信息明细

method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}

attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}

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];
}

16进制翻译成对应的字节码指令的官方网站
https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5

0002(方法数量为2) 0001(访问标识为public) 000F(方法名称索引<init>) 0010(方法返回索引()V) 0001(属性数量为1)
0011(属性名称索引Code) 0000 0039(属性长度57) 0002(操作数栈容量为2) 0001(局部变量表为1)
0000 000B(代码长度为12) 2A(aload_0)B7(invokespecial) 0001(常量池索引为1) 2A(aload_0)12(ldc)
02(常量池索引为2)B5(putfield) 0003(常量池索引为3) B1(return)00(常量池索引为0) 0000 0200 1200

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.