Chapter 6th Class Documents
Structure of the 6.3 class file
A class file is a set of binary streams that are based on 8-byte units.
The class file format stores data in a pseudo-structure similar to the C language structure, with only two data types: unsigned number and table
The unsigned number belongs to the basic data type with U1,U2,U4,U8 to represent one byte, 2 bytes, 4 bytes, 8 bytes of unsigned number, unsigned number can be used to describe a number, index reference, quantity value or a note string value according to UTF-8 encoding.
A table is a composite data type consisting of multiple unsigned numbers or other tables as data items, and all tables habitually end with _info.
Table class file format
Type |
Name |
Number |
U4 |
Magic |
1 |
U2 |
Minor_version |
1 |
U2 |
Major_version |
1 |
U2 |
Constant_pool_count |
1 |
Cp_info |
Constant_pool |
Constant_pool_count-1 |
U2 |
Access_flags |
1 |
U2 |
This_class |
1 |
U2 |
Super_class |
1 |
U2 |
Interface_count |
1 |
U2 |
Interfaces |
Interfaces_count |
U2 |
Fields_count |
1 |
Field_info |
Fields |
Field_count |
U2 |
Methods_count |
1 |
Method_info |
Methods |
Methods_count |
U2 |
Attributes_count |
1 |
Attribute_info |
Attributes |
Attributes_count |
1 magic number and class version
Public class Test { privateint m; Private int Inc () { return m + 1; }}
such as the above class compiled class file, after the UE opened:
The first four bits are magic number: Cafebabe
56th Digit (00 00) is the minor version number (Minor versions)
7th 8 bits (00 32) The main version number (Major version) hexadecimal 0x0032 for decimal 50 indicates that it can be executed by JDK1.6 or more (JDK1.1 can support 45.0~45.65535,jdk1.2 to support 45.0~46.65535, JDK1.5 Generally, JDK1.6 generally 50, need to confirm)
2 constant Pool
After the main version is the number of constant pools U2 and constant pool cp_info, the constant pool can be understood as the resource warehouse in the class file. Constants in a constant pool are the number of constant pools-1.
The constant pool is dominated by two of the class constants: Literal (Literal) and symbolic references (symbolic References). Each constant is a table, JDK1.7 a total of 14 different table structures (Constant_utf8_info (1), Constant_integer_info (3), Constant_float_info (4), Constant_ Long_info (5), Constant_double_info (6), Constant_class_info (7), etc.), each constant has its own type, each of which has a common characteristic, The first bit of each structure is a U1 type of flag bit (tag).
: The number of constant pools (00 16) is 16 0x0016 to 10 binary 22, which represents 21 constants in the constant pool.
0X07 is the tag in the type of the first constant, the type Constant_class_info (7) is found, and the structure of Constant_class_info is as follows:
Type |
Name |
Number |
U1 |
Tag |
1 |
U2 |
Name_index |
1 |
0x07 followed by two bytes: 0x0002 represents name_index; Name_inde is an indexed value that refers to a bribery constant in the Constant_utf8_info constant pool, which represents the fully qualified name of the class (or interface). Here, the 0x0002 represents the second constant in the constant pool.
The next field is 01, and the tag,0x01 for the second constant indicates that the constant is of type constant_utf8_info, and the Constant_utf8_info type structure is as follows:
Type |
Name |
Number |
U1 |
Tag |
1 |
U2 |
Length |
1 |
U1 |
bytes |
Length |
Length indicates how many bytes of the UTF-8 encoded note, such as
0x0004 represents a byte length of 4 bytes 0x54657374 represented as Test
The remaining 19 constants can be analyzed using the JAVAP tool:
Many of the 21 constants are not directly present in the Java code, and they are referenced by the field table (Field_info), the method table (Method_info), and the property sheet (attribute_info), which is about to be referred to later.
3 Access Flags
Go down. Two bytes for the access flag Access_flag, the access information used to identify the class or interface hierarchy includes: Whether this class is an interface or not, defines the public type
4 Class Index parent class index and interface index collection
The class index (This_class) and the parent class index (SUPER_CLASS) are data of a U2 type, and the interface index collection (interfaces) is a collection of data of a set of U2 types.
Their values will point to the position in the constant pool:
The value of This_class is 0x0001 points to the first const #1 =class in the constant pool #2
The value of Super_class is 0x0003 points to 3 in the constant pool
5 Field table Collection
Used to describe variables declared in an interface or class, you can include information such as whether the scope of the field (public, private, protected) is an instance variable or a class variable (static modifier), variability (final), concurrency visibility (volatile decoration, Whether to enforce read and write from within the master), can be serialized, field data type (base type, object, array), field name, etc.
Field structure: Access Flag (Access_flags) U2, name Index (NAME_INDEX) U2, descriptor Index (DESCRIPTOR_INDEX) U2, Number of attributes (Attributs_count) U2, property sheet collection (attributs ) Attributs_count
Descriptors are used to describe the data type of a field, the list of method parameters (including quantity, type, and order), and the return value. The base data type (Byte,char, double, float, int, long, short, Boolean), and void types that represent no return values are represented by a uppercase character, and the object type is represented by the full-qualified name of the character L plus the object.
B-bype, C-char, d-double, F-float, I-int, J-long, S-short,z-boolean, V-void, L-Object type
For arrays, each set of degrees has a front [to describe java.lang.string[][]--[[Java/lang/string; Int[]---> [I
6 Method table Collection
is basically the same as a collection of field tables, and differs only in the optional recommendation for access flags and property sheet collections.
The Java code of the method is compiled into bytecode instructions by the compiler and stored in a property named code in the method's attribute table collection.
7 Property Sheet Collection
The class file, the field table, and the method table can all carry their own set of property sheets that describe the information specific to certain scenarios
Introduction to 6.4-byte code instruction
1 byte code and data type
In the instruction set of a Java virtual machine, most directives contain data type information corresponding to their operation, for example: the iload directive is used to load int data from the local variable table into the operand stack.
There are special notes in the opcode mnemonics to indicate which data types are specifically served, i:int, L:long, S:short, B:byte, C:char, F:float, D:double, A:reference.
The Java virtual machine's opcode is only one byte long, and if each data type related instruction supports all runtime data types of the Java virtual machine, the data for that instruction will probably exceed the number of bytes that a byte can represent. Therefore, the instruction set of the Java Virtual Machine provides only a limited type-related instruction to support it for a specific operation.
2 Loading and storing instructions
Load and store directives are used to transfer data back and forth between the local variable table and the operational data stack in the stack frame
Load a local variable into the Operation stack: Iload, iload_<n>, Lload, fload .....
Store a numeric value from the operand stack to a local variable table: istore, Istore_<n>
Load a constant into the operand stack: Bipush, Sipush, LDC .....
An instruction to expand the access index of a local variable table: wide.
3 Operational Instructions
An operation or arithmetic instruction is used to perform a specific operation on the values on the two operand stacks and to re-deposit the results to the top of the Operation Stack. Because there is no direct support for Byte,short, chart and Boolean arithmetic instructions, the operation of this type of data should be replaced with an instruction that operates int.
Addition: Idadd, Ladd, Fadd, Dadd.
Subtraction: Isub, Lsub, Fsub, dsub,
Multiplication: Imul, Lmul, Fmul, Dmul
Division: Idiv, Ldiv, Fdiv, Ddiv
Redundancy: Irem, Lrem, Frem, Drem
Take the reverse: Ineg, Lneg, Fneg, Dneg
Displacement: ISHL, Ishr, Iushr, LSHL, LSHR, LUSHR
Bitwise OR instruction: IOR, Lor
Bitwise AND INSTRUCTION: Iand, land
Bitwise XOR OR instruction: Ixor, Lxor
Local variable self-increment directive: iinc
Comparison instructions: Dcmpg, Dcmpl, Fcmpg, Fcmpl, lcmp
4 Type conversion directives
Type conversion directives can think of two different numeric types for conversion
The Java Virtual machine directly supports (that is, conversion without explicit conversion instructions) of the following data types of the widening type conversion (widening Numeric Conversions, that is, the small-range type to a wide-range type of security conversion. 1, int type to long, float or double type, 2, long type to float, double type, 3, float type to double type
In contrast, when dealing with narrowing type conversions (Narrowing Numeric Conversions), you must explicitly use the conversion directives, which include: I2b, I²c, i2s ...
5 object creation and access directives
Instructions for creating an instance of a class: New
Instructions for creating an array: NewArray, Anewarray, Multianewarray
Instructions for accessing class fields (static fields) and instance fields (non-static fields, or instance variables): GetField, Putfield, Getstatic, putstatic
The command to load an array element into the operand stack: baload, Caload, Saload, Iaload, laload ...
An instruction that stores the value of an operand stack into an array element: Bastore, Castore,sastore ....
instruction to take array length: arraylength
Instructions for checking the class instance type: Instanceof, Checkcast
6 Operand Stack Management instructions
The Java Virtual Machine provides instructions for manipulating the operand stack directly.
Stack top one or two elements of the operand stack: Pop, POP2
Copy the stack top one or two values and re-press the copied value or double copy value into the top of the stack: DUP, dup2, dpu_x1 ....
Swap the top two values of the stack: swap
7 Control Transfer Instructions
Conditional branches: ifeq, Iflt, ifle ....
Compound Conditions Branch: Tableswitch, Lookpuswitch
Unconditional Branch: Goto, Toto_w ....
8 method invocation and return instruction
The invokevirtual directive is used to invoke an instance method of an object.
Invokeinterface for calling interface methods
Invokespecial
Invokestatic
Invokedynamic is used to dynamically parse the method referenced by the call Point qualifier at run time
9 Exception handling
Explicit exception operations in Java programs (throw statements) are implemented by the Athrow directive
10 Synchronization Instructions
Deep understanding of Java Virtual Machines-sixth chapter