05 Java的class文件的组成介绍

一 类文件结构结合hello world分析

1-1总体概览

  • 类文件结构的信息主要包含有版本信息,常量池表,类/接口信息,类的成员变量/方法信息!!!!!!!!!!!

类的组成结构与对应字节数(左边一列是对应字节个数)

ClassFile {
    u4             magic;                                 // 前四个字节是魔数,用于表示这个文件是class类型
    u2             minor_version;
    u2             major_version;                         // Java版本信息
    u2             constant_pool_count;                   //  constant_pool_count = 常量池中项目的数目+1 (从01开始算)
    cp_info        constant_pool[constant_pool_count-1];  // 具体的常量池信息 (constant pool information)
    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 固定为0xCAFEBABE. 用于class 文件格式
minor_version, major_version 类文件的版本号
constant_pool_count 数值 = 常量池表的大小+1
constant_pool[] 常量表,存储不同的字符串常量,类/接口/成员变量的名称以及other constants that are referred to within the ClassFile structure and its substructures. 常量表的索引是 1 to constant_pool_count - 1.
access_flags 描述类的访问权限以及类/接口的类型(denote access permissions to and properties of this class or interface.)
this_class 类的名称在常量表中的索引 用于获取类的名称
super_class 要么是0,要么是常量表中索引,如果是0代表当前类是Object class/interface,所以没有父类 用于获取父类名称
interfaces_count the number of direct super interfaces of this class or interface type(这个类直接父接口的数目). 标识这个类直接父接口的数目
interfaces[] 常量表的接口名称的索引数组每个索引都能在常量池中找到对应的父接口名称 用于在常量表中获取该类的直接父接口名称
methods_count 方法的统计数目
methods[]
attributes_count 方法的属性数目
attributes[]
  • 值得注意的是方法与属性这部分信息比较繁杂,深入了解需要看官方文档
实际类文件的字节码信息展示
// HelloWorld.java 示例
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}
/*
最左边一列是八进制的字节数信息,每一列有16个字节的信息。等于八进制的020
*/
root@localhost ~]# od -t xC HelloWorld.class
0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09
0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07
0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29
0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e
0000100 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63
0000120 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01
0000140 00 04 74 68 69 73 01 00 1d 4c 63 6e 2f 69 74 63
0000160 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c 6f
0000200 57 6f 72 6c 64 3b 01 00 04 6d 61 69 6e 01 00 16
0000220 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72
0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13
0000260 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69
0000300 6e 67 3b 01 00 10 4d 65 74 68 6f 64 50 61 72 61
0000320 6d 65 74 65 72 73 01 00 0a 53 6f 75 72 63 65 46
0000340 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64
0000360 2e 6a 61 76 61 0c 00 07 00 08 07 00 1d 0c 00 1e
0000400 00 1f 01 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64
0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74
0000440 63 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c
0000460 6f 57 6f 72 6c 64 01 00 10 6a 61 76 61 2f 6c 61
0000500 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61
0000520 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00 03 6f
0000540 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72
0000560 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a 61 76
0000600 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d
0000620 01 00 07 70 72 69 6e 74 6c 6e 01 00 15 28 4c 6a
0000640 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b
0000660 29 56 00 21 00 05 00 06 00 00 00 00 00 02 00 01
0000700 00 07 00 08 00 01 00 09 00 00 00 2f 00 01 00 01
0000720 00 00 00 05 2a b7 00 01 b1 00 00 00 02 00 0a 00
0000740 00 00 06 00 01 00 00 00 04 00 0b 00 00 00 0c 00
0000760 01 00 00 00 05 00 0c 00 0d 00 00 00 09 00 0e 00
0001000 0f 00 02 00 09 00 00 00 37 00 02 00 01 00 00 00
0001020 09 b2 00 02 12 03 b6 00 04 b1 00 00 00 02 00 0a
0001040 00 00 00 0a 00 02 00 00 00 06 00 08 00 07 00 0b
0001060 00 00 00 0c 00 01 00 00 00 09 00 10 00 11 00 00
0001100 00 12 00 00 00 05 01 00 10 00 00 00 01 00 13 00
0001120 00 00 02 00 14
  • 上面是真实的Java字节码

1-2 class文件的魔术信息(magic:0-3字节)

u4             magic;                                 // 前四个字节是魔数,用于表示这个文件是class类型
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000000 ca fe ba be
  • 魔术信息用于表示文件是[class]类型的文件。所有的Java class文件都是这四个字节开头

