In-depth understanding of JVM: class file structure

One,

In java born when there is a slogan: "once, run-everywhere!." Bytecode this part functions.

Note : JVM and do not include any language, including java binding only with Class files associated with this particular binary file format.

Other languages ​​may generate byte code by a compiler, and then run the above jvm.

javaèææº

Two, Class Files

Any Class file corresponds to which only one class or interface definition information, but in turn the right to say no.

Class file is a set of 8-bit binary stream in units of bytes based on respective data items are arranged compactly in strict accordance with another Class file, without any increase intermediate break character.

Class file format uses a structure similar to the dummy data storing structure of the C language, this pseudo data structures are only two types: unsigned and tables

Unsigned belong to the basic data types, to u1, u2, u4, u8 represented 1-8 byte unsigned numbers.

A plurality of tables are unsigned number or other complex data types as a table data item configuration.

Class entire file is essentially a table, it is customary to "_info" at the end.

ClassFile {
    u4             magic; //Class 文件的标志
    u2             minor_version;//Class 的小版本号
    u2             major_version;//Class 的大版本号
    u2             constant_pool_count;//常量池的数量
    cp_info        constant_pool[constant_pool_count-1];//常量池
    u2             access_flags;//Class 的访问标记
    u2             this_class;//当前类
    u2             super_class;//父类
    u2             interfaces_count;//接口
    u2             interfaces[interfaces_count];//一个类可以实现多个接口
    u2             fields_count;//Class 文件的字段属性
    field_info     fields[fields_count];//一个类会可以有个字段
    u2             methods_count;//Class 文件的方法数量
    method_info    methods[methods_count];//一个类可以有个多个方法
    u2             attributes_count;//此类的属性表中的属性数
    attribute_info attributes[attributes_count];//属性表集合
}

ç ± »AEA» ¶åèç ç »AEC» ç »ç¤ºæå¾

1, the number of magic

u4             magic; //Class 文件的标志

Before each Class 4-byte file called magic number, which is a sign identifying class files, the first four bytes: 0xCAFBBABE.

2, class version

u2             minor_version;//Class 的次版本号
u2             major_version;//Class 的主版本号

Followed by the version number is 4 bytes, byte minor version number 5,6, 7,8 byte major version number.

java version numbers starting from 45 (primary version), after JDK1.1, +1 every time.

(Jdk1.0 - jdk1.1 use 45.0 - 45.3), jdk1.1 can support 45.0 - 45.65535

The jdk1.2 supports 45.0 - 46.65535. jdk backward compatible.

3, constant pool

u2             constant_pool_count;//常量池的数量
cp_info        constant_pool[constant_pool_count-1];//常量池

Immediately after the major and minor version number is constant pool, the number of the constant pool is constant_pool_count-1 ( constant pool counter starts counting from 1, 0 to empty out the first constant is of special consideration, the index value 0 represents " a constant pool does not reference any item " ).

The constant pool can be understood as class file resource warehouse, it is the largest type of data file structure associated with other projects.

The main constant pool storing two constants: literal and symbolic references . Literal constant relatively close to the concept of Java language level, such as text strings, declared as a constant value final and so on. The concept of symbolic references principle aspects of the compilation belongs. Comprising the following three constants: classes and interfaces fully qualified name, the name and descriptor, the method name and descriptor fields.

Each is a constant in the constant pool table, the 14 tables have a common characteristic: Which constants beginning of the first type is a u1 flag -tag to identify the type of constants representing the current part of this constant Types of.

Types of Mark (tag) description
CONSTANT_utf8_info 1 UTF-8 encoded string
CONSTANT_Integer_info 3 Plastic literal
CONSTANT_Float_info 4 Float literals
CONSTANT_Long_info Long integer literals
CONSTANT_Double_info Double precision floating point literals
CONSTANT_Class_info Reference symbol class or interface
CONSTANT_String_info Type string literals
CONSTANT_Fieldref_info A reference to the symbol field
CONSTANT_Methodref_info 10 Reference symbol class method
CONSTANT_InterfaceMethodref_info 11 The method of the interface reference symbol
CONSTANT_NameAndType_info 12 Reference symbol field or method
CONSTANT_MothodType_info 16 The method of sign types
CONSTANT_MethodHandle_info 15 Representation handle
CONSTANT_InvokeDynamic_info 18 It represents a dynamic method invocation point

