Java Virtual Machine Learning --- Class file parsing

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u010838555/article/details/102768273

the overall structure of the class file: 

Types of name    Explanation length
u4 magic Magic number, identifying Class File Format 4 bytes
u2             minor_version Minor version 2 bytes
u2          major_version  The major version number  2 bytes
u2            constant_pool_count Constant pool Calculator  2 bytes
cp_info             constant_pool Constant pool n bytes
u2             access_flags Access flag 2 bytes
u2             this_class Class Index 2 bytes
u2             super_class Parent index 2 bytes
u2           interfaces_count Interface counter  2 bytes
u2            interfaces Interface index set  2 bytes
u2             fields_count Number of fields 2 bytes
field_info          fields Fields Collection n bytes
u2            methods_count Methods counter  2 bytes
method_info    methods Collection method n bytes
u2             attributes_count Additional property counters 2 bytes
attribute_info   attributes Additional property collection   n bytes

   Table u1, u2, u4, u8 to represent one byte, unsigned 2-byte, four-byte and eight-byte, unsigned number can be used to describe figures, reference index, the number of value or string value encoded in accordance with UTF-8 configuration.

Person class declaration as follows:

package build;

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name:"+name+",age:"+age;
    }
}

Main class declaration as follows:

package build;

public class Main {

    public static String TAG = "This is a tag";
    public static int mType = 1234;

    public static void main(String[] params) {
        Person person = new Person();
        person.setAge(18);
        person.setName("范冰冰");
        print(person);
    }

    public static void print(Person person) {
        System.out.println(person.toString());
    }
}

Here only parse class files compiled after the Main class --- Main.class

Content class files compiled using the command javac Main.java as follows (because of the use javac command to compile only the current directory files, and object references Main Person object, there will be problems compilation fails, only you need to build the upper folder to javac . build / *):

cafe babe 0000 0034 003a 0a00 0f00 1f07
0020 0a00 0200 1f0a 0002 0021 0800 220a
0002 0023 0a00 0e00 2409 0025 0026 0a00
0200 270a 0028 0029 0800 2a09 000e 002b
0900 0e00 2c07 002d 0700 2e01 0003 5441
4701 0012 4c6a 6176 612f 6c61 6e67 2f53
7472 696e 673b 0100 056d 5479 7065 0100
0149 0100 063c 696e 6974 3e01 0003 2829
5601 0004 436f 6465 0100 0f4c 696e 654e
756d 6265 7254 6162 6c65 0100 046d 6169
6e01 0016 285b 4c6a 6176 612f 6c61 6e67
2f53 7472 696e 673b 2956 0100 0570 7269
6e74 0100 1128 4c62 7569 6c64 2f50 6572
736f 6e3b 2956 0100 083c 636c 696e 6974
3e01 000a 536f 7572 6365 4669 6c65 0100
094d 6169 6e2e 6a61 7661 0c00 1400 1501
000c 6275 696c 642f 5065 7273 6f6e 0c00
2f00 3001 0009 e88c 83e5 86b0 e586 b00c
0031 0032 0c00 1a00 1b07 0033 0c00 3400
350c 0036 0037 0700 380c 0039 0032 0100
0d54 6869 7320 6973 2061 2074 6167 0c00
1000 110c 0012 0013 0100 0a62 7569 6c64
2f4d 6169 6e01 0010 6a61 7661 2f6c 616e
672f 4f62 6a65 6374 0100 0673 6574 4167
6501 0004 2849 2956 0100 0773 6574 4e61
6d65 0100 1528 4c6a 6176 612f 6c61 6e67
2f53 7472 696e 673b 2956 0100 106a 6176
612f 6c61 6e67 2f53 7973 7465 6d01 0003
6f75 7401 0015 4c6a 6176 612f 696f 2f50
7269 6e74 5374 7265 616d 3b01 0008 746f
5374 7269 6e67 0100 1428 294c 6a61 7661
2f6c 616e 672f 5374 7269 6e67 3b01 0013
6a61 7661 2f69 6f2f 5072 696e 7453 7472
6561 6d01 0007 7072 696e 746c 6e00 2100
0e00 0f00 0000 0200 0900 1000 1100 0000
0900 1200 1300 0000 0400 0100 1400 1500
0100 1600 0000 1d00 0100 0100 0000 052a
b700 01b1 0000 0001 0017 0000 0006 0001
0000 0003 0009 0018 0019 0001 0016 0000
0041 0002 0002 0000 0019 bb00 0259 b700
034c 2b10 12b6 0004 2b12 05b6 0006 2bb8
0007 b100 0000 0100 1700 0000 1600 0500
0000 0900 0800 0a00 0e00 0b00 1400 0c00
1800 0d00 0900 1a00 1b00 0100 1600 0000
2700 0200 0100 0000 0bb2 0008 2ab6 0009
b600 0ab1 0000 0001 0017 0000 000a 0002
0000 0010 000a 0011 0008 001c 0015 0001
0016 0000 0028 0001 0000 0000 000c 120b
b300 0c11 04d2 b300 0db1 0000 0001 0017
0000 000a 0002 0000 0005 0005 0006 0001
001d 0000 0002 001e 

