preface

This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

Recently in learning Java virtual machine related knowledge, mainly is to read “In-depth Understanding of Java virtual Machine” this book, combined with the official website virtual machine specification documents, notes

Reference documentation

  1. Class file structure
  2. Class bytecode specification JDK7
  3. Class bytecode specification JDK8
  4. Understanding the Java Virtual Machine

Understanding the Java Virtual Machine — Class file structure (JDK8)

What is the JVM

The Java Virtual Machine JVM recognizes Java compiled class files, not the Java language source code, and the Java language is only its primary service object. Virtually any programming language can run on the JVM as long as it is “compiled” into a CLASS file recognized by the JVM.

Class bytecode representation

To save bytecode space, the class file does not divide the data by such things as separators, but rather defines the sorting of the parts.

Such as

  1. Magic takes up four bytes

  2. The secondary version number is one byte

  3. The major version number is one byte

  4. Number of constant pools, two bytes

  5. Class names are expressed in two parts

(1) The first part is a byte, indicating how many class names represent bytes

(2) Part 2

  1. Property two parts

    (1) The first byte records how many properties there are

    (2) Record each attribute. The first byte of the attribute indicates the byte occupied by the attribute, followed by the content of the attribute.

And so on.

Class byte structure

ClassFile {
    u4             magic;/ / the digital
    u2             minor_version;// This version number
    u2             major_version;// Major version number
    u2             constant_pool_count;// Number of constant pools
    cp_info        constant_pool[constant_pool_count-1];// Const array
    u2             access_flags;// Access flags
    
    u2             this_class;/ / class indexes
    u2             super_class;// Superclass index
    u2             interfaces_count;// Number of interface indexes
    u2             interfaces[interfaces_count];// Interface index array, each interface index in U2
    
    u2             fields_count;// Number of fields
    field_info     fields[fields_count];// An array of fields for each attribute
    u2             methods_count;/ / method
    method_info    methods[methods_count];// Array of methods
    u2             attributes_count;/ / property
    attribute_info attributes[attributes_count];// Attribute array
}
Copy the code

As shown above, the byte structure of the Java class, where the u number indicates the number of bytes occupied, such as U4 for 4 bytes occupied. The class bytecode is a binary number that can be converted to a text description using the javap command.

javap -v Helloc.class

Image from << Deeper Understanding of the Java Virtual Machine >>

1.1 Magic number and version number of class files

The magic number

The first is the magic number, which takes up four bytes. The only purpose of the magic number is to determine whether the file is a class acceptable to the virtual machine. Many file stores are identified by magic head, such as JPEG images.

Text version number

The second is the minor version number and the major version number, which are used to indicate the version number of the class file. A higher version of the JVM can run classes that are compatible with a lower version, but not the other way around. Higher JDK versions are backward compatible with Class files from earlier versions, but cannot run Class files from later versions. Even if the file format has not changed, the VIRTUAL machine must refuse to execute Class files older than the version.

1.2 Constant Pool

Number of constant pools

The class file uses two bytes to indicate the number of constant pools. From the following description we can see that the actual array size is only constant_pool_count-1, because the capacity count starts at 1. The designer left 0 blank, so that data that satisfies some subsequent index value pointing to a constant pool can express the meaning of “not referring to any constant pool” under certain circumstances. However, only constant pools in the Class file structure have capacity counts that start at 1; all other collection types start at 0.

The basic structure of constants

The constant information is as follows

cp_info {
    u1 tag;
    u1 info[];
}
Copy the code

A constant first has a byte, a tag, which is used to indicate the type of the constant. The tag corresponds to the following value.

Constant type table

