java - 匿名內部類和繼承類,在實現ClassLoader時為什么會有區別
問題描述
最近在看Java虛擬機,類加載器那節舉了這么一個例子:
/***類加載器與instanceof關鍵字演示**@author zzm*/public class ClassLoaderTest{ public static void main(String[]args) throws Exception{ClassLoader myLoader=new ClassLoader(){ @Override public Class<?>loadClass(String name)throws ClassNotFoundException{try{ String fileName=name.substring(name.lastIndexOf('.')+1)+'.class'; InputStream is=getClass().getResourceAsStream(fileName); if(is==null){return super.loadClass(name); } byte[]b=new byte[is.available()]; is.read(b); return defineClass(name,b,0,b.length);}catch(IOException e){ throw new ClassNotFoundException(name);} }};Object obj=myLoader.loadClass('org.fenixsoft.classloading.ClassLoaderTest').newInstance();System.out.println(obj.getClass());System.out.println(obj instanceof org.fenixsoft.classloading.ClassLoaderTest);//false }}
總而言之就是用自定義的類加載器去加載一個類,返回的類與jvm自帶的SystemClassLoader加載的類不同。
那么如果用一個繼承自ClasserLoader的類,按理說也是同樣的效果
package jvm;public class MyClassLoader extends ClassLoader{ @Override public Class<?> loadClass(String fullClassName) throws ClassNotFoundException{try { String fileName = fullClassName.substring(fullClassName.lastIndexOf('.')+1,fullClassName.length())+'.class'; InputStream is = getResourceAsStream(fileName); if(is==null){return super.loadClass(fullClassName); } byte[] bytes = new byte[is.available()]; is.read(bytes); return defineClass(fullClassName,bytes,0,bytes.length);}catch (Exception e){ throw new ClassNotFoundException();} } public static void main(String[] args) throws Exception{ClassLoader myClassLoader = new MyClassLoader();/**ClassLoader myClassLoader = new ClassLoader() { @Override public Class<?> loadClass(String fullClassName) throws ClassNotFoundException {try { String fileName = fullClassName.substring(fullClassName.lastIndexOf('.')+1,fullClassName.length())+'.class'; InputStream is = getClass().getResourceAsStream(fileName); if(is==null){return super.loadClass(fullClassName); } byte[] bytes = new byte[is.available()]; is.read(bytes); return defineClass(fullClassName,bytes,0,bytes.length);}catch (Exception e){ throw new ClassNotFoundException();} }};/**/ClassLoader systemClassLoader = java.lang.ClassLoader.getSystemClassLoader();Class myClass = myClassLoader.loadClass('jvm.MyClassLoader');Class systemClass = systemClassLoader.loadClass('jvm.MyClassLoader');Object myObj = myClass.newInstance();Object systemObj = systemClass.newInstance();System.out.println(myClass.equals(systemClass));//trueSystem.out.println(myObj.getClass().equals(systemObj.getClass()));//true }}
注釋里的代碼就是匿名內部類的繼承,注意到這里myClass和systemClass是完全相等的,但是myClass是使用繼承自ClassLoader的類MyClassLoader生成的,請問這是為什么呢。。。
問題解答
回答1:第二個代碼走到
if(is==null){ return super.loadClass(fullClassName);}
加載class文件用getClass().getResourceAsStream
