2

In an implementation of org.dozer.BeanFactory.createBean(Object, Class<?>, String) I try to cast the object to type of it. If I deploy all my bundles, that is to shut down and start all bundles I got a ClassCastException:

java.lang.ClassCastException: de.xxx.Configuration cannot be cast to de.xxx.Configuration

I suspect a problem with the ClassLoaders of karaf and/or dozer. This class only one time exists in my jars and they isn't modified. Configuration doesn't implement serializeable and has no serial version id.

How to avoid this Exception?

kind regards

Thomas Richter
  • 49
  • 1
  • 10

3 Answers3

3

Spring devtools is the culprit.

In my case the object instance coming out of dozer mapping was not able to cast to target reference which was of same type.

order.setPricingConfig(dozerBeanMapper.map(orderPricing, PricingConfig.class));

There are two solutions

  1. Disable devtools by simply commenting out the dependency in maven or gradle.
  2. Include dozer jar (anything else as per your circumstances) through META-INF/spring-devtools.properties as shown below,

restart.include.dozer=/dozer-5.5.1.jar

Please note that I am using Intellij IDEA.

Amit
  • 110
  • 8
1

If you have exception that SomeClass can not be cast to SomeClass its a signal that these two classes have different classloaders. This means that it was loaded by two different classloaders. For example if you have bundle A and B, both contains SomeClass, then each class is different and object of class loaded from bundle A can not be cast to type loaded from bundle B.

Since you are pointing that you have this class in one jar I recommend to check if you have no extra entries in lib/ directory with it (dozer can load it through sun.misc.AppClassLoader). Put breakpoint in dozer BeanFactory and inspect object instance class and class object passed to createBean method. You should then get bundle class loaders used. Since then fixing issue is much easier.

splatch
  • 1,547
  • 2
  • 10
  • 15
1

maybe too old question, but... do you use in your development something like "spring-boot-devtools" that make something with the "classloader".

In my case, debugging the dozer library do it well, and the exception throws when the library return to my own class the result of the mapping.

accreativos
  • 161
  • 2
  • 9