1-3 class文件的版本信息(version:4-7个字节)

u2             minor_version;
u2             major_version;                         // Java版本信息
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000000 00 00 00 34

第4,5个字节表示minor_version,而第6,7表示major_version。

上面表格中16进制34是52代表Java 8,类推51是Java7,53是Java 9

1-4 class文件的常量池信息(非常重要,后续的信息都是依托常量池进行查表)

 u2             constant_pool_count;                   //  constant_pool_count = 常量池中项目的数目+1 (从01开始算)
 cp_info        constant_pool[constant_pool_count-1];  // 具体的常量池信息 (constant pool information)
constant_pool_count
The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one. A constant_pool index is considered valid if it is greater than zero and less than constant_pool_count, with the exception for constants of type long and double noted in §4.4.5.

constant_pool[]
The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte.
The constant_pool table is indexed from 1 to constant_pool_count - 1.

常量池的查表信息

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

常量池的项数

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000000 00 23

第8-9个字节用于表示常量池中项数,比如上面表格中23等于35代表有34项

  • 表示常量池有 #1~#34项,注意 #0 项不计入,也没有值
0000000 ca fe ba be 00 00 00 34 00 23 | 0a 00 06 00 15 | 09
  • 第#1项 0a 表示一个 Method 信息(查表),00 06 和 00 15(21)表示它引用了常量池中 #6 和 #21 项来获得这个方法的【所属类】和【方法名】

总结: 0a 00 06 00 15 这5个字节传达出该类中有一个方法(0a),这个方法的所属类与方法名在第6项与第21项查询。

0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07
0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29
0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e
0000100 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63
0000120 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01
0000140 00 04 74 68 69 73 01 00 1d 4c 63 6e 2f 69 74 63
0000160 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c 6f
0000200 57 6f 72 6c 64 3b 01 00 04 6d 61 69 6e 01 00 16
0000220 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72
0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13
0000260 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69
0000300 6e 67 3b 01 00 10 4d 65 74 68 6f 64 50 61 72 61
0000320 6d 65 74 65 72 73 01 00 0a 53 6f 75 72 63 65 46
0000340 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64
0000360 2e 6a 61 76 61 0c 00 07 00 08 07 00 1d 0c 00 1e
0000400 00 1f 01 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64
0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74
0000440 63 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c
0000460 6f 57 6f 72 6c 64 01 00 10 6a 61 76 61 2f 6c 61
0000500 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61
0000520 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00 03 6f
0000540 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72
0000560 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a 61 76
0000600 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d
0000620 01 00 07 70 72 69 6e 74 6c 6e 01 00 15 28 4c 6a
0000640 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b
0000660 29 56 

上面的信息都是常量池中信息

1-5 class文件的访问标识与继承信息

访问标识与继承信息的表(不同版本的JDK有差异)

下面图片是JDK9的表

下面图片是JDK8的表

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.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000660 00 21 00 05 00 06 00 00 00 00 00 02 00 01
    u2             access_flags;                          // 访问修饰,类是公共的还是包私有的
    u2             this_class;                            // 类的类名
    u2             super_class;                           // 父类信息
    u2             interfaces_count;                   
    u2             interfaces[interfaces_count];          // 接口信息

第2-3字节:21 表示该 class 是一个类,公共的

第4-5字节:05 表示根据常量池中 #5 找到本类全限定名

第6-7字节:06表示根据常量池中 #6 找到父类全限定名

第8-9字节:表示接口的数量本类为 0

1-6 Field 信息 (变量信息)

成员变量信息查表

FieldType Type Interpretation
B byte signed byte
C char Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
D double double-precision floating-point value
F float single-precision floating-point value
I int integer
J long long integer
L ClassName ; reference an instance of class ClassName
S short signed short
Z boolean true or false
[ reference one array dimension
 u2             fields_count;
 field_info     fields[fields_count];                  // 类中变量信息
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000660 00 00 00 02 00 01

第10-11字节:成员变量的统计信息,由于class中没有定义任何field,所以是0,符合实际。

1-7 method信息(复杂)

1-7-1 方法与属性信息的具体结构

更加详细的信息参考:官方文档

上面表格中12-13字节是方法的统计信息,总共有2个方法

一个方法由 访问修饰符,名称,参数描述,方法属性数量,方法属性组成 :

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;                     // 属性名称在constant pool索引
    u4 attribute_length;                         // 属性长度
    u1 info[attribute_length];                   // 属性信息
}
1-7-2 构造方法字节码分析

