File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Saving state of an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Saving state of an object" Watch "Saving state of an object" New topic
Author

Saving state of an object

Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
Hi all,

I just want to figure out that is there any way to save the state of an object of a class which does not implement Serializable or Extenalizable?.

If yes, how can i do that? please help me out.

Thanx


SCJP1.4(96%)<br />A drop of ink can make millions think.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Is it a JavaBean (or JavaBean object graph)? What about XML serialization - i.e. java.beans.XML(En|De)coder?

- Peter
Dan Pollitt
Greenhorn

Joined: Jun 30, 2004
Posts: 9
Perhaps something like XStream ("XStream is a simple library to serialize objects to XML and back again.") more at: http://xstream.codehaus.org/

There is a short tutorial on its use here: http://xstream.codehaus.org/tutorial.html but nice to know you can de/serialize custom objects without the need for mapping files etc, check out the features list on the project homepage (first linked above).

XStream is an open source project (BSD Style License)
Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
Hello guys,

Thanks for your posts, meanwhile i have found that the state of the object can be saved by writing the value of each and every instance variable into the persistent storage.

Am I going to the right direction....

Chao..
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I don't think you are on the right track, because - unless you are using reflection with access checking off - you cannot get at the field values unless they are something other than private. Opening fields up like that is generally bad OO.

If you expose them using JavaBean style getters and setters, they you can just as well use the standard serialization mechanism for that: XML serialization.

- Peter
Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
Thanks Peter,

Well the class I'm talking about is not the JavaBean. Its a plain Java class having some instance members...

Is it possible now..?
Dan Pollitt
Greenhorn

Joined: Jun 30, 2004
Posts: 9
Hi Vicky,

Perhaps if you are allowed you could give us some more information about your class (source code if not too big) then we could suggest approaches that might be most suitable.

Things that will affect which method to choose are whether you have control of the interface (to add public gettXXX() and setXXX(x) methods) being a java bean is basically having a no argument constructor and getter and setter methods for each instance property. If however you want to maintain "good" object orientation then exposing these properties (the internal state of the object) violates encapsulation. Some of the persistance frameworks will use reflection to access the state of an object but without you having to reveal or use these accessors in your code.

Of course you can construct/deconstruct your object by hand - but the joy of using some of the open source persistence frameworks is that you aren't reinventing the wheel and being open-source the solutions are usually of very high quality. Also if you change your class later then you will alsoo have to update your bespoke persistance methods whereas a 3rd party framework will usually adapt as it relies on reflection to interrogate an object and so no updates would be necessary.

Dan
[ July 26, 2004: Message edited by: Dan Pollitt ]
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
You can use classes like java.lang.reflect.Field to recursively get the primative fields from the class object and save those. This could be written so that it works with any class object - might have already been done.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Rovas Kram:
You can use classes like java.lang.reflect.Field to recursively get the primative fields from the class object and save those. This could be written so that it works with any class object - might have already been done.
It has been done... it's called "serialization" The thing is that you cannot in general do this blindly; for example, some of the state may refer to resources such as sockets or files that cannot be externalised. That is where infrastructure such as Serializable, Externalizable and "transient" comes in.

- Peter
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Peter den Haan:
Is it a JavaBean (or JavaBean object graph)? What about XML serialization - i.e. java.beans.XML(En|De)coder?

- Peter


if it were a proper bean it would implement Serializable which is a requirement according to the Java Beans API.


42
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Jeroen Wenting:
Is it a JavaBean (or JavaBean object graph)? What about XML serialization - i.e. java.beans.XML(En|De)coder?
if it were a proper bean it would implement Serializable which is a requirement according to the Java Beans API.
Ok. Let me rephrase that.
Is it a Java class with a zero-argument constructor and property accessors following the JavaBean method naming patterns, or with an associated BeanInfo object facilitating access to the properties, but not necessarily Serializable or Externalizable and therefore not necessarily formally a JavaBean[tm]; or an object graph of Java classes with zero-argument constructors and property accessors following the JavaBean method naming patterns, or with BeanInfo objects, but not necessarily Serializable or Externalizable and therefore not necessarily formally an object graph of JavaBean[tm]s? What about XML serialization - i.e. java.beans.XML(En|De)coder?
You're right, it's so much better now that we've cleared that up.

- Peter
[ July 29, 2004: Message edited by: Peter den Haan ]
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
It has been done... it's called "serialization" The thing is that you cannot in general do this blindly; for example, some of the state may refer to resources such as sockets or files that cannot be externalised. That is where infrastructure such as Serializable, Externalizable and "transient" comes in.


Why don't we ask Vicky if the data that she wants to save includes either a socket connection or a file(yah right). She can't use XMLEncoder if here class does not implement Serializable - whether it really can be serialized or not.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Rovas Kram:
She can't use XMLEncoder if here class does not implement Serializable [...]
Why not? As long as the public property accessors are sufficient to save and restore the bean's state - and do return either primitives or objects that can be persisted in the same way - things are fine. And even where this is not the case all is not lost: you can provide your own PersistenceDelegate for a bean to customise XML persistence.

- Peter
[ July 31, 2004: Message edited by: Peter den Haan ]
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
If you are encountering problems using the language mechanisms for saving object state due to limitations of the design itself, you might find the Memento design pattern useful. Of course, once you create a Memento, objects of that class would have to be serializable/externalizable/XMLizable/save-able just the same...

sev
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
Why not? As long as the public property accessors are sufficient to save and restore the bean's state


You're right. The class object does not have to implement Serializable in order to be encoded by XMLEncoder.

My vote is for XMLEncoder - very simple.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Saving state of an object