The following analytical content inside:

CA FE BA BE ---- magic, this logo is a class file. 

00 00 ---- minor version number

00 34 ---- main version number: 52, Java version number from 45 (Java1.1) start, when each major release, version number plus 1, on behalf of the Class file can be compiled JDK 1.8, JDK version of the high You can be backward compatible with previous versions of Class files.

00 3A ---- constant pool count, representing 58 constant. It starts counting from 1, representing the document 57 Class constant index range of 1 to 57.

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 0F ---- pointing to declare the method of the class descriptor CONSTANT_Class_info index entry: 15.

  00 1F ---- pointing to the name and type descriptor CONSTANT_NameAndType index entry: 31.

07 ---- 7, a symbol which is a reference to a class or interface. According to its known table structure, it took two bytes is its content.

  20 ---- 00 points to the fully qualified name of the constant index: 32.

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 02 ---- pointing to declare the method of the class descriptor CONSTANT_Class_info index entry: 2.

  00 1F ---- pointing to the name and type descriptor CONSTANT_NameAndType index entry: 31.

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 02 ---- pointing to declare the method of the class descriptor CONSTANT_Class_info index entry: 2.

  00 ---- 21 is pointing to the name and type descriptor CONSTANT_NameAndType index entry: 33.

08 ---- 8 illustrate a literal string type, based on its known table structure, it took two bytes is its content.

  00 22 ---- its index entries are: 34.

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 02 ---- pointing to declare the method of the class descriptor CONSTANT_Class_info index entry: 2.

  00 23 ---- pointing to the name and type descriptor CONSTANT_NameAndType index entry: 35.

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 0E ---- pointing to declare the method of the class descriptor CONSTANT_Class_info index entry: 14.

  00 ---- 24-point type descriptor CONSTANT_NameAndType name and index entries: 36.

09 --- 9, a symbol which is a reference field, based on its known table structure, is 4 bytes took its contents.

  25 ---- 00 point type field declared class or interface descriptor indexers CONSTANT_Class_Info: 37,

  00 26 ---- point field descriptor CONSTANT_NameAndType index entries: 38

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 02 ---- pointing to declare the method of the class descriptor CONSTANT_Class_info index entry: 2.

  00 27 ---- pointing to the name and type descriptor CONSTANT_NameAndType index entry: 39.

0A ---- 10, to illustrate this point is a symbolic reference to a method in the class, based on its known table structure, it is 4 bytes took its contents.

  00 28 ---- pointing to declare class method descriptor CONSTANT_Class_info index entry: 40.

  00 29 ---- pointing to the name and type descriptor CONSTANT_NameAndType index entry: 41.

08 ---- 8 illustrate a literal string type, based on its known table structure, it took two bytes is its content.

  00 2A ---- its index entries are: 42.

09 --- 9, a symbol which is a reference field, based on its known table structure, is 4 bytes took its contents.

  00 0E ---- point field type declaration of the class or interface identifier CONSTANT_Class_Info the description index: 14.

  00 2B ---- point field descriptor CONSTANT_NameAndType index entry: 43.

09 --- 9, a symbol which is a reference field, based on its known table structure, is 4 bytes took its contents.

  00 0E ---- point field type declaration of the class or interface identifier CONSTANT_Class_Info the description index: 14.

  00 2C ---- point field descriptor CONSTANT_NameAndType index entry: 44.

