类加载器的命名空间 实例验证

所属文集:ClassLoader串烧


同一个命名空间中,类只加载一份

AppClassLoader加载程序中自定义的类。无论加载多少次,只要是被AppClassLoader加载的,其Class信息hashcode都是相同的。

子加载器可见父加载器加载的类

到处都是例子。比如核心类库的类,AppClassLoader所加载的类,都能使用String

父加载器不可见子加载器所加载的类,只能验证加载不到

应用中有Parent类,Son类。Parent类中有个getSon方法中,使用Son类。
编译后,在classpath下,移除Son类到子加载器的加载目录中,让AppClassLoader无法加载。
达到我们的运行环境要求:Parent由父加载器加载,Son由子加载器加载.
调用Parent的getSon方法的时候要new Son类,会触发Son类的加载,但是加载不到Son类,报错信息为:java.lang.NoClassDefFoundError: com/rock/Son

public class Parent {

    private Object son;

    public Object getSon(){
        return new Son();
    }
    public Object setSon(Object son){
        this.son = son;
        return this.son;
    }
}  
public class Son {
}
/**
     * 父加载器加载不了,子加载器所加载的类,
     * 父加载器加载Parent
     * 子加载器加载son
     * Parent#getSon 方法里 new Son()对象.//报错,找不到Son的类定义.
     */


    @Test
    public void testParentCanntFindSon(){
        CustomClassLoader01 customClassLoader01 = new CustomClassLoader01(ClassLoader.getSystemClassLoader());
        try {
            Class classParent = customClassLoader01.loadClass("com.rock.Parent");
            System.out.println("classParent:" + classParent.getClassLoader());
            Class classSon = customClassLoader01.loadClass("com.rock.Son");
            System.out.println("classSon:" + classSon.getClassLoader());
            Object objParent = classParent.newInstance();

            Object objSon = classSon.newInstance();
            
            Method setSon = classParent.getMethod("setSon",Object.class);
            Object resultSon = setSon.invoke(objParent, objSon);
            System.out.println(resultSon.getClass());
            System.out.println(resultSon.getClass().getClassLoader());

            try {
                Method getSon = classParent.getMethod("getSon");
                Object invoke = getSon.invoke(objParent);
                System.out.println(invoke.getClass().getClassLoader());
            }catch (Exception exp){
                //java.lang.NoClassDefFoundError: com/rock/Son
                exp.printStackTrace();
            }
        }catch (Exception exp){
            exp.printStackTrace();
        }
    }
不同命名空间的互相不可见

自定义类加载器的多个实例,分别加载Man类,并反射实例化对象,对象之间互相调用setFather赋值,会报错。a空间的不识别b空间的,即使全限定名相同;但也不识别。

public class Man {
    private Man father;
    public void setFather(Object obj){
        father = (Man)obj;
    }
}
 @Test
    public void testSifferentNamespaceClass(){
        CustomClassLoader01 customClassLoader01 = new CustomClassLoader01(ClassLoader.getSystemClassLoader());
        CustomClassLoader01 customClassLoader02 = new CustomClassLoader01(ClassLoader.getSystemClassLoader());
        try {
            Class aClass1 = customClassLoader01.loadClass("com.rock.Man");
            System.out.println("class1 : " + aClass1.getClassLoader());
            System.out.println("class1 : " + aClass1.);

            Class aClass2 = customClassLoader02.loadClass("com.rock.Man");
            System.out.println("class2 : " + aClass1.getClassLoader());
            System.out.println("class2 : " + aClass2);
            Object man1 = aClass1.newInstance();
            Object man2 = aClass2.newInstance();
            Method setFather = aClass1.getMethod("setFather", Object.class);
            setFather.invoke(man1,man2);


        }catch (Exception exp){
            exp.printStackTrace();
        }
    }
class1 : com.rock.classLoader.CustomClassLoader01@1f28c152
class1 : 2006034581
class2 : com.rock.classLoader.CustomClassLoader01@1f28c152
class2 : 488044861
...
Caused by: java.lang.ClassCastException: com.rock.Man cannot be cast to com.rock.Man
    at com.rock.Man.setFather(Man.java:6)
    ... 27 more

命名空间的设计实现

类加载器的命名空间 实例验证_第1张图片
image.png

传送门:ClassLoader串烧

你可能感兴趣的:(类加载器的命名空间 实例验证)