When the Java application runs, the Java Virtual machine saves a copy of the internal run-time pool, which differs from the constant pool of class files and is the data structure that the class file constant pool maps to the virtual machines.
1.constant_class Entrance Analysis
The symbolic resolution of an array class is special. In the case of a primitive type array, the virtual machine creates a new array class of that base type and creates a class instance to represent that type, and the class loader that defines the array class as the startup class loader . If you have an array of reference types, then the parsing of the reference type is done before, and the class loader that defines the array class is the class loader that defines the reference type .
Parsing of non-array classes and interfaces goes through the following steps:
(1). Load the type and all of its super-types
If the type has previously been loaded into the current namespace of the virtual machine, then directly use the type that has already been loaded, otherwise it is loaded by the initial class loader of the referenced originating class. The loading of a superclass of the target type must be done on the basis of loading the current type, because only the current type is loaded, the symbolic reference to its immediate superclass can be found from the Super_class domain of the class file, and then recursively parsed and loaded until the Java.lang.Object class. In the recursive return process, the interfaces domain is examined to see which interfaces are implemented or extended, and again recursively traverse the symbolic references to the interface.
(2). Check access rights
The connection and initialization of the target type is followed so that the type can be used normally. As mentioned earlier, the initialization of the target type requires that all of its superclass must be initialized (the hyper-interface is not required), and because its superclass has already been loaded, it is no longer dependent on the parsing order from the class to the object class, but rather from the object class to initialize the class. Type of connection and initialization steps are as follows:
(3). Type check
(4). Type Preparation
(5). Type resolution (can be deferred)
Note that this procedure is a parsing of the symbolic references to the referenced type and its superclass, because some symbolic references to the referenced type are not immediately used, so the step is strictly a process of symbolic parsing of the type that originated the reference. These symbol references are parsed, loaded, concatenated, and initialized to the type they are pointing to, only if they are actively using the referenced type's symbol references to the type they are pointing to.
(6). Type initialization
2.constant_fieldref Entrance Analysis
Because a type does not have a field defined by its superclass, the search for the target field begins with the type that the field points to, searches from that type, recursively searches its implemented or extended interfaces, and then recursively searches its superclass until the target field is found, and the field entry that runs the constant pool is marked as resolved. and change the data on the constant pool to a direct reference to the field.
3.constant_methodref Entrance Analysis
Similar but different from field searches, its search order begins with the type, recursively searches its superclass, and recursively searches its implemented or extended interfaces.
4.constant_interfacemethodref Entrance Analysis
The search for an interface method begins with the parsed interface and recursively searches its hyper-interface.
5.constant_string Entrance Analysis
Java virtual machines treat strings as a string object to maintain, and the virtual machine maintains a string pool that contains references to all of the string objects being "detained". The parsing of the constant_string constant pool is the first to see if the reference to the string object exists in the string pool, and if it exists, resolves the constant pool data directly to the reference of the string object, and if it does not exist, it is necessary to create a string object from the string sequence. and adds its reference to the string pool and resolves the constant pool data to that reference.
You can also use the Intern object of a string object to detain a string (note that it is not a string object), and if a reference to the object of that string sequence exists in the string pool, return the reference directly, otherwise the string will be detained, but note Detention the returned string object reference will not point to the original string object because the original string object is in the Java heap, and the object of the string pool is created by the virtual machine and maintained by the virtual machine .
PackageCom.ice.intern; Public classInterntest { Public Static voidMain (String args[]) {string a=NewString ("123"); String b=A; String C=NewString ("123");; System.out.println ("Before Intern:"); System.out.println ("A = B?:" + (A = = b));//trueSystem.out.println ("a = c?:" + (A = = c));//false, different objectsA =A.intern (); C=C.intern (); System.out.println ("After Intern:"); System.out.println ("A = B?:" + (A = = b));//false,System.out.println ("a = c?:" + (A = = c));//true}}
(6). Other types (data base types) entry parsing
Use the constant value contained in the constant pool directly to
6. Direct reference
Constant pool resolution eventually replaces the symbolic reference with a direct reference. Direct references to types, class variables, and class methods may be pointers in the method area. A direct reference to an instance variable and an instance method is an offset from the beginning of the object image to the instance variable or method table.
Instance variables are organized by starting from the object class to the type of the instance, placing the instance variables declared in the class in the object image in the order in which they appear in the classes file.
Instance methods are organized in much the same way: from the object class to the type of the instance, the instance method pointers declared in the class are placed in the object image in the order in which they appear in the classes file. However, the overridden method appears in the location of the superclass (the first occurrence of the method).
However, the Access interface method cannot be accessed simply by the offset of the method table, but must search the method table of the object's class to find the method.
For example, the factory interface consists of A and B to implement its produce () method, but since A and B can not guarantee a superclass derived from the same implementation of the factory interface, that is, the same produce () method offset, The factory produce () method cannot be accessed by the offset of the method table.
7. Load constraints
For a symbol reference of a type that points to another type, if the referenced and referenced types are not loaded by the same initial loader (possibly through a user-defined ClassLoader), the virtual machine must ensure that the referenced type is consistent across the different namespaces. This way, when you load an untrusted type with a custom ClassLoader, you do not resolve the symbolic reference to the referenced type, as the type of the trusted is an untrusted type that has already been parsed (because the symbolic reference to the method has only permission naming and descriptors, and it does not know its initial classloader). The untrusted type's method is called to access the protected member of the trusted type.
Introduction to Java Constant pool parsing and string intern