Showing posts with label clone. Show all posts
Showing posts with label clone. Show all posts

Tuesday, September 9, 2014

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):
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.

Thursday, May 8, 2014

How do I copy an object in Java?

Basic: Object Copying in Java.
Let us Assume an object- obj1, that contains two objects, containedObj1 and containedObj2.
enter image description here
shallow copying:
shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it. Object class provides a clone method and provides support for the shallow copying.
enter image description here
Deep copying:
A deep copy occurs when an object is copied along with the objects to which it refers. Below image shows obj1 after a deep copy has been performed on it. Not only has obj1 been copied, but the objects contained within it have been copied as well. We can use Java Object Serialization to make a deep copy. Unfortunately, this approach has some problems too(detailed examples).
enter image description here
Possible Problems:
clone is tricky to implement correctly.
It's better to use Defensive copying, copy constructors(as @egaga reply) or static factory methods.
  1. If you have an object, that you know has a public clone() method, but you don’t know the type of the object at compile time, then you have problem. Java has an interface called Cloneable. In practice, we should implement this interface if we want to make an object Cloneable. Object.clone is protected, so we must override it with a public method in order for it to be accessible.
  2. Another problem arises when we try deep copying of a complex object. Assume that the clone()method of all member object variables also does deep copy, this is too risky of an assumption. You must control the code in all classes.
For example org.apache.commons.lang.SerializationUtils will have method for Deep clone using serialization(Source). If we need to clone Bean then there are couple of utility methods inorg.apache.commons.beanutils (Source).
  • cloneBean will Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.
  • copyProperties will Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

Wednesday, September 18, 2013

Git fork is git clone?

Fork, in the GitHub context, doesn't extend Git.
It only allows clone on the server side.
When you are cloning a GitHub repo on your local workstation, you cannot contribute back to the upstream repo unless you are explicitly declared as "contributor".
So that clone (to your local workstation) isn't a "fork". It is just a clone.
The other solution to contribute to that GitHub project is to:
  • clone that GitHub repo on your GitHub account (that is the "fork" part, a clone on the server side)
  • contribute commits to that GitHub repo (it is in your own GitHub account, so you have every right to push to it)
  • signal any interesting contribution back to the original GitHub repo (that is the "pull request" part)
If yo want to keep a link with the original repo (also called upstream), you need to add a remote referring that original repo.
See "What is the difference between origin and upstream in github"
fork and upstream