JVM Class file structure

JVM Class file structure

 

Each class file corresponds to the structure shown below:



 Where u1, u2 and u4 are private data types of the Class file structure, representing 1 byte, 2 bytes and 4 bytes of unsigned numbers respectively

 

Field Explanation:

 

  • magic

The magic number, the only function of the magic number is to determine whether the file is a Class file that can be accepted by the virtual machine. The magic value is fixed at 0xCAFEBABE and will not change.

 

  • minor_version和major_version

Minor version number and major version number. The values ​​of minor_version and major_version represent the minor and major versions of the Class file, respectively. Together they form the format version number of the Class file. For example, if the major version number of a class file is M and the minor version number is m, then the format version number of the class file is determined to be Mm. The order of the size of the version number of the Class file format is: 1.5 < 2.0 < 2.1.

 

A Java virtual machine instance can only support major version numbers in a specific range (Mi to Mj) and minor version numbers in a specific range (0 to m). Assuming that the format version number of a Class file is V, only when Mi.0 ≤ v ≤ Mj.m is established, the Class file can be supported by the Java virtual machine. Different versions of Java virtual machine implementations support different version numbers. Java virtual machine implementations with higher version numbers can support Class files with lower version numbers, and vice versa.

 

Major version correspondence:

version major hex
Java SE 9 53 0x35
Java SE 8 52 0x34
Java SE 7 51 0x33
Java SE 6.0 50 0x32
Java SE 5.0 49 0x31
JDK 1.4 48 0x30
JDK 1.3 47 0x2F
JDK 1.2 46 0x2E
JDK 1.1 45 0x2D

 

 

  • constant_pool-count

Constant pool counter, the value of constant_pool_count is equal to the number of members in the constant_pool table plus 1. The index value of the constant_pool table is only considered valid if it is greater than 0 and less than constant_pool_count, with exceptions for long and double types

 

  • constant_pool[]

The constant pool, constant_pool is a table structure (§4.4) that contains all string constants, class or interface names, field names, and other constants referenced in the Class file structure and its substructures. Each item in the constant pool has the same format characteristics - the first byte is used as a type tag to identify which type of constant the item is, called "tag byte". The index range of the constant pool is 1 to constant_pool_count−1

 

  • access_flags

Access flags, access_flags is a mask flag used to indicate the access rights and basic attributes of a class or interface . The value range and corresponding meaning of access_flags:

ACC_PUBLIC: The value is 0x0001

ACC_FINAL:: The value is 0x0010

ACC_SUPER: The value is 0x0020, which is used to determine which execution semantics the invokespecial instruction in the Class file uses. All current Java virtual machine compilers should set this flag. The ACC_SUPER flag exists for backward compatibility with Class files compiled by old compilers. In the Class files generated by compilers prior to JDK1.0.2, there is no ACC_SUPER flag in access_flag. At the same time, the Java virtual machine before JDK1.0.2 will automatically ignore it when encountering the ACC_SUPER flag

ACC_INTERFACE: The value is 0x0200, indicating that it is an interface

ACC_ABSTRACT: the value is 0x0400

ACC_SYNTHETIC: The value is 0x1000, indicating that it is not the bytecode generated by the Java source code

ACC_ANOTATION: 0x2000, indicating that it is an annotation

ACC_ENUM: The value is 0x4000, indicating that it is an enumeration

 

  • this_class

Class index, the value of this_class must be a valid index value for the item in the constant_pool table. The entry of the constant_pool table at this index must be a constant of type CONSTANT_Class_info, representing the class or interface defined by this Class file

 

  • super_class

Parent class index. For classes, the value of super_class must be 0 or a valid index value for the item in the constant_pool table. If its value is not 0, the entry of the constant_pool table at this index must be a constant of type CONSTANT_Class_info, indicating the direct parent class of the class defined in this Class file. The direct parent class of the current class, and all its indirect parent classes cannot have the ACC_FINAL flag in the access_flag. For an interface, the value of the super_class entry in its Class file must be a valid index value for the entry in the constant_pool table. The entry of the constant_pool table at this index must be a constant of type CONSTANT_Class_info representing java.lang.Object. If the value of super_class of the Class file is 0, then the Class file can only define the java.lang.Object class, and it is the only class without a parent class

 

  • interfaces_count

Interface counter, the value of interfaces_count indicates the number of direct parent interfaces of the current class or interface

 

  • interfaces[]

The interface table, the value of each member in the interfaces[] array must be a valid index value to an item in the constant_pool table, and its length is interfaces_count. Each member interfaces[i] must be a constant of type CONSTANT_Class_info, where 0 ≤ i < interfaces_count. In the interfaces[] array, the interface order represented by the members is the same as the interface order (from left to right) given in the corresponding source code, that is, interfaces[0] corresponds to the leftmost interface in the source code.

 

  • fields_count

Field counter, the value of fields_count indicates the number of members of the fields[] array of the current Class file. Each item in the fields[] array is a data item of the field_info structure, which is used to represent the class field or instance field declared by the class or interface

 

  • fields[]

Field table, each member of the fields[] array must be a data item of the fields_info structure, which is used to represent the complete description of a field in the current class or interface. The fields[] array describes all fields declared by the current class or interface, excluding those inherited from the parent class or interface

 

  • methods_count

Method counter, the value of methods_count indicates the number of members of the methods[] array of the current Class file. Each item in the Methods[] array is a data item of a method_info structure

 

  • methods[]

Method table, each member of the methods[] array must be a data item of a method_info structure, which is used to represent a complete description of a method in the current class or interface. If the access_flags item of a method_info structure neither sets the ACC_NATIVE flag nor the ACC_ABSTRACT flag, then its corresponding method body should be directly loaded from the current class by the Java virtual machine without referencing other classes. The method_info structure can represent all methods defined in classes and interfaces, including instance methods, class methods, instance initialization methods, and class or interface initialization methods. The methods[] array only describes the methods declared in the current class or interface, excluding methods inherited from the parent class or parent interface

 

  • attributes_count

Attribute counter, the value of attributes_count indicates the number of members in the attributes table of the current Class file. Each item in the attributes table is a data item of an attribute_info structure

 

  • attributes[]

Attribute table, the value of each item in the attributes table must be an attribute_info structure. In this specification, the entries of the attributes table in the Class file structure include the following defined attributes: InnerClasses, EnclosingMethod, Synthetic, Signature, SourceFile, SourceDebugExtension, Deprecated, RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, and BootstrapMethods attributes. For Java virtual machine implementations that support the Class file format version number 49.0 or higher, the Signature, RuntimeVisibleAnnotations, and RuntimeInvisibleAnnotations properties in the attributes table must be correctly identified and read. For Java virtual machine implementations that support the Class file format version number 51.0 or higher, the BootstrapMethods attribute in the attributes table must be correctly identified and read. This specification requires that any Java virtual machine implementation can automatically ignore some (or even all) of its unrecognized attribute items in the attributes table of the Class file. Any attributes not defined in this specification cannot affect the semantics of the Class file, but only provide additional descriptive information

 

Guess you like

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