0

This question is very similar to the questions in can't cast to implemented interface. Classloader issues - How to determine which library versions (jar-files) are loaded

I have a class Apple which implements Beet. Beet extends Carrot

Carrot c= x.getCarrot();
c.getClass() //returns Apple
c.getClass().getInterfaces()[0] //returns Beet.

I want to access methods inside Apple. So what I did was cast c to Beet.

 Beet b=(Beet)c;

At this point I am receiving a ClassCastException. I think this is due to the ClassLoader problem discussed in the above links.

But I don't understand how to fix this. I don't know OSGi much and I don't know how to refer bundles. Can someone explain to me how to fix this problem and a good reference point.

Community
  • 1
  • 1
user77005
  • 1,769
  • 4
  • 18
  • 26
  • 2
    Could you restate your question with classes/interfaces that make sense? A Beet is not a Carrot. An Apple is not something that can do "Beet". Perhaps Apple -> Human (class), Beet -> Thinking (interface), Carrot -> Living (interface) – Mshnik Aug 12 '14 at 04:11
  • How are your OSGi bundles set up? Which bundles are Beet/Carrot/Apple in respectively, and how do they relate to the bundle that runs this code? – Mark Peters Aug 12 '14 at 04:36
  • As the post you referred to suggested, what did you get when you run `c.getClass().getInterfaces()[0].getClassLoader();`, `Beet.class.getClassLoader();`, `Carrot.class.getClassLoader();`? This will help to determine if this is truly a class loader problem. – ivan.sim Aug 12 '14 at 04:49
  • There is nothing wrong in the code. The cast doesn't violate the class hierarchy. What is your Java version / Execution Environment – naveejr Aug 12 '14 at 05:45
  • @isim here are the outputs c.getClass().getInterfaces()[0].getClassLoader())-->[5] c.getClass().getClassLoader())-->[5] b.getClass().getClassLoader())-->[7] – user77005 Aug 13 '14 at 05:34
  • @MarkPeters I don't know how to answer those questions. I did not setup any bundles but I got the project from a maven repo. Can you tell me what I need to look for and where please if you can. – user77005 Aug 13 '14 at 05:38
  • @Mshnik since others have commented using the same class names I did not change. Next time i'll try to post a more real life example. Sorry – user77005 Aug 13 '14 at 05:39

1 Answers1

0

Sounds to me like you do have a classloader problem. Are you exporting the same package from multiple bundles, or embedding a class/package as well as exporting it somewhere else?

One recommendation is to always import what you export. That way, all bundles will import each class from the same bundle and your class cast should work.

Helpful blog post: http://blog.osgi.org/2007/04/importance-of-exporting-nd-importing.html

mjmeno
  • 76
  • 1
  • 2
  • Yes this was the problem. I gave a dependency in my maven repository to the jar with the class Apple and interface Beet. Note that I marked this jar as provided. Then in my maven project I gave instructions to import the package with these classes in run-time. Worked ok for me. :) Thanks for all the help ! – user77005 Aug 25 '14 at 05:41