Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Class loading and serialization
Class loading and serialization [message #462404] Mon, 29 January 2007 10:14 Go to next message
Dan Phifer is currently offline Dan PhiferFriend
Messages: 72
Registered: July 2009
Member
Is there an easy way to create a class loader that can load classes from
within any plug-in that is bundled in a feature or, even better, and RCP
application? I have two plug-ins, A and B. Plug-in A creates an instance
of a class in B, and then stores objects in that instance. For instance,
here's an example where variables that start with "a" are in plug-in A,
and variables that start with "b" are in plug-in B:

a1.b1.put(a2) // store an "a" object in a "b" object
outStream.writeObject(a1)

So, a1 has a reference to b1 and it attempts to put some other "a" class
in b1.
When I try to deserialize a1, b1 tries to recreate its reference to a2,
but fails (ClassNotFoundException) because plug-in B cannot load classes
in A, since it has no reference to A. If the BundleLoader (or some
similar construct) had a scope larger than just a plug-in, this wouldn't
be a problem.
Re: Class loading and serialization [message #462411 is a reply to message #462404] Mon, 29 January 2007 11:32 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
You could try this with buddy classloaders, but it would become messy quite quickly. Also note that this isn't really anything to do with serialization; you'd have the same problems if you were just inserting items into a Vector/List in the other plugin, too.

The fact that bundles prevent you loading classes across boundaries that you should have no knowledge of should be seen as a benefit, and not something that should be worked around.

PS ObjectOutputStreams can be dangerous; they cache references to every instance ever passed. As such, they're great for causing massive memory leaks without you knowing about it.

Alex.
Re: Class loading and serialization [message #462413 is a reply to message #462411] Mon, 29 January 2007 12:18 Go to previous messageGo to next message
Dan Phifer is currently offline Dan PhiferFriend
Messages: 72
Registered: July 2009
Member
I agree that there are great reasons for not allowing classes to be loaded across bundles, but in this case, I found that a package in my main plug-in would be useful in other applications and contained no reference in the code to any other packages in my plug-in. So, I refactored and created a separate plug-in for that package, thinking that this would allow reuse. Unfortunately, I ran into the problem with class loading mentioned below.

In order to get the same effect, I suppose I could put the package into a jar file, and then include it in my original plug-in (and any other apps that might benefit from it) and I wouldn't have any trouble with class loading.... I can't think of any other clean way to allow reuse of the package without taking away the option for using java's default serialization.

In general I would avoid java's default serialization like the plague, but in this case I'm stuck with it, at least for now.
Re: Class loading and serialization [message #462415 is a reply to message #462411] Mon, 29 January 2007 12:22 Go to previous messageGo to next message
Dan Phifer is currently offline Dan PhiferFriend
Messages: 72
Registered: July 2009
Member
Thanks Alex.

I agree that there are great reasons for not allowing classes to be loaded across bundles, but in this case, I found that a package in my main plug-in would be useful in other applications and contained no reference in the code to any other packages in my plug-in. So, I refactored and created a separate plug-in for that package, thinking that this would allow reuse. Unfortunately, I ran into the problem with class loading mentioned below.

In order to get the same effect, I suppose I could put the package into a jar file, and then include it in my original plug-in (and any other apps that might benefit from it) and I wouldn't have any trouble with class loading.... I can't think of any other clean way to allow reuse of the package without taking away the option for using java's default serialization.

In general I would avoid java's default serialization like the plague, but in this case I'm stuck with it, at least for now.
Re: Class loading and serialization [message #462425 is a reply to message #462415] Mon, 29 January 2007 13:54 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
Why use serialization/deserialization at all? Many objects are passed around inside Eclipse that conform to a known interface but whose type isn't directly visible -- after all, that's pretty much how all View and Editor objects are created. What's wrong with just passing around object references?

Alex.
Re: Class loading and serialization [message #462427 is a reply to message #462425] Mon, 29 January 2007 14:13 Go to previous messageGo to next message
Dan Phifer is currently offline Dan PhiferFriend
Messages: 72
Registered: July 2009
Member
Maybe I'm missing something...

Serialization/Deserialization is being used to save an object model to file and then restore it at some later time. When plug-in A attempts to restore the model, the default readObject method attempts to recreate it's reference to the class from plug-in B, which is fine. However, since the class from plug-in B has references to objects in Plug-in A, when it attempts to recreate them, the class loader uses Plug-in B's classpath, and thows a ClassNotFoundException.

Other than using java's default serialization to store my model, am I doing something really stupid, or am I missing some obvious solution?
Re: Class loading and serialization [message #462429 is a reply to message #462427] Mon, 29 January 2007 14:26 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
It originally sounded like you were passing the objecst around by in-memory serialisation.

I really wouldn't use serialization for any kind of storage out of memory. The format is too brittle to be sensibly used. Yes, it also has problems in the Eclipse case but it will have problems anywhere ...

The 'normal' approach in these cases is to have some kind of external file format, possibly XML based, and do the conversion on the fly.

But that's a separate issue to getting the classes to load; what may well work for your case is to have a registered buddy class loading mechanism, which will enable what you're trying to do. I wrote an article recently ( http://eclipsezone.com/articles/eclipse-vms/?source=archives) that discussed it. In essence, you'd have:

(Plugin a)
Eclipse-BuddyPolicy: registered

(Plugin b)
Eclipse-RegisterBuddy: a
Requires-Bundle: a (or whatever)

That would give plugin 'a' visibility to plugin 'b's classes, which would then allow the deserialisation to take place in plugin a.

Alex.
Re: Class loading and serialization [message #462495 is a reply to message #462404] Tue, 30 January 2007 03:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: askme.fake.net

On Mon, 29 Jan 2007 10:14:02 +0000, Dan Phifer wrote:

> Is there an easy way to create a class loader that can load classes from
> within any plug-in that is bundled in a feature or, even better, and RCP
> application? I have two plug-ins, A and B. Plug-in A creates an instance
> of a class in B, and then stores objects in that instance. For instance,
> here's an example where variables that start with "a" are in plug-in A,
> and variables that start with "b" are in plug-in B:
>
> a1.b1.put(a2) // store an "a" object in a "b" object
> outStream.writeObject(a1)
>
> So, a1 has a reference to b1 and it attempts to put some other "a" class
> in b1.
> When I try to deserialize a1, b1 tries to recreate its reference to a2,
> but fails (ClassNotFoundException) because plug-in B cannot load classes
> in A, since it has no reference to A. If the BundleLoader (or some
> similar construct) had a scope larger than just a plug-in, this wouldn't
> be a problem.


I do not particularly like this design. My approach to this has been to
let the classloader that created the class be responsible for storing and
loading the class. So my plugins hand out tokens that refer to particular
objects. And my plugins have an object registry. So when one plugin
wants to deserialize the class, it deserializes its token, and passes that
to the responsible plugin, and that plugin will deserialize the object and
pass it back.

This has been very manageable for me. Minimizes the classloading
headaches.
Re: Class loading and serialization [message #462497 is a reply to message #462427] Tue, 30 January 2007 03:43 Go to previous message
Eclipse UserFriend
Originally posted by: askme.fake.net

On Mon, 29 Jan 2007 09:13:58 -0500, Dan Phifer wrote:

> Maybe I'm missing something...
>
> Serialization/Deserialization is being used to save an object model to
> file and then restore it at some later time. When plug-in A attempts to
> restore the model, the default readObject method attempts to recreate it's
> reference to the class from plug-in B, which is fine. However, since the
> class from plug-in B has references to objects in Plug-in A, when it
> attempts to recreate them, the class loader uses Plug-in B's classpath,
> and thows a ClassNotFoundException.
>

oh its much worse than that my friend. Your lucky to get CNFE. Because
if not what you end up with is two classes that look the same to the naked
eye, but in fact are not because they were loaded by seperate
classloaders. And you start getting weird class cast exceptions.

> Other than using java's default serialization to store my model, am I
> doing something really stupid, or am I missing some obvious solution?

Tokens.
Previous Topic:invoking a WST web service from RCP application.
Next Topic:using Preferences
Goto Forum:
  


Current Time: Mon Sep 16 19:19:41 GMT 2024

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

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

Back to the top