Equivalence comparison of class types for Java important Technology (30) ClassLoader

Source: Internet
Author: User

1.1. Equality Comparison of class classes

for objects of class classes, only the same class loader is loaded, and the class name is the same.

At the same time, for the class of the delegate loading mechanism, can follow, or can not follow.

definition Hello class.

 Package com.test.javatechnology.classloader.test; // located in the test directory.  Publicclass  Hello {}

Defines the class loader.

/*** @Title: specialclassloader.java* @Package com.test.javatechnology.classloader* @Description:*@author http://www.cnblogs.com/coe2coe/* @date April 5, 2017 pm 10:07:53*@versionV1.0*/ PackageCom.test.javatechnology.classloader;Importjava.io.IOException;ImportJava.io.InputStream;/*** @ClassName: specialclassloader* @Description:*@author http://www.cnblogs.com/coe2coe/* @date April 5, 2017 pm 10:07:53**/ Public classSpecialclassloaderextendsClassLoader {//completely changing the way the class is loaded, you can not follow the class's delegate loading mechanism. @Override PublicClass<?> loadclass (String name)throwsclassnotfoundexception {System.out.println ("LoadClass:" +name);//the object class is loaded by the system itself.     if("Java.lang.Object". Equals (name)) {     return Super. LoadClass (name); }    //Special loading mode. returnSpecialloadclass (name);} /*** Load the class file in the test directory. * @paramname *@return */PrivateClass<?> Specialloadclass (String name)throwsclassnotfoundexception {Class<?>Clazz; String FullName= "test/" + name+ ". Class"; System.out.println ("Specialloadclass:" +fullname); Clazz= This. Loadclassfromfile (fullname); System.out.println ("---resolveclass--");Super. Resolveclass (clazz);returnClazz;} /*** Load the class file from the file name. * @paramName class file name *@return * @throwsclassnotfoundexception*/PrivateClass<?> loadclassfromfile (String name)throwsClassnotfoundexception{inputstream is=NULL; Class Clazz=NULL;byte[] bytes =NULL; Try{//reads the contents of the class file.  is= Specialclassloader.class. getResourceAsStream (name);intSize =is.available (); bytes=New byte[Size];is.read (bytes);//defines a class. Clazz=Super. DefineClass (Bytes, 0, size);}Catch(Exception e) {e.printstacktrace ();Throw Newclassnotfoundexception (name);}finally{if(is!=NULL){Try{is.close ();=NULL;} Catch(IOException e) {e.printstacktrace ();}}if(bytes!=NULL) {bytes=NULL;}}returnClazz;} }

Write the test code.

 Public Static voidMain (string[] args) {Class clazz=NULL;  ClassLoader ClassLoader; Try{ClassLoader=NewSpecialclassloader (); Clazz= Classloader.loadclass ("Hello"); System.out.println (Clazz);  System.out.println (Clazz.getclassloader ()); } Catch(ClassNotFoundException e) {e.printstacktrace ();} Class Helloclazz;helloclazz= Hello.class; System.out.println ("Helloclazz:" +Helloclazz); System.out.println (Helloclazz.getclassloader ()); System.out.println (Helloclazz==clazz); System.out.println (Helloclazz.equals (clazz)); } 

Operation Result:

Loadclass:hello

Specialloadclass:test/hello.class

LoadClass:java.lang.Object

---resolveclass--

Class Com.test.javatechnology.classloader.test.Hello

[Email protected]

Helloclazz:class Com.test.javatechnology.classloader.test.Hello

[Email protected]

False

False

Equivalence comparison of class types for Java important Technology (30) 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.