I. Class File Content
Note: Each Java class can only have one class file or interface. In the following discussion, U4 represents four bytes, while U2 represents two bytes.
The content of the class file is as follows:
- Magic (magic number)-U4 (U4 indicates 4 bytes)
- 0 xcafebabe, which is used to easily identify Java class files and non-Java class files;
- Minor_version-u2 and major_version-u2 (times, major version number)
- For virtual machines, the primary and secondary version numbers determine the specific class file format. Generally, the virtual machine can read the class file only after the primary version number and a series of minor version numbers are given;
- As shown in
5th and 6 bytes indicate the minor version = 0
7th and 8 bytes indicate the primary version number = 50 (the front position is high)
- Constant_pool_count-u2 and constant_pool (constant pool)
- The number of constant pools is constant_pool_count-1, and its index starts from 1;
- In addition to direct constants, the constant pool also contains symbol references such as the full qualified names of classes and interfaces, field names and descriptors, and method names and descriptors;
- Each constant pool entry has a byte mark, indicating the constant type of the position in the list. After obtaining the flag, the virtual machine will know the constant type behind the flag. Each flag has a corresponding table structure to describe the constant pool. The specific types are shown in the following table:
- Access_flags (access flag)-U2
- The access flag displays the following information about classes or interfaces in a file:
Flag name |
Value |
Description after setting |
Acc_public |
Zero X 0001 |
Public type |
Acc_final |
Zero X 0010 |
Class is final type |
Acc_super |
Zero X 0020 |
Use the new invokespecial Semantics |
Acc_interface |
Zero X 0200 |
Interface type, not class type |
Acc_abstract |
Zero X 0400 |
Abstract type |
Appendix: invokespecial and invokevirtual
Invokespecial refers to the method that is called by JVM after static binding. For example, super () and super. somemethod () all belong to invokespecial;
Invokevirtual refers to the method that is called by JVM after dynamic binding. For example, obj. somemethod (), which belongs to invokevirtual;
Because of the differences between the two types of binding, polymorphism and other special call results are generated only after the subclass covers the superclass method and references are transformed up.
- This_class-u2 (current class)
- It is an index on the constant pool. The entry to the constant pool at the this_class position must be the constant_class-type table structure constant_class_info ...;
- Shows how to use the constant pool:
In the bytes of the constant_utf8_info table, the fully qualified name string of the current class is stored;
- Super_class-u2 (direct superclass)
- It is the same as this_class;
- Except java. Lang. object classes, the constant pool index super_class is valid for all classes;
- For Java. Lang. Object, its super_class is 0;
- For interfaces, super_class points to the constant pool of Java. Lang. object;
- Interfaces_count-u2 and interfaces (interface List)
- Number of interfaces that can be directly implemented or extended by the class;
- The interface appears in the order in which implements clauses and extends clauses appear;
- Each item in the interfaces table is an index to the constant pool of the parent interface, which is described using constant_class_info;
- If the number of interfaces is 0, no interfaces table exists;
- Fields_count-u2 and fields (Field List)
- Fields declared by the class or interface are listed in the fields list only. Fields inherited from the superclass or parent interface are not listed;
- The fields list may contain fields not described in the corresponding Java source file, because the Java compiler may add fields to the class or interface during compilation; for example, for the fields list of an internal class, to maintain reference to the peripheral class instance, the Java compiler adds instance variables for each peripheral class instance;
- The field_info table contains the following fields:
- Field name
- Field descriptor and Modifier
- If this field is declared as final, it also contains its constant value;
- Methods_count-u2 and methods (method List)
- Methods_count indicates the total number of all methods declared in this class or interface, excluding methods inherited from superclasses or all parent interfaces;
- The Methods list contains the following information:
- Method Name and Descriptor (Return Value Type and parameter type)
- If the method is neither abstract nor local, it also contains the stack space length required for the local variables of the method, the exception table captured by the method, the bytecode sequence, and the optional number of rows and the local variable table.
- If the method can throw any verified exceptions, the methods list includes a list of these verified exceptions.
- Attributes_count-u2 and attributes (attribute list)
- The last part of the class file is the attribute, which provides the basic information of the attributes defined by the class or interface in the file;
- The first entry of each attribute_info table is the index pointing to the constant_utf8_info table in the constant pool, which provides the attribute name;
Example of class file content:
- Code
Public interface myinterface {
Void Hello ();
}
- The bytecode after compilation is as follows:
- The number of constant pools "0009" indicates that there are eight constant pool items next to it.
- Constant pool Index 1 indicates a constant_class_info table (07), which references the constant pool whose index is 7;
- Constant pool index 2 indicates a constant_class_info table (07), which references the constant pool with an index of 8;
- Constant pool index 3 indicates a constant_utf8_info table (01), whose content is Hello (method name );
- Constant pool index 4 indicates a constant_utf8_info table (01), whose content is () V (method parameters and return values );
- Constant pool index 5 indicates a constant_utf8_info table (01), whose content is sourcefile (a property value );
- Constant pool index 6 indicates a constant_utf8_info table (01), whose content is myinterface. Java (a property value );
- Constant pool index 7 indicates a constant_utf8_info table (01) with the content of myinterface, which is referenced by the constant pool Index 1 (current class );
- Constant pool index 8 indicates a constant_utf8_info table (01). Its content is Java/lang/object and is referenced by constant pool index 2 (superclass );
- The access flag "0601" indicates public (0001), Abstract (0400), and interface (0200 );
- The current class index number "0001" indicates pointing to the constant pool Index 1, indicating that the current class is myinterface;
- The superclass index number "0002" indicates pointing to the constant pool index 2, indicating that the superclass is Java/lang/object;
- The number of Implemented interfaces "0000" indicates that no interfaces are implemented;
- The number of fields "0000" indicates that this interface has no fields;
- The number of methods "0001" indicates that the interface has a method;
- The remaining bytecode is the method list and attribute list;
Ii. Special strings
Fully Qualified name
When the constant pool points to a class or interface, it gives a fully qualified name, such as Java/util/hashtable;
Simple name
Field Names and method names appear in the constant pool entry in the form of simple names. In the above example, the constant pool index 3 contains "hello ";
Descriptor
- Symbol references pointing to fields and methods also contain descriptor strings. field descriptors provide the field types, and method descriptors provide the return values of methods and the quantity, type, and sequence of method parameters;
- Descriptor uses context-independent Syntax: italic indicates non-Terminator, and an equal-width font indicates Terminator;
- As follows:
- Terminator of the basic type
Terminator |
Type |
B |
Byte |
C |
Char |
D |
Double |
F |
Float |
I |
Int |
J |
Long |
S |
Short |
Z |
Boolean |
- Descriptor example
Descriptor |
Description |
I |
Int I; |
[Ljava/lang/object; |
Java. Lang. object [] OBJ; |
([Bii) ljava/lang/string; |
String method (byte [] B, int I, Int J) |
Ziljava/lang/string; II () Z |
Boolean method (Boolean B, int I, string S, Int J, int K) |
This article is reproduced, original: http://diecui1202.iteye.com/blog/611476