Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException because of classloaders?

While playing with classloaders i got the following exception:

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

Does this mean that an instance from a classloader is not castable to an class of another classloader?

Check my code where i'm able to instanciate 3 singletons thanks to classloaders, even with the "" security.

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);
    }

}
like image 851
Sebastien Lorber Avatar asked Aug 08 '12 13:08

Sebastien Lorber


People also ask

What causes a ClassCastException?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What code will cause a ClassCastException to be thrown?

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. So, for example, when one tries to cast an Integer to a String , String is not an subclass of Integer , so a ClassCastException will be thrown. Object i = Integer.

How do you avoid ClassCastException in your code?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

Is ClassCastException checked?

ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.


3 Answers

You cannot cast between class loaders. Class identity is composed of fully qualified name and the class loader. Check class identity crysis here.

like image 115
tibtof Avatar answered Oct 12 '22 03:10

tibtof


Yes, you are correct.

This often happens in OSGi projects, because of bad dependency management.

like image 29
Sebastian Łaskawiec Avatar answered Oct 12 '22 03:10

Sebastian Łaskawiec


That's exactly the case. You can't use casting between classes loaded by different classloaders.

This question, "Cast across classloader" may make things clearer...

like image 44
Filipe Fedalto Avatar answered Oct 12 '22 04:10

Filipe Fedalto