Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » ClassLoader Problem (ClassCastException)?
ClassLoader Problem (ClassCastException)? [message #461543] Thu, 11 January 2007 17:45 Go to next message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Member
Hi,

I'm having some difficulties with osgi classloading and I'm hoping
someone here can help me.

I have one class ResultEntry and a class FactorialResult which extends
ResultEntry. Now in one of my bundles I get an resultEntry object which
really is a FactorialTask from another bundle an then I want to
FactorialTask using the following code

FactorialResult fact = (FactorialResult) resultEntry;

Now when I try to do so I get a the following exception.

java.lang.ClassCastException:
org.ac.computegrid.jobs.fibfact.FactorialResult
at
org.ac.computegrid.jobs.fibfact.FibFactJob.addResultEntry(Fi bFactJob.java
:79)
at
org.ac.computegrid.master.GenericMaster$2.run(GenericMaster. java:167)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:58)

Following this I have also displayed the ClassLoaderTrees as I get them
with this code

ClassLoaderUtil.displayContextClassLoaderTree ();
ClassLoaderUtil.displayClassLoaderTree(FactorialResult.class
..getClassLoader ());
ClassLoaderUtil.displayClassLoaderTree (resultEntry.getClass
().getClassLoader ());
ClassLoaderUtil.displayClassLoaderTree (ResultEntry.class.getClassLoader
());

and here the output


ClassLoader Tree has 3 levels
cl0 -- Boot ClassLoader
|
cl1 -- ClassLoader
org.eclipse.osgi.baseadaptor.BaseAdaptor$ParentClassLoader@1b06041: is
unknown ClassLoader type
|
cl2 -- ClassLoader
org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@19422d2: is
unknown ClassLoader type

ClassLoader Tree has 3 levels
cl0 -- Boot ClassLoader
|
cl1 -- ClassLoader
org.eclipse.osgi.baseadaptor.BaseAdaptor$ParentClassLoader@1b06041: is
unknown ClassLoader type
|
cl2 -- ClassLoader
org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@467991: is
unknown ClassLoader type

