Introduction to the

Java code is compiled into class files by the Java compiler, and then loaded and executed by the JVM. The JVM hides the details of the underlying platform system execution, so we can Compile Once, Run Anywhere.

The compiled class file is a binary stream file, such as the following class:

public class ServiceResult<T> {

    private static final int SUCCESS_CODE = 200;
    private static final String SUCCESS_MESSAGE = "Success";

    private int code;
    private String message;
    private T data;

    public ServiceResult(T data) {
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MESSAGE;
        this.data = data;
    }

    public ServiceResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess(a) {
        return code == SUCCESS_CODE;
    }

    public int getCode(a) {
        return code;
    }

    public String getMessage(a) {
        return message;
    }

    public T getData(a) {
        return data;
    }

    @Override
    public String toString(a) {
        final StringBuilder sb = new StringBuilder("ServiceResult{");
        sb.append("code=").append(code);
        sb.append(", message='").append(message).append('\' ');
        sb.append(", data=").append(data);
        sb.append('} ');
        returnsb.toString(); }}Copy the code

The compiled class opens in hexadecimal format as follows:

Note: The class file is in bytes (8 bits). U1, U2,u4,u8 represent 1 byte, 2 byte, 4 byte, and 8 byte unsigned numbers respectively in the big-edian form, that is, the highest byte comes first.

The Class structure

If the logical structure of binary class file is described in the form of C-like language structure, it is shown as follows:

This_class, minor version, major version, constant pool, Access flags, this_class, super class, interfaces, fields, Methods and Attributes are 11 parts, each part is tightly spliced together without delimiter. Each structure is introduced separately below.

Before we get into the various constructs, it is important to note that jVM1.8 is the guide for this article

This article is a bit long, so it looks more comfortable to type here

1. magic

Magic number: an unsigned 4-byte number, fixed to 0xCAFEBABE, used to indicate that the file is a class file

2. minor version

Minor version: an unsigned number of two bytes, ranging from 0 to 65535. Together with major Version, it indicates the current version of the class file. The JVM is compatible with earlier versions but not backward versions, that is, JDK7 VMS cannot run jdK8-compiled classes

3. major version

Major version number: an unsigned number of two bytes. Jdk1.1 uses a major version number of 45 and increments each major version by 1. For example, JDK1.8 is 52

4. constant pool

Constant pool: The constant pool is a very important part of the class. It not only holds the constants defined in the class, but also holds the metadata in the class file, including strings, class names, interface names, field names, method names, etc. The constant pool section first has two bytes u2 to record the number of constants it contains.

PS1: A constant pool is an array of constants whose subscripts start at 1, i.e. the effective size is constant_pool_count-1. The 0th entry is invalid. Some structures can use index 0 to indicate that there is no reference to a constant

PS2: The constant pool design has effectively reduced the size of class files. Think of all the class names that are reused. Strings now only need to be kept in one copy, and references only need to be stored in u2’s index in the constant pool

Since each constant has a specific type that represents a different meaning, it is not possible to parse the specific item by knowing the number of constants. Therefore, the first byte of each constant is defined u1, which represents the type tag of the constant, and can then be parsed according to the storage structure of the constant.

Constant tags include CONSTANT_Utf8, CONSTANT_Integer, CONSTANT_Float, CONSTANT_Long, CONSTANT_Double, CONSTANT_Class, CONSTANT_String, CONSTANT_Fieldref, CONSTANT_Methodref, CONSTANT_InterfaceMethodref, CONSTANT_NameAndType, CONSTANT_MethodHandle, CONSTANT_MethodType, CONSTANT_InvokeDynamic, etc. 14. Each type structure (type + “_info”) is introduced below:

4.1 CONSTANT_Utf8_info

The most basic constant in the constant pool, used to hold a UTF8 encoded string, such as a constant string, class name, field name, method name, etc. are a reference to it (index).

CONSTANT_Utf8_info {
    u1 tag;
    u2 length;
    u1 bytes[length];
}
Copy the code

