The java class loading mechanism

 

1.Java class loading mechanism parents entrusted mode 

Class loading sequence diagram

Loading phase

Class loading phase by a class loader is responsible for reading of such binary byte according to the full class name of a class of flow inside the JVM, and is stored in the memory area running zone method, and then convert it to a target type java.lang.Class corresponding
object instance, the access entry Class objects will in the future as a method for such various areas of the data.

Two types JVM class loader, namely the bootstrap class loader (BootStrap ClassLoader) and a custom class loader (User-Defined Classloader)

We used include: Extension ClassLoader, Application ClassLoader three class loader.

BootStrap ClassLoader:

      Start class loader, written by C ++ language, and nested inside the JVM, it is mainly responsible for loading JAVA_HOME/liball types directory.

   The root class loader written by C ++, is responsible for loading $ JAVA_HOME in jre / lib in all of the class

Extension ClassLoader:

     ExtClassLoader derived ClassLoader, written in Java, the class responsible for loading ext folder (jre \ lib \ ext) inside;

It is responsible for loading the JRE extension directory, class lib / ext or specified by the system property java.ext.dirs JAR directory packet. 

Application ClassLoader:

     AppClassLoader derived ClassLoader, written in Java, in charge of the class path loader application level, such as the environment variable path provided.

    It is responsible for loading the library at user class path (directory java -classpath or -Djava.class.path variables within the meaning of that class where the current path and the path of the referenced third-party libraries) into memory.

 

2. Parents delegate mechanism:
it works is that if a class loader loads the class received a request, it will not go to their own load, but to entrust the request to the parent class loader to perform.
If there is a parent class loader parent class loader, then further delegate up sequentially and recursively, the request will eventually reach the top boot class loader.
If the parent class loader class loader can complete the task successfully return, if the parent class loader to load can not complete this task, the child loader will try to load your own.

Advantages parents appointed mechanisms:
the use of parental benefits is delegated model is the Java class loader as its class with a hierarchical relationship of priority together with,
this kind of level off to avoid repeated loading, when the father when the class has been loaded, there is no need child ClassLoader loaded once again.
Followed by safety reasons, java api defined core types are not replaced arbitrarily assumed java.lang.Integer class named pass through a network,
it is transmitted to the boot class loader parents by entrusting mode, the boot class loader the name of the class found in the core Java API, found that the class has been loaded, and will not reload the network passed over java.lang.Integer, return the loaded directly off Integer.class, so that we can prevent the core API library is tampering.

Parents entrusted mode

One kind is loaded mechanism established designers JVM class loader. Accordance with the rules parents commissioned by mode, in addition to the boot class loader, a program in each class loader should have a super class loader, such as Application ClassLoader
super class loader is the Extension ClassLoader, a developer of custom loader superclass is the Application ClassLoader, when a class loader loads receive a task, and does not expand load immediately
, but will load the task assigned to it by the super class loader to perform each layer are used loader in this way, up until delegate to the top of the boot class loader, if the superclass can not load the class, it will load the contents of the class returned to its next level
loader to load. The advantage is that parents entrusted mode: to have effectively ensure global uniqueness of a class.

 
Parents entrusted mode

Note: Java Virtual Machine does not explicitly require loading mechanism class loader must use parents trust model, but recommended, and in the Tomcatmiddle, when a default class loader receives a load task, it first made
automatically load it when fails to load, the class will be delegated to its super class loader to perform, this is the Servletnorm suggested practice.

Connection phase

Connected by the verification phase, prepared, resolved three stages configuration.

verification

The main task is to verify whether the verification JVM specification class information, whether it is a valid byte code file, validation and verification class covers data format, semantic analysis, operation verification

ready

Preparation phase main task is to allocate memory space for static variables of all classes, and to set (because the subject has not produced, and therefore will not in this instance variable operating range) an initial value

Resolve

The main task is to resolve all stages of the symbolic constant pool reference in its entirety converted to a direct reference, because the Java virtual machine specification does not explicitly require resolution phase must be executed in order, so wait until the initialization phase can be resolved
at a later time.

Initialization phase

