Home » Eclipse Projects » Rich Client Platform (RCP) » Class loading and serialization
Class loading and serialization [message #462404] |
Mon, 29 January 2007 10:14 |
Dan Phifer 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 #462413 is a reply to message #462411] |
Mon, 29 January 2007 12:18 |
Dan Phifer 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 |
Dan Phifer 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 #462495 is a reply to message #462404] |
Tue, 30 January 2007 03:40 |
Eclipse User |
|
|
|
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 |
Eclipse User |
|
|
|
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.
|
|
|
Goto Forum:
Current Time: Mon Sep 16 19:19:41 GMT 2024
Powered by FUDForum. Page generated in 0.04892 seconds
|