Java conversion / classloader problem

The following is a simplified version of the problem:

SomeClass c = (SomeClass) obj.getSomeClassParent()

Not always, exceptions are sometimes triggered

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

How is this possible? I think this is related to the fact that Jai imageio is a native lib, but how can this happen to the relay? I may have missed something, but what?

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

This 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 http://img215.imageshack.us/img215/3894/exception.jpg

Solution

I think it might happen

>A someclass instance is loaded from classloader x (so its class is someclass of CL X or let's call it: Cl (x)) Someclass) > but it is using a different class loader For example 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 - so class cast exceptions

Possible repetition: ClassCastException when casting to the same class – it also has some good suggestions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>