2. Construct the input stream
FileInputStream fin = new FileInputStream (file);
DataInputStream in= New DataInputStream (New Bufferedinputstream (Fin));
3. Read the contents of the bytecode file (. Class)
3.1 Magic number
Unsigned 4
-byte int magic = In.readint ();
3.2 times version number
Unsigned 2 bytes
int minor_version = In.readunsignedshort ();
3.3 Major Version number
Unsigned 2
-byte int major_version = In.readunsignedshort ();
3.4 Number of constant pools
Unsigned 2 bytes
int count = Cr.readunsignedshort ();
3.5 contents of a constant pool
Cpinfo is the type of constant in a constant pool, and cannot simply be interpreted as a string type cpinfo[] pool = new Cpinfo[count] in a constant pool;
for (int i = 1; i < count; i++) {//constant pool of constant type tag int tag = Cr.readunsignedbyte ();
Switch (TAG) {//class or interface notation reference case Constant_class:pool[i] = new Constant_class_info (this, CR);
Break
Double-precision floating-point literal case constant_double:pool[i] = new Constant_double_info (CR);
i++;
Break
Field symbol reference case Constant_fieldref:pool[i] = new Constant_fieldref_info (this, CR);
Break
Floating-point literal case constant_float:pool[i] = new Constant_float_info (CR);
Break
Integer literal case Constant_integer:pool[i] = new Constant_integer_info (CR);
Break
Interface method Reference Case Constant_interfacemethodref:pool[i] = new Constant_interfacemethodref_info (this, CR);
Break
Dynamic invoke command case constant_invokedynamic:pool[i] = new Constant_invokedynamic_info (this, CR);
Break Long-integer literal case constant_long:pool[i] = new Constant_long_info(CR);
i++;
Break
Method Handler Case Constant_methodhandle:pool[i] = new Constant_methodhandle_info (this, CR);
Break
Method Type Case Constant_methodtype:pool[i] = new Constant_methodtype_info (this, CR);
Break
Method refers to case constant_methodref:pool[i] = new Constant_methodref_info (this, CR);
Break
Name and type case Constant_nameandtype:pool[i] = new Constant_nameandtype_info (this, CR);
Break
String case Constant_string:pool[i] = new Constant_string_info (this, CR);
Break
UTF8 encoded string case Constant_utf8:pool[i] = new Constant_utf8_info (CR);
Break
Illegal type default:throw new Invalidentry (i, tag); }
}
Construction details
For example, if you construct a constant structure of double type constant_double_info, you save a double value in this structure.
In addition, some other information is maintained, the byte length of the structure, the type tag.
Some other structures, similar.
3.6 Access Identifiers
Unsigned 2
-byte int access_flag= cr.readunsignedshort ()
3.7 Subscript of this class in a constant pool
Unsigned 2
-byte int this_class = In.readunsignedshort ();
3.8 Subscript for the parent class of this class in a constant pool
Unsigned 2
-byte int super_class = In.readunsignedshort ();
3.9 Number of parent interfaces
Unsigned 2 bytes
int int interfaces_count = Cr.readunsignedshort ();
3.10 Contents of the Parent interface
interfaces = new Int[interfaces_count];
for (int i = 0; i < Interfaces_count; i++) {
//parent interface type, unsigned 2 bytes
interfaces[i] = Cr.readunsignedshort ();
}
3.11 Number of fields
Unsigned 2
-byte int int fields_count = Cr.readunsignedshort ();
3.12 The contents of the field
Fields = new Field[fields_count];
for (int i = 0; i < Fields_count i++) {
fields[i] = new Field (CR);
A field has an access identifier, name, descriptor, attribute
access_flags = Cr.readunsignedshort ();
Name_index = Cr.readunsignedshort ();
Descriptor = Cr.readunsignedshort ()
attributes, encapsulates more details for a field
3.13 Number of methods, unsigned 2 bytes
int methods_count = Cr.readunsignedshort ();
3.14 Content of the method
methods = new Method[methods_count];
for (int i = 0; i < methods_count i++)
methods[i] = new Method (CR); A method has access identifier, name, descriptor, attribute
access_flags = Cr.readunsignedshort ();
Name_index = Cr.readunsignedshort ();
Descriptor = Cr.readunsignedshort ()
attributes, encapsulates more detailed information about the method
3.15 number of attributes
Unsigned 2
-byte int attrs_count = Cr.readunsignedshort ();
3.16 Contents of the property
attribute[] attrs = new Attribute[attrs_count];
for (int i = 0; i < Attrs_count i++) {
Attrs[i] = readattribute ();
}
/**
* Read an attribute/public attribute
Readattribute () throws IOException {
//name index
int name_ index = Readunsignedshort ();
The length of the property content
int = readInt ();
byte[] data = new Byte[length];
Reads the length of bytes of information into the data
readfully (data);
DataInputStream prev = in;
in = new DataInputStream (new Bytearrayinputstream (data));
try {
//construct an attribute object by the index of the name, the content of the byte array
attributefactory.createattribute (this, name_index, data) ;
} finally {in
= prev;
}
}
4. Summary
A bytecode file (. Class) contains almost all of the information for a class or interface.
Although this information exists in binary form, their storage structure is still regular.
A byte-code file can be parsed completely by reading the corresponding number of bytes in the stored format.
5. Challenge the Limit
Having finished talking about the structure of bytecode files and the method of reading bytecode files, some readers may ask how bytecode files are generated.
The Java programs we write are all text-formatted. java files, and how they are converted.
In fact, the. Java to. class conversion process is the main function of the Java compiler Javac.
java compiler javac more difficult, just started research, such as experience and research success, must be the first time to share.
6. related reading openjdk Source Study notes (i)-parameter checking & throwing exception OpenJDK Source research notes with critical error messages (ii)- The roles and distinctions of comparable and COMPARATOR2 interfaces (a classic Java written interview question) openjdk Source Research Notes (iii) the role of-randomaccess and other tag interfaces
OpenJDK Source Research Notes (iv)-Authoring and organizing reusable tool classes and methodologies
OpenJDK Source Research Notes (v)-cache integer and other types of frequently used data and objects, significantly improve performance (a classic Java written questions)
OpenJDK Source Research Notes (VI)--observer Mode tool classes (Observer and observable) and application examples
OpenJDK Source Research Notes (vii) The structure of the –java bytecode file (. Class)
original text See also : http://FansUnion.cn/articles/2974