07 ---- 7, a symbol which is a reference to a class or interface. According to its known table structure, it took two bytes is its content.

  00 2D ---- point to the fully qualified name of the constant index: 45.

07 ---- 7, a symbol which is a reference to a class or interface. According to its known table structure, it took two bytes is its content.

  00 2E ---- point to the fully qualified name of the constant index: 46.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes 00 03 ---- UTF-8 encoded string occupied: 3.

  54 is 41 is 47 ---- string content, can be obtained according to the ASCII code table string content: TAG.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 12 ---- UTF-8 encoded string occupied: 18.

  4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B ---- string content, obtained according to the ASCII code string table: Ljava / lang / String;

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 05 ---- the number of bytes occupied by a string of UTF-8 encoding: 5.

  6D 54 79 70 65 ---- string content, can be obtained according to the ASCII code table string content: mType.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 01 ---- the number of bytes occupied by a string of UTF-8 encoding: 1.

  49 ---- string content, can be obtained according to the ASCII code table string content: I.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes 00 06 ---- UTF-8 encoded string occupied: 6.

  3C 69 6E 69 74 3E ---- string content, can be obtained according to the ASCII code table string content: <init>.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes 00 03 ---- UTF-8 encoded string occupied: 3.

  28 29 ---- 56 is string content, can be obtained according to the ASCII code table string content :() V.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 04 ---- UTF-8 encoded string occupied: 4.

  43 6F 64 65 ---- string content, the content is obtained according to the ASCII code string table: Code.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 0F ---- bytes of UTF-8 encoded string occupied: 15.

  4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65 ---- string content, can be obtained according to the ASCII code table string content: LineNumberTable.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 04 ---- UTF-8 encoded string occupied: 4.

  6D 61 69 6E ---- string content, can be obtained according to the ASCII code table string contents: main.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 16 ---- UTF-8 encoded string occupied: 22.

  28 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56 ---- string content, the content is obtained according to the ASCII code table string: (Ljava / lang / String;) V.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 05 ---- the number of bytes occupied by a string of UTF-8 encoding: 5.

  70 72 69 6E 74 ---- string content, can be obtained according to the ASCII code table string content: print.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes string 00 11 ---- UTF-8 encoded occupied: 17.

  28 4C 62 75 69 6C 64 2F 50 65 72 73 6F 6E 3B 29 56 ---- string content, the content is obtained according to the ASCII code table string: (Lbuild / Person;) V.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  String of bytes occupied 00 08 ---- UTF-8 encoding: 8.

  3C 63 6C 69 6E 69 74 3E ---- string content, can be obtained according to the ASCII code table string content: <clinit>.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 0A ---- the number of bytes occupied by the string encoded in UTF-8: 10.

  53 6F 75 72 63 65 46 69 6C 65 ---- string content, can be obtained according to the ASCII code table string content: SourceFile.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 09 ---- UTF-8 encoded string occupied: 9.

  4D 61 69 6E 2E 6A 61 76 61 ---- string content, can be obtained according to the ASCII code table string content: Main.java.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 14 ---- pointing to that field or method name of the constant term of the index: 20.

  15 ---- 00 directed to the field or method described indexers constant term: 21.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes occupied by the string 00 0C ---- UTF-8 encoding: 12.

  62 75 69 6C 64 2F 50 65 72 73 6F 6E ---- string content, can be obtained according to the ASCII code table string content: build / Person.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 2F ---- pointing to that field or method name of the constant term of the index: 47.

  30 ---- 00 directed to the field or method described indexers constant term: 48.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 09 ---- UTF-8 encoded string occupied: 9.

  E8 8C 83 E5 86 B0 E5 86 B0 ---- string content, can be obtained according to the ASCII code table string content: Bingbing.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 31 ---- pointing to that field or method name of the constant term of the index: 49.

  00 32 ---- indexers directed to the constant term of the field or the method described: 50.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 1A ---- pointing to that field or method name of the constant term of the index: 26.

  00 1B ---- directed to the field or method descriptor constant term index: 27.

