Java Class File Format Analysis
Preface
Learn jvm word code, you need to understand the way class is composed, this article explains the process of parsing the class file format in java method, I hope it can help you.
Class file specification
The code that is compiled and executed by the Java virtual machine uses a platform-neutral (hardware- and operating system-independent). It is often (but not always) stored as a file, so this format is called the Class file format. The Class file format defines the precise representation of classes and interfaces, including the conventions in the platform-dependent target file format. The Class file format defines the precise representation of classes and interfaces, including some detailed conventions in the platform-related target file format
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
Next, we start to parse out how each field is identified What do u4, u2 mean? u is an unsigned number, followed by a number indicating how many bytes it takes up u4 occupies 4 bytes u2 occupies 2 bytes
- magic takes up 4 bytes, (ca fe ba be)
- minor_version subversion number, 2 bytes
- major_version major version good 2 byte number
- constant_pool_count Number of constant pools 2 bytes
- constant_pool[constant_pool_count-1] Array of constant pools
- access_flags access_flags 2 byte number
- this_class index of the class name
- super_class index of the superclass name
- interfaces_count the number of interfaces
- interfaces[interfaces_count] Array of interfaces
- fields_count Number of fields
- fields[fields_count] Array of fields
- methods_count Number of methods
- methods[methods_count] Array of methods
- attributes_count The number of attributes
- attributes[attributes_count] array of attributes
How to solve a class file by yourself
I believe that most of the first time I saw the above protocol, I could read it, but to parse out the meaning of each field by myself. It is impossible to start.
- read the class file
FileInputStream in= new FileInputStream("d:/javaisland.class");
- read magic, (magic u4 takes up 4 bytes)
byte[] bytes=new byte[4];
in.read(bytes);
- read minor_version u2 occupies 2 bytes
byte[] minorByte=new byte[2];
in.read(minorByte);
- read major_version u2 occupies 2 bytes
byte[] majorVersion=new byte[2];
in.read(majorVersion);
See the above parsing, whether to understand, in fact, or very regular, as long as you look carefully at the protocol document (to see many times to do)
The final parsing class document is this
ClassFile classFile = new ClassFile();
PcBufferInputStream in = new PcBufferInputStream(new FileInputStream(fileName));
classFile.setMagic(readMagic(in));
classFile.setMinorVersion(readMinorVersion(in));
classFile.setMajorVersion(readMajorVersion(in));
classFile.setConstantPoolCount(readConstantPoolCount(in));
classFile.setCpInfo(readCpInfo(in));
classFile.setAccessFlags(readAccessFlags(in));
classFile.setThisClass(readThisClass(in));
classFile.setSuperClass(readSuperClass(in));
classFile.setInterfacesCount(readInterfacesCount(in));
// u2 interfaces interfaces_count
classFile.setInterfaces(readInterfaces(in));
// u2 fields_count
classFile.setFieldsCount(readFieldsCount(in));
// field_info fields fields_count
classFile.setFields(readFields(in));
// u2 methods_count 1
// method_info methods methods_count
classFile.setMethodsCount(readMethodsCount(in));
classFile.setMethods(readMethods(in));
// u2 attribute_count 1
classFile.setAttributeCount(readAttributeCount(in));
// attribute_info attributes attributes_count
classFile.setAttributes(readAttributes(in));
classFile.setPcRecord(recordMap);
return classFile;