Notes on "Deep Understanding of JVM"---Chapter 6

class file structure

1. The cornerstone of Java write once, execute everywhere:

   Java compilation produces bytecode (bytecode). Sun and other virtual machine providers publish virtual machines on various platforms. These virtual machines are capable of loading and executing these platform-independent bytecodes.

2. Class class file structure:

A class file is a set of binary byte streams based on bytes , and each data item is arranged in the class file in strict accordance with the order and compactness. There is no separator in between.

Class files use unsigned numbers and tables to store all the data.

Unsigned numbers are basic data types, and u1, u2, u4, and u8 are used to represent 1-byte, 2-byte, 4-byte, and 8-byte unsigned numbers; the table is composed of multiple unsigned numbers Conforms to data types formed by other tables.

The entire class file is essentially a table. Its data items are as follows:

Class file format


Whether it is an unsigned number or a table. When describing multiple data of the same type but an indeterminate number, it is often used in the form of a preceding capacity counter plus several consecutive data items. called a set.

What needs to be emphasized here is that the Class file does not have any delimiters, so all the data items in the above figure, no matter the order or quantity, and even the byte order of data storage, are strictly limited and do not agree to change .

 

The constant pool in the class file: the constant pool is the resource repository of the class file. Stores two types of constants: literals and symbolic references. Literals contain literal strings, constants declared as final. Symbolic references include: ① fully qualified names of classes and interfaces, ② field names and descriptive descriptors, ③ method names and descriptive descriptors. Each constant in the constant pool is a table , with a total of 14 table structures. for example:

Structure of constants of type Constant_Class_info


The constant pool does not have a fixed structure. It is just a resource repository . To find the content in it, you only need to provide the index value.


The class index, parent class index, and interface index collection in the Class file: The class index and the parent class index are both a u2 type of data, they have one and only one (because java does not agree with multiple inheritance), their values ​​are An index value pointing to the corresponding entry in the constant pool. The first entry in the interface index collection entry is the number of interface indexes. This is followed by a series of indices of type u2 pointing to corresponding values ​​in the constant pool.

 

Field table collection: Field table is used to describe the variables declared in the description class or interface.

The structure of the field table is as follows:

Field table structure


The attributes are in the order of access_flags, name_index, descriptor_index, attributes_count, attributes.

(1) The access_flags storage field modifier is very similar to the access_flags in the class. It is a u2 data type. The flag bits and meanings that can be set are as follows:

field access flag


(2) name_index stores the index to the constant pool. A simple name that represents the field.

GDdescriptor_index stores the index to the constant pool and stores the description descriptor of the field. Regarding simple names, descriptive descriptors, and fully qualified names, here is an introduction, such as the following:

①A simple name is the name of a method or field without type and parameter modification. Simple names such as the inc() method and the m field are inc and m.

②The fully qualified name is easy to understand. The fully qualified name of the class is package1.package2....classname. In the class file, the ascii code of . is not used but / is used as the separator. That is, package 1/package 2/…/class name;

③ Descriptive descriptors are more complicated. The function of descriptive descriptors is to describe the data type of the descriptive field , the parameter list of the method (including the number, type and order) and the return value . The basic data type and void are represented by an uppercase character. The object type is represented by L plus the fully qualified name of the object. The details are as follows:

Descriptor Identifier Character Meaning


For array types, each dimension is described with a leading [ character. For example, a two-dimensional array of type java.lang.String[][] will be recorded as [[Ljava/lang/String;, and an array of type int[] will be recorded as [I. When using descriptive descriptors to describe the descriptive method, describe the description in the order of the parameter list first and then the return value. The parameter list is stored in () according to the strict order of the parameters. For example, the void inc() descriptor is () V. The description of java.lang.String toString() is ()Ljava/lang/String;, the description of intindexOf(char[] source, int sourceOffset, int sourceCount, char[] target, inttargetOffset, int targetCount, int fromIndex) The symbol is ([CII[CIII)I.

⑷ After descriptor_index, a property table collection is used to store some additional information.

The field table collection does not list fields inherited from superclasses or parent interfaces. But it is possible to list fields that do not exist in the original Java code. For example, in order to maintain access to the outer class, the inner class will actively add fields pointing to the instance of the outer class.

 

Method table collection: The structure of the method table is similar to that of the field table, such as the following:

Method table structure


The meanings of these data items are also similar, differing only in the options for access flags and attribute table collections.

method access flag


The definition of the method can be expressed clearly through the access flag, name index, and descriptor index. So where does the code for the method go? Java code inside the method. After being compiled into bytecode instructions by the compiler, it is stored in an attribute named Code in the method attribute table. Assuming that the method of the parent class is not overridden by the child class, the method information of the parent class will not appear in the method table collection. But in the same way, there may be methods added by the compiler itself, such as the class constructor <cinit> method and the instance constructor <init> method.

 

Attribute Table Collection: Attribute tables are used to describe information specific to the narrative scene .

The attribute table does not need to have a strict order for each attribute. Its name needs to be represented by referring to a constant of type Constant_Utf8_info from the constant pool, and the structure of the attribute value is completely defined by itself. It only needs to use a u4 type data to describe the number of bits occupied by the attribute value. The table structure is as follows:

Property table structure:


Typical properties. Such as Code, its structure is as follows:

3. Bytecode instructions:

A Java virtual machine instruction consists of a one-byte opcode plus zero to multiple operands .

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324778005&siteId=291194637