Classcastexception When Casting to the Same Class

ClassCastException when casting to the same class

I am not quite following your description of the program flow, but usually when you get ClassCastExceptions you cannot explain you have loaded the class with one classloader then try to cast it to the same class loaded by another classloader. This will not work - they are represented by two different Class objects inside the JVM and the cast will fail.

There is an article about classloading in WebSphere. I cannot say how it applies to your application, but there are a number of possible solutions. I can think of at least:

  1. Change the context class loader manually. Requires that you can actually get a reference to an appropriate class loader, which may not be possible in your case.

    Thread.currentThread().setContextClassLoader(...);
  2. Make sure the class is loaded by a class loader higher in the hierarchy.

  3. Serialize and deserialize the object. (Yuck!)

There is probably a more appropriate way for your particular situation though.

java.lang.ClassCastException (Cannot cast class to same class)

Since this approach didnt work. We ended up using MDC to put the keys and AOP @Pointcut and @Around the inject the data into the logger.

ClassCastException from exact same class

It looks like you have 2 ClassLoaders involved. You can check the classloaders with

Customer.class.getClassLoader();

em.createNamedQuery("Customer.findByEmail").setParameter("email", user).getSingleResult().getClass().getClassLoader();

Then you can try to find more information.

ClassCastException when casting to the same class

I am not quite following your description of the program flow, but usually when you get ClassCastExceptions you cannot explain you have loaded the class with one classloader then try to cast it to the same class loaded by another classloader. This will not work - they are represented by two different Class objects inside the JVM and the cast will fail.

There is an article about classloading in WebSphere. I cannot say how it applies to your application, but there are a number of possible solutions. I can think of at least:

  1. Change the context class loader manually. Requires that you can actually get a reference to an appropriate class loader, which may not be possible in your case.

    Thread.currentThread().setContextClassLoader(...);
  2. Make sure the class is loaded by a class loader higher in the hierarchy.

  3. Serialize and deserialize the object. (Yuck!)

There is probably a more appropriate way for your particular situation though.

SpringXD: ClassCastException of same class between modules in a stream

See ClassCastException when casting to the same class . This is almost certainly a class loader issue. Try removing spring-social from the modules' class path and add it to xd/lib so it's loaded by the same class loader.

Getting class cast exception where both classes are exactly the same

This happens when two different ClassLoader objects load classes with the same name. The equality of two classes in Java depends on the fully qualified name and the class loader that loaded it.

So if two independent class loaders load classes from the same location, then objects of those types will not be able to be cast to each others type, even if their classes are called the same.



Related Topics



Leave a reply



Submit