原文链接:
https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4
Java Virtual Machine instructions do not rely on the run-time layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in the constant_pool table.
JVM虚拟机指令不依赖于类、接口、类实例、或者数组的运行时布局。反而,指令引用在常量池中的符号信息。
All constant_pool table entries have the following general format:
所有常量池表中条目有下面的通用格式:
cp_info {
u1 tag;
u1 info[];
}
Each item in the constant_pool table must begin with a 1-byte tag indicating the kind of cp_info entry. The contents of the info array vary with the value of tag. The valid tags and their values are listed in Table 4.4-A. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.
在常量表中的每个条目必须以一个字节的标签开始,标识条目的类型。info数组的内容根据tag的值而发生变化。下面的表格列出了所有有效的标记和他们的值。
每个标记类型必须跟随两个或更多字节的信息,标识特定的常量。额外信息的格式随tag的值而变化。

4.4.1. The CONSTANT_Class_info Structure
CONSTANT_Class_info 的结构:
The CONSTANT_Class_info structure is used to represent a class or an interface:
CONSTANT_Class_info结构代表一个类或者一个接口

The items of the CONSTANT_Class_info structure are as follows:
CONSTANT_Class_info 结构的条目如下:
-
tag
-
The
tagitem has the valueCONSTANT_Class(7).tag项的值为
CONSTANT_Class(7).
name_index
-
The value of the
name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing a valid binary class or interface name encoded in internal form (§4.2.1).name_index的值必须是一个有效的
constant_pool表中条目的索引。constant_pool表中的该索引对应的项必须是一个CONSTANT_Utf8_info结构,代表一个有效的二进制类或者接口名(内部格式编码)。
Because arrays are objects, the opcodes anewarray and multianewarray - but not the opcode new - can reference array "classes" via CONSTANT_Class_info structures in the constant_pool table. For such array classes, the name of the class is the descriptor of the array type (§4.3.2).
因为数组是对象,操作码anewarray 和multianewarray -但不是操作码new -能够通过本结构体来引用数组类型。对于这样的数组类型,类的名字是数组类型的描述符。

比如,代表二维数组int[][]的类的名字是[[I,代表线程数组Thread[]的类的名字是[Ljava/lang/Thread;
An array type descriptor is valid only if it represents 255 or fewer dimensions.
数组描述符只在数组维数少于255时有效。
4.4.2. The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures
Fields, methods, and interface methods are represented by similar structures:
域,方法和接口方法用类似的结构来代表:

The items of these structures are as follows:
结构体中的条目如下:
-
tag
-
The
tagitem of aCONSTANT_Fieldref_infostructure has the valueCONSTANT_Fieldref(9).The
tagitem of aCONSTANT_Methodref_infostructure has the valueCONSTANT_Methodref(10).The
tagitem of aCONSTANT_InterfaceMethodref_infostructure has the valueCONSTANT_InterfaceMethodref(11).CONSTANT_Fieldref_info中的tag值为CONSTANT_Fieldref。CONSTANT_Methodref_info中的tag值为CONSTANT_Methodref。CONSTANT_InterfaceMethodref_info中的tag值为valueCONSTANT_InterfaceMethodref。
class_index
-
The value of the
class_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_infostructure (§4.4.1) representing a class or interface type that has the field or method as a member.The
class_indexitem of aCONSTANT_Methodref_infostructure must be a class type, not an interface type.The
class_indexitem of aCONSTANT_InterfaceMethodref_infostructure must be an interface type.The
class_indexitem of aCONSTANT_Fieldref_infostructure may be either a class type or an interface type.class_index的值必须是常量表中有效的索引。常量表中该索引所指向的条目必须是
CONSTANT_Class_info类型的结构体,代表拥有该field或者方法作为其成员的类或接口。
CONSTANT_Methodref_info中class_index的值必须是class类型,不能是接口。
CONSTANT_InterfaceMethodref_info 中的class_index必须是接口。
CONSTANT_Fieldref_info中的class_index可以是接口,也可以是类。
name_and_type_index
The value of the name_and_type_index item must be a valid index into the constant_pool table. Theconstant_pool entry at that index must be a CONSTANT_NameAndType_info structure (§4.4.6). This constant_pool entry indicates the name and descriptor of the field or method.
In a CONSTANT_Fieldref_info, the indicated descriptor must be a field descriptor (§4.3.2). Otherwise, the indicated descriptor must be a method descriptor (§4.3.3).
If the name of the method of a CONSTANT_Methodref_info structure begins with a '<' ('\u003c'), then the name must be the special name <init>, representing an instance initialization method (§2.9). The return type of such a method must be void.
name_and_type_index 的值必须是常量表中的有效索引。其值必须是CONSTANT_NameAndType_info结构体。这个条目标识字段或者方法的名字和描述符。
在CONSTANT_Fieldref_info中,指示的描述符必须是一个字段描述符。否则,必须是一个方法描述符。
如果CONSTANT_Methodref_info 的方法名字以'<' 开头,那么这个名字一定是<init>,代表对象的初始化方法。返回值必须为void。
4.4.3. The CONSTANT_String_info Structure
The CONSTANT_String_info structure is used to represent constant objects of the type String:
CONSTANT_String_info 结构体用来代表字符串常量:

The items of the CONSTANT_String_info structure are as follows:
-
tag
-
The
tagitem of theCONSTANT_String_infostructure has the valueCONSTANT_String(8).tag值为
CONSTANT_String
string_index
-
The value of the
string_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing the sequence of Unicode code points to which theStringobject is to be initialized.值必须为常量表有效索引。必须是一个
CONSTANT_Utf8_info结构体,代表unicode序列,指向将被初始化的String对象
本文解析了JVM中常量池的结构及其在类、接口、字符串等元素中的应用方式,介绍了不同类型的常量池条目如何表示类、字段、方法及字符串等信息。
697

被折叠的 条评论
为什么被折叠?



