How the Java Parental Delegation model is implemented recursively

Source: Internet
Author: User

The following are the most important methods of ClassLoader, the so-called parental delegation model. This model for the first time in the book of Zhou Zhiming see, at that time only know is the class loading process is the first delegate to the parent loader, otherwise the parent can not load, then self-loading, the whole process is actually a very simple recursive process, this article to explain the model in the end is how to achieve?

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); } } if(Resolve) {resolveclass (c); } returnC; } }

Class The class file that the ClassLoader uses to test

 Public class Testloaderclass {    public  testloaderclass ()    {        System.out.println ("Loader is" + Testloaderclass. class . GetClass ());                    }    }


//Custom class Loader
ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.nio.ByteOrder; Public classMyclassloaderextendsClassLoader {//class loader name PrivateString name; //the path of the load class PrivateString Path = "d:/"; Private FinalString FileType = ". Class"; PublicMyclassloader (String name) {//to make the system ClassLoader the parent loader for that class loader Super(); This. Name =name; } //Display the parent class loader for the Setup class loader PublicMyclassloader (ClassLoader parent, String name) {//Displays the parent loader that specifies the class loader Super(parent); This. Name =name; } PublicString GetPath () {returnpath; } Public voidSetPath (String path) { This. Path =path; } @Override PublicString toString () {return This. Name; } /*** Get a byte array of the. class file *@paramname *@return */ /*** Get Class object *@throwsIOException*/ Private byte[] GetBytes (String name)throwsIOException {//name=name.replace (".", "/");File f=NewFile (path+name+FileType); InputStream in=NewFileInputStream (f); System.out.println (in.available ()+ "File"); Bytearrayoutputstream Bao=NewBytearrayoutputstream (); intR=In.read (); while(R!=-1) {bao.write (R); R=In.read (); } System.out.println (Bao.tobytearray (). toString ()); returnBao.tobytearray (); } PublicClass<?>Findclass (String name) {byte[] data =NULL; Try{Data=getBytes (name); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (); return This. defineclass (name, data, 0, data.length); } Public Static voidMain (string[] args)throwsclassnotfoundexception, Instantiationexception, illegalaccessexception {//Loader1 's parent loader is the System class loaderMyclassloader Loader1 =NewMyclassloader ("Loader1"); Loader1.setpath ("D:/lib1/"); //Loader2 's parent loader is Loader1Myclassloader Loader2 =NewMyclassloader (Loader1, "Loader2"); Loader2.setpath ("D:/lib2/"); //Loader3 's parent loader is the root class loaderMyclassloader Loader3 =NewMyclassloader (Loader2, "Loader3"); Loader3.setpath ("D:/lib3/"); Class<?> clazz = Loader3.loadclass ("Testloaderclass"); System.out.println (Clazz.getclassloader ()); ClassLoader CL=Loader3; }}

How the Java Parental Delegation model is implemented recursively

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.