We have implemented a general purpose deep copy mechanism using serialization.
import java.io.*;
public class CopyUtil {
public static Object clone(Obj
A much faster alternative to serialization is implemented in Hibernate (specifically in the 2nd level cache); I don't know the details but you can check out the source code.
You may be aware that the clone()
interface is broken, thus is better avoided, unless there is a really really compelling reason to use it. From Effective Java 2nd Edition, Item 11: Override clone judiciously
Given all of the problems associated with
Cloneable
, it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override theclone
method and never to invoke it except, perhaps, to copy arrays. If you design a class for inheritance, be aware that if you choose not to provide a well-behaved protectedclone
method, it will be impossible for subclasses to implementCloneable
.
From the clone() API:
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. [...]
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
So in fact, the convention is to do a deep copy.
Still, the preferred alternative is to define a copy constructor or an independent method instead of overriding clone()
.
You may want to check out Deep Cloning Library. I don't know how it's implemented, but you may find it's a faster solution.
Although it doesn't address speed, this question has some relevant resources that would be worth investigating.