Constant Type Value describe
CONSTANT_Utf8 1 Utf-8 encoded string
CONSTANT_Integer 3 Int literal
CONSTANT_Float 4 Float literal
CONSTANT_Long 5 Long literal
CONSTANT_Double 6 Literal of type Double
CONSTANT_Class 7 A symbolic reference to a class or interface
CONSTANT_String 8 A string literal
CONSTANT_Fieldref 9 Field symbolic reference
CONSTANT_Methodref 10 A symbolic reference to a method in a class
CONSTANT_InterfaceMethodref 11 A symbolic reference to an interface method
CONSTANT_NameAndType 12 A partial symbolic reference to a field or method
CONSTANT_MethodHandle 15 Represents a handle to a method
CONSTANT_MethodType 16 Identify method type
CONSTANT_InvokeDynamic 18 Represents a dynamic method call point

The info[] information varies according to the tag. Each constant pool has its own structure

Constant pool contents

There are two main types of constants in the constant pool: Literal and Symbolic References.

  • Literals Literals are close to the Java language concept of constants, such as text strings, constant values that are declared final, and so on.

  • Symbols refer to concepts that are part of compilation principles. It includes three kinds of constants:

    Fully Qualified Name of the class and interface (Fully Qualified Name is the full path that includes the class, such as java.utils.list)

    Name of the field and Descriptor

    The name and descriptor of the method

Why does the constant pool have so many constants?

Java code does not “connect” as C and C++ do when compiling Javac. Instead, it dynamically connects when the virtual machine loads a Class file. In other words, the final memory layout information of individual methods and fields is not stored in the Class file. Therefore, the symbolic references of these fields and methods cannot be directly used by the virtual machine without a run-time transformation to obtain the true memory entry address. When the virtual machine is running, the corresponding symbolic reference needs to be obtained from the constant pool, and then resolved at class creation or run time, and translated into the specific memory address.

There were 11 constants in the constant pool before Jdk1.7, but they were added later to better support dynamic language calls

CONSTANT_MethodHandle, CONSTANT_MethodType, and CONSTANT_InvokeDynamic.

Above, from << Deeper Understanding of the Java Virtual Machine >>

CONSTANT_Utf8

CONSTANT_Utf8 is used to represent the value of a constant string. Basically, a class file without a constant can not be missing CONSTANT_Utf8, at least the class name needs to be represented by a CONSTANT_Utf8 constant class.

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

literal

For example, CONSTANT_Integer and CONSTANT_FLOAT, the tag represents the type and the corresponding data in 4 bytes. Long and Double are 8 bytes

CONSTANT_Integer_info {
    u1 tag;
    u4 bytes;
}

CONSTANT_Float_info {
    u1 tag;
    u4 bytes;
}

CONSTANT_Long_info {
    u1 tag;
    u4 high_bytes;
    u4 low_bytes;
}

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

For CONSTANT_String_info, string_index refers to the index in the constant pool table, which must be of type CONSTANT_Utf8

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