4, the access flag

After the end of the constant pool, followed by two bytes represent access flag. This flag is used to identify some of the class or interface level access information, including: The Class is a class or an interface, whether it is public or abstract type, if it is class the words have been declared as final, and so on.

± ç »¥ 访é®åå ° ± æ§ä¿®é ç¬|

5, class index, the index and the parent index set Interface

u2             this_class;//当前类
u2             super_class;//父类
u2             interfaces_count;//接口
u2             interfaces[interfaces_count];//一个雷可以实现多个接口

Class index, the index is a parent class u2 data type, and the interface is a collection of index type u2,

Class and parent class index each index points to a type descriptor for the CONSTANT_Class_info constants, can be found by fully qualified class name String Class constant index value.

Class index is used to determine the fully qualified name of the class, parent class index is used to determine the fully qualified name of the parent class of this class, because the Java language, single inheritance, so the index is only one parent, in addition to  java.lang.Object outside, all the java class has a parent class, so in addition to  java.lang.Object outside, parent index of all Java classes are different from zero.

Interface index set is used to describe this class implements those interfaces, these interfaces will be implemented implents(if class is an interface itself, it is extends) the interface after the order from left to right in the interface index set.

6, a set of fields of the table

u2             fields_count;//Class 文件的字段的个数
field_info     fields[fields_count];//一个类会可以有个字段

Fields table: Variable used to describe an interface or declared in the class, but does not include a local variable.

Field table structure
Types of name Quantity description
u2 access_flags 1 This describes the access flag field
u2 name_index 1 Simple name, a reference constant pool constants.
u2 description_index 1 Used to describe the field data type, the method parameter list (including the number, type and order), and the return value
u2 attributes_count 1 Counters, are 0 (except constants) for the average field
attribute_info attributes attributes_count It used to store some extra information

access_flags values:

å­æ®µçaccess_flagsçåå¼

Note : In the java language field is not overloaded, two fields of data type, modifiers are the same must use a different name, but for the byte code, if the segment descriptor word is inconsistent, that heavy field name it legal.

7. The method set table

The method of the structure is almost similar to the structure of the table column of the table, there are differences only in the options and access flag are set attribute table.

Note : In the java language, to override a method, in addition to having the same name as the original method, also called must have a different and original method signatures, this signature feature is a method of each parameter in the constant pool reference symbol field set, wherein the return value is not so java language not rely solely on a return value different from the existing method overloads.
 

u2             methods_count;//Class 文件的方法的数量
method_info    methods[methods_count];//一个类可以有个多个方法

æ¹æ³è¡¨ç access_flag åå¼
8, the attribute table

u2             attributes_count;//此类的属性表中的属性数
attribute_info attributes[attributes_count];//属性表集合

Can bring their own set of Class file attribute table, field table, the method table, for certain scenarios describe proprietary information. Class files and the order of other data items required different lengths and content restrictions set attribute table is slightly looser, the attribute table is no longer required each having strict order, and they do not duplicate existing attribute name, and any achieve compilers can write their own definition of the attribute information to the attribute table, ignores attribute it does not recognize the Java virtual machine is running.

property code:

java program code in the method body after javac compilation process, eventually it becomes bytecode instructions stored in the code property.

code attribute table structure
Types of name Quantity description
u2 attribute_name_index 1 Pointing to a string constant index, code as "code"
u4 attribute_length 1 Point attribute value of the length
u2 max_stack 1 Representative of the maximum depth of the operand stack, JVM stack depth value is assigned according to the operand stack frame.
u2 max_locals 1 Stored in the local variable table space. Wherein the slot is the smallest unit of memory allocation of local variables used for no more than 32 ... other data type with a slot, for the 64-bit data type is required to store two slot.
u4 code_length 1 Representative byte code length. jvm in a predetermined method does not allow more than 65,535 byte code instructions, although it is a type length value u4, but in fact only a length u2.
u1 code ·code_length For storing a series of bytecode instructions in the byte stream.
u2 exception_table_length 1  
exception_info exception_table exception_table_length  
u2 attributes_count 1  
attribute_info attributes attributes_count  

Local variable table: The method parameters (including hidden parameters in the this example), show abnormal processing parameters (that is, try-catch catch exception statement defined), the local variables defined in the method body.

Published 134 original articles · won praise 91 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_44588495/article/details/104121539