结构规范
根据JVM规范文档来说,ClassFile由以下这几个部分组成:
u1、u2、u4什么意思?
This chapter defines its own set of data types representing class file data: The types u1 , u2 , and u4 represent an unsigned one-, two-, or four-byte quantity, respectively.
用来表述数据所占用的空间, u1 表示一个 1 unsigned byte 、 u2 表示一个 2 unsigned byte …
怎么理解常量池
常量池可以理解为每个元素长度不定的数组。每个元素里的内容根据元素类型(通常是 tag 决定)有不同的形式,但通常长下面这样子:
cp_info {u1 tag;u1 info[];}
tag表示该元素的类型(JVM里会有一张 数值-类型 映射表)info[],该部分内容取决于tag,不同的元素类型,存放的数据也不一样
tag 的类型有许多种,这些可以查阅官方手册《The Constant Pool》,这里列出几个供参考:
CONSTANT_ClassCONSTANT_FieldrefCONSTANT_Methodref
比如 CONSTANT_Fieldref 类型的元素,它的结构如下所示:
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
tag,该元素的类型,对于field_info类型通常是数值9class_index,指向一个表示CONSTANT_Class_info类型的cp_infoname_and_type_index,指向一个表示CONSTANT_NameAndType_info类型的cp_info
看懂常量池的引用
我们可以把常量池里的元素理解为任人挑选的元件,有的元件独立存在,有的元件可能还会引用其他元件。比如在字节码文件里的 this_class 属性,该属性规定必须指向一个类型为 CONSTANT_Class_info 的常量池元素:
The value of the this_class item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure (§4.4.1) representing the class or interface defined by this class file.

this class属性引用了常量池下标为1的元素[01]CONSTANT_Class_info是CONSTANT_Class_info类型的,该元素的属性Class name(规范里称name_index)指向了常量池下标为 10 的元素[10]CONSTANT_Utf8_info是CONSTANT_Utf8_info类型的,该元素保存了字符串内容、字符串长度两个属性。
Tips:感觉最后各种引用都会回归到 CONSTANT_Utf8_info
