Analysis of the reflection mechanism of Java (II.)-class Loader

Source: Internet
Author: User

The previous blog briefly mentioned some of the relevant knowledge involved in the Java reflection mechanism, so classloader is one of them. This blog on the detailed classloader to do a relatively deep understanding. What you need to know is that the class type is not ClassLoader, because the information that ClassLoader needs is provided by it. The class type will be introduced in the next blog post.
IntroductionClassLoader is the object that is responsible for loading the class, which loads the requested class in memory or into the JVM, based on the class information provided by the JVM request. In addition, each class object (note that class is of type) holds a reference to the corresponding ClassLoader. Can be obtained through the getClassLoader () method of the Class object. The class and its classloader are corresponding, so the class can only be loaded by its corresponding classloader.
Note : The class object of the array class is not created by the ClassLoader, but is created automatically by the Java runtime as needed. the class loader for an array class is returned by Class.getclassloader (), which is the same as the class loader of its element type, and if the element type is a base type, the array class has no classloader.
categorythe JVM generates three Classloader,bootstrap ClassLoader, Extension ClassLoader, and app ClassLoader at run time.
Bootstrap ClassLoader: is written in C + +, is the JVM's built-in loader, its name is null. It is used to load the core class library, which is the class library under Lib. To do an experiment, first of all, the string class is definitely the core class of Java, so let's take a look at it as an example:
public static void Main (string[] args) {String a= "x"; System.out.println (A.getclass (). getClassLoader ());}
We pass the code to get the string load corresponding to the ClassLoader name of the output of the result is null.
Extension ClassLoader: Loads the class library under Lib/ext.
APP ClassLoader: Loads the class library in the Classpath.
Hierarchical Relationshipsas we said before, each class object will hold a reference to the corresponding ClassLoader. Each ClassLoader object also holds a reference to the parent ClassLoader. It is important to note here that the parent ClassLoader referred to here is not the inherited relationship we are familiar with, not the parent class!! first, we need to know that this is the ClassLoader object, which means that the parent ClassLoader is actually a reference to an object. Let's look at a diagram below to see the hierarchical relationship between ClassLoader objects:


We can do an experiment here to understand this hierarchical relationship. The code is as follows:
public static void Main (string[] args) {ClassLoader C =testclassloader.class.getclassloader ();d o {System.out.println ( C.getclass (). GetName ()); C=c.getparent ();} while (c!=null);}}
The result of the output is:
Sun.misc.launcher$appclassloader
Sun.misc.launcher$extclassloader
Parental loading mechanismwe learned a very important point in the hierarchical relationship: 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. Then the parental loading mechanism is based on the hierarchical relationship of this reference. That is, when a classloader receives a request, it does not load the corresponding class directly, but asks if the classloader it refers to can be loaded, and the parent ClassLoader asks if the class is loaded by the ClassLoader of the reference. Only if all the parent ClassLoader did not load the class, the original classloader to load the requested class.
Very round Ah, the text is not clear or it!


the parental loading mechanism can guarantee the security to some extent, because as long as the top layer ClassLoader can load things will certainly not let the lower ClassLoader have the chance to load. It also ensures that some of the custom, destructive classes are not loaded into the JVM core.
Conclusion : ClassLoader is a relatively difficult place to understand one is that the object will be hierarchical and the parent loader. The other is the parental loading mechanism.  Here is a video for you to refer to.

Analysis of the reflection mechanism of Java (II.)-class Loader

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.