Java virtual machine knowledge - the class loader

Class load

1. Start Virtual Machine

  An initial virtual machine created by the bootstrap class loader class to complete, this class is specified by the specific implementation of the virtual machine. Then the Java virtual machine links to this initial class, initialize it and call the method public static void main(String[] args), after they started the whole process. Execute the main method of Java virtual machine instructions may cause the Java Virtual Machine link to other classes, interfaces or methods.

2. Load

  1. To obtain such a binary byte stream defined by the fully qualified name of a class;
  2. This represents the byte stream of static storage structure into a run-time data structure;
  3. Generating in memory represents java.lang.Class object of this class, as the access class of the various data entry;

  Part of the process of loading phase and the connection phase is carried out cross-load stage has not been completed, the connection phase may have started, but they were caught in the loading phase of operation, still belong to the contents of the connection phase, the start time these two stages It remained fixed sequence.

3. Verify

  The first step is to verify the connection phase, the purpose is to ensure that the information byte stream file included in Class meet the requirements of the current virtual machine and the virtual machine will not jeopardize their own safety.

  Non-essential validation phase, it does not affect the program run, if the referenced class after repeated verification, consider using -Xverifynone parameter to shut down most of the class verification measures to shorten the time the virtual machine class load.

4. Prepare

  Preparation phase is formally allocate memory for class variables and set the stage class variable initial values. This time includes only the memory allocation class variables (static variables modified), do not include instance variables, instance variables will be assigned together with the object when the object is instantiated in the heap. "Normally" referred to herein is an initial value of zero value data type.

  For example, consider the definition of a class variable is: public static int value=123;that the variable value with the initial value after the preparation phase 0 instead of 123. Because this time has not yet started any java method, and the value assigned to the 123 putstaticinstruction after the program is compiled, shall be deposited in the class constructor method, so the value assigned to the action of the 123 will be performed during the initialization phase.

  As for the "special case" means: public static final int value=123, i.e., when the class attribute field is ConstantValue, the preparation will be in the initialization phase the specified value, then it is marked as a final, value of the value in the preparation stage 123 is initialized to 0 instead.

5. Parse

  Parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references.

6. initialization

  Initialization phase is the final step in the process of loading the class, this time really began to execute java code defined in the class. During the preparation phase, the variable has been assigned over the initial value once the system requirements, and in the initialization phase, to initialize the class variables based on subjective Scheduler ape developed through programs and other resources, or: initialization phase is the implementation class constructor <clinit>()method process.

  <clinit>()The method is an assignment operation of the compiler automatically collected class all class variables and static block of statements static{}statements merger sequentially compiler collection was in order of appearance by the statement in the source file of the decision, the static statement block only access to the static variables defined in a block of statements before, the definition of variables behind it, can be assigned a static statement in front of the block, but can not be accessed.

Class loader

  The class loading phase "to get through the fully qualified name of a class description of such a binary byte stream" this action to the outside Java virtual machine to achieve, to allow the application to decide how to get the class needed to achieve this action code modules called "class loader."

 1. The parent delegation model

  Class loader when requested class loader, it first does not own to try to load this class, but this request is delegated to the parent class loader to complete, so all classes should ultimately deliver a load request to start the top class loader, only when the parent class loader to load the feedback they can not complete the request, the child loader will try to load your own.

  Parents benefit delegation model : a hierarchical relationship with the priority of java class together with the class loader. Ensure a class in a variety of class loader environment, they are the same class.

  • Start class loader: load \ lib directory
  • Extension class loader: load \ lib \ ext directory
  • Application class loader: the default class loader
  • Custom class loader

 2. destruction parents delegation model

 How to destroy the parents delegate model?

  ClassLoader class inheritance, and override loadClass findClass and methods, the following sample code:

public class TestClassLoader extends ClassLoader {
 
  private String name;
 
  public TestClassLoader(ClassLoader parent, String name) {
    super(parent);
    this.name = name;
  }
 
  @Override
  public String toString() {
    return this.name;
  }
 
  @Override
  public Class<?> loadClass(String name) throws ClassNotFoundException {
    ClassLoader system = getSystemClassLoader();
    Class<?> clazz = system.loadClass(name);
    if (clazz != null)
      return clazz;
    return findClass(name);
  }
 
  @Override
  public Class<?> findClass(String name) {
 
    InputStream is = null;
    byte[] data = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      is = new FileInputStream(new File("d:/Test.class"));
      int c;
      while (-1 != (c = is.read())) {
        baos.write(c);
      }
      data = baos.toByteArray();
    } catch (Exception e) {
      //...
    } finally {
      try {
        is.close();
        baos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return this.defineClass(name, data, 0, data.length);
  }
 
  public static void main(String[] args) throws Exception {
    TestClassLoader loader = new TestClassLoader(TestClassLoader.class.getClassLoader(), "TestLoaderN");
    Class clazz = loader.loadClass("test.classloader.Test");
    clazz.newInstance();
  }
 
}
public class Test {
  public Test(){
    System.out.println(this.getClass().getClassLoader().toString());
  }
}

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159640.htm