Symbolic reference

  • CONSTANT_Class_info A constant of this type represents a symbolic reference to a class or interface.

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

    Name_index: is an index value that points to a constant of type CONSTANT_Utf8 in the constant pool that represents the fully qualified name of the class (or interface).

  • CONSTANT_NameAndType_info

    Used to represent a field or method without indicating which class or interface type it belongs to.

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

    Name_index: is an index value that points to a constant of type CONSTANT_Utf8 in the constant pool.

    Descriptor_index: An index value that points to the CONSTANT_Utf8 constant where the field or method’s descriptor resides.

  • CONSTANT_Fieldref_info CONSTANT_Methodref_info, CONSTANT_InterfaceMethodref_info structure

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

    Class_index: is an index that points to a CONSTANT_Class_info constant.

    The CONSTANT_Class_info constant to which the class_index of CONSTANT_Methodref_info points must be a class and not an interface.

    The class_index of CONSTANT_InterfaceMethodref_info must point to an interface, not a class.

    The class_index of CONSTANT_Fieldref_info may point to a class or an interface.

    Name_and_type_index: Index that points to a CONSTANT_NameAndType_info constant that stores the name and descriptor of a field or method

  • CONSTANT_MethodHandle_info (new) JDK1.7

    This structure is used to represent a method handle

    CONSTANT_MethodHandle_info {
        u1 tag;
        u1 reference_kind;
        u2 reference_index;
    }
    Copy the code

    Reference_kind: The value ranges from 1 to 9. This value represents the type of this method handle and is used to characterize its bytecode behavior.

    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.:(A*)V
    9 REF_invokeInterface invokeinterface C.m:(A*)T

    Reference_index: The value of the reference_index entry must be a valid index for the Constant_Pool table. The constant_pool entry at the index must look like this:

    • If the reference_kind item has a value of 1 (REF_getField), 2 (REF_getStatic), 3 (REF_putField), 4 (REF_putStatic),constant_pool entry index must be of the CONSTANT_Fieldref_info structure, which represents the creation of a field processing method. (That is, the processing of fields)

    • If the reference_kind value is 5 (REF_invokeVirtual) or 8 (REF_newInvokeSpecial), the index of the Constant_pool entry must be CONSTANT_Methodref_info. Represents a method or method to be created by a class’s constructor.

    • If the reference_kind item has a value of 6 (REF_invokeStatic) or 7 (REF_invokeSpecial),

      If the class file version is less than 52.0 (that is, JDK1.8 or later), the constant_pool entry is the CONSTANT_Methodref_info structure, which represents the method creation handle of a class method.

      If the version number of the class file is 52.0 or higher (that is, JDK1.8 or later), the constant_pool entry at the index is a CONSTANT_Methodref_info structure or a CONSTANT_InterfaceMethodref_info structure. A method representing a class or interface for which a handle will be created.

      (** in JDK1.8, an interface can define static methods and default methods. Static methods can be called by the interface class, and default methods can be called by the interface method. Default methods can have a default implementation **)

    • If the reference_kind item has a value of 9 (REF_invokeInterface), then the constant_pool item at the index must be a CONSTANT_InterfaceMethodref_info structure, This structure represents the interface method for which a method handle is to be created.

  • CONSTANT_MethodType_info (new) JDK1.7

    The CONSTANT_MethodType_info structure is used to represent a method type

    CONSTANT_MethodType_info {
        u1 tag;
        u2 descriptor_index;
    }
    Copy the code

    Descriptor_index: Points to a CONSTANT_Utf8_info constant that represents a method type description

  • CONSTANT_InvokeDynamic_info (new) JDK1.7

    The CONSTANT_InvokeDynamic_info structure is used by the invokedynamic instruction (§invokedynamic) to specify the boot method, the name of the dynamic call, the parameters, the return type of the call, and, optionally, a sequence of additional constants for the static parameters of the boot method.

    CONSTANT_InvokeDynamic_info {
        u1 tag;
        u2 bootstrap_method_attr_index;
        u2 name_and_type_index;
    }
    Copy the code

    Bootstrap_method_attr_index: The value must be a valid index of the bootstrap_Methods array in the boot methods table of this class file.

    Name_and_type_index: The value of the name_AND_type_index entry must be a valid index of the Constant_pool table. The constant_POOL entry at the index must be of the CONSTANT_NameAndType_info structure, representing the method name and method descriptor.

1.3 Access_flags

At the end of the constant pool, the next two bytes represent access_flags, which identify some Class or interface level access information, such as whether the Class is a Class or an interface; Whether to define a public class type; Whether to define as abstract type; If it is a class, whether it is declared final, etc. The following table:

Sign the name Flag values Containing righteousness
ACC_ PUBLIC 0x0001 Whether it is a public class
ACC_FINAL 0x0010 Whether or not it is declared final can only be set by the class
ACC_SUPER 0x0020 Whether to allow the use of the new semantics of the InvokEspecial bytecode directive. The semantics of the Invokespecial directive changed in JDK 1.0.2. To distinguish the meaning of the instruction. This flag must be true for classes that have been coded since JDK 1.0.2
ACC_INTERFACE 0x0200 Identify that this is an interface
ACC_ABSTRACT 0x0400 Is an abstract type? True for an interface or a parallel abstract class, false for other classes
ACC_SYNTHETIC 0x1000 Identifies that this class is not generated by user code
ACC_ANNOTATION 0x2000 Indicate that this is an annotation
ACC_ENUM 0x4000 Identifies this as an enumeration