ClassLoader Tree has 4 levels
cl0 -- Boot ClassLoader
|
cl1 -- ClassLoader
org.eclipse.osgi.baseadaptor.BaseAdaptor$ParentClassLoader@1b06041: is
unknown ClassLoader type
|
cl2 -- ClassLoader
org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@19422d2: is
unknown ClassLoader type
|
cl3 -- ClassLoader
sun.rmi.server.LoaderHandler$Loader@1174ec5["ahttp://computegrid:computeg
rid@192.168.0.200/codebase/computegrid_jobs.jar"]:
ahttp://computegrid:computegrid@192.168.0.200/codebase/compu tegrid_jobs.j
ar

ClassLoader Tree has 3 levels
cl0 -- Boot ClassLoader
|
cl1 -- ClassLoader
org.eclipse.osgi.baseadaptor.BaseAdaptor$ParentClassLoader@1b06041: is
unknown ClassLoader type
|
cl2 -- ClassLoader
org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@19422d2: is
unknown ClassLoader type


Can anyone tell me how to solve this? Ah if it helps the ResultEntry
object comes from a JavaSpace

Thanks for any help

Christian Hager
Re: ClassLoader Problem (ClassCastException)? [message #461553 is a reply to message #461543] Fri, 12 January 2007 00:11 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Christian Hager wrote:
> Hi,
>
> I'm having some difficulties with osgi classloading and I'm hoping
> someone here can help me.
>
> I have one class ResultEntry and a class FactorialResult which extends
> ResultEntry. Now in one of my bundles I get an resultEntry object which
> really is a FactorialTask from another bundle an then I want to
> FactorialTask using the following code
>
> FactorialResult fact = (FactorialResult) resultEntry;
>
> Now when I try to do so I get a the following exception.
>
> java.lang.ClassCastException:
> org.ac.computegrid.jobs.fibfact.FactorialResult

I know this sounds odd, but is FactorialTask a subclass of
FactorialResult (since you only mentioned FactorialResult is a subclass
of ResultEntry).

If it is, then something to be careful about is that each class is only
contributed once. i.e. if plugin A has ResultEntry and FactorialResult
and plugin B had FactorialResult and FactorialTask, you can get into
trouble.

Later,
PW


Re: ClassLoader Problem (ClassCastException)? [message #461561 is a reply to message #461553] Fri, 12 January 2007 08:06 Go to previous messageGo to next message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Member
In article <eo6jnd$iie$1@utils.eclipse.org>,
Paul Webster <pwebster@ca.ibm.com> wrote:

> Christian Hager wrote:
> > Hi,
> >
> > I'm having some difficulties with osgi classloading and I'm hoping
> > someone here can help me.
> >
> > I have one class ResultEntry and a class FactorialResult which extends
> > ResultEntry. Now in one of my bundles I get an resultEntry object which
> > really is a FactorialTask from another bundle an then I want to
> > FactorialTask using the following code
> >
> > FactorialResult fact = (FactorialResult) resultEntry;
> >
> > Now when I try to do so I get a the following exception.
> >
> > java.lang.ClassCastException:
> > org.ac.computegrid.jobs.fibfact.FactorialResult
>
> I know this sounds odd, but is FactorialTask a subclass of
> FactorialResult (since you only mentioned FactorialResult is a subclass
> of ResultEntry).
>
> If it is, then something to be careful about is that each class is only
> contributed once. i.e. if plugin A has ResultEntry and FactorialResult
> and plugin B had FactorialResult and FactorialTask, you can get into
> trouble.
>
> Later,
> PW

Sorry FactorialTask has nothing to do with the Problem. It was just a
typing mistake. I meant FactorialResult is a subclass of ResultEntry and
resultEntry is an instance of FactorialResult which I receive as an
ResultEntry and then want to cast to FactorialResult.

Sorry for the mistake.

Any ideas?
Re: ClassLoader Problem (ClassCastException)? [message #461564 is a reply to message #461553] Fri, 12 January 2007 10:41 Go to previous messageGo to next message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Member
In article <eo6jnd$iie$1@utils.eclipse.org>,
Paul Webster <pwebster@ca.ibm.com> wrote:

> Christian Hager wrote:
> > Hi,
> >
> > I'm having some difficulties with osgi classloading and I'm hoping
> > someone here can help me.
> >
> > I have one class ResultEntry and a class FactorialResult which extends
> > ResultEntry. Now in one of my bundles I get an resultEntry object which
> > really is a FactorialTask from another bundle an then I want to
> > FactorialTask using the following code
> >
> > FactorialResult fact = (FactorialResult) resultEntry;
> >
> > Now when I try to do so I get a the following exception.
> >
> > java.lang.ClassCastException:
> > org.ac.computegrid.jobs.fibfact.FactorialResult
>
> I know this sounds odd, but is FactorialTask a subclass of
> FactorialResult (since you only mentioned FactorialResult is a subclass
> of ResultEntry).
>
> If it is, then something to be careful about is that each class is only
> contributed once. i.e. if plugin A has ResultEntry and FactorialResult
> and plugin B had FactorialResult and FactorialTask, you can get into
> trouble.
>
> Later,
> PW

Sorry, here a little correction. FactorialTask has nothing to do with
the Problem. It was just a
typing mistake. I meant FactorialResult is a subclass of ResultEntry and
resultEntry is an instance of FactorialResult which I receive as an
ResultEntry and then want to cast to FactorialResult.

Sorry for the mistake.

Any ideas?
Re: ClassLoader Problem (ClassCastException)? [message #461595 is a reply to message #461543] Fri, 12 January 2007 23:33 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Christian Hager wrote:
> Hi,
>
> I'm having some difficulties with osgi classloading and I'm hoping
> someone here can help me.
>
> I have one class ResultEntry and a class FactorialResult which extends
> ResultEntry. Now in one of my bundles I get an resultEntry object which
> really is a FactorialTask from another bundle an then I want to
> FactorialTask using the following code
>
> FactorialResult fact = (FactorialResult) resultEntry;
>
> Now when I try to do so I get a the following exception.
>
> java.lang.ClassCastException:
> org.ac.computegrid.jobs.fibfact.FactorialResult

OK, sorry about that. Let me try a different tact.

Plugins can see classes in their Bundle-ClassPath (PDE Editor>Runtime
tab>Classpath) ... which means they can see classes in that plugin + any
jars included in the plugin.

Plugins can see classes from their required plugins, if the required
plugin exports the appropriate package.

So if plugin A has ResultEntry and FactorialResult and exports the
correct packages, plugin B can cast a ResultEntry to a FactorialResult.

If Plugin A exports ResultEntry and plugin B exports FactorialResult and
plugin C gets a ResultEntry from A (which happens to be a
FactorialResult), plugin C cannot cast the ResultEntry to a
FactorialResult because it cannot see plugin B (unless it requires both
plugin A and plugin B).

In your setup, which plugin defines ResultEntry and FactorialResult?
Which plugin returns a ResultEntry that is a FactorialResult? Which
plugin requests the ResultEntry that is a FactorialResult?

What are the plugin dependencies?

Later,
PW


Re: ClassLoader Problem (ClassCastException)? [message #461611 is a reply to message #461595] Sat, 13 January 2007 16:05 Go to previous messageGo to next message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Member
Ok let me try to explain more in detail. First we have Plugin A which
contains ResultEntry, second Plugin B which contains the
FactorialResult. Now I've Plugin C which requires A and takes the
ResultEntry from the JavaSpace and hands it over to Plugin B. Plugin B
requires Plugin A and itsself contains FactorialResult as told above. So
if I get your description right I should be able to cast, right?
Unfortunately I can't.
Also if there were any problems with the required plugins I think I
would get a ClassNotFoundException or Eclipse would mark red, or not?

I thought about it and was wondering if the problem could be in the
following code.

ClassLoader cl=Thread.currentThread ().getContextClassLoader ();
Thread.currentThread().setContextClassLoader(ResultEntry.cla ss.getClassLo
ader ());
ResultEntry result = (ResultEntry) space.take (template, null,
Long.MAX_VALUE);
Thread.currentThread ().setContextClassLoader (cl);


This is the code in Plugin C where I take the ResultEntry from the
JavaSpace. I have to set the ContextClassloader there because if I don't
I get a ClassCastException at this point.

Ok any more ideas?

Thanks again.
Re: ClassLoader Problem (ClassCastException)? [message #461614 is a reply to message #461611] Sat, 13 January 2007 17:42 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Christian Hager wrote:
> Ok let me try to explain more in detail. First we have Plugin A which
> contains ResultEntry, second Plugin B which contains the
> FactorialResult. Now I've Plugin C which requires A and takes the
> ResultEntry from the JavaSpace and hands it over to Plugin B. Plugin B
> requires Plugin A and itsself contains FactorialResult as told above. So
> if I get your description right I should be able to cast, right?

Who is doing the cast? Plugin B, after Plugin C hands it the resultEntry?

If plugin A creates the object, it will only be a ResultEntry, right?
That cannot be cast to a FactorialResult, since it isn't one.

If I've misinterpreted your description, please post again :-)

Later,
PW


Re: ClassLoader Problem (ClassCastException)? [message #461616 is a reply to message #461614] Sat, 13 January 2007 19:07 Go to previous messageGo to next message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Member
In article <eob5n1$49b$1@utils.eclipse.org>,
Paul Webster <pwebster@ca.ibm.com> wrote:

> Christian Hager wrote:
> > Ok let me try to explain more in detail. First we have Plugin A which
> > contains ResultEntry, second Plugin B which contains the
> > FactorialResult. Now I've Plugin C which requires A and takes the
> > ResultEntry from the JavaSpace and hands it over to Plugin B. Plugin B
> > requires Plugin A and itsself contains FactorialResult as told above. So
> > if I get your description right I should be able to cast, right?
>
> Who is doing the cast? Plugin B, after Plugin C hands it the resultEntry?
>
> If plugin A creates the object, it will only be a ResultEntry, right?
> That cannot be cast to a FactorialResult, since it isn't one.
>
> If I've misinterpreted your description, please post again :-)
>
> Later,
> PW

Plugin B is doing the cast. The object really is a FactorialResult which
I can see from the eclipse debugger. It is created remotely by my worker
application and stored in the javaspace. Plugin C takes it from the
JavaSpace with the code from my post before.
Re: ClassLoader Problem (ClassCastException)? [message #461621 is a reply to message #461616] Sun, 14 January 2007 00:02 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Christian Hager wrote:
>
> Plugin B is doing the cast. The object really is a FactorialResult which
> I can see from the eclipse debugger. It is created remotely by my worker
> application and stored in the javaspace. Plugin C takes it from the
> JavaSpace with the code from my post before.

JavaSpace is obviously doing it's own thing ... where is JavaSpace
loading FactorialResult from (since in theory you gave it plugin A's
classloader as its context class loader). How can it see FactorialResult?

It sounds like JavaSpace is doing its own serialization/deserialization
of classes, and that is interfering with the OSGi classloading management.

I would post your question and problem description to
news://news.eclipse.org/eclipse.technology.equinox

Equinox is the eclipse implementation of OSGi and they understand its
classloading (and might be able to help understand the OSGi + JavaSpace
interaction).

Maybe you need to pass it plugin C's classloader?

Later,
PW


Re: ClassLoader Problem (ClassCastException)? [message #461622 is a reply to message #461621] Sun, 14 January 2007 00:04 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Paul Webster wrote:
> Maybe you need to pass it plugin C's classloader?

After I posted this, I think this might not work either (although it's
worth a try). I think the OSGi classloaders don't use parent
classloading ... or do they? :-)

Later,
PW


Re: ClassLoader Problem (ClassCastException)? [message #461643 is a reply to message #461622] Mon, 15 January 2007 10:41 Go to previous message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Member
Thanks you're great. Setting the classloader of plugin B before taking from
the space actually did the job.

"Paul Webster" <pwebster@ca.ibm.com> schrieb im Newsbeitrag
news:eobs2c$i89$2@utils.eclipse.org...
> Paul Webster wrote:
>> Maybe you need to pass it plugin C's classloader?
>
> After I posted this, I think this might not work either (although it's
> worth a try). I think the OSGi classloaders don't use parent classloading
> ... or do they? :-)
>
> Later,
> PW
Previous Topic:Deploying to different platforms
Next Topic:How to set a view non-moveable programmatically
Goto Forum:
  


Current Time: Thu Apr 25 13:15:58 GMT 2024

Powered by FUDForum. Page generated in 0.03999 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top