Java Virtual machine Learning (iv) class loading process

Source: Internet
Author: User
Tags field table

Classes are loaded from virtual machine memory to unloading from memory, through the life cycle: load, validate, prepare, parse, initialize, use, unload these phases, where validation, parsing, initialization is called the connection process (linking).

(To see the class file structure after this block and the loading principle)

In addition to parsing and using, the other basic sequence of procedures is this, which can be resolved after initialization is complete, for dynamic binding at run time.

In the virtual machine specification, it is defined in 5 cases (with and only) that the class must be initialized (previously, loaded, validated, prepared):

1. If the class is not initialized when it encounters the New,getstatic,putstatic,invokestatic 4 bytecode Directive,

2. When using reflection, if the class is not initialized.

3. When the parent class of this class is not initialized.

4. The main class that is specified when the virtual machine starts.

5. When using dynamic language support, the corresponding class of a particular method handle is not initialized (ref_getstatic,ref_putstatic,ref_invokestatic)


classes are loaded :

1. Get a binary stream that defines a class, a binary stream can be obtained from a network, a zip package, a jar package can be, also appear in the JSP, Dynamic Agent (Java.lang.reflect.Proxy) and so on.

2. Convert the static storage structure represented by the binary stream into a method area runtime data structure.

3. In memory, the Java.lang.class object representing this class is generated as a variety of data access portals for the class of the method area.

The non-array class is the most controllable developer, it can have a system-provided classloader load, or can have a user-defined ClassLoader load, and the array class is created directly by the virtual machine, but the class of the array class is still loaded by ClassLoader. The process of creating an array class follows the principle:

1. If the array's component type (similar to foo[] Fooarray) is a reference type , the array will be identified on the class name space of the class loader for that component type.

2. If the array's component type is not a reference type (such as int[]), the Java Virtual machine will mark the array as associated with the bootstrap ClassLoader.

3. The visibility of an array class is consistent with the visibility of its component types

After loading is complete, in memory (not explicitly defined in the Java heap, class objects are special, in the hotspot virtual machine This block of memory refers to the method area memory) in the instantiation class of the Java.lang.class object , This object acts as an external interface for the program to access the data type of the method area, and the load is interleaved with the connection process (because the connection process includes the validation process), and the other connection processes remain in sequence with the loading process.

Verify:

Java's class file is not necessarily generated by the Java source code, it can be written directly by the hexadecimal compiler, but the Java virtual machine does not access data outside the bounds of the array, and does not convert the object to a class that does not have an implementation, and does not have to execute something like that. But these all need to go through the virtual machine verification process. It is also an attack that prevents a virtual machine from being exposed to malicious code, and throws a Java.lang.VerifyError or its subclass exception if the stream to be verified does not have a specification in the class file format. Detailed reference to the Java Virtual Machine specification. The approximate verification has the following 4 phase inspection actions:

1. File Format verification:

This phase verifies that the bytecode conforms to the specification and whether the virtual can be processed, including the verification point:

Whether to start with the magic number 0xCAFEBABEK.

Whether the primary or secondary version is within the current virtual machine processing range.

Whether a constant pool contains a constant type that is not supported, and so on.

Much more than that, but this verification passes the post-byte code to the memory's method area and into the runtime's data structure.

2. Meta-data validation:

The main is to detect whether metadata conforms to Java specifications such as:

Whether this class has a parent class.

Class inherits a class that does not allow inheritance.

Whether all the methods of the interface are implemented, etc.


3. Bytecode verification (which should be understood as validation of the legality of the method):

The most complex step in the entire verification process, mainly through data flow and control flow analysis, the legitimacy of the program syntax, to ensure that the logical, in the second phase of metadata validation, this stage is mainly to the class of the method body validation, to ensure that the runtime does not compromise the virtual machine, such as:

1. Ensure that the data type of the operand stack works in conjunction with the instruction code sequence and does not appear to be of type int in the stack, but is loaded into the local variable table as long when used.

2. Ensure that the jump instruction does not jump to the bytecode instruction other than the method body.

3. Ensure that the type conversion is valid. For example, it is safe to assign a class object to a parent class object, but assigning the parent object to a subclass, or even assigning the object to a class that is irrelevant, is dangerous and the latter is illegal.

But this process does not guarantee that it will be safe after all. (keywords: stackmaptable,-xx:-usespliteverifier,-xx:+failovertooldverifier later when used to see.) First note:))

4. Symbol Reference validation (class, method, field can be parsed for validation):

The purpose is to ensure that the parsing action works properly, if it cannot be thrown by symbolic validation, Java.lang.incompatibleClassChangeError subclasses, such as: Java.lang.IllegalAccessError, Java.lang.nosuchfielderror,java.lang.nosuchmethoderror and so on, if the code you are running has been reused and validated, consider using the-xverify:none parameter to turn off most class validation. To shorten the class load time.

What you usually need to verify:

1. The fully qualified name described by the string in the symbol reference can find the corresponding class.

2. Whether there is a field descriptor in the specified class that conforms to the method and the Method field described by the simple name (it should be possible to find the meaning of the method based on the field description).