Tag =1, length indicates the length of the string bytes. For example, length=20 indicates that the next 20 bytes are a UTF8 encoded string.

Here are two additional points:

  • Java uses variable UTF8 encoding: ASCII characters (‘\u0001’ ~ ‘\u007F’, i.e. 1~127) are represented by 1 byte, null (‘\u0000’) and characters between ‘\u0080’ and ‘\u07FF’ are represented by 2 bytes, Characters between ‘\u0800’ and ‘\uFFFF’ are represented by 3 bytes.

    The reverse is that if a byte is read with the highest bit being 0, it is a single-byte character.

    A byte read up to 110 is a double-byte character, followed by another byte read.

    The highest four bits of a byte read are 1110, which is a three-byte character followed by two more bytes.

    See the official documentation for how to decode. In Java, we just need to use new String(bytes, standardCharset.utf8) to get the decoded String

  • If u2 (0-65535) is used to represent length, the maximum length of the string is 65535

4.2 CONSTANT_Integer_info
CONSTANT_Integer_info {
    u1 tag;
    u4 bytes;
}
Copy the code

Int, tag=3, and the next four bytes represent the value of that int. Add the following about CONSTANT_Integer:

  • Big-endian, the highest byte is first, and the same is true for the following

    If you parse it yourself, do something like this:

    int value = 0;
    byte[] data = new byte[4];
    is.read(data);
    value = (value | (((int) data[0]) & 0xff)) << Byte.SIZE * 3;
    value = (value | (((int) data[1]) & 0xff)) << Byte.SIZE * 2;
    value = (value | (((int) data[2]) & 0xff)) << Byte.SIZE;
    value = (value | (((int) data[3]) & 0xff));
    Copy the code

    We can read an int value using the readInt() method of DataInputStream.

  • In Java, short, char, byte, Boolean are represented by int, and Boolean arrays are represented by byte arrays (1 byte represents 1 Boolean element).

4.3 CONSTANT_Float_info
CONSTANT_Float_info {
    u1 tag;
    u4 bytes;
}
Copy the code

Float A floating point number. Tag =4. The next four bytes represent its value, as defined in IEEE 754 standards. A float value can be read using the readFloat() method of DataInputStream.

4.4 CONSTANT_Long_info
CONSTANT_Long_info {
    u1 tag;
    u4 high_bytes;
    u4 low_bytes;
}
Copy the code

Tag =5, long integers, long, and double are stored in class in two parts (high level 4 bytes, position 4 bytes). A float value can be read using the readLong() method of DataInputStream.

4.5 CONSTANT_Double_info
CONSTANT_Double_info {
    u1 tag;
    u4 high_bytes;
    u4 low_bytes;
}
Copy the code

Tag =6, a double precision floating point number, defined in IEEE 754 standard. Storage is the same as CONSTANT_Long.

4.6 CONSTANT_Class_info
CONSTANT_Class_info {
    u1 tag;
    u2 name_index;
}
Copy the code

Tag =7: indicates a class or interface, not the type of field, parameter or return value of method. Name_index is the constant pool index, where the constant must be a CONSTANT_Utf8_info

4.7 CONSTANT_String_info
CONSTANT_String_info {
    u1 tag;
    u2 string_index;
}
Copy the code

Tag =8 represents a constant string, string_index is the constant pool index, and the constant at the index must be a CONSTANT_Utf8_info that stores the value of the string

4.8 CONSTANT_Fieldref_info
CONSTANT_Fieldref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}
Copy the code

Tag =9, representing a reference field, including static field and instance field.

Class_index is a constant (class/interface) index of type CONSTANT_Class_info in the constant pool, representing the class to which field belongs. Name_and_type_index is a constant index of type CONSTANT_NameAndType_info (see below) in the constant pool, representing the name and type of field.

