views:

204

answers:

3

Here is the simplified version of the problem:

 SomeClass c = (SomeClass) obj.getSomeClassParent()

not always but it happens sometimes to trigger exception

 org.somepackage.SomeClass can't be cast to org.somepackage.SomeClass 

How is this possible ? I suppose it has something to do with the fact that JAI imageio is native lib, but relay how can this happen ? I'm probably missing something but what ?

I'm using JAI imageio version 1.1 
dcm4che 2.0.21  DICOM lib

Here is the original code

        ImageInputStream iis = ImageIO.createImageInputStream(src);
        Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
        ImageReader reader = iter.next();
        DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();

And the original exception

 org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam can't be cast to  org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam

Exception Image

+7  A: 

I think it can happen if

  1. a SomeClass instance was loaded from ClassLoader X (so its class is SomeClass of CL X or let's call it: CL(X).SomeClass)
  2. but it is being cast in a different class loader. E.g. the current Threads class loader is Y so SomeClass is actually CL(Y).SomeClass

So you have:

  • instance class = CL(X).SomeClass
  • class cast target = CL(Y).SomeClass

Or in other words - not the same class - thus the class cast exception.


Possible duplicate of: http://stackoverflow.com/questions/826319/classcastexception-when-casting-to-the-same-class - it has some good suggestions as well.

Ehrann Mehdan
You basically have it here. The problem is that each instance is loaded by a different classloader, thus getClass() on each of these instances will return a different instance of the same class. You should edit this somewhat since a Class is loaded in both classloaders, not an instance in one and a Class in another, that isn't actually possible.
Robin
True that `SomeClass` is loaded in both classloaders, but I ment that the casting is from an instance of a class loaded in CL X, to a class loaded to CL Y. I'll edit the answer.
Ehrann Mehdan
@Ehrann Mehdan - much more clear, but you should remove the bit about serialization, that isn't applicable in this case.
Robin
It worked for me once though... but I accept your comment
Ehrann Mehdan
@Robin @ Ehrann the problem is in the class-loader for sure. But why i have different class loaders in one app it is still mistery. I'm tying to find nice solution.
Mite Mitreski
Probably unrelated, but worth a look: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24566
Ehrann Mehdan
+1  A: 

Strange have you tried casting it to a Object it extends, not sure if it has the functionality you require but might be worth trying to see if it still throws the exception.

Paul Whelan
+1  A: 

From the image I see that it looks like a web application. I read 'catalina'. So there is a big chance, that it is a pure classloading problem.

It could happen, for example, if the ImageReader you get from the ImageIO class was loaded by a different classloader (maybe because it's deployed in a different webapp), so the DicomImageReadParam object returned by the getDefaultReadParam() method is an instance of a - technically spoken - different class.

Andreas_D