Java Class File Format Analysis


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

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)


  1. minor_version subversion number, 2 bytes


  1. major_version major version good 2 byte number


  1. constant_pool_count Number of constant pools 2 bytes


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


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

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));
        // u2 interfaces interfaces_count
        // u2 fields_count
        // field_info fields fields_count
        // u2 methods_count 1
        // method_info methods methods_count
        // u2 attribute_count 1
        // attribute_info attributes attributes_count
        return classFile;