Java reflection mechanism analysis (2)-Class Loader
The previous blog briefly introduced some knowledge related to the java reflection mechanism, so ClassLoader is one of them. This blog provides a more in-depth understanding of ClassLoader. To understand this, you need to know that the Class is inseparable from the ClassLoader, because the information required by the ClassLoader is provided by it. Class types will be introduced in the next blog.
Introduction ClassLoader is the object responsible for loading classes. It is used to load the requested classes into the memory or into the Jvm based on the class information provided by the Jvm request. In addition, the Class Object of each Class (note that the Class is of the Class type) holds a reference of the corresponding ClassLoader. You can use the getClassLoader () method of the Class Object. The class corresponds to its ClassLoader, so the class can only be loaded through its corresponding ClassLoader.
Note:: The Class Object of the array Class is not created by the Class loader, but automatically created as needed during Java runtime. The Class loader of the array Class is composed of Class. getClassLoader () returns the same class loader as its element type. If the element type is of the basic type, no class loader exists for the array class.
Classification JVM generates three ClassLoader, Bootstrap ClassLoader, Extension ClassLoader, and App ClassLoader at runtime.
Bootstrap ClassLoader: it is written in C ++ and is the built-in loader of JVM. Its name is null. It is used to load the core class library, that is, the class library under lib. First of all, the String class must be the core class of java. Let's take it as an example:
public static void main(String[] args){String a="x";System.out.println(a.getClass().getClassLoader());}
The code is used to obtain the name output of the ClassLoader corresponding to String loading. The result is NULL.
Extension ClassLoader: load the class library under lib/ext.
App ClassLoader: load the class library in Classpath.
As we have said before, each Class object will hold a reference to the corresponding ClassLoader. Each ClassLoader object also holds a reference to the Parent ClassLoader. Note that the Parent ClassLoader mentioned here is not a familiar inheritance relation, not a Parent class !! First, we need to know that the ClassLoader object is mentioned here, that is, the Parent ClassLoader is actually a reference to an object. The following figure shows the hierarchical relationship between ClassLoader objects:
Here we can do an experiment to understand this level of relationship. The Code is as follows:
public static void main(String[] args){ClassLoader c =TestClassLoader.class.getClassLoader();do {System.out.println(c.getClass().getName());c=c.getParent();}while(c!=null);}}
The output result is:
Sun. misc. Launcher $ AppClassLoader
Sun. misc. Launcher $ ExtClassLoader
We have learned an important point in the hierarchical relationship of the parent-child load mechanism: the reference relationship between the loader objects. The referenced object is called the parent loader of the referenced object, which can be obtained through the getParent () method. The Parent-parent load mechanism is based on the reference hierarchy. That is, when a ClassLoader receives a request, it does not directly load the corresponding class, but asks whether the referenced ClassLoader can be loaded, the parent ClassLoader will ask whether the referenced ClassLoader has loaded the class. Only when none of the parent ClassLoader has loaded the applied class can the original ClassLoader load the applied class.
The text is unclear!
The Parent-parent loading mechanism ensures security to some extent, because as long as the top-level ClassLoader can load things, it will not give the lower-level ClassLoader a chance to load. This ensures that some custom destructive classes are not loaded into the Jvm core.
Conclusion: ClassLoader is hard to understand. It refers to the hierarchy of objects and the parent loader. The other is the parent-parent load mechanism. Here is a video for your reference.