07 ---- 7, a symbol which is a reference to a class or interface. According to its known table structure, it took two bytes is its content.

  00 ---- 33 is fully qualified name of the constant directivity index: 51.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 34 ---- pointing to that field or method name of the constant term of the index: 52.

  35 ---- 00 directed to the field or method described indexers constant term: 53.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 36 ---- pointing to that field or method name of the constant term of the index: 54.

  ---- 00 37 [pointing method described in the field or constant term symbol index: 55.

07 ---- 7, a symbol which is a reference to a class or interface. According to its known table structure, it took two bytes is its content.

  38 ---- 00 points to the fully qualified name of the constant index: 56.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  39 ---- 00 points to the index of the field or method name of the constant term of: 57.

  00 32 ---- indexers directed to the constant term of the field or the method described: 50.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes occupied by the string 00 0D ---- UTF-8 encoding: 13.

  68 69 54 is 73 is 20 is 6,973,206,120,746,167 ---- string content, can be obtained according to the ASCII code table string content: This is a tag.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  10 ---- 00 points to the index of the field or method name of the constant term of: 16.

  ---- 00. 11 directed to the field or method descriptor constant term index: 17.

0C ---- 12, which illustrate a symbolic reference to a field or method, based on its known table structure, is 4 bytes took its contents.

  00 12 ---- pointing to that field or method name of the constant term of the index: 18.

  00 ---- 13 is directed to the field or method descriptor constant term index: 19.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 0A ---- the number of bytes occupied by the string encoded in UTF-8: 10.

  62 75 69 6C 64 2F 4D 61 69 6E ---- string content, can be obtained according to the ASCII code table string content: Build / Main.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 10 ---- UTF-8 encoded string occupied: 16.

 6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 ---- string content, can be obtained according to the ASCII code table string content: java / lang / Object.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes 00 06 ---- UTF-8 encoded string occupied: 6.

 736574416765 ---- string content, can be obtained according to the ASCII code table string content: setAge.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 04 ---- UTF-8 encoded string occupied: 4.

 28 49 29 ---- 56 is string content, can be obtained according to the ASCII code table string content: (I) V.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 07 ---- number of bytes in a string of UTF-8 encoded occupied: 7.

 73 65 74 4E 61 6D 65 ---- string content, can be obtained according to the ASCII code table string content: setName.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 15 ---- UTF-8 encoded string occupied: 21.

 28 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56 ---- string content, the content is obtained according to the ASCII code table string: (Ljava / lang / String;) V.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 10 ---- UTF-8 encoded string occupied: 16.

 6A 61 76 61 2F 6C 61 6E 67 2F 53 79 73 74 65 6D ---- string content, can be obtained according to the ASCII code table string content: java / lang / System.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  The number of bytes 00 03 ---- UTF-8 encoded string occupied: 3.

 6F 75 74 ---- string content, can be obtained according to the ASCII code table string content: out.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 15 ---- UTF-8 encoded string occupied: 21.

 4C 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 3B ---- string content, can be obtained according to the ASCII code table string content: Ljava / io / PrintSystem ;.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  String of bytes occupied 00 08 ---- UTF-8 encoding: 8.

 74 6F 53 74 72 69 6E 67 ---- string content, can be obtained according to the ASCII code table string content: toString.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  14 ---- 00 bytes occupied string encoded in UTF-8: 20.

 28 29 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B ---- string content, the content is obtained according to the ASCII code string table :() Ljava / lang / String ;.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  Bytes 00 13 ---- UTF-8 encoded string occupied: 19.

 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 ---- string content, the content is obtained according to the ASCII code table string: java / io / PrintStream.

01 ---- 1. Description This is a UTF-8 encoded string, based on its known table structure, it took two bytes is its length.

  00 07 ---- number of bytes in a string of UTF-8 encoded occupied: 7.

 70 72 69 6E 74 6C 6E ---- string content, can be obtained according to the ASCII code table string content: println.

00 21 ---- visit marks, logos This is a normal Java class, not an interface, enumeration, or annotation, the public keyword is modified but has not been declared final and abstrace.

00 0E ---- class index, indicating the fully qualified name of the class, the index value of 14.

00 0F ---- parent index, indicating the fully qualified name of the parent class of this class, the index is 15.

00 00 ---- interface counters, indicating that this class is no interface, there is no interface index collection.

00 02 ---- number of fields: 2

 

to be continued. . .

 

Guess you like

Origin blog.csdn.net/u010838555/article/details/102768273