Java Virtual machine class loading mechanism

Source: Internet
Author: User
Tags array definition field table

virtual machine class loading mechanism : the virtual machine loads the data of the description class from the class file into the memory, verifies the data, parses and initializes it, and eventually forms a Java type that can be used directly by the virtual machine.

In the Java language, the loading and joining process of a type is done during the run of the program.

the life cycle of a class :

Load loading

Verify Verification

Prepare preparation

Parsing resolution

Initialize initialization

Use using

Uninstalling unloading

There are only four cases in which the class must be "initialized" (called an active reference to a Class):

    1. When encountering the four bytecode directives of new, getstatic, Putstatic, Invokestatic (when using new to instantiate an object, reading or setting a static field of a class, invoking a static method of a Class).
    2. When you use the Java.lang.reflet package method to make a reflection call to a class.
    3. When initializing a class, it is necessary to first trigger the initialization of its parent class if it finds that its negative class has not been initialized.
    4. The virtual machine initializes the main class (the class that contains the main method) when the VM is started.

Passive references :

    1. Referencing a static field of a parent class through a subclass does not result in subclasses being initialized (only classes that directly define the field will be initialized for static fields).
    2. Class application class by array definition: ClassA [] array=new classa[10]. Triggers the initialization of a class named [Lclassa, which is a class that is automatically generated by the virtual machine and inherits directly from object, and the creation action is triggered by the bytecode directive NewArray.
    3. Constants are stored in the constant pool of the calling class during the compilation phase.

The compiler generates the <clinit> () constructor for the interface to initialize the member variables defined in the interface. When an interface is initialized, it does not require that its parent interface be initialized at all, but only when it is actually used to the parent interface.

1. Loading
    1. The binary byte stream of this class is obtained through the fully qualified name of a class.
    2. Converts the static storage structure represented by this byte stream into the run-time data structure of the method area.
    3. In the Java heap, generate a class object that represents it as the access entry for the data in the method area.

2. Verification

Verify: Ensure that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.

Virtual Machine Specification: If you verify that the input byte stream does not conform to the storage format of the class file, it throws an Java.lang.VerifyError exception or its subclass exception.

    1. File format validation: Verifies that the byte stream conforms to the specification of the class file format and can be processed by the current version of the virtual machine. This phase of validation is given to the byte stream, after this phase of validation, the byte stream will go into the memory of the method area for storage, so the subsequent validation phase is given to the storage structure of the method area.
    2. Metadata validation: Semantic checking of the metadata information of a class ensures that there is no metadata information that does not conform to the Java language specification.
    3. Bytecode verification: Data flow and control flow analysis, the method body of the class to verify the analysis, to ensure that the method of the class is checked at runtime will not compromise the virtual machine security behavior.
    4. Symbol Reference validation: Occurs when a virtual machine converts a symbolic reference to a direct reference (parsing phase), matching the information referenced by various symbols in a constant pool.

3. Preparation

The prep phase is a phase that formally allocates memory for class variables and sets the initial values of class variables (0 values for each data type), which are allocated in the method area. However, if the Constantvalue attribute exists in the field property sheet of the Class field, the value of the variable in the prepare phase is initialized to the value specified by the Constantvalue property.

public static final int value=122;

4. Parsing

The parsing phase is the process of replacing a symbolic reference within a constant pool with a direct reference in a virtual machine.

Symbol Reference: A symbol reference is a set of symbols to describe the referenced target, the symbol can be any form of the literal, as long as the use can be used without ambiguity to locate the target. The symbolic reference is independent of the memory layout implemented by the virtual machine, and the referenced target is not necessarily loaded into memory.

Direct reference: A direct reference can be a pointer to a target directly, a relative offset, or a handle that can be indirectly anchored to the target. If there is a direct reference, the referenced target must already exist in memory.

  A. parsing of classes or interfaces (corresponding to the constant_class_info type of a constant pool):

Assuming that the current code is in the Class D, you need to parse an never-parsed symbol reference n into a direct reference to a class or interface C:

    1. If C is not an array type, the virtual machine will pass the fully qualified name representing C to the class loader of D to load the class.
    2. If C is an array type, and the element type of the array is an object (the descriptor of n is similar to [Ljava.lang.Integer], the array element type (Java.lang.Integer) is loaded, and the virtual machine generates an Array object that represents this array dimension and element.
    3. If the above procedure does not have an exception, then C has become a valid class and interface in the virtual machine, followed by a symbolic reference validation, confirming that D has access to C, if not, throws a Java.lang.IllegalAccessError exception.

B. the field (corresponding to the constant_fieldref_info type of the constant pool) is resolved:

    1. Resolves the constant_class_info symbol reference for the index in the Class_index item in the Field table. Use C to indicate the class or interface to which this field belongs.
    2. If C itself contains a field where both the simple name and the field descriptor match the target, the direct reference to the field is returned.
    3. Otherwise, if C implements the interface , the interface and his parent interface are searched recursively from bottom to top, and if the interface contains a field whose simple name and field descriptor match the target, the direct reference to the field is returned.
    4. Otherwise, if C is not a java.lang.Object type, the parent class is searched recursively from bottom to top, and if the parent class contains a field whose simple name and field descriptor match the target, the direct reference to the field is returned.
    5. Otherwise, the lookup fails, throwing a java.lang.NoSuchFieldError exception.

The compiler implementation of a virtual machine may be stricter: if a field of the same name appears in both the interface and parent class of the C implementation, or if it appears in both the interfaces of itself or the parent class, the compiler may refuse to compile.

  C. class methods (corresponding to the constant_methodref_info type of the constant pool) are resolved:

    1. Resolves the constant_class_info symbol reference for the index in the Class_index item in the method table. Use C to indicate the class or interface to which this method belongs.
    2. A class method and interface method symbol references a constant type definition that is separate and throws Java.lang.IncompatibleClassChangeError if the Class_index index in the class method table is found to be an interface of C.
    3. In Class C, look for a method that has a simple name and a descriptor that matches the target, and if so, returns a direct reference to the method.
    4. Otherwise, the parent class in C recursively looks for a method that has both a simple name and a descriptor that matches the target, and if so, returns a direct reference to the method.
    5. Otherwise, in the list of interfaces implemented by C and their parent interfaces recursively find out if there are simple names and descriptors that match the target, if there is a description C is an abstract class, the lookup ends and throws a Java.lang.AbstractMethodError exception.
    6. Otherwise, the lookup fails, throwing a java.lang.NoSuchMethodError exception.
    7. If the lookup returns a direct reference, the method is granted permission validation and throws a Java.lang.IllegalAccessError exception if it is found to have no access to the method.

D. interface Method (corresponds to the constant_interfacemethodref_info type of the constant pool):

    1. Resolves the constant_class_info symbol reference for the index in the Class_index item in the method table. Use C to indicate the class or interface to which this method belongs.
    2. If the C in the Class_index index in the interface method table is found to be a class, the Java.lang.IncompatibleClassChangeError is thrown.
    3. Otherwise, look in interface C for a method that has a simple name and a descriptor that matches the target, and if so, returns a direct reference to the method.
    4. Otherwise, recursively find in interface C's parent interface, know the Java.lang.Object class (included), see if a simple name and descriptor match the target, and if so, return a direct reference to the method.
    5. Otherwise, the lookup fails, throwing java.lang.NoSuchMethodError.

5. Initialization

The initialization phase is the process of executing the class constructor <clinit> () method.

    1. The <clinit> () method is generated by the combination of the assignment action of all class variables in the compiler's auto-collection class and the statement merge in the static statement block (static{} block) , which is determined by the order in which the statements appear in the source file. A static statement block can only access variables that are defined before a static statement block, a variable that is defined after it, and is assignable in the preceding static statement block, but cannot be accessed.

2. Unlike instance constructors <init> (), the method does not need to be displayed to call the parent class constructor, and the virtual opportunity guarantees that the parent class's <clinit> () has been executed before the subclass's <clinit> () method executes.

3. The <clinit> () method is not necessary for a class or interface, and the compiler can not generate an <clinit> () method for this class if there is no static statement block in a class and no assignment to the variable.

4. The <clinit> () execution interface does not require the <clinit> () method of the parent interface first, and the parent interface is initialized only if the variables defined in the parent interface are used. The implementation class of the interface does not execute the <clinit> () method of the interface at initialization time .

5. virtual opportunity to ensure that a <clinit> () method of a class is correctly locking and synchronized in a multithreaded environment, and that if multiple threads initialize a class at the same time, only one thread executes the <clinit> () method of the class. Other threads need to block the wait .

Java Virtual machine class loading mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.