We all know that the Java compiler is responsible. compile java files. the class file stores Java bytecode and. java files are irrelevant (as long as you are willing to write a compiler, you can also compile the source code written in other languages. class file), this article will prepare a detailed anatomy of the internal structure of the class file, and read and display the class file structure.
The format of the class file is defined by the JVM specification and consists of the following parts:
1. magic number, which must be 0 xcafebabe, is used to quickly identify whether it is a class file.
2. version, including major and minor. if the version number exceeds the JVM recognition range, the JVM rejects execution.
3. Constant pool, a constant pool, stores all constants used.
4. Access flag, which defines the class access permissions.
5. This class and super class indicate how to find this class and super class.
6. Interfaces: stores all interfaces.
7. fields: stores all fields.
8. Methods: stores all methods.
9. attributes: stores all attributes.
First write a test. Java:
Package example. test;
Public final class testclass {
Public int id = 1234567;
Public void test (){}
}
Compile the program and place it in C:/example/test. Class.
We use Java to read and analyze the class. The function of classanalyzer is to read test. Class, analyze the structure, and then display it:
Package classfile. format;
Import java. Io .*;
Public class classanalyzer {
Public static void main (string [] ARGs ){
Datainputstream input = NULL;
Try {
Input = new datainputstream (New bufferedinputstream (New fileinputstream (
"C: // example // test // testclass. Class"
)));
Analyze (input );
}
Catch (exception e ){
System. Out. println ("analyze failed! ");
}
Finally {
Try {input. Close ();} catch (exception e ){}
}
}
Public static void analyze (datainputstream input) throws ioexception {
// Read magic number:
Int magic = input. readint ();
If (MAGIC = 0 xcafebabe)
System. Out. println ("magic number = 0 xcafebabe ");
Else
Throw new runtimeexception ("invalid magic number! ");
// Read minor version and major version:
Short minor_ver = input. readshort ();
Short major_ver = input. readshort ();
System. Out. println ("version =" + major_ver + "." + minor_ver );
// Read Constant pool:
Short const_pool_count = input. readshort ();
System. Out. println ("constant pool size =" + const_pool_count );
// Read each constant:
For (INT I = 1; I analyzeconstant (input, I );
}
}
Public static void analyzeconstant (datainputstream input, int index) throws ioexception {
Byte flag = input. readbyte ();
// For read:
Byte n8;
Short n16;
Int n32;
Long n64;
Float F;
Double D;
Byte [] buffer;
System. Out. println ("/nconst Index =" + index + ", flag =" + (INT) Flag );
Switch (FLAG ){
Case 1: // UTF-8 string
System. Out. println ("const type = utf8 ");
N16 = input. readshort ();
System. Out. println ("length =" + n16 );
Buffer = new byte [n16];
Input. readfully (buffer );
System. Out. println ("value =" + new string (buffer ));
Break;
Case 3: // integer
System. Out. println ("const type = integer ");
N32 = input. readint ();
System. Out. println ("value =" + n32 );
Break;
Case 4: // float
System. Out. println ("const type = float ");
F = input. readfloat ();
System. Out. println ("value =" + F );
Break;
Case 5: // long
System. Out. println ("const type = long ");
N64 = input. readlong ();
System. Out. println ("value =" + n64 );
Break;
Case 6: // double
System. Out. println ("const type = Double ");
D = input. readdouble ();
System. Out. println ("value =" + D );
Break;
Case 7: // class or interface reference
System. Out. println ("const type = Class ");
N16 = input. readshort ();
System. Out. println ("Index =" + n16 + "(where to find the class name )");
Break;
Case 8: // string
System. Out. println ("const type = string ");
N16 = input. readshort ();
System. Out. println ("Index =" + n16 );
Break;
Case 9: // Field Reference
System. Out. println ("const type = fieldref ");
N16 = input. readshort ();
System. Out. println ("class index =" + n16 + "(where to find the class )");
N16 = input. readshort ();
System. Out. println ("nameandtype =" + n16 + "(where to find the nameandtype )");
Break;
Case 10: // method reference
System. Out. println ("const type = methodref ");
N16 = input. readshort ();
System. Out. println ("class index =" + n16 + "(where to find the class )");
N16 = input. readshort ();
System. Out. println ("nameandtype =" + n16 + "(where to find the nameandtype )");
Break;
Case 11: // interface method reference
System. Out. println ("const type = interfacemethodref ");
N16 = input. readshort ();
System. Out. println ("class index =" + n16 + "(where to find the interface )");
N16 = input. readshort ();
System. Out. println ("nameandtype =" + n16 + "(where to find the nameandtype )");
Break;
Case 12: // name and type reference
System. Out. println ("const type = nameandtype ");
N16 = input. readshort ();
System. Out. println ("name index =" + n16 + "(where to find the name )");
N16 = input. readshort ();
System. Out. println ("descripter =" + n16 + "(where to find the descriptor )");
Break;
Default:
Throw new runtimeexception ("invalid constant pool flag:" + flag );
}
}
}
Output result:
Magic number = 0 xcafebabe
Version = 48.0
Constant pool size = 22
Const Index = 1, flag = 1
Const type = utf8
Length = 22
Value = example/test/testclass
Const Index = 2, flag = 7
Const type = Class
Index = 1 (where to find the class name)
Const Index = 3, flag = 1
Const type = utf8
Length = 16
Value = Java/lang/Object
Const Index = 4, flag = 7
Const type = Class
Index = 3 (where to find the class name)
Const Index = 5, flag = 1
Const type = utf8
Length = 2
Value = ID
Const Index = 6, flag = 1
Const type = utf8
Length = 1
Value = I
Const Index = 7, flag = 1
Const type = utf8
Length = 6
Value =
Const Index = 8, flag = 1
Const type = utf8
Length = 3
Value = () V
Const Index = 9, flag = 1
Const type = utf8
Length = 4
Value = Code
Const Index = 10, flag = 12
Const type = nameandtype
Name Index = 7 (where to find the name)
Descripter = 8 (where to find the descriptor)
Const Index = 11, flag = 10
Const type = methodref
Class Index = 4 (where to find the class)
Nameandtype = 10 (where to find the nameandtype)
Const Index = 12, flag = 3
Const type = integer
Value = 1234567
Const Index = 13, flag = 12
Const type = nameandtype
Name Index = 5 (where to find the name)
Descripter = 6 (where to find the descriptor)
Const Index = 14, flag = 9
Const type = fieldref
Class Index = 2 (where to find the class)
Nameandtype = 13 (where to find the nameandtype)
Const Index = 15, flag = 1
Const type = utf8
Length = 15
Value = linenumbertable
Const Index = 16, flag = 1
Const type = utf8
Length = 18
Value = localvariabletable
Const Index = 17, flag = 1
Const type = utf8
Length = 4
Value = This
Const Index = 18, flag = 1
Const type = utf8
Length = 24
Value = lexample/test/testclass;
Const Index = 19, flag = 1
Const type = utf8
Length = 4
Value = test
Const Index = 20, flag = 1
Const type = utf8
Length = 10
Value = sourcefile
Const Index = 21, flag = 1
Const type = utf8
Length = 14
Value = testclass. Java
We are currently read-only to get the constant pool, and the subsequent information will continue later.
Continue to analyze the structure of the class file.
The constant pool was read last time, followed by access flags of this class or interface. The access flags defined by JVM for class or interface include:
Private Static short acc_public = 0x0001;
Private Static short acc_final = 0x0010;
Private Static short acc_super = 0x0020;
Private Static short acc_interface = 0x0200;
Private Static short acc_abstract = 0x0400;
// Read access flags:
Short access_flags = input. readshort
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.