10

在玩类加载器时,我遇到了以下异常:

Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton

这是否意味着来自类加载器的实例不能转换为另一个类加载器的类?

检查我的代码,感谢类加载器,即使使用“”安全性,我也可以在其中实例化 3 个单例。

public static void main(String[] args) throws Exception {
        URL basePath = new URL("file:/myMavenPath/target/classes/");

    Object instance = getClassInstance(Singleton.class);
    System.out.println(instance);
    //
    Object instance2 = getClassInstance(
            new URLClassLoader( new URL[]{basePath} , null )
                    .loadClass("my.Singleton")
    );
    System.out.println(instance2);
    //
    Object instance3 = getClassInstance(
            new URLClassLoader( new URL[]{basePath} , null )
                    .loadClass("my.Singleton")
    );
    System.out.println(instance3);

    // Only the 1st cast is ok
    Singleton testCast1 = (Singleton) instance;
    System.out.println("1st cast ok");
    Singleton testCast2 = (Singleton) instance2;
    System.out.println("2nd cast ok");
    Singleton testCast3 = (Singleton) instance3;
    System.out.println("3rd cast ok");
}

private static Object getClassInstance(Class clazz) throws Exception {
    Method method = clazz.getMethod("getInstance");
    method.setAccessible(true);
    return method.invoke(null);
}


class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    public static Singleton getInstance() {
        return INSTANCE;
    }

    private Singleton() {
        Exception e = new Exception();
        StackTraceElement[] stackTrace = e.getStackTrace();
        if (!"<clinit>".equals(stackTrace[1].getMethodName())) {
            throw new IllegalStateException("You shall not instanciate the Singleton twice !",e);
        }
    }

    public void sayHello() {
        System.out.println("Hello World ! " + this);
    }

}
4

3 回答 3

10

您不能在类加载器之间进行转换。类标识由完全限定名和类加载器组成。在此处检查类身份危机

于 2012-08-08T13:53:27.653 回答
4

是的,你是对的。

这在 OSGi 项目中经常发生,因为依赖管理不好。

于 2012-08-08T13:48:27.613 回答
4

情况正是如此。您不能在由不同类加载器加载的类之间使用强制转换。

这个问题,“跨类加载器转换”可能会让事情更清楚......

于 2012-08-08T13:52:47.487 回答