Achieving serialization of non-serializable objects

Q

Qu0ll

I need to serialize some Java2D objects of classes such as Shape,
GlyphVector and Composite, none of which implements or extends Serializable.
Is it possible somehow? If I can achieve the same thing where I can
reconstruct those objects on the other side of a network connection without
strictly doing serialization then that will be fine. I thought about
extracting the "data" from each object and serializing that but some of that
information is either internal or not always available.

Any ideas? Perhaps it cannot be done.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
T

Tom Anderson

I need to serialize some Java2D objects of classes such as Shape,
GlyphVector and Composite, none of which implements or extends
Serializable. Is it possible somehow? If I can achieve the same thing
where I can reconstruct those objects on the other side of a network
connection without strictly doing serialization then that will be fine.
I thought about extracting the "data" from each object and serializing
that but some of that information is either internal or not always
available.

Any ideas? Perhaps it cannot be done.

Basically, it can't. Sucks, i know.

You can use reflection to get at the fields - even if they're private, you
can call Field.setAccessible(true), and if your SecurityManager doesn't
complain, you can then read them. However, putting that information back
together on the far side is a lot harder: you can write the field contents
in much the same way, but you need to be able to create instances of the
relevant classes first, and i don't know of any general-purpose way to do
that for classes with no default constructor.

tom
 
D

Daniel Pitts

Tom said:
Basically, it can't. Sucks, i know.

You can use reflection to get at the fields - even if they're private,
you can call Field.setAccessible(true), and if your SecurityManager
doesn't complain, you can then read them. However, putting that
information back together on the far side is a lot harder: you can write
the field contents in much the same way, but you need to be able to
create instances of the relevant classes first, and i don't know of any
general-purpose way to do that for classes with no default constructor.

tom
If you control the allocation of the given objects, you can create your
own versions that are Serializable.

public class SerializableGlyphVector extends GlyphVector implements
Serializable {}

You might also be able to use JavaBean/XML transport instead.
 
E

EJP

Daniel said:
If you control the allocation of the given objects, you can create your
own versions that are Serializable.

public class SerializableGlyphVector extends GlyphVector implements
Serializable {}

.... as long as you provide your own readObject() and writeObject()
methods that take care of the data in the base class ...
 
Q

Qu0ll

... as long as you provide your own readObject() and writeObject() methods
that take care of the data in the base class ...

Which brings us back to square one doesn't it?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
O

Owen Jacobson

I need to serialize some Java2D objects of classes such as Shape,
GlyphVector and Composite, none of which implements or extends Serializable.
Is it possible somehow?  If I can achieve the same thing where I can
reconstruct those objects on the other side of a network connection without
strictly doing serialization then that will be fine.  I thought about
extracting the "data" from each object and serializing that but some of that
information is either internal or not always available.

Any ideas?  Perhaps it cannot be done.

Think carefully about what serialization does. It does not, contary
to the illusion the API maintains, "store objects" - given an object,
serialization produces a description of the object that can be
reconstituted into another object with the same properties. The
description is flat data.

With that in mind, you should already have some idea how to
"serialize", for example, Shape: what properties (completely) describe
a Shape instance? Can you map a Shape to a serializable object that
carries those properties? And, once you have those properties, can
you constitute a Shape from them?

Granted, writing serializable proxies for every Java2D class you want
to serialize is tedious. It's possible someone's already written one;
you'd have to look around a bit on google. It may also be more
sensible to serialize your own, app-specific classes which just happen
to be able to produce Java2D objects as needed at runtime. What you
can't (usefully) do is serialize the existing Java2D objects blindly,
either using ObjectOutputStream or reflection - that they're not
Serializable suggests that they may have internal state that's not
appropriate for serialization.

-o
 
E

EJP

Qu0ll said:
Which brings us back to square one doesn't it?

Not really, you can provide such methods in your derived class, but the
real problem is that you *can't* control the allocation of GlyphVectors,
as they are abstract classes implemented by hidden things in the
implementation.

The real question is why do you want to serialize things that weren't
intended to be serialized? A GlyphVector for instance is created by a
Font, which is inherently not only platform-dependent but
installation-dependent, so it has no permanent meaning, and certainly no
meaning on a platform other than the one it was created on. Same applies
therefore to GlyphVector itself and probably more than half the AWT classes.
 
Q

Qu0ll

[...]
The real question is why do you want to serialize things that weren't
intended to be serialized? A GlyphVector for instance is created by a
Font, which is inherently not only platform-dependent but
installation-dependent, so it has no permanent meaning, and certainly no
meaning on a platform other than the one it was created on. Same applies
therefore to GlyphVector itself and probably more than half the AWT
classes.

Good point. I realise now that what I was envisaging was actually a bad
idea. As you say, there is most probably a reason why those classes are not
serializable.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
A

Arne Vajhøj

Qu0ll said:
I need to serialize some Java2D objects of classes such as Shape,
GlyphVector and Composite, none of which implements or extends
Serializable. Is it possible somehow? If I can achieve the same thing
where I can reconstruct those objects on the other side of a network
connection without strictly doing serialization then that will be fine.
I thought about extracting the "data" from each object and serializing
that but some of that information is either internal or not always
available.

Any ideas? Perhaps it cannot be done.

Basically it can not be done.

The best workaround is probably this one:

http://www.javageeks.com/Papers/SerializableAdapter/SerializableAdapter.pdf

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top