File structure of virtual machine Class

Class file structure

Any Class file corresponds to the definition information of a unique class or interface, but the class or interface does not necessarily have to be defined in the file (dynamically generated).
The Class file is a set of binary streams based on 8 bytes. The Class file uses a pseudo structure similar to the C language structure to store data. This structure has only two data types: "unsigned number" and "table".
Unsigned number : U1, u2, u4, u8 are used to represent 1 byte, 2 bytes, 4 bytes, and 8 bytes of unsigned numbers respectively. Unsigned numbers can be used to describe numbers and index references , Quantity value or string in UTF-8 encoding.
Table : A table is a composite data type composed of multiple unsigned numbers or other tables as data items. The name of the table ends with "_info". The essence of the entire Class file can also be regarded as a table.

Magic Number

The first 4 bytes of each Class file is called the magic number. The only function is to determine whether this file is a Class file that can be received by the virtual machine (identification identification), and its value is 0xCAFEBABE.

version number

The 5 and 6 bytes are the Minor Version, and the 6,7 bytes are the Major Version.

Constant pool

The next two bytes are the constant pool count value (constant_pool_count), which is used to store the number of constants in the constant pool (counting from 1), followed by the constant pool entry, which is the first table structure in the Class file .
There are mainly two types of constants stored in the constant pool: literal and symbolic references.
Literal : text string, constant value declared as final, etc.
Symbol reference :

  • Package exported or opened by module
  • Fully qualified names of classes and interfaces
  • Field name and descriptor
  • Method handle and method type
  • Dynamic call points and dynamic constants
    When the virtual machine performs class loading, the corresponding symbol references will be obtained from the constant pool, and then parsed and translated into specific memory addresses during class creation or runtime.
    Each constant in the constant pool is a table. As of JDK13, there are 17 different types of constants. The 17 types of tables have one thing in common. The first bit at the beginning of the table structure is a u1 type tag (tag) indicating that the current constant belongs to that type.

Access sign

Access flags (access_flags) occupies two bytes and are used to identify some class or interface level access information, including whether this Class is a class or an interface, whether it is a public type, and whether it is an Abstract type

Class index, parent class index and interface index collection

The class index (this.class) and the parent class index (super_class) both occupy two bytes. The interface index collection is a set of u2 type data collection.
The class index is used to determine the fully qualified name of this class, the parent class index is used to determine the fully qualified name of the parent class of this class, and the interface index collection is used to describe which interfaces this class implements. These implemented interfaces are based on implements The interface sequence after the keyword is arranged in the interface index set from left to right.

Field table collection

The field table collection (field_info) is used to describe the variables declared in the interface or class.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109341261
Recommended