Here I selected OpenJDK12 version, the download address: hg.openjdk.java.net/jdk/jdk12/f… Click zip to download it.

The so-called Java virtual machine Class loading mechanism is a Java type that the Java VIRTUAL machine can directly use after obtaining the Class data from the Class file and loading it into the memory for a series of operations.

When to load the class

For a class (and, of course, for an interface). , its whole life cycle can be divided into loading, verification, preparation, parsing, initialization, use and uninstall seven stages.In the Java Virtual Machine Specification (see below), there are six cases where “initialization operations” must be performed immediately. We also call these six situations “unsolicited quotations.”

  • When you encounter four bytecode instructions — New, getstatic, putstatic, and Invokestatic — you should enter the initialization phase.
  • Reflect class when using java.lang.Reflect
  • When initializing a class, if the parent class is not initialized, the parent class is initialized first.
  • Classes that contain the main method are initialized first
  • When using dynamic language support, If the Java lang. Invoke. MethodHandle instance analytical results of the final REF_getStatic, REF_putStatic, REF_invokeStatic, REF_newInvokeSpecial handle to four kinds of methods, If the class corresponding to the method handle cannot be initialized, the class is initialized first
  • If the implementation class of a default-decorated interface is to be initialized, the interface is initialized before it.

Correspondingly, all other reference types are referred to as “passive references”.

The process of class loading

Let’s start with loading.

A load.

Loading is the first stage of class loading. At this stage, the Java virtual machine does three things: (1) retrieve the binary byte stream of the Class by its fully qualified name; (2) convert the static storage structure of the binary byte stream into the runtime data structure of the method area; and (3) generate a java.lang.Class object in memory that represents the Class as an access point to the various data of that Class

The three phases of validation, preparation, and parsing are collectively referred to as connections. We talk about the first step in connection: validation.

2. Verify

Validation is the first step in connection to ensure that the byte streams in the Class file comply with the constraints of the virtual machine specification to prevent the code from causing harm to the virtual machine after it is run. The validation phase is very important. The verification phase will probably complete the following four verification actions: file format verification, metadata verification, bytecode verification and symbol reference verification.

1. Verify the file formatCopy the code

The main verification is whether the byte stream conforms to the Class file format specification, and can match the current version of the virtual machine. This verification point includes, but is not limited to, verifying whether magic numbers start with coffee babies, whether constants in the constant pool have unsupported constant types, and so on. Details: gitee.com/sitr/openJD…

2. Verify metadataCopy the code

The first step of file format validation means that the byte stream has entered the Method area of the Java virtual machine, so the next three validation phases are based on the storage structure of the method area. Metadata verification is the semantic analysis of the information described by bytecode to ensure compliance with the requirements of virtual machine specifications. Verification points included in this phase include, but are not limited to, whether the class has a parent class, whether the class implements all the methods in the parent class if it is not an abstract class, and so on.

3. Bytecode verificationCopy the code

The third stage is the most complicated part of the verification process. The main purpose is to determine semantic legitimacy through data flow analysis and control flow analysis. In the second phase, the data types in the metadata information are verified. In this phase, the method body of the class is verified to ensure vm security. But even landscape bytecode validation is still not secure.

There are also several validation AIDS in the Javac compiler. The result is the Code property in the method body. See class file Structure) added a new attribute “StackMapTable” to the property table.

4. Symbol reference verificationCopy the code

Java Virtual Machine Specification Chinese version PDF: Link: pan.baidu.com/s/1RVLCLYI-… Extraction code: 7USA