Field references include the following methods:

  • ServiceResult class at the beginning of this articlecodeField, for example, is useful in multiple methods, so it’s better to store one copy of the field in a constant pool and then store its index elsewhere than to store multiple copies.
  • CONSTANT_Fieldref_info is used to separate fields (which may be of this class or of an external class) referenced in code into constants, as described belowfield_infoDon’t confuse
4.9 CONSTANT_Methodref_info
CONSTANT_Methodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}
Copy the code

Tag =10 represents a reference to method information, including static method and instance method.

Class_index is a CONSTANT_Class_info constant (class only) index in the constant pool that represents the class to which method belongs. Name_and_type_index is a constant index of type CONSTANT_NameAndType_info in the constant pool. It represents the name and parameters of method and returns value information.

4.10 CONSTANT_InterfaceMethodref_info
CONSTANT_InterfaceMethodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}
Copy the code

Tag =11 indicates the method information of an interface.

Class_index is a CONSTANT_Class_info constant (interface only) index in the constant pool that represents the interface to which method belongs. Name_and_type_index CONSTANT_Methodref_info.

4.11 CONSTANT_NameAndType_info
CONSTANT_NameAndType_info {
    u1 tag;
    u2 name_index;
    u2 descriptor_index;
}
Copy the code

Tag =12 stores the name and type of the field or method. Name_index points to a CONSTANT_Utf8_info that represents the unqualified name of a field or method. Descriptor_index also points to a CONSTANT_Utf8_info indicating the description of the field/method.

Descriptor

