Sunday, May 16, 2010

Deep Copy of java object graph

The following method uses serialization to make deep copies and avoid extensive manual editing or extending of classes. Need to make sure that all classes in the object's graph are serializable.

import java.io.*;

public class ObjectCloner {
 // so that nobody can accidentally create an ObjectCloner object
 private ObjectCloner() {
 }

 // returns a deep copy of an object
 static public Object deepCopy(Object oldObj) throws Exception {
  ObjectOutputStream oos = null;
  ObjectInputStream ois = null;
  try {
   ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
   oos = new ObjectOutputStream(bos); // B
   // serialize and pass the object
   oos.writeObject(oldObj); // C
   oos.flush(); // D
   ByteArrayInputStream bin = new ByteArrayInputStream(bos
     .toByteArray()); // E
   ois = new ObjectInputStream(bin); // F
   // return the new object
   return ois.readObject(); // G
  } catch (Exception e) {
   System.out.println("Exception in ObjectCloner = " + e);
   throw (e);
  } finally {
   oos.close();
   ois.close();
  }
 }

}

No comments:

Post a Comment