Initialization phase, JVM will be in a class all of the code identified all of the static keyword to perform again, if you are performing a static variable, it will use the value specified by the user to cover the preparation phase before it is set out in the JVM the initial value,
if performing the static代码块 JVM will perform all operations on static code.

 ======

JVM class loader
class loading process
to load
the class loader This is usually done by the JVM provided, we can customize by implementing a class loader ClassLoder.
It refers to the class of the class file read into the memory of them, and whom to create an object java.lang.Class.
Connection
verification
verify the connection phase is the first step, this step is to ensure that information about the current file byte stream Class meet the requirements contained in the current virtual machine, the virtual machine will not jeopardize their own safety.
1. Verify file format
- whether the magic number (0xCAFEBABE) at the beginning
- major and minor version numbers are within the scope of the current virtual machine processing
- whether there is an unsupported type constants (constant tag check mark) Constants in the pool
- the constant pointing various index value if there is a constant point does not exist or do not meet the type of constants.
- Are there does not comply with UTF8-encoded data CONSTANT_Utf8_info type constants.
- Class of each part of the file and the file itself is deleted if there are other or additional information.
- ......
2. metadata validation
- Verify that the class has a parent class, in addition to java.lang.Object, other class must have a parent class.
- This class is not allowed to be inherited inherited class (such as the final modified class)
- If this is not an abstract class, whether all the requirements to achieve a method of the parent class or interface implementation.
- whether the class attribute had a conflict with the parent class's attributes (such as covering the final properties of the parent class, does not conform to the rules of method overloading, for example, but the same consistent method name parameter list)
- ......
3. bytecode verification
by the data flow and control flow to the semantic analysis is valid, the main logical object. This stage will verify the method body (primarily metadata verification to verify class).
It is verified to ensure that the class will not cause harm to the virtual machine at runtime.
- to ensure that any time the data type of the operand stack with the code sequence of instructions can cooperate. It means the operand stack placed int type data, the local variable table has a long type of data stored.
- to ensure that the jump is not a jump instruction bytecode instructions other than the method body.
- ensure the conversion method thereof is effective. For example the parent object class assigned to the sub-type, the object to give the object type assignment and the like irrelevant.
- ......
4. verification of symbolic references
symbol is referenced in a class, the references to other classes, when the code is compiled into class files, the compiler does not know the actual memory address referenced class, then only use a unique symbol to represent.
In the resolution phase reference symbols will be replaced by a direct reference.
When this step in order to ensure the main symbolic references to classes that can be accessed correctly, the class does not appear inaccessible, illegal access.
- if the symbol references can be found for the class by the fully qualified name string.
- and if there is a method in line with the method described in Field name and a simple description of the specified class.
- Access class, fields, methods referenced in a symbolic (public, private, protected, default ) whether the current class can be accessed.
- ......

preparation
This phase is to allocate memory class static variable, and set the default initial value.
Note: the initial value is not set the default value of the actual code, e.g. int = 1, the value 1 is not set at this time, but is 0 (because int, default 0).
Resolve
this stage is mainly symbolic reference to class replaced by a direct reference.
Direct reference: Pointer to the target, or can be positioned directly offset handle. The reference is in memory layout and relevant, and be sure to come in loaded.
Initialization
This phase is to assign static variables, different values and preparation process of Fu here in the assigned value, the value assigned to prepare the initial default values, and here is the code in real value.
Note: Initialization is in order, the order of initialization code written down. And can not access the static variable has not been initialized variables.

// this writing, the initialized after the final value i 0. Because the first i value of 1, then the static code block i has values 0.
int i = 1 static;
static {
i = 0;
}

// this writing, the initialized after the final value of i 1. Because the static code block i is assigned the value 0. And then i value of 1.
{static
I = 0;
}
static int I =. 1;

// this writing, the error in System.out.print (i) this line.
// because they can not access has not been initialized variable value. But it can be assigned (i = 0 will be assigned the value of the line variable assignment overwritten, such as the results of Case 2)
static {
I = 0;
of System.out.print (I);
}
static int. 1 = I;

 

Guess you like

Origin www.cnblogs.com/awkflf11/p/12628360.html