A custom class loader running case in complex class analysis
1, previously created using a class loader
class MyTest16 the extends ClassLoader {public Private String className; // directory Private String path; Private Final fileExtension String = ".class"; public MyTest16 (String classLoadName) { Super (); // the system class loader class loader as parent loader this.className = classLoadName; } public MyTest16 (ClassLoader parent, String classLoadName) { Super (parent); // displays the parent class loader loader is this.className = classLoadName; } public void setPath ( path String) { this.path = path; } @Override public String toString () { return "[" + this.className + "]"; } @Override protected Class<?> findClass(String clasName) throws ClassNotFoundException { System.out.println("findClass invoked:" + clasName); System.out.println("class loader name: " + this.className); byte[] data = this.loadClassData(clasName); return this.defineClass(clasName,data, 0, data.length); } private byte[] loadClassData(String className){ InputStream is = null; byte[] data = null; ByteArrayOutputStream baos = null; try{ className = className.replace(".","//"); //System.out.println("className:" +this.className); is = new FileInputStream(new File(this.path + className + this.fileExtension)); baos = new ByteArrayOutputStream(); int ch = 0; while ( -1 != (ch = is.read())){ baos.write(ch); } data = baos.toByteArray(); }catch (Exception ex){ ex.printStackTrace(); }finally { try { is.close(); baos.close(); }catch (Exception ex){ ex.printStackTrace(); } } return data; } }
2. Create a class MyCat
public class MyCat { public MyCat(){ System.out.println("MyCat is loaded by:" + this.getClass().getClassLoader()); } }
3. Create MySample class
public class MySample { public MySample(){ System.out.println("MySample is loaded by:" + this.getClass().getClassLoader()); new MyCat(); } }
4, create a test class
{class MyTest17 public public static void main (String [] args) throws Exception { MyTest16 loader1 new new MyTest16 = ( "loader1"); <?> = loader1.loadClass clazz Class ( "com.example.jvm.classloader.MySample") ; System.out.println ( "class:" + clazz.hashCode ()); // if commented diverted, then MySample not instantiate objects, i.e. MySample constructor will not be called // therefore not instantiated myCat objects, i.e. no myCat actively used here does not aggravate Class myCat Object = clazz.newInstance Object (); } }
Print results
class:1735600054 MySample is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2 MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
Increase -XX: + printing results after TraceClassLoading
If you remove the Object object = clazz.newInstance ();
Print results
Note: If commented Object object = clazz.newInstance (); the row, MySample not instantiate objects, i.e. MySample constructor will not be called
MyCat therefore does not instantiate the object, i.e. no myCat actively used here does not load MyCat Class.
This example illustrates but is not preloaded MyCat
Second, a transformation of the Code
In one basis, to test a new class
{class MyTest17_1 public public static void main (String [] args) throws Exception { MyTest16 loader1 new new MyTest16 = ( "loader1"); loader1.setPath:; ( "D / TEMP /") <?> = loader1.loadClass Class clazz ( "com.example.jvm.classloader.MySample"); System.out.println ( "class:" + clazz.hashCode ()); // comment out the line, and it does not instantiate objects MySample, i.e. MySample constructor will not be called // myCat therefore does not instantiate the object, i.e. no myCat actively used here does not load Class myCat Object = clazz.newInstance Object (); } }
A method which adds loader1.setPath ( "D: / temp / ");
MyCat.class and then cut to MySample.class D: / lower temp / directory, following two figures
Print Results:
findClass invoked:com.example.jvm.classloader.MySample class loader name: loader1 class:2133927002 MySample is loaded by:[loader1] findClass invoked:com.example.jvm.classloader.MyCat class loader name: loader1 MyCat is loaded by:[loader1]