Value preparation and analysis of class loading process (9)

preparation stage

The preparation stage is the stage of formally allocating memory for variables defined in the class (static variables, variables modified by static) and setting the initial value of the class variables , excluding instance variables.

public static int value = 123;
此时只会将value赋值为初始值0.真正赋值为123是在类构造器 < clinit>() 方法中。


public static final int value = 123;
准备阶段就会将value赋值为123

Parsing

The parsing phase is a process in which the Java virtual machine replaces symbol references in the constant pool with direct references .

Symbolic reference

Symbol reference uses a set of symbols to describe the referenced target. Symbols can be literal in any form. The reference target is not necessarily within the virtual machine's memory. The literal form of symbol reference is clearly specified in the Class file.

Direct quote

A direct reference is a pointer that can directly point to the target, a relative offset, or a handle that can be located indirectly to the target, and the reference target must exist in the virtual machine.

The parsing action mainly targets classes or interfaces, fields, class methods, and interface methods. The 7 types of symbolic reference , method type, method handle and call point qualifier

Class or interface resolution

Assuming that the current class is D, if you want to resolve an unresolved symbol reference N into a direct reference to a class or interface C, the entire resolution process of the virtual machine requires three steps:

  1. If C is not an array type, the virtual machine passes the fully qualified name representing N to the class loader of D to load C. During the loading process, other loading actions may be triggered.
  2. If C is an array type, and the array load element type is an object, load the array element type.
  3. If there are no exceptions in the above two steps, then C has actually become a valid class or interface in the virtual machine, but symbol verification is required before parsing is completed to confirm whether D has access to C. If you do not have permission, a java.lang.IllegalAccessError exception will be thrown.

After JDK9 introduces modularity, check the access rights between modules.
If D has access rights for C, it means that at least one of the following three items is true:

  1. The accessed class C is public and is in the same module as the accessed class D.
  2. The accessed class C is public and is not in the same module as the accessed class D, but the module of the accessed class C allows the module of the accessed class D to access.
  3. The visited class C is not public, but it is in the same package as the visited class D.

Field resolution

To resolve an unresolved field symbol reference, the CONSTANT_Class_info symbol reference in the class_index item in the field table will be resolved first.
After the analysis is completed, the subsequent field search for this class or interface C is required:

  1. If C itself contains a field whose simple name and field descriptor match the target, a direct reference to this field is returned, and the search ends.
  2. Otherwise, if the interface is implemented in C, each interface will be searched recursively according to the inheritance relationship. If the interface contains a field whose simple name and field descriptor match the target, a direct reference to this field will be returned, and the search ends .
  3. Otherwise, if C is not java.lang.Object, the parent class will be searched from bottom to top according to the recursive relationship. If the parent class contains a field whose simple name and field descriptor match the target, the value of this field will be returned Quote directly and end the search.
  4. Otherwise, the search fails and a java.lang.NoSuchFieldError exception is thrown.
    If the result is put back for reference, it will be authenticated.

Method analysis

First parse out the symbolic reference of the class or interface to which the index method belongs in the class_index item of the method table. If the analysis is successful, use C to represent this class, and then the virtual machine will search according to the following steps:

  1. Because the constant types referenced by the class method and the interface method symbol in the Class file format are separate, if C is found to be an interface, an exception java.lang.IncompatibleClassChangeError will be thrown.
  2. Find the method in C that contains the simple name and field descriptor that match the target, if there is a direct reference to this method, the search ends.
  3. Otherwise, recursively search for methods that contain simple names and field descriptors that match the target in the parent class of class C. If there are methods, return a direct reference to this method, and the search ends.
  4. Otherwise, recursively find in the list of implementation interfaces of class C and their parent class interfaces whether there are methods that contain simple names and field descriptors that match the target. If so, it means that C is an abstract class, which is a search The exception java.lang.AbstractMethodError is thrown at the end.
  5. Otherwise, the method search fails and java.lang.NoSuchMethodError is thrown.
    Finally, if the search succeeds and a direct reference is returned, the authorization of this method is authenticated.

Interface method analysis

First parse out the symbolic reference of the class or interface to which the index method belongs in the class_index item published by the interface party. If the analysis is successful, use C to represent this interface, and then the virtual machine will search according to the following steps:

  1. Contrary to method resolution, an exception is thrown if it is a class.
  2. Otherwise, search for a method that contains a simple name and a field descriptor that match the target in interface C, if there is a direct reference to this method, and the search ends.
  3. Otherwise, search recursively in its parent interface until the java.lang.Object class is found, and see if there is a method with a simple name and field descriptor that matches the target, if there is a direct reference to this method, the search ends .
  4. Since multiple inheritance of Java interfaces is allowed, if there are methods with simple names and field descriptors matching the target in different parent classes, one of them will be put back.
  5. Otherwise, the search fails.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109289643