3. The symbol references the class, the field, whether the method can be accessed by the current class (modifier-decorated access may be a row).

Get ready:

The preparation stage formally allocates memory for the variables of the class, where the memory refers to the method area, the variables are static variables, non-static variables and class object instances are allocated memory in the Java heap, such as:

static int value=123

In the preparation phase value is 0, this time the method has not been executed, only the value of the initialization phase will be 123 (Putstatic is compiled into the class constructor <clinit> () method), the basic data type is a number of 0, 0f,0l , 0d, Char is \u0000,string is not a base type, reference is null, if the above int is final decorated, the prepare stage virtual opportunity sets value to 123 based on constantvalue settings


Analytical:

The parsing phase is the process of converting a symbolic reference to a direct reference.

Symbol References:

Describe the referenced target with a set of symbols (theJava class does not know the actual memory address of the reference class, so it can only be replaced with a symbolic reference ), regardless of the memory layout of the virtual machine, the target of the reference is not necessarily loaded into memory, The memory layouts implemented by various virtual machines can be different, but they can accept symbolic references that must be the same, and the literal form of the symbolic reference is clearly defined in the Java Virtual Machine specification class file.

Direct reference:

A direct reference can be a pointer to a target, an offset, or a target handle that can be positioned indirectly. In relation to the memory layout, it is possible to refer directly to the description object that is already present in memory, and the direct references that are translated on different virtual machine instances may not be the same.

Virtual machine According to the need to judge, is in the class load the constant pool of symbols to parse, parse fields, methods, interfaces in the process will go to the Class_index index class, and parse the class symbol, in this process if an error, will cause the above parsing can not continue.

1. Class or interface parsing:

If the code is in a Class A, you will parse the parsed symbol reference n into a direct reference to a class or interface C (that is, a reference to Class C in Class A). Need a step.

1. If this class C is not an array type, the virtual opportunity passes the full name of the symbol reference to the Class D loader to load, which may trigger the loading of other related classes, such as the parent class of the class or the implementation interface. If any exceptions occur during the load process, the resolution fails.

2. If c is an array and the element is an object type, the object is loaded and the virtual machine generates an array object representing the latitude and the elements of this array.

3. After completion of the above steps to do symbolic reference verification, determine if D has access to C full limit, if not have to throw java.lang.illgalAcessError.

2. Field Resolution:

Parsing a field reference that has been parsed will first parse the Constant_class_info symbol reference for the index in the field table, Class_index the class or interface symbol referenced by the field, and result in the failure of the field symbol reference. If successful, you have the following steps:

1. If a class itself contains a simple name and the field description matches the Target field, the field reference is returned, and the lookup ends.

2. Otherwise go to find if C implements the interface, if it is going up recursive query, if there is a name and the field description are matched, return the reference to find the end.

3. Otherwise find out if the parent class exists, and if both the name and the field description match, the return reference lookup ends.

4. If the parent class, the interface is not found, throws a Java.lang.NoSuchFieldError exception.

If it finds but does not have access to the full limit, it throws java.lang.IllegalAccessError. It may actually be stricter, and if the field appears more than once in the parent class and interface, the compilation may be rejected.

3. Method analysis;

In the method parsing, the class method parsing and the interface method parsing are different, and in the constant pool the two symbols are referenced differently.

1. Once a class is parsed into a class rather than an interface, in the class method table, if the class that the method belongs to is an interface, then the Java.lang.incompatibleClassChangeError is thrown directly, in other words, when trying to reference a pure interface instead of implementing a method of the class. This exception is thrown.

2. If the simple name and description are matched to the target, the reference to the method is returned, and the lookup ends.

3. If the second part is not found, it will be looked up in the recursive parent class.

4. If the parent class is not found, it will go to the implementation of the interface or interface of the parent class to find, if found will throw, Java.lang.AbstractMethodError, stating that the class is an abstract class.

5. If none of the above is found, the Java.lang.NoSuchMethodError will be thrown.

Find success will go to do permission verification, if no access full limit reported: Java.lang.IllegalAccessError exception

4: Interface parsing:

1. The first article, in contrast to method parsing, is queried in the interface-side publication, and if the method contains an interface that is a class, it is reported Java.lang.incompatibleClassChangeError.

2. Then to determine that the simple name and the description symbol are matched to the target, and if found, return the direct reference to this method.

3. Otherwise it will always be recursive to the object class, and if found, returns a direct reference if the Java.lang.NoSuchMethodError exception is reported if not found.

In the case of success, as with the success of the class method, the full-limit check will be done.

Initialization

The <cinit> method is executed first, and the Cinit method is not necessarily generated only if there are static and variable initial assignments in the class, and the <cinit> method of the parent class is guaranteed to execute before the sub-method executes <cinit>. Although there is no static variable block in the interface, however, the <cinit> method is still generated, the interface does not need to execute the Cinit method of the parent class before executing the Cinit method, and the implementation class initialization of the interface does not execute the Cinit method of the interface, in a multithreaded environment, Cinit execution of a class blocks other classes from executing the Cinit method of the class, and executes only once.


To the end of the loading process of this class, this time the class can really go to use.

Java Virtual machine Learning (iv) class loading process

Related Article

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.