Analyze the Class file structure
1. An overview of the Class file structure
2. Description of each data
type | The name of the | The number of | instructions |
---|---|---|---|
u4 | magic | 1 | Magic number: Determines whether a file is a Class file |
u2 | minor_version | 1 | The secondary version number of the Class file |
u2 | major_version | 1 | Major version number of Class files: A JVM instance can only support a certain range of version numbers of Class files (which are backward compatible). |
u2 | constant_pool_count | 1 | Quantity of constant scale |
cp_info | constant_pool | constant_pool_count-1 | Constant pool: To be understood as a repository of Class files, other subsequent data items can refer to the constant pool contents. |
u2 | access_flags | 1 | Class access flag information: used to indicate the access permissions and underlying properties of the class or interface. |
u2 | this_class | 1 | Constant index to the current class: used to determine the fully qualified name of this class. |
u2 | super_class | 1 | Index to superclass constants: Used to determine the fully qualified name of the class’s superclass. |
u2 | interfaces_count | 1 | Number of interfaces |
u2 | interfaces | interfaces_count | Constant index to the interface: used to describe which interfaces this class implements. |
u2 | fields_count | 1 | Number of field tables |
field_info | fields | fields_count | Field table collection: Describes all the fields declared by the current class or interface. |
u2 | methods_count | 1 | Number of method tables |
method_info | methods | methods_count | Collection of method tables: describes only methods declared in the current class or interface, excluding methods inherited from a parent class or interface. |
u2 | attributes_count | 1 | Number of property sheets |
attributes_info | attributes | attributes_count | Collection of property sheets: Used to describe information that is specific to certain scenarios, such as bytecode instruction information, etc. |
3. Hexadecimal parsing of Class files
3.1 the magic number.
- The Class file starts with 4 bytes defined as the Magic Number;
- Only function: determine whether a file is a Class file;
- The magic number can be chosen freely, as long as it is not widely used and not confusing, so that it is not unrecognizable because of the extension change; Many other file type format headers have magic numbers, such as GIF, JPEG, and so on
- The Class file has a magic number of “0xCAFEBABE”, compared to ClassFileTest. Class:
3.2 Method table parsing
- public void bar()
00 01 public 00 11 = Utf8 bar 00 0b = Utf8 bar 00 0b #11 = Utf8 ()V Public void bar() 00 01 indicates that there is one attribute table attribute_count Attribute table attribute_info(u2,u4,u1*length) 00 0c indicates that attribute_name_index, index, 0c = Constant pool 0c = Constant pool #12 = Utf8 Code 00 00 00 38 = attribute_length 00 02 means max_stack is 2 00 01 means max_locals is 1 00 00 00 0a means code_length lines of code, In this case, 0a is the number of arguments to go from hexadecimal to 10 to args_size (the default is this, If the method is static then 0) corresponds to the bytecode 00 02 00 01 00 00 0a B2 00 02 B2 00 03 B6 00 04 B1 00 00 00 00 02 00 0d 00 00 00 0a 00 02 00 00 00 00 0f 00 09 00 10 00 0e 00 00 00 0c 00 01 00 00 00 0a 00 0f 00 10 00 00 b2 getstatic 00 nop 03 iconst_0 b6 invokevirtual 04 iconST_1 LineNumberTable: line 15: 0 Line 16: 9 LocalVariableTable: Start Length Slot Name Signature 0 10 0 this Lcom/cecjx/TestM; Start + Length according to a local variable scope (0 corresponding line is 15, 10 corresponding line is 17 | | indicates that in the method, the scope of the variable is lines 15 to 17) Slot said several tank storage Name said simple Name Signature Signature pseudo generics.Copy the code
- Static variable initialization
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: iconst_2
1: putstatic #3 // Field m:I
4: return
LineNumberTable:
line 12: 0
Copy the code