Java: Class loader (ClassLoader)

Source: Internet
Author: User

Sounds very high-end, in fact, the general custom class loader does not require the user to implement the parsing process, as long as the responsible implementation of the acquisition class corresponding to the. class byte stream section is OK, excerpts from a deep understanding of Java Virtual Machine

The virtual machine design team takes the "fully qualified name of a class to obtain a binary byte stream that describes this class" in the class-loading phase, which is implemented outside the Java virtual machine, so that the application can decide for itself how to get the required class. The code module that implements this action is called the ClassLoader.

Implementing the ClassLoader requires inheriting the abstract class ClassLoader, as long as the user implements the Findclass method. An example is given in the Javadoc of this class:

      classNetworkclassloaderextendsClassLoader {String host; intPort;  PublicClass Findclass (String name) {byte[] B =loadclassdata (name); returnDefineClass (name, B, 0, b.length); }           Private byte[] loadClassData (String name) {//load the class data from the connection          }      }

That is, the user of this custom ClassLoader class implements the file in a location on the network rather than the local file. DefineClass is a function in the ClassLoader class, and the underlying invocation of the local method is used to do the real class parsing work. The user-defined Findclass method is called when a class with the specified name cannot be loaded by the existing loader. Specifically in the LoadClass method:

    protectedClass<?> loadclass (String name,Booleanresolve)throwsClassNotFoundException {synchronized(Getclassloadinglock (name)) {//First , check if the class has already been loadedClass C =Findloadedclass (name); if(c = =NULL) {                LongT0 =System.nanotime (); Try {                    if(Parent! =NULL) {C= Parent.loadclass (Name,false); } Else{C=findbootstrapclassornull (name); }                } Catch(ClassNotFoundException e) {//ClassNotFoundException thrown if class not found//From the Non-null parent class loader                }                if(c = =NULL) {                    //If still not found, then invoke Findclass in order//To find the class.                    LongT1 =System.nanotime (); C=Findclass (name); //This is the defining class loader; record the statsSun.misc.PerfCounter.getParentDelegationTime (). Addtime (T1-t0);                    Sun.misc.PerfCounter.getFindClassTime (). Addelapsedtimefrom (t1);                Sun.misc.PerfCounter.getFindClasses (). increment (); }            }            if(Resolve) {resolveclass (c); }            returnC; }    }

The ClassLoader we inherit contains a member of the parent ClassLoader, and if it is not NULL, then the class will let it try to load, and then call the user-defined Findclass procedure to load when it cannot find the specified class.

Parents delegation Model (parental delegation models)

The parent class loader exists in the ClassLoader first, and the parent class loader first attempts the class loading process to involve the "parental delegation Model" of the ClassLoader. Parents are actually an ancestor (parents corresponds to the Chinese only) is the current ClassLoader in the parent member, the delegation is to let the parent loader proxy class loading process, no longer use their own class loading process. According to in-depth understanding of the JVM, there are three kinds of system-supplied loaders that have the following levels:

| ——————————————— |

| Bootstrap ClassLoader |

| ——————————————— |

|

|----------------------------------|

| Extension Classloder |

|----------------------------------|

|

|----------------------------------|

| Application Classloder |

|----------------------------------|

Bootstrap ClassLoader: is responsible for loading in the Java_home/lib directory or is specified by the-xbootclasspath parameter, and is recognized by the virtual machine by name (such as Rt.jar, the name does not recognize)

Extension ClassLoader: Responsible for loading all class libraries in the Java_home/lib/ext directory or in the path specified by the JAVA.EXT.DIRS system variable

Application ClassLoader: Responsible for loading the class library specified on the user path (CLASSPATH), typically this is the default class loader in the program

Bootstrap ClassLoader cannot be directly obtained, the following code will output null, which means that the class is loaded by bootstrap, or it can be seen from ClassLoader loadclass judging process if parent== Null is used bootstrap to load.

System.out.println (Integer.  Class. getClassLoader ());

Application ClassLoader can be obtained by Classloader.getsystemclassloader () static method, the following code will return True,klass is a class property of the user-defined classes

System.out.println (classloader.getsystemclassloader () = = Klass.getclassloader ());

The class loader that you define will applicationclassloader as the parent class loader by default.

Java: Class loader (ClassLoader)

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.