Access_falgs occupies two bytes and 16 bits, that is, it has 16 flag bits. At present, a total of 8 flag bits are used, and the other flag bits are all 0.

public final class Test{}Copy the code

As shown above, the Test class is a public class and is declared final. The ACC_PUBLIC, ACC_SUPER, and ACC_FINAL flag bits are true, and the other flag bits are false. Access_flags value 0 x0001 | 0 x0010 | 0 x0031 x0020 = 0.

This_class, super_class, interfaces

Class index, superclass index, interface index table, class file through these three describes the class inheritance relationship.

Because all classes in Java inherit from Object, all classes except Object do not have a parent index of 0, while Object has a parent index of 0. For the interface interfaces_count, the value may be 0 depending on the situation. The index table of the interface behind does not occupy any values.

1.5 Field_info (collection of field tables)

The field table (field_infb) is used to describe variables declared in an interface or class. Fields include class-level variables as well as instance-level variables, but do not include local variables declared inside methods.

field_info {
    u2             access_flags;
    u2             name_index;
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}
Copy the code
  • Access_flag: The function is similar to that of access_flags of Class.
Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Public Indicates the keyword
ACC_PRIVATE 0x0002 When the private keyword is modified
ACC_PROTECTED 0x0004 Protect keyword
ACC_STATIC 0x0008 The static keyword modifies
ACC_FINAL 0x0010 The final keyword is modified
ACC_VOLATILE 0x0040 The volatile keyword is modified
ACC_TRANSIENT 0x0080 Transient keyword modification
ACC_SYNTHETIC 0x1000 Whether the field is automatically generated by the compiler
ACC_ENUM 0x4000 Enum Specifies the enum class
  • Name_index: refers to a CONSTANT_Utf8_info constant, which is a simple name such as the field int a, which corresponds to a

  • Descriptor_index: points to a CONSTANT_Utf8_info constant that is the descriptor for the field type. For example, define a field int a; The corresponding descriptor is I

Fully qualified name: Contains information about the package in which a class resides. Like/Java /utils/List, the class is simply named List.

Simple name: No other prefix

Descriptors: Descriptors are used to describe the data type of the field, the parameter list (including number, type, and order) of the method, and the return value. According to the descriptor rules, the basic data types (byte, char, double, float, int, long, short, Boolean) and the void type representing no return value are represented by an uppercase character, while object types are represented by the character L plus the fully qualified name of the object.

