Read the Java class loader ClassLoader in-depth source

Source: Internet
Author: User

The ClassLoader class loader is the object that is responsible for loading classes. The ClassLoader class is an abstract class. If the binary name of the given class (that is, the full name of the package name plus the name of the Class), the ClassLoader attempts to find or generate the data that makes up the class definition. The general strategy is to convert the name to a file name and then read the class file for that name from the file system. The basic function of the Java.lang.ClassLoader class is to find or generate its corresponding byte code based on the name of a given class, and then define a Java class from those byte codes, which is an instance of the Java.lang.Class class. In addition, ClassLoader is responsible for loading the resources needed for Java applications, such as files and configuration files. As the base class for all class loaders, the internal implementation mechanism of ClassLoader is worth studying carefully. So today I'll take a look at ClassLoader's internal realization source code.

First, let's take a look at the two construction methods of the ClassLoader class.

From the help document above we can find that when we create an instance of ClassLoader, we can show that his parent loader is not specified, and his default parent loader is the system loader. Let's take a look at the source code implementation:

/**  * creates a new  class loader using the <tt>classloader</tt> returned by  *  the method {@link   #getSystemClassLoader ()   * <tt> Getsystemclassloader () </tt>} as the parent class loader.  */protected  classloader ()  {    SecurityManager security =  System.getsecuritymanager ();    if  (security != null)  {         security.checkcreateclassloader ();    }     this.parent = getsystemclassloader ();    initialized =  true;} 
/** * Creates a new class loader using the specified parent class loader for */protected ClassLoader (ClassLoader parent)    {SecurityManager security = System.getsecuritymanager ();    if (Security! = null) {Security.checkcreateclassloader ();    } this.parent = parent; initialized = true;}

From the above source, we can find that there must be a parent property in ClassLoader to specify the current loader's add-on. The source code also proves what we have just said above.

After reading the implementation of the construction method, let's take a look at a particularly important method in ClassLoader LoadClass method, which is used to load the class. This method has an overloaded method in the JDK, but in fact it is a kind. A method with a link class, one without. Let's look at the source code for this method:

Public class<?> loadclass (String name)  throws classnotfoundexception {     return loadclass (Name, false);} /**  * loads the class with the specified <a href= "#name" >binary name</a>.  the  * default implementation of this  method searches for classes in the       *   @throws   ClassNotFoundException  *           If the class could not be found  */protected  Synchronized class<?> loadclass (string name, boolean resolve)      throws ClassNotFoundException {    // First, check if  the class has already been loaded  &Nbsp; class c = findloadedclass (name);    if  (c == null )  {        try {             if  (parent != null)  {                 c = parent.loadclass (Name, false);             } else {                 c =  FINDBOOTSTRAPCLASS0 (name);            }         } catch  (classnotfoundexception e)  {             // If still not found,  Then invoke findclass in&nbsP;order            // to find the  class.            c = findclass ( name);         }    }    if   (Resolve)  {        resolveclass (c);     }    return c;}

As for the source code interpretation of this method, I feel that I do not have to explain anything, because in the API has been explained in detail. I'm certainly not as good at explaining it as he explained, so let's take a look at how the API explains it:

Although the JDK has been explained very clearly, but one thing I would like to add. From the above source code we can see that the LoadClass method is a recursive method, always looking up, always find the root class loader, and then let the class loader to load the class. As for how the loader is implemented, we don't know. Because a local method is used when loading a class with a class load, he is written in C + +. We can't see his source code. This validates the parent class delegate mechanism of the class loader that we referred to earlier.

Let's take a look at the Findclass method, which we found in the code above. There are calls to this Findclass method in the LoadClass method, let's first look at the API's introduction to this method:

As we can see from the above, this approach is primarily to look for our class files. We should rewrite this method when we customize our own ClassLoader, because there is basically no implementation of this method in the JDK, which requires us to rewrite this method and find the class file in our own defined way. Don't believe it. You can take a look at his source code implementation:

Protected class<?> Findclass (String name) throws ClassNotFoundException {throw new classnotfoundexception (name); }

Let's introduce these for the time being. Other methods are almost the same, and recently it has become increasingly difficult for blogs to write. The lack of underlying theory is increasingly being found. Hope that through their own efforts, can slowly change the status quo.

Read the Java class loader ClassLoader in-depth source

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.