Descriptor is saved as a string CONSTANT_Utf8_info.

  • Field descriptors (FieldType), FieldType can be a basic type: B(byte) C(char) D(double) F(float) I(int) J(long) S(short) Z(Boolean), object type: L+ Fully qualified class name, array type: [+ element type

    int a; // I
    Integer b; //Ljava/lang/Integer
    double[] c; //[D
    double[][] d; //[[D
    Object[] e; //[Ljava/lang/Object
    Object[][][] f; //[[[Ljava/lang/Object
    Copy the code
  • Method descriptors (method Descriptors), method descriptors in the format of (parameter type) return type

    /** * descriptors :(IDLjava/lang/Thread;) Ljava/lang/Object; * /
    Object m(int i, double d, Thread t) {... }Copy the code
4.12 CONSTANT_MethodHandle_info
CONSTANT_MethodHandle_info {
    u1 tag;
    u1 reference_kind;
    u2 reference_index;
}
Copy the code

Tag =15, method handles, such as fetching a class static field, instance field, calling a method, constructor, etc. are converted to a handle reference.

  • reference_kind

    Kind Description Interpretation
    1 REF_getField getfield C.f:T
    2 REF_getStatic getstatic C.f:T
    3 REF_putField putfield C.f:T
    4 REF_putStatic putstatic C.f:T
    5 REF_invokeVirtual invokevirtual C.m:(A*)T
    6 REF_invokeStatic invokestatic C.m:(A*)T
    7 REF_invokeSpecial invokespecial C.m:(A*)T
    8 REF_newInvokeSpecial new C; dup; invokespecial C.<init>:(A*)V
    9 REF_invokeInterface invokeinterface C.m:(A*)T

    F: field, M: method, : instance constructor

  • reference_index

    • For Kind= 1,2,3,4, reference_index refers to a CONSTANT_Fieldref_info
    • For Kind= 5,6,7,8, reference_index refers to a CONSTANT_Methodref_info
    • For Kind=9, reference_index refers to a CONSTANT_InterfaceMethodref_info
4.13 CONSTANT_MethodType_info
CONSTANT_MethodType_info {
    u1 tag;
    u2 descriptor_index;
}

Copy the code

Tag =16, describes a method type. Descriptor_index references a CONSTANT_Utf8_info, representing the method descriptor

4.14 CONSTANT_InvokeDynamic_info
CONSTANT_InvokeDynamic_info {
    u1 tag;
    u2 bootstrap_method_attr_index;
    u2 name_and_type_index;
}

Copy the code

Tag =18, invokeDynamic invokes the instruction reference information dynamically.

  • Bootstrap_method_attr_index, the index of the bootSTRap_methods [] array in the BootstrapMethods property. Each bootstrap method references CONSTANT_MethodHandle_info
  • Name_and_type_index, referring to a constant CONSTANT_NameAndType_info
5. access flags

Access Flags represents access control and decoration information for classes, interfaces, fields, and methods.

Access Flag(u2) Value Function object
ACC_PUBLIC 0x0001 class, inner, field, method
ACC_PRIVATE 0x0002 inner, field, method
ACC_PROTECTED 0x0004 inner, field, method
ACC_STATIC 0x0008 inner, field, method
ACC_FINAL 0x0010 class, inner, field, method
ACC_SUPER 0x0020 class
ACC_SYNCHRONIZED 0x0020 method
ACC_VOLATILE 0x0040 field
ACC_BRIDGE 0x0040 method
ACC_TRANSIENT 0x0080 field
ACC_VARARGS 0x0080 method
ACC_NATIVE 0x0100 method
ACC_INTERFACE 0x0200 class, inner
ACC_ABSTRACT 0x0400 class, inner, method
ACC_STRICT 0x0800 method
ACC_SYNTHETIC 0x1000 class, inner, field, method
ACC_ANNOTATION 0x2000 class, inner
ACC_ENUM 0x4000 class, inner, field

Most of them are obvious, but add the following:

  • ACC_SUPER: A superclass method used in the Invokespecial directive that requires special processing
  • ACC_BRIDGE: the ACC_SYNTHETIC mark of a bridge method. Methods with this mark also have the ACC_SYNTHETIC mark
  • ACC_STRICT: strictFP, strict float point, the method uses the STRICT FP-strict floating point format
  • ACC_SYNTHETIC: the ACC_SYNTHETIC flag is generated by the compiler and does not exist in the source code
6. this class

Current class or interface that points to a CONSTANT_Class_info constant from which the fully qualified name of the current class can be resolved. The package name hierarchy is split with/instead of., as in Java /lang/Object.

7. super class

The direct parent index of the current class, pointing to a CONSTANT_Class_info constant, super_class=0 if there is no direct parent

8. interfaces

First, use U2 to indicate the number of direct parent interfaces of the current class or interface, n. The following array of N U2s is the constant pool index of these parent interfaces, of type CONSTANT_Class_info, declared from left to right.

9. fields
field_info {
    u2             access_flags;
    u2             name_index;
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

Copy the code

Field_info saves the fields information of the current class. Quite simply, most of these are covered earlier, but the attributes are covered in Section 11 below. Note that fields only contain fields of the current class, such as field C of inner class B of A, in class A$B

10. methods
method_info {
    u2             access_flags;
    u2             name_index;
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

Copy the code

Save method information for the current class, same as field_info

11. attributes

Property sheet: Attributes exist in ClassFile, field_info, method_info, and Code attributes also contain nested attribute information. Attributes are used to describe instructions, exceptions, annotations, generics, etc. JLS8 predefined 23 attributes, each with a different structure (variable length). But it can be abstracted into the following general structure.

attribute_info {
    u2 attribute_name_index;
    u4 attribute_length;
    u1 info[attribute_length];
}

Copy the code

Attribute_name_index: indicates the index of the attribute name in the constant pool. This parameter is used to determine which attribute the current attribute belongs to. For example, Code indicates that the current attribute is a Code_attribute

Attribute_length: Indicates the next number of bytes of information about the attribute. Java allows you to customize a new attribute, and if the JVM does not recognize it, it reads attribute_length in a generic structure.

The 23 attributes can be divided into three groups according to their functions:

  • Translated by JVM for use: ConstantValue, Code, StackMapTable, Exceptions, BootstrapMethods
  • Used by Java class libraries for parsing: InnerClasses EnclosingMethod, Synthetic, Signature, RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations/RuntimeInvisibleParameterAnnotations, RuntimeVisibleTypeAnnotations/RuntimeInvisibleTypeAnnotations, AnnotationDefault MethodParameters
  • Neither JVM nor Java class library parsing is required for debugging tools and other scenarios: SourceFile, SourceDebugExtension, LineNumberTable, LocalVariableTable, LocalVariableTypeTable, Deprecated

Note: I’ll explain how to parse classes later, so this article is just a brief introduction to the structure and function of each attribute

11.1 ConstantValue
ConstantValue_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 constantvalue_index;
}

Copy the code

Exists in field_info and represents a constant value, such as 5 in private final int x = 5. The value referred to by attribute_name_index is “ConstantValue”, and attribute_length is fixed to 2. The next two bytes of constantvalue_index are the index of the ConstantValue in the constant pool, which is “CONSTANT_Long”. CONSTANT_Float, CONSTANT_Double, CONSTANT_Integer, CONSTANT_String.

11.2 Code
Code_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 max_stack;
    u2 max_locals;
    u4 code_length;
    u1 code[code_length];
    u2 exception_table_length;
    {   u2 start_pc;
        u2 end_pc;
        u2 handler_pc;
        u2 catch_type;
    } exception_table[exception_table_length];
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}

Copy the code

Describes the bytecode instructions compiled by the method body. The method_info structure that describes a method is described earlier, and the method body information is stored in the code property of its property table. If it is an abstract method, it does not have this property.

We talked about attribute_name_index, attribute_length in the general structure attribute_info, and that’s true for every attribute, so I’m not going to go into that, I’m just going to cover the rest.

  • Max_stack, the maximum depth of the operand stack, used to allocate stack sizes

  • Max_locals, the maximum capacity of the local variable table in method stack frames, which stores local variables, method parameters, exception parameters, etc. In slot units, variables less than 32 bits are allocated one slot, and variables greater than 32 bits, such as long and double, are allocated two slots. Note that objects store references. Also, for instance methods, the this object pointer is passed in by default, so max_locals is at least 1.

  • Code [code_length] stores a list of bytecode instructions, each bytecode instruction is a byte, so 8 bits can represent 256 different instructions at most. It should be pointed out that this byte stream array does not store all instructions, some instructions also have corresponding operands. Skip the corresponding n-byte operands and move on to the next instruction, which I’ll demonstrate in another article.

  • Exception_table [EXCEPtion_table_length], a table of exception_exceptions. Notice that it is not the exception thrown by the method declaration, but the exception displayed in the try-catch exception_table. Each catch exception is an item of the EXCEPtion_table.

    • Catch_type, the catch exception type, points to a CONSTANT_Class_info constant
    • Start_pc, the offset of the bytecode instruction relative to the start of the method, equivalent to the index in code[code_length]
    • End_pc, the offset of the bytecode instruction relative to the start of the method, equivalent to the index in code[code_length]
    • Handler_pc, the offset of the bytecode instruction relative to the start of the method, equivalent to the index in code[code_length]

    If an exception of catch_type type or a subclass of catch_type=0 occurs within [start_PC, end_PC], the instruction that jumps to handler_PC continues execution.

    Three points to add:

    1) The instructions in the Finaly block are redundant in each code branch.

    2) The athrow instruction continues to throw exceptions that are not displayed as caught

    3) Although instruction length code_length is U4, start_PC, end_PC, and handler_pc all have only 2 bytes of unsigned u2, and the maximum representation range is only 65535, so the method can have a maximum of 65535 instructions (if each instruction does not contain an operation number).

  • Attributes [attributes_count], a nested list of attributes

11.3 StackMapTable
StackMapTable_attribute {
    u2              attribute_name_index;
    u4              attribute_length;
    u2              number_of_entries;
    stack_map_frame entries[number_of_entries];
}

Copy the code

As mentioned above, Code_attribute can also contain attribute tables. StackMapTable is located in the attribute table of the Code attribute, which was added for type derivation validation during JVM bytecode validation

11.4 Exceptions
Exceptions_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 number_of_exceptions;
    u2 exception_index_table[number_of_exceptions];
}

Copy the code

Exception_index_table Each item U2 points to a CONSTANT_Class_info constant

11.5 BootstrapMethods
BootstrapMethods_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 num_bootstrap_methods;
    {   u2 bootstrap_method_ref;
        u2 num_bootstrap_arguments;
        u2 bootstrap_arguments[num_bootstrap_arguments];
    } bootstrap_methods[num_bootstrap_methods];
}

Copy the code

Located in a ClassFile, it holds the bootstrap method referenced by the InvokeDynamic directive

  • Bootstrap_method_ref, referring to a CONSTANT_MethodHandle_info constant, The reference_KIND of the MethodHandle must be REF_invokeStatic or REF_newInvokeSpecial
  • Num_bootstrap_arguments, bootmethod argument list, one for each entry in the arrayCONSTANT_String_info.CONSTANT_Class_info.CONSTANT_Integer_info.CONSTANT_Long_info.CONSTANT_Float_info.CONSTANT_Double_info.CONSTANT_MethodHandle_info, or CONSTANT_MethodType_inforeference
11.6 InnerClasses
InnerClasses_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 number_of_classes;
    {   u2 inner_class_info_index;
        u2 outer_class_info_index;
        u2 inner_name_index;
        u2 inner_class_access_flags;
    } classes[number_of_classes];
}
Copy the code

Classes is the inner class list of the current class. Inner_class_info_index and outer_class_info_index point to CONSTANT_Class constants, representing the inner and outer class references, respectively. The inner_name_index is a reference to the inner class name (CONSTANT_Utf8_info). A value equal to 0 indicates an anonymous inner class. The inner_class_access_flags is the inner class access flag, the same as access_flags

11.7 EnclosingMethod
EnclosingMethod_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 class_index;
    u2 method_index;
}
Copy the code

Located in the ClassFile structure, stores local or anonymous class information.

  • Class_index, a reference to the class that contains it directly, refers to a CONSTANT_Class_info constant that represents the innermost class that contains the current class declaration
  • Method_index, which refers to a CONSTANT_NameAndType_info constant and represents the method name and type that directly contains the local and anonymous classes
11.8 Synthetic
Synthetic_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
}

Copy the code

This is true if the class, method, or field is generated by the compiler, which is synonymous with ACC_SYNTHETIC.

11.9 Signature
Signature_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 signature_index;
}

Copy the code

Exists in the property tables of classes, methods, and fields, used to store generic information about classes, methods, and fields (Type Variables, Parameterized Types).

You can refer to generics here

  • Signature_index, which references a CONSTANT_Utf8_info constant to indicate the signature
11.10 RuntimeVisibleAnnotations
RuntimeVisibleAnnotations_attribute {
    u2         attribute_name_index;
    u4         attribute_length;
    u2         num_annotations;
    annotation annotations[num_annotations];
}

Copy the code

Retentionpolicy.runtime annotations exist in classes, methods, fields, and stores visible at RUNTIME, and can be retrieved by the reflection API. For annotations, see here

The annotation structure stores information about the annotation name and element value pairs, which can be referenced in the official documentation or in the article I’ll parse on class later

11.11 RuntimeInvisibleAnnotations
RuntimeInvisibleAnnotations_attribute {
    u2         attribute_name_index;
    u4         attribute_length;
    u2         num_annotations;
    annotation annotations[num_annotations];
}

Copy the code

And RuntimeVisibleAnnotations structure is the same, but invisible, which cannot be the reflection API access to, the JVM to ignore this property

11.12 RuntimeVisibleParameterAnnotations
RuntimeVisibleParameterAnnotations_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u1 num_parameters;
    {   u2         num_annotations;
        annotation annotations[num_annotations];
    } parameter_annotations[num_parameters];
}

Copy the code

Exists in method_info attribute table, storage runtime visible method parameter annotation information, compared with RuntimeVisibleAnnotations found, RuntimeVisibleParameterAnnotations storage is a method of each parameter in the parameter list of annotations (equivalent to a group of RuntimeVisibleParameterAnnotations), sequence and method descriptors parameters in the order

11.13 RuntimeInvisibleParameterAnnotations
RuntimeInvisibleParameterAnnotations_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u1 num_parameters;
    {   u2         num_annotations;
        annotation annotations[num_annotations];
    } parameter_annotations[num_parameters];
}
Copy the code

I don’t want to talk too much

11.14 RuntimeVisibleTypeAnnotations
RuntimeVisibleTypeAnnotations_attribute {
    u2              attribute_name_index;
    u4              attribute_length;
    u2              num_annotations;
    type_annotation annotations[num_annotations];
}
Copy the code

Class_file, method_info, field_info, code, java8. JLS8 adds two elementTypes (elementtype.type_parameter, elementtype.type_use) to describe annotation attributes. Type_annotation stores annotation information and its object.

11.15 RuntimeInvisibleTypeAnnotations
RuntimeInvisibleTypeAnnotations_attribute {
    u2              attribute_name_index;
    u4              attribute_length;
    u2              num_annotations;
    type_annotation annotations[num_annotations];
}
Copy the code

A little…

11.16 AnnotationDefault
AnnotationDefault_attribute {
    u2            attribute_name_index;
    u4            attribute_length;
    element_value default_value;
}

Copy the code

Exists in the method_info property table, which records the default value of the annotation element

11.17 MethodParameters
MethodParameters_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u1 parameters_count;
    {   u2 name_index;
        u2 access_flags;
    } parameters[parameters_count];
}

Copy the code

It exists in the method_info property table, which records method parameter information, name_index parameter name, and access_flags has ACC_FINAL, ACC_SYNTHETIC, and ACC_MANDATED

11.18 SourceFile
SourceFile_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 sourcefile_index;
}

Copy the code

In the class_file property table, record the file name of the file that generated it. The exception stack may display this information, generally the same as the class name, but not the inner class. This is an optional attribute, meaning that the compiler is not forced to generate this information.

11.19 SourceDebugExtension
SourceDebugExtension_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u1 debug_extension[attribute_length];
}
Copy the code

Exists in an optional class structure that holds extended debugging information for non-Java languages. The debug_Extension array is the index to CONSTAN_Utf8_info

11.20 LineNumberTable
LineNumberTable_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 line_number_table_length;
    {   u2 start_pc;
        u2 line_number;	
    } line_number_table[line_number_table_length];
}
Copy the code

Code’s property table stores the mapping between the source line number and the bytecode offset (method instruction number), start_PC bytecode offset, and line_number source line number (optional).

Question: How do I print out the source line number of the error in the error stack? How to support breakpoint debugging on source code?

11.21 LocalVariableTable
LocalVariableTable_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 local_variable_table_length;
    {   u2 start_pc;
        u2 length;
        u2 name_index;
        u2 descriptor_index;
        u2 index;
    } local_variable_table[local_variable_table_length];
}
Copy the code

In the property table of code, the mapping between the variables of the local variable table in stack frame and the variables defined in the source code can be associated with the variable name of the local variable table in the source code when parsing the property, and so on, optional.

11.22 LocalVariableTypeTable
LocalVariableTypeTable_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 local_variable_type_table_length;
    {   u2 start_pc;
        u2 length;
        u2 name_index;
        u2 signature_index;
        u2 index;
    } local_variable_type_table[local_variable_type_table_length];
}
Copy the code

In code’s property table, signature_index also refers to a constant CONSTANT_Utf8_info, similar to LocalVariableTable, Variables that contain generics are stored in both LocalVariableTable and LocalVariableTypeTable

11.23 Deprecated
Deprecated_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
}
Copy the code

Class, method, field expiration flag, no additional information, attribute_length=0, if this attribute is present it is deprecated @deprecated

Finished! If you think the writing is ok, give a thumbs-up to encourage it!


Next period: start to write a resolution class (bytecode) file procedures

Follow wechat, more exciting waiting for you