Java Virtual Machine Learning Notes (d) - the class file and class loading

Disclaimer: This article is a blogger original, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weixin_36904568/article/details/90294140

A: JVM properties

1. Platform independence

  • Translator program needs to be translated into 0 and 1 binary format consisting of - machine code to be executed computer
  • Virtual machines on different operating systems, you can load and execute bytecode (class file) with a platform-independent, and "write once, execute everywhere" to translate the byte code into machine code

2. language independence

Languages ​​can run on the Java virtual machine, by different compilers, program the translated code into bytecodes (class files), then the virtual machine is translated into machine code

Two: class loading mechanism

The virtual machine data that describes the classes loaded from Class file into memory, data validation, conversion, resolution, initialization, eventually Java virtual machine type can be directly used.

  1. load
  2. connection
    1. verification
    2. ready
    3. Parsing (possibly after initialization)
  3. initialization
  4. use
  5. Uninstall

1. class loading process

(1) class load

  1. Obtaining such a binary byte stream defined by the fully qualified class name (may be a variety of ways)
  2. Converting the byte stream storage structure represents static data structures for the operating method of the area
  3. Generating a representative of the java heap java.lang.Class object of this class, as a method of accessing a data entry area

Verification (2) Class

  • File format validation: validation of a byte stream for compliance with Class file format (constant, version number, etc.), and can be a current version of the virtual machine process, into the memory area storing method
  • Metadata validation: bytecode semantic description information (classes, interfaces, etc.) analysis, to ensure compliance with the Java language specification
  • Bytecode verification: the procedure body class data flow and control flow analysis, does not harm VM guaranty
  • Compliance verification reference: performed (reference into line with the direct reference) in the analysis stage, the information other than the own class matching check is performed to ensure that the analysis operation can be normally performed

(3) preparation class

Formally class variables allocated memory and set the class variable initial value (variable to 0, constant target value) of the stage, in the process zone

Parsing (4) of the class

The constant pool is replaced symbolic references directly referenced process

  • Symbolic references: a set of symbols to represent the target reference. Symbols can be independent of any literal memory layout, and virtual machine implementation. Refer to the destination is not necessarily loaded into memory
  • Direct reference: direct pointer to the object, relative offset or indirect targets of the handle. Associated with the memory layout of the virtual machine implementation, the goal of some references in memory
Opportunity to resolve
  • When the class is parsed class loader
  • Analyzing the sign of the reference is used
Resolution cache

In the runtime constant pool record direct reference, and identified as the constant state resolved, resolved to avoid duplication

Class or interface parsing

The current code in which the class B1, the symbol A resolve a reference to a class or interface, then B2

  • Load Class B2
    • B2 is not an array type: A virtual machine on behalf of the transfer of the B1 to a fully qualified name of the class loader to load the class B2
    • B2 is an array type and array elements as objects: a virtual opportunity to load class of array elements, and then generate an array object dimensions and elements of this array representatives
  • Symbolic references verification, validation B1 B2 have access to
Field parsing
  • Class_index field of entries in the index table analyzing CONSTANT_Class_info symbolic references, i.e. symbolic class or interface is parsed to reference A belongs, is represented by B1
    • If B1 itself contains simple field names and field descriptors are matched, the process directly returns a reference to this field
    • If B1 implements the interface, follow the inheritance from the father of a recursive search for each interface and the interface down. If the interface contains a simple field names and field descriptors match, then the direct return of this field reference
    • If the Object class B1 is not the case, according to inheritance from the father down the recursive search, if the parent class contains a simple field names and field descriptors match, then the direct return of this field reference
  • A reference to the fields of permissions verification, validation of the field have access
Class analytical method
  • Class_index for entry in the class method table index CONSTANT_Class_info symbolic references parses the class or interface is the symbol method of the class A belongs analytical references, represented by B1
    • If B1 is an interface, resolution failure
    • If B1 itself contains simple name and descriptor matching, this method returns directly references
    • Object class B1 if not, then recursively search in accordance with the parent class inheritance from the top down, if the parent class name itself contains simple and descriptor matching, the process directly returns a reference to this method
    • If B1 implements the interface, follow the inheritance from the father of a recursive search for each interface and the interface down. If the interface contains the field name and the simple matching descriptor, then the method is an abstract method, resolution failure
  • Method references in the authority verification, validation of the method has access
Interface method resolution
  • Class_index interface for entry in the method table index CONSTANT_Class_info reference symbol parsing, i.e. the class or interface symbol parsing A belongs interface method references, represented by B1
    • If B1 is a class, resolution failure
    • If B1 itself contains simple name and descriptor matching, this method returns directly references
    • Recursive search parent interface in accordance with the inheritance until the Object class. If the interface contains the field name and the simple matching descriptor, then the method is an abstract method, resolution failure
  • There is no access problem

Initialization (5) Class

Real bytecode execution program executes the class constructor <client> () method

<Client> () method
  • Assignment operation by the compiler automatically collected class in all classes and static variables of the statement statement block merger
  • Performing a first parent <client> () method, and then performing the subclass <client> () method
  • You do not need to perform sub-interface interface parent <client> () method, if the parent when the interface definition is performed using variables
  • Without first interface implementation class that implements the interface <client> () method
Initialization timing

Initiative to use the class:

  • Met bytecode instructions: new, getstatic, putstatic, invokestatic
    • new a new object instance
    • Read or set static field
    • Call the static method
  • Java.lang.reflect reflection method using call class
  • If you initialize the subclass, you need to initialize the parent class (the interface is not)
  • Main method of the main class initialization

Passive use categories:

  • Reference a static field of the parent class by subclass does not lead to initialize the subclass
  • To refer to the class by the array definition, does not cause class initialization
  • Constants at compile time will be stored in the constant pool, will not cause class initialization

2. The class loader

Described by the fully qualified name of a class of such a binary byte stream, and class loader uniquely identify a class

(1) parent delegation model

Type class loader

Represented by a combination of parent-child relationship rather than inheritance

  • Start class loader Bootstrap ClassLoader: part of the virtual machine itself. Loading the class stored in the JRE jar package lib directory, designated by the virtual machine class parameter -Xbootclasspath
  • Other class loader: outside the virtual machine
    • Extension classloader Extension ClassLoader: Inheritance boot class loader to load the class stored in the jar package under the JRE lib / ext directory, the java.ext.dirs class specified by the system variable
    • Application Loader Application ClassLoader / system class loader: inherit extension class loader, load the virtual machine parameters -cp / -classpath specified class, system variables java.class.path specified class, the class specified by the environment variable CLASSPATH
    • Custom class loader: Inheritance application loader
work process
  1. Class loader receives a request to load a class, assigned to the parent loader
  2. If the parent can not complete its feedback loader loading request (not find the required class), and then try to load the class loader

(2) destruction of the parent delegation model

  • A user override loadClass () method to override the findClass () method
  • Base class needs to call the user class: class loader through the parent class loader context, request class loader sub-class loading operation is completed (JNDI, JDBC, JCE, JAXB, JBI)
  • Hot code Alternatively, hot deployment module: Each module has its own class loader, the development of a mesh model

Guess you like

Origin blog.csdn.net/weixin_36904568/article/details/90294140