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

Related documents

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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

  1. magic takes up 4 bytes, (ca fe ba be)

image

  1. minor_version subversion number, 2 bytes

image

  1. major_version major version good 2 byte number

image

  1. constant_pool_count Number of constant pools 2 bytes

image

  1. constant_pool[constant_pool_count-1] Array of constant pools

image

  1. access_flags access_flags 2 byte number
  2. this_class index of the class name
  3. super_class index of the superclass name
  4. interfaces_count the number of interfaces
  5. interfaces[interfaces_count] Array of interfaces
  6. fields_count Number of fields
  7. fields[fields_count] Array of fields
  8. methods_count Number of methods
  9. methods[methods_count] Array of methods
  10. attributes_count The number of attributes
  11. 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.

  1. read the class file
1
 FileInputStream in= new FileInputStream("d:/javaisland.class");
  1. read magic, (magic u4 takes up 4 bytes)
1
2
 byte[] bytes=new byte[4];
       in.read(bytes);
  1. read minor_version u2 occupies 2 bytes
1
2
 byte[] minorByte=new byte[2];
       in.read(minorByte);
  1. read major_version u2 occupies 2 bytes
1
2
 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;