Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » Class Cast Exception for same class
Class Cast Exception for same class [message #672353] Fri, 20 May 2011 15:08 Go to next message
rshelley  is currently offline rshelley
Messages: 59
Registered: April 2010
Member
Caused by: java.lang.ClassCastException: org.jboss.resteasy.spi.ResteasyProviderFactory cannot be cast to org.jboss.resteasy.spi.ResteasyProviderFactory


I created a JAR from RESTEasy's resteasy-jaxrs-2.0.1.GA.jar file and updated it with an OSGI-ified manifest exposing all of the packages in the JAR, importing packages it needed (javax.ws.rs.* and org.slf4j). The RESTEasy bundle loads in fine, and exports those packages as expected. When I try to import another bundle that uses the RESTEasy bundle via an Import-Package (in particular, the ResteasyProviderFactory class), it can't cast it, even though they are the same class from the same package.

Any ideas why I'd get such a class cast exception when the package and class are identical?
Re: Class Cast Exception for same class [message #672434 is a reply to message #672353] Fri, 20 May 2011 22:16 Go to previous messageGo to next message
Ryan  is currently offline Ryan
Messages: 17
Registered: May 2011
Junior Member
Hey, I think you're facing the same problem with classes that I am in another thread that we've been talking in. We both have the problem that a class loaded in one bundle is not the same as exactly the same class loaded in another bundle. You seem to be under the impression that if bundle A and bundle B both import package com.foo from bundle C, then any class in com.foo will be the same class in bundle A as in bundle B, but I'm fairly certain that's not how it works. OSGi is all about letting each bundle live in its own little world, and to the best of my knowledge, it does that by giving each one its own classloader. The same class loaded in two different classloaders will *never* be the same class, either by .equals(), by an instanceof check, a Class.isAssignableFrom, or in casting. It's just the way Java is designed. I'm thinking that a way to solve it--perhaps *the* way to solve it--might be with the Bundle.loadClass method, which lets you choose the bundle, and thereby the classloader, that will load a class. I'm not sure I've got my head all the way around this problem yet, though. It's a tricky one.
Re: Class Cast Exception for same class [message #672452 is a reply to message #672434] Sat, 21 May 2011 00:11 Go to previous messageGo to next message
Ryan  is currently offline Ryan
Messages: 17
Registered: May 2011
Junior Member
Some testing appears to show that my previous post was wrong, which is probably good news for both of us. I created a bundle-A, bundle-B, and bundle-C, where C defines type Foo, B defines service FooCreator, and A uses the FooCreator service to get a Foo. I created or obtained the Foo class in several different ways in bundle-A. I got a Foo from the FooCreator service and called getClass(), I used Foo.class, and I used Class.forName(). No matter which way I did it, it always showed that the classloader for class Foo was that of bundle-C. What that says to me is that we each have a class that's being exported by at least two different bundles, and somehow, two different users of that class are loading it from two different places. You might try to get a look at the classloaders of both of your ResteasyProviderFactory classes to see where each is coming from. That might give you an idea of where to look.
Re: Class Cast Exception for same class [message #672576 is a reply to message #672452] Sat, 21 May 2011 17:43 Go to previous messageGo to next message
Hristo Iliev is currently offline Hristo Iliev
Messages: 156
Registered: May 2010
Location: Sofia, Bulgaria
Senior Member

You may use the class loading commands that the milestones of Virgo 3.0 provide. Using "clload" should give you the origin of the class.

More documentation on the commands and some examples can be found in the User Guide

Since you have ClassCast there should be 2 loaders that have as a resource the same class. For example this might happen if you packaged in your bundle a class that is exported by another bundle.
Re: Class Cast Exception for same class [message #673753 is a reply to message #672576] Tue, 24 May 2011 12:21 Go to previous message
rshelley  is currently offline rshelley
Messages: 59
Registered: April 2010
Member
I need to try this again (I found a work around by just not using the class I was getting the error with) but I think Hristo points out the issue accurately. I have a WAR that (incorrectly) included a JAR (via a Maven POM dependency) and the JAR was also OSGI-ified and loaded as a separate bundle. Therefore, the JAR was being loaded twice in two different classloaders.
Previous Topic:Sorry for those messages without subject
Next Topic:java.lang.VerifyError and Virgo
Goto Forum:
  


Current Time: Fri Jul 25 18:16:56 EDT 2014

Powered by FUDForum. Page generated in 0.01724 seconds