Thursday, 24 June 2010

How to deep copy an object whose structure is defined at runtime?

My application needed to make a deep copy of an object, type of which was defined at runtime. It is a complex object like map which had still more complex objects inside it.

The simplest solution to do this is to serialize the object in memory using ByteArrayOutputStream instead of using FileOutputStream.
Then deserialize it using ByteArrayInputStream instead of using FileInputStream. The final object that you get is the deep copy of your object.

But the main constraint is it needs the object to be serializable.

No comments: