Java: Recommended Solution For Deep Cloning/Copying an Instance

Java: recommended solution for deep cloning/copying an instance

For deep cloning (clones the entire object hierarchy):

  • commons-lang SerializationUtils - using serialization - if all classes are in your control and you can force implementing Serializable.

  • Java Deep Cloning Library - using reflection - in cases when the classes or the objects you want to clone are out of your control (a 3rd party library) and you can't make them implement Serializable, or in cases you don't want to implement Serializable.

For shallow cloning (clones only the first level properties):

  • commons-beanutils BeanUtils - in most cases.

  • Spring BeanUtils - if you are already using spring and hence have this utility on the classpath.

I deliberately omitted the "do-it-yourself" option - the API's above provide a good control over what to and what not to clone (for example using transient, or String[] ignoreProperties), so reinventing the wheel isn't preferred.

Deep clone utility recommendation

I think the previous green answer was bad , why you might ask?

  • It adds a lot of code
  • It requires you to list all fields to be copied and do this
  • This will not work for Lists when using clone()
    (This is what clone() for HashMap says: Returns a shallow copy of this HashMap instance: the keys and valuesthemselves are not cloned.) so you end up doing it manually (this makes me cry)

Oh and by the way serialization is also bad, you might have to add Serializable all over the place (this also makes me cry).

So what is the solution:

Java Deep-Cloning library
The cloning library is a small, open source (apache licence) java library which deep-clones objects. The objects don't have to implement the Cloneable interface. Effectivelly, this library can clone ANY java objects. It can be used i.e. in cache implementations if you don't want the cached object to be modified or whenever you want to create a deep copy of objects.

Cloner cloner=new Cloner();
XX clone = cloner.deepClone(someObjectOfTypeXX);

Check it out at https://github.com/kostaskougios/cloning

Deep clone an object in java

Java Deep-Cloning library The cloning library is a small, open source
java library which deep-clones objects. The objects
don't have to implement the Cloneable interface. Effectivelly, this
library can clone ANY java objects.

Cloner cloner = new Cloner();
MyClass clone = cloner.deepClone(o);

So and here is example cloning.

Deep-cloning while preserving shared references to mutable objects

There is no "standard" way of such a clone operation. Additionally, I am not aware of any library that supports that.

What your requirement really says, is to build a 1-to-1 mapping (a bijection) from original object to a cloned object.

A technique would be to first build up such a hierarchy by cloning each object if it is not in the map, once the clone method is called on your root object. Afterwards, assemble the references in the new clone hierarchy.

That technique - by the way - is already implemented by the serialization technique in Java. Have all your classes implement Serializable, then write the root object to an ObjectOutputStream, pipe it to an ObjectInputStream, and then deserialize all objects. The serializing mechanism takes care of your requirement.

Is there an easy way for deep cloning a list in Kotlin?

fun cloned(arrayList: ArrayList<Any>): ArrayList<Any> {
return arrayList.map {
when (it) {
is ArrayList<*> -> cloned(it.toList() as ArrayList<Any>)
else -> it
}
} as ArrayList<Any>
}


Related Topics



Leave a reply



Submit