Copying JavaBeans Instances: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 32: Line 32:


Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
Implementation example:
<syntaxhighlight lang='java'>
</syntaxhighlight>


=BeanUtils=
=BeanUtils=


<tt>org.apache.commons.beanutils.BeanUtils</tt> is a utility method for populating JavaBeans properties via reflection. This method can be used even if we don't have access to the JavaBeans instance class code.
<tt>org.apache.commons.beanutils.BeanUtils</tt> is a utility method for populating JavaBeans properties via reflection. This method can be used even if we don't have access to the JavaBeans instance class code.

Revision as of 21:05, 15 October 2018

External

Internal

Overview

This article describes methods of copying Java Bean instances in such a way that changing the copy leaves the original object intact.

Object.clone()

This method can be used when we have access to the JavaBeans instance class code.

Javadoc Object.clone()

Essentially, the cloned object should generally behave as follows (though this is not a requirement):

 x.clone() != x
 x.clone.getClass() == x.getClass();
 x.clone().equals(x)

By convention, the returned object should be obtained by calling super.clone(). If a class and all of its superclasses (except Object) obey this convention, x.clone().getClass() == x.getClass() will be true.

The general intention behind cloning is to obtain an identical, but independent object, so the clone() method should deep-copy any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing reference to these objects with reference to 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.

If the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

Implementation example:

BeanUtils

org.apache.commons.beanutils.BeanUtils is a utility method for populating JavaBeans properties via reflection. This method can be used even if we don't have access to the JavaBeans instance class code.