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