A. class file
Note: Each class file corresponds to a unique class or interface definition information, but the class or interface is not necessarily defined in a file (for example, a class or interface can also be generated directly from the ClassLoader).
Second, the file structure of class file
The class file format uses a pseudo-structure similar to the C language structure to store data, which has only two data types: unsigned number and table.
Structure description of class file
Classfile {U4 magic; <span style= "White-space:pre" ></SPAN>//4-bit unsigned number, indicating magic number U2 Minor_version;<span style= "White-space:pre" ></span>//minor version number U2 Major_version;<span style= "White-space:pre" ></span>// Major version number U2 Constant_pool_count;<span style= "White-space:pre" ></span>//constant pool counter cp_info constant_pool[ Constant_pool_count-1];<span style= "White-space:pre" ></span>//Changlianchi U2 access_flags;<span style= " White-space:pre "></span>//Access flag U2 this_class;<span style=" White-space:pre "></span>//class index U2 Super_class;<span style= "White-space:pre" ></span>//parent class index U2 Interfaces_count;<span style= " White-space:pre "></span>//Interface counter U2 interfaces[interfaces_count];<span style=" White-space:pre ">< /span>//Interface Table U2 fields_count;<span style= "White-space:pre" ></span>//field counter Field_info Fields[fields_ Count];<span style= "white-space:pre" ></span>//Field table U2 methods_count;<span style= "White-space:pre" ></span>//method Counter Method_info methods[methods_count];<span style= "White-space:pre" ></span>//method table U2 Attributes_ Count;<span style= "White-space:pre" ></span>//Property counter Attribute_info attributes[attributes_count];< Span style= "White-space:pre" ></span>//property sheet}
Preparing the Java file
public class Test {private int age;public int getage () {return age;} public static void Main (string[] args) {System.out.println ("Hello World");}}
The corresponding Test.class file
The corresponding text file
Cafe babe 0000 0034 0023 0a00 0700 14090006 0015 0900 1708 0018 0a00 19001a07 001b 0700 1c01 0003 6167 6501 00014901 0006 3c69 6e69 743e 0100 0328 29560100 0443 6f64 6501 000f 4c69 6e65 4e756d62 6572 5461 626c 6501 0006 6765 74416765 0100 0328 2949 0100 046d 6169 6e010016 285b 4c6a 6176 612f 6c61 6e67 2f537472 696e 673b 2956 0100 0a53 6f75 72636546 696c 6501 0009 5465 7374 2e6a 6176610c 000a 000b 0c00 0800 0907 001d 0c001e00 1f01 000b 4865 6c6c 6f20 576f 726c6407 0020 0c00 2100 2201 0004 5465 73740100 106a 6176 612f 6c61 6e67 2f4f 626a6563 7401 0010 6a61 7661 2f6c 616e 672f5379 7374 656d 0100 036f 7574 0100 154c6a61 7661 2f69 6f2f 5072 696e 7453 74726561 6d3b 0100 136a 6176 612f 696f 2f507269 6e74 5374 7265 616d 0100 0770 72696e74 6c6e 0100 1528 4c6a 6176 612f 6c616e67 2f53 7472 696e 673b 2956 0021 00060007 0000 0001 0002 0008 0009 0000 00030001 000a 000b 0001 000c 0000 001d 00010001 0000 0005 2ab7 0001 b100 0000 01000d00 0000 0600 0100 0000 0100 0100 0e000 F00 0100 0c00 0000 1d000100 0100 0000052a b400 02ac 0000 0001 000d 0000 00060001 0000 0005 0009 0010 0011 0001 000c0000 0025 0002 0001 0000 0009 B200 031204b6 0005 b100 0000 0100 0d00 0000 0a000200 0000 0800 0800 0900 0100 1200 00000200 13
Results viewed using the Javap-verbose test.class
The corresponding text
C:\users\weixu_000\desktop>javap-verbose Test.classclassfile/c:/users/weixu_000/desktop/test.class Last Modified 2015-7-4; Size 499 bytes MD5 checksum 580d9204b4544445687dea33327cdb0c Compiled from ' Test.java ' public class Test minor version: 0 Major version:52 Flags:acc_public, acc_superconstant pool: #1 = methodref #7. #20//Java/lang/obj ect. " <init>:() V #2 = Fieldref #6. #21//Test.age:i #3 = Fieldref #22. #23//java/ Lang/system.out:ljava/io/printstream; #4 = String #24//Hello World #5 = MethodRef #25. #26//Java/io/printstream.prin TLN: (ljava/lang/string;) V #6 = Class #27//Test #7 = Class #28//JAV A/lang/object #8 = Utf8 Age #9 = Utf8 I #10 = Utf8 <init> #11 = Utf8 () V #12 = Utf8 Code #13 = Utf8 linenumbertable #Utf8 getage #15 = Utf8 () I #16 = Utf8 main #17 = Utf8 ([Lj ava/lang/string;) V #18 = Utf8 sourcefile #19 = Utf8 Test.java #20 = Nameandtype #10: #11//"<init>":() V #21 = Nameandtype #8: #9//Age:i #22 = Class #29 Java/lang/system #23 = Nameandtype #30: #31//Out:ljava/io/printstream; #24 = Utf8 Hello World #25 = Class #32//Java/io/printstream #26 = Nameandtype #33: #34//println: (ljava/lang/string;) V #27 = Utf8 Test #28 = Utf8 java/lang/ob Ject #29 = Utf8 Java/lang/system #30 = Utf8 out #31 = Utf8 ljava/io/printstre Am #32 = Utf8 Java/io/printstream #33 = Utf8 println #34 = Utf8 (Ljava/lang/strin g;) v{public Test (); Descriptor: () V flagS:acc_public code:stack=1, Locals=1, args_size=1 0:aload_0 1:invokespecial #1 Method java/lang/object. " <init> ":() V 4:return linenumbertable:line 1:0 public int getage (); Descriptor: () I flags:acc_public code:stack=1, Locals=1, args_size=1 0:aload_0 1:getfield #2//Field age:i 4:ireturn linenumbertable:line 5:0 public static void Main (java.lang.string[]); Descriptor: ([ljava/lang/string;) V flags:acc_public, Acc_static code:stack=2, Locals=1, args_size=1 0 : getstatic #3//Field Java/lang/system.out:ljava/io/printstream; 3:LDC #4//String Hello World 5:invokevirtual #5//Method java/io/ PRINTSTREAM.PRINTLN: (ljava/lang/string;) V 8:return linenumbertable:line 8:0 line 9:8}source File: "Test.java"
Preparation completed.
Iii. Overview of class files
1. Magic number and primary and secondary version information
The first four bytes of the class file are magic numbers, which determines whether the file is a class file that can be received by the virtual machine. The 4 bytes of the magic number are the version number of the class file. The 5th and 6 bytes are the minor version number, 7, and 8 bytes are the major version number.
2, Chang
followed by the primary and secondary version of the constant pool entrance, the constant pool can be understood as a class file in the repository, is one of the most space in the class file.
Because the number of constants in a constant pool is not fixed, a U2 (unsigned 2-byte size) data is placed at the entrance of the constant pool, representing the constant pool capacity counter (Constant_pool_count), which starts at 1 and is not zero-based.
0023, the decimal 35, which has 34 item constants, the index value range is 1---34.
Verify: Javap-verbose test.class View the picture, you can see that there are 34 items in the constant pool.
2.1 Why is the index not starting from 0?
The purpose of doing this is to satisfy some of the subsequent data that points to the index value of the constant pool, in a particular case, to express the meaning of "do not refer to any of the constant pool items", which requires the index value to be expressed as a number of.
2.2 What data is stored in a constant pool? Two main classes: literal and symbolic references (related to the compilation principle)
Literals such as literal strings, constant values declared final, and so on.
Symbol References: Fully qualified names of classes and interfaces, names and descriptors of fields, and method names and qualifiers
Since Java code is javac compiled, it is not a step like C and C + +, but a dynamic link when the virtual machine loads the class file. This means that the final memory layout information for each method field is not saved in the class file. When the virtual machine is running, it needs to obtain the corresponding symbol reference from Chang, and then parse it at the time of class creation or run-time, translating it into the memory address of the specific memory.
Data in a constant pool, each item is a table
Format of each item in a constant pool
cp_info {u1 tag;u1 info[]; ...}
0a00 0a is the tag bit, 0a is the decimal 10, check the table can get 10 is method_info methods[methods_count];//method table. That is, here is the method table
3. Access Mark Access_flags
An access flag, Access_flags is a mask flag that represents the access rights and underlying properties of a class or interface
After the Chang is finished, two bytes represents the access flag access_flags.
A total of 16 flags in Access_flag can be used. Only 8 are currently defined. No use of the flag is 0
The test class is a normal class, not an interface, enumeration, or annotation. be modified by public, no final
So Acc_public,acc_super is true, Acc_final,acc_interface,acc_abstract,acc_synthetic,acc_annotation,
Acc_enum These six flag bits are false, so the flag bit is 0x0001|0x0020=0x0021
In validation, you can also see that the last position of the constant pool is V, followed by the access flag 0021.
The specific access flags and access flags are as follows
4. Class index, parent class index, interface index collection
Class index, the value of the parent class index must be a valid index value for the item in the Constant_pool table. The class index and the parent class index are the index value representations of two U2 types, respectively.
After the access flag, it is the Sequential class index, the parent class index, the interface index
Interface index, the first U2 type of data is the interface counter Interfaces_count, which represents the capacity of the index table
Indicates that the class index is 6, the parent class index is 7, and the interface index collection size is 0
And then the constant pool calculated based on the JAVAP
0006 0007 0000 Indicates that the class index is 6, the parent class index is 7, the interface index collection size is 0, and then the constant pool class index for the JAVAP is calculated as 6, the sixth item is #6 = Class #27 //Testindex is 27 found 27th, is #27 = Utf8 test, that is, the class name is the Test parent class index is 7, find the seventh Item # # = Class #28 //Java/lang/objectindex is 28 found, #28 = Utf8 java/ Lang/object, which is the parent class is an Object
5. The Field table collection describes the variables declared in the interface or class. The variables here are class-level variables and do not include the local variables of the method.
Before the field table, there are U2 capacity counters, then access_flags
Field table structure
The value of the Field_info {//access_flags key is a mask flag U2 access_flags that defines the access rights and underlying properties of the field, and the value of the//name_index item must be a valid index to the constant pool. Chang the entry at the index must be a CONSTANT_UTF8_INFO structure that represents a valid field with a non-fully qualified name U2 Name_index; The value of the Descriptor_index key must be a valid index to the constant pool. Chang the item at that index must be a CONSTANT_UTF8_INFO structure that represents a valid field descriptor U2 the value of the item Descriptor_index;////attributes_count represents the number of attached properties for the current field. U2 the value of each member of a attributes_count;//attributes table must be a attribute structure, and a field can have any of the associated properties. Attribute_info Attributes[attributes_ Count];}
Field Access Flags
Fields with the ACC_SYNTHETIC flag indicate that the field is not generated by the source code, but is generated automatically by the compiler.
Describe the meaning of the Descriptor literacy character
B ==byte, C==char, d==double,f==float,i==int,j==long,s==short,z==boolean,v==void,l== object type
Array type, each dimension is denoted by a predecessor ' [', such as string[][], as "[[java/lang/string], int[] is recorded as" [I "
Descriptor describes the method, the method that returns the value after the first argument list, such as lang.tostring (), is described as "() ljava/lang/string"
int indexOf (char[] source, int sourceoffset, int sourcecount,char[] target, int tagetoffset, int targetcount, int frominde X
This method is described as "([CII[CIII) I"
In the Test.class instance.
<span style= "White-space:pre" ></span>0001 0002 0008 0009 0000 instance 0001 fields_count Capacity table, 1, indicating only one field table data. 0002 for private decoration 0008 Name_index for 8, table #8 = Utf8 Age , for attribute name 0009 Descriptor_index, 9, table #9 = Utf8 I, you can draw field PRI vate int age;0000 No additional information
6. Collection of method tables
Structure description of the collection
The value of the Method_info {//access_flags key is the mask flag that defines the access rights and basic properties of the current method. Flags such as table U2 the value of the Access_flags;//name_index item must be a valid index to the constant pool. Chang the entry at that index must be a constant_utf8_info structure, or it either represents the name of the initialization method (<init> or <clinit>), or a valid non-fully qualified name of a method U2 name_index;/ The value of the/descriptor_index key must be a valid index to the constant pool. Chang the entry at that index must be a CONSTANT_UTF8_INFO structure that represents a valid method descriptor U2 Descriptor_index;//attributes_count The value of the item that represents the number of additional properties of this method U2 The value of each member of a attributes_count;//attributes table must be a attribute structure, and a method can have any property associated with it. Attribute_info Attributes[attributes_count];}
Note: The definition of the method and the property information are in the method table, but the code inside the method is not here, and is stored in a property named code in the method's attribute table collection.
The entry is a U2 type counter (Methods_count), which represents the number of methods, and then the access flag.
Method Access Flags
In the Test.class instance.
<span style= "White-space:pre" ></span>0003 0001 000a 000b 0001 000c0003:methods_count with a value of 3, three methods, GetAge (), Main (), as well as the compiler added instance constructor <init> method 0001:access_flags, the access flag bit of the first method, that is, only Acc_public is true 000a: The Name_index of the first method, with a value of 10, Look up the table #10 = Utf8 <init>, is the Init method 000b: The first method descriptor index, the value is 11, look up the table #11 = Utf8 () V, that is, the return value of the method is Void0001:attributes_count, the method's The property sheet collection has an attribute 000c:attributes_name_index, a value of 12, a table #12 = Utf8 Code
6.1 Overloading of methods
In Java, overloading a method, in addition to having the same simple name as the original method, requires that you have a signature that differs from the original method, which is a collection of field symbol references for each parameter in a method in a constant pool. The return value is not included in signature signatures, so the Java language cannot judge method overloads by relying only on the return value.
7. Attribute table Collection
Common formatting for property sheets
Attribute_info {//attribute_name_index must be a valid 16-bit unsigned index for the constant pool of the current class file U2 Attribute_name_index;//attribute_length The value of the item gives the length of the byte followed,//This length does not include the 6 bytes of the Attribute_name_index and Attribute_name_index entries U4 attribute_length;u1 info[attribute_ Length];}
To correctly parse the class file, the Java Virtual Machine Specification pre-defines the attributes that should be recognized by the 20 virtual machines.
Several common property descriptions in the property sheet
7.1code
After the code in the Java program method body is processed by the Javac compiler, the bytecode instruction is eventually stored inside the code attribute.
code_attribute {//attribute_name_index key must be a valid index to the constant pool,//Chang the entry at that index must be Constant_utf8 A _info structure that represents the string "Code". The value of the U2 Attribute_name_index;//attribute_length item represents the length of the current property, not including the starting 6 bytes U4 Attribute_length;//max_stack The value of the item gives the maximum depth of the current method's operand stack at any point in time at which execution is run U2 max_stack;//represents the storage space required for the local variable table, in Slot,slot is the smallest unit used by the virtual machine to allocate memory for local variables,// Double and long require two slots, other basic types include returnaddress require a SLOTU2 max_locals;//code_length item gives the number of bytes in the code[] array of the current method, Code_length The value must be greater than 0, that is, the code[] array cannot be empty U4 code_length;//code[] array gives the Java Virtual machine byte code to implement the current method U1 code[code_length];//exception_table_length The value of the item gives the number of members of the exception_table[] array U2 exception_table_length;//exception_table[] Each member of the array represents code[] An exception handler (Exception Handler) in the array. Exception_table[] Array, the exception handler order is meaningful (cannot be arbitrarily changed) {U2 start_pc;u2 end_pc;u2 handler_pc;u2 Catch_type;} exception_table[ The value of the Exception_table_length];//attributes_count key gives the number of members of the attributes table in the code attribute U2 attributes_count;// The value of each member of the property sheet must be a attribute structure. A code property can have any number of optional attributes associated with it attribute_info Attributes[attributes_count];}
The Code property is a variable-length property that is located in the property sheet of the method_info structure. A Code property holds Java Virtual machine directives for only one method, instance class initialization method, or class initialization method, and related auxiliary
Help information. All Java Virtual machine implementations must be able to recognize the code attribute. If a method is declared as a native or abstract type, then the corresponding METHOD_INFO structure cannot have an explicit code attribute, and in other cases, the method_info must have an explicit code attribute.
7.2Exceptions Properties
Exceptions_attribute {//must be a valid index to a constant pool. Chang the member at that index must be a CONSTANT_UTF8_INFO structure that represents the value of the string "Exceptions" U2 attribute_name_index;//attribute_length item gives the length of the current property, Does not include the starting 6 bytes. The value of the U4 attribute_length;//number_of_exceptions item gives the number of members in the exception_index_table[] array U2 number_of_exceptions;// Exception_index_table[] The value of each member of the array must be a valid index to the constant pool. Chang members at these indexes must all be constant_class_info structures, representing the type of class that this method declares the exception to throw U2 exception_index_table[number_of_exceptions];
A method if you want to throw an exception, you must satisfy at least one of the following three conditions:
to throw an instance of RuntimeException or its subclasses.
you want to throw an instance of error or its subclass.
to throw an instance of the exception class or its subclasses declared in the exception_index_table[] array.
These requirements are not forced to be checked in the Java virtual machine, they are only enforced at compile time.
The 7.3ConstantValue property notifies the virtual machine to automatically assign a value to a static variable, which can only be used by static modified variables.
The value of the Constantvalue_attribute {//attribute_name_index key must be a valid index to the constant pool. Chang the entry at that index must be a CONSTANT_UTF8_INFO structure that represents the string "Constantvalue". The value of the Attribute_length entry for the U2 attribute_name_index;//constantvalue_attribute structure is fixed to 2u4 attribute_length;//constantvalue_ The value of the index entry must be a valid index to the constant pool. Chang the entry at the index gives the constant value represented by the property. U2 Constantvalue_index;}
The 7.4LineNumberTable attribute is used to describe the correspondence between a Java source line number and a byte code line number.
Linenumbertable_attribute {u2 attribute_name_index;u4 attribute_length;u2 line_number_table_length;{U2 START_PC;U2 Line_number;} Line_number_table[line_number_table_length];}
The 7.5LocalVariableTable attribute is used to describe the relationship between variables in a local variable table in a stack frame and variables defined in Java source code.
Localvariabletable_attribute {u2 attribute_name_index;u4 attribute_length;u2 local_variable_table_length;{U2 Start_ PC;U2 length;u2 name_index;u2 DESCRIPTOR_INDEX;U2 Index;} Local_variable_table[local_variable_table_length];}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java class post-compilation class file Overview (top)