1)红色代表访问修饰符(本类中是 public)
2)蓝色代表引用了常量池 #07 项作为方法名称
3)绿色代表引用了常量池 #08 项作为方法参数描述
4)黄色代表方法属性数量,本方法是 1
5)红色代表方法属性

方法属性的解析

  • A Code attribute contains the Java Virtual Machine instructions and auxiliary information for a method, including an instance initialization method and a class or interface initialization method(Code属性包括实例初始化方法以及类/接口初始化方法
  • The LineNumberTable attribute may be used by debuggers to determine which part of the code array corresponds to a given line number in the original source file.(这个属性可以用于调试
  • The LocalVariableTable attribute may be used by debuggers to determine the value of a given local variable during the execution of a method(这个属性可以用于调试过程中确定局部变量的值).
00 09 表示引用了常量池 #09 项,发现是【Code】属性                // code属性
00 00 00 2f 表示此属性的长度是 47
00 01 表示【操作数栈】最大深度
00 01 表示【局部变量表】最大槽(slot)数
2a b7 00 01 b1 是字节码指令
00 00 00 02 表示方法细节属性数量,本例是 2
00 0a 表示引用了常量池 #10 项,发现是【LineNumberTable】属性
    00 00 00 06 表示此属性的总长度,本例是 6
    00 01 表示【LineNumberTable】长度
    00 00 表示【字节码】行号 00 04 表示【java 源码】行号
00 0b 表示引用了常量池 #11 项,发现是【LocalVariableTable】属性
    00 00 00 0c 表示此属性的总长度,本例是 12
    00 01 表示【LocalVariableTable】长度
    00 00 表示局部变量生命周期开始,相对于字节码的偏移量
    00 05 表示局部变量覆盖的范围长度
    00 0c 表示局部变量名称,本例引用了常量池 #12 项,是【this】
    00 0d 表示局部变量的类型,本例引用了常量池 #13 项,是
    【Lcn/itcast/jvm/t5/HelloWorld;】
    00 00 表示局部变量占有的槽位(slot)编号,本例是 0
1-7-3 main方法字节码分析


红色代表访问修饰符(本类中是 public static)
蓝色代表引用了常量池 #14 项作为方法名称
绿色代表引用了常量池 #15 项作为方法参数描述
黄色代表方法属性数量,本方法是2
红色代表方法属性(属性1)
    00 09 表示引用了常量池 #09 项,发现是【Code】属性
    00 00 00 37 表示此属性的长度是 55
    00 02 表示【操作数栈】最大深度
    00 01 表示【局部变量表】最大槽(slot)数
    00 00 00 05 表示字节码长度,本例是 9
    b2 00 02 12 03 b6 00 04 b1 是字节码指令
    00 00 00 02 表示方法细节属性数量,本例是 2
	00 0a 表示引用了常量池 #10 项,发现是【LineNumberTable】属性
        00 00 00 0a 表示此属性的总长度,本例是 10
        00 02 表示【LineNumberTable】长度
        00 00 表示【字节码】行号 00 06 表示【java 源码】行号
        00 08 表示【字节码】行号 00 07 表示【java 源码】行号
    00 0b 表示引用了常量池 #11 项,发现是【LocalVariableTable】属性
        00 00 00 0c 表示此属性的总长度,本例是 12
        00 01 表示【LocalVariableTable】长度
        00 10 表示局部变量名称,本例引用了常量池 #16 项,是【args】
        00 11 表示局部变量的类型,本例引用了常量池 #17 项,是【[Ljava/lang/String;】
        00 00 表示局部变量占有的槽位(slot)编号,本例是 0
红色代表方法属性(属性2)
00 12 表示引用了常量池 #18 项,发现是【MethodParameters】属性
    00 00 00 05 表示此属性的总长度,本例是 5
    01 参数数量
    00 10 表示引用了常量池 #16 项,是【args】       // 参数名称
    00 00 访问修饰符

1-8 附加属性

00 01 表示附加属性数量
00 13 表示引用了常量池 #19 项,即【SourceFile】
00 00 00 02 表示此属性的长度
00 14 表示引用了常量池 #20 项,即【HelloWorld.java】

可以看到附加属性提供了文件名称信息。

参考资料

01 JVM基础课程

02 JKD9的类文件结构说明

03 JDK8的类文件结构说明

posted @ 2021-04-13 09:51  狗星  阅读(110)  评论(0编辑  收藏  举报
/* 返回顶部代码 */ TOP