Identification character meaning Identification character meaning
B Basic type byte J Basic type Long
C Base type CHAR S Basic type short
D Basic type double Z Basic type Boolean
F Basic type float L Object types, such as Ljava/lang/Object;
I Basic type int [ Represents a one-dimensional array, each dimension of which is denoted by a prefix **[**] before the type, as inint[][I.int[][]for[[I.

Note:

  1. There is also an identifierV, which appears only in the method description, which is in the return typevoid.
  2. There is one object type identifier;If not;Ljava/lang/ObjectI = Ljava/lang/ObjectI = Ljava/lang/ObjectI = Ljava/lang/ObjectI .

Method descriptors are covered in the next paragraph, so let me introduce you to the method descriptor structure in advance. The following method descriptor structure is copied from the Oracle official website.

MethodDescriptor: ( {ParameterDescriptor} ) ReturnDescriptor ParameterDescriptor FieldType ReturnDescriptor: FieldType VoidDescriptor VoidDescriptor:// void Return value corresponding to the descriptor 'V', described in the preceding descriptor tableCopy the code

The structure of a method descriptor is a combination of the parameter type and the return type.

For void m(int a,int c[][],String d), the method descriptor is (I[IILjava/lang/String;)V

For String m(), the method descriptor is (Ljava/lang/String;) V

  • Attributes_count, attributes [attributes_count] : Two bytes, attributes_count represents the number of attributes, the Attributes array stores additional information about the field, usually attributes_count =0. When decorating for final, there is a name calledConstantValueProperties.

1.6 Method_info (method table collection)

The structure of the method table is basically the same as the field table, as follows.

method_info {
    u2             access_flags;
    u2             name_index;
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}
Copy the code

Since the method cannot be modified by the volaile and TRANSIENT keywords, ACC_VOLATILE and ACC_TRANSIENT are removed. Synchronized, native, Strictfp, and Abstract can modify methods.

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Public Indicates the keyword
ACC_PRIVATE 0x0002 When the private keyword is modified
ACC_PROTECTED 0x0004 Protect keyword
ACC_STATIC 0x0008 The static keyword modifies
ACC_FINAL 0x0010 The method cannot be overridden
ACC_SYNTHETIC 0x1000 Whether the field is automatically generated by the compiler
ACC_ENUM 0x4000 Enum Specifies the enum class
ACC_SYNCHRONIZED 0x0020 Synchronized to modify
ACC_NATIVE 0x0010 Native modifiers that call methods in the SO library
ACC_STRICTFP 0x0800 strictfpModifiers that perform operations on floating-point numbers within the range of modified methods in accordance with the floating-point specification IEEE-754
ACC_ABSTRACT 0x0400 The abstract modifier represents an abstract method

Note: SstrictFP is strict short for Strict float Point, which is used to ensure the accuracy of floating-point operations. If strictFP is not specified for floating-point operations in the Java virtual Machine, the Java compiler and runtime environment will behave in an almost arbitrary manner in the expression of floating-point operations, resulting in unsatisfactory results. Once strictFP is used to declare a class, interface, or method, the Java compiler and runtime environment within the declared scope will be executed in accordance with the FLOATing-point specification EEE-754 (the most widely used standard for floating-point operations since the 1980s, adopted by many cpus and floating-point processors). So use the keyword strictfp if you want to make your floating-point operations more accurate and not perform the same results on different hardware platforms.

You can declare a class, interface, and method strictfp, but you are not allowed to declare strictfp keywords for methods and constructors in the interface.

  • Name_index: indicates the name of the method

  • Descriptor_index: Points to the method’s descriptor, which is described at the end of the 1.5 field table.

  • Attributes: An attribute sheet for a method that stores additional information. Where the property named Code stores the bytecode instructions within the method’s method!

1.7 Attributes

Attributeinfo, which has been mentioned several times in the previous section, can carry its own set of attribute sheets in Class files, field tables, and method tables to describe information that is specific to certain scenarios.

With other data in CIass document project requires strict order, different length and content, attribute table limits set slightly loose some, no longer table for each attribute has a strict order, and as long as not with existing attribute name repetition, anyone to realize the compiler can be to write their attributes in the table definition of attribute information, The Java virtual machine runs by ignoring properties it does not recognize. In order to correctly parse CIass files, Java Virtual Machine Specification (2nd edition) has predefined 9 attributes that virtual machine implementation should be able to recognize, while in the latest Java Virtual Machine Specification (JavaSE7) edition, predefined attributes have been increased to 21, see Table 6.13 for the specific content. The key commonly used parts of some of these attributes are addressed below.

The above is from Understanding the Java Virtual Machine Edition 2.

The property sheet types are as follows:

Attribute Location meaning
SourceFile ClassFile An optional attribute (fixed-length attribute) that can be turned off or generated with Javac’s -g: None or -g:source options. For most classes, the class name and file name are the same, but there are some special cases (such as inner classes, non-public classes in the file). Without this information, the stack will not show the file name of the error code when the error is reported.
InnerClasses ClassFile A list of inner classes that records all inner classes of the current class, including those defined in methods. If the current class is an inner class, then the outer classes of the current class are also recorded until the outer class is not an inner class.
EnclosingMethod ClassFile Only if a class is local or anonymous can you have this method property, which represents the enclosing methods of the class.
SourceDebugExtension ClassFile Jdk5 added forStores additional debugging informationFor example, when a JSP file is being debugged, it is impossible to locate the line number of the JSP file by playing a match. In the JSR45 case, programs written in non-Java languages that need to be compiled into bytecode and run in the JVM provide a standard mechanism for debugging. Use this property to store debugging information added to the standard.
BootstrapMethods ClassFile A complex variable-length property added to JDk7 that holds the boot method qualifier for the Invokedyamic directive reference. The real use is in Java8.
ConstantValue field_info The final keyword defines the ConstantValue (there is only one ConstantValue attribute in the field_info structure at most), and the attribute value is limited to the basic data types (int,char, etc., note: Integer and other encapsulated classes are not included) and String. More on this later. The attribute value is just a String pointing to the constant pool, so it’s easy to see why only basic types and strings are supported.
Code method_info Bytecode instructions compiled from Java code
Exceptions method_info Lists possible Checked exceptions, that is, the Exception that follows the throws keyword of the method.
MethodParameters method_info A variable-length attribute added to JDK8 to record the names and information of method parameters. In Java7 and before, Class files do not have method parameter names by default (for storage space), because it has little impact on program execution, but it does affect Jar package propagation. Without JavaDoc jars, the IDE editor cannot provide intelligent hints.
Synthetic ClassFile.field_info.method_info (Boolean attributes of flag types, there is only a difference between have and do not.) Indicates that the field or method is not generated directly from the Java source code, but is added by the compiler itself (or by setting ACC_SYNTHETIC). This method achieves unauthorized access (such as accessing private fields) by generating Synthetic methods, fields, or even entire classes that do not exist in the source code. All methods, fields, and classes that are not generated from user code should have either the Synthetic property or the ACC_SYNTHETIC flag set. (Except for instance constructors<init>()And the class constructorcinit())
Deprecated ClassFile.field_info.method_info (Boolean attributes of flag types, there is only a difference between have and do not.) A class, field, or method that is declared deprecated
Signature ClassFile.field_info.method_info JDK5 added, you can select long attributes. This parameter is used to record generic signature information. Because Java generics are pseudo-generics implemented by erasures, all generics information will be erased after compilation, so that the runtime reflection will not get the generics information. With the addition of the Signature property, Java’s reflection API is able to obtain generic types from this property.
LineNumberTable Code Describes the Java source code line number and bytecode line number. This is not a required property at runtime, but if you cancel this information,An error is reported, the error line number is not displayed on the stack, and breakpoints cannot be set on source lines during debugging. The information can be cancelled by -g:gone and generated by -g:lines.
LocalVariableTable Code Describes the relationship between variables in the stack frame’s local variable table and variables defined in the Java source code. This is not a run-time mandatory attribute. You can run -g:gone to cancel and -g:vars to generate this information. Without this item, the biggest impact is that when someone references the method **, all parameter names disappear, coding is inconvenient, and parameter values cannot be retrieved from context by parameter name during debugging. 六四事件
LocalVariableTypeTable Code The use of feature signatures instead of descriptors was added in JDK 1.5 in order to describe generic parameterized types after the introduction of generics. It is used to obtain information about generic local variables at the time a method is run.
StackMapTable Code JDK1.6 has a new property for the new Type Checker to verify that the Type Checker needs to check and process the target method’s local variables and operand stack. (Not clear yet)
AnnotationDefault method_info JDK5 added. Used to recordAnnotate class elementsThe default value for.
RuntimeVisibleAnnotations. ClassFile.field_info.method_info New attributes in Jdk1.5. RuntimeVisibleAnnotations provide support for dynamic annotation, used to specify that the annotations are runtime (actually the runtime is reflection calls).
RuntimeInvisibleAnnotations ClassFile.field_info.method_info New attributes in JDK1.5. In contrast to RuntimeVisibleAnnotations, used to specify that the annotations are not visible at runtime
RuntimeVisibleParameterAnnotations method_info JDK 1.5 new properties, function similar to RuntimeVisibleAnnotations, just role for objectThe method parameters
RuntimeInvisibleParameterAnnotations method_info New property in JDK 1.5 that acts on objects asThe method parameters, in contrast to the RuntimeInvisibleParameterAnnotations, used to specify that the annotations are not visible at runtime, namely through reflection calls.
RuntimeVisibleTypeAnnotations. ClassFile.field_info.method_info.Code New attributes in JDK1.8 provide support for implementing the new type annotations in JSR 308 to indicate which class annotations run timeType of visibleAnnotation. (not yet clear and RuntimeVisibleAnnotations difference)
RuntimeInvisibleTypeAnnotations ClassFile.field_info.method_info.Code JDK1.8 added attributes at runtimeInvisible typeAnnotation.

Local class: A class defined within a code block, as shown below. The scope of the LocalClass1 class is within the function method, and the scope of the LocalClass2 class is within the if code block

public class Outer{

pulic void function(a){

  class LocalClass1{/ / partial classes

  }

  if(true) {class LocalClass2{/ / partial classes}}}}Copy the code

ConstantValue: Note that fields S1, S2, i1, and i4 have a ConstantValue attribute, as shown below. If you’re interested in doing javap -v on their class files, those have a ConstantValue.

class Constant{
 public static final int SING_1 = 1;
}
class Test{
 public final String s1 = "1";
 public static final String s2 = "2";
 public final String s3 = new String("3");
 public final String s4;
 public final int i1 = 1;
	public final Integer i2 = 2;
	public final int i3 = new Integer(3);
 public final int i4 = Constant.SING_1;
 {
     s4 = "4"; }}Copy the code

AnnitionDefault: Used to record the default values of the elements of the annotation class and the default values of the methods it decorates. The following code corresponds to the class file structure.

@Target(ElementType.TYPE)  
@Retention(RetentionPolicy.RUNTIME)  
@interface Color {  
    String color(a) default "red";  
}
Copy the code
Last modified 2021-8-1; size 435 bytes MD5 checksum ed35b25f697b62b2640fe4067ae482d0 Compiled from "Color.java" interface Color extends java.lang.annotation.Annotation minor version: 0 major version: 52 flags: ACC_INTERFACE, ACC_ABSTRACT, ACC_ANNOTATION Constant pool: #1 = Class #2 // Color #2 = Utf8 Color #3 = Class #4 // java/lang/Object #4 = Utf8 java/lang/Object #5 = Class #6 // java/lang/annotation/Annotation #6 = Utf8 java/lang/annotation/Annotation #7 = Utf8 color #8 = Utf8 ()Ljava/lang/String;  #9 = Utf8 AnnotationDefault #10 = Utf8 red #11 = Utf8 SourceFile #12 = Utf8 Color.java #13 = Utf8 RuntimeVisibleAnnotations #14 = Utf8 Ljava/lang/annotation/Target; #15 = Utf8 value #16 = Utf8 Ljava/lang/annotation/ElementType; #17 = Utf8 TYPE #18 = Utf8 Ljava/lang/annotation/Retention; #19 = Utf8 Ljava/lang/annotation/RetentionPolicy; #20 = Utf8 RUNTIME { public abstract java.lang.String color(); descriptor: ()Ljava/lang/String; flags: ACC_PUBLIC, ACC_ABSTRACT AnnotationDefault: default_value: s#10} SourceFile: "Color.java" RuntimeVisibleAnnotations: 0: #14(#15=[e#16.#17]) 1: #18(#15=e#19.#20)Copy the code

Expand the knowledge

  1. The maximum length of a String is 655354, which is compiler limited

  2. Java anonymous inner classes pass in objects of the outer class, while Kotlin optimizes not to pass in objects of the outer class if the anonymous inner class does not reference them.

  3. Each non-static method of the class is actually passed a parameter, this, at position 0 in the parameter list.

  4. For anonymous inner classes, how does Java pass in the object of the method? This is by adding hidden constructors to anonymous inner classes, passing in external variables