Package org.apache.sis.internal.util
Class Cloner
java.lang.Object
org.apache.sis.internal.util.Cloner
Clones objects of arbitrary type using reflection methods. This is a workaround
for the lack of public
clone()
method in the Cloneable
interface.- Since:
- 0.3
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final IdentityHashMap
<Object, Object> Results of cloning instances previously meet during thisCloner
lifetime.private final boolean
Action to take when an object cannot be cloned because no publicclone()
method has been found.private Method
Theclone()
method, ornull
if not yet determined.private Class
<?> The type of the object to clone, ornull
if not yet specified. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionClones the given object.private Object
cloneArray
(Object array, Class<?> componentType) Clones the given array, then clone all array elements recursively.static Object
cloneIfPublic
(Object object) Clones the given object if itsclone()
method is public, or returns the same object otherwise.private static CloneNotSupportedException
Returns an exception telling that the object cannot be cloned because of the given error.private static void
Throws the given exception if it is an instance ofCloneNotSupportedException
or an unchecked exception, or do nothing otherwise.
-
Field Details
-
type
The type of the object to clone, ornull
if not yet specified. Used for checking if the cached method is still valid. -
method
Theclone()
method, ornull
if not yet determined. This is cached for better performance if many instances of the same type are cloned. -
isCloneRequired
private final boolean isCloneRequiredAction to take when an object cannot be cloned because no publicclone()
method has been found. If this field istrue
, then theclone(Object)
method in this class will throw aCloneNotSupportedException
. Otherwise theclone(Object)
method will return the original object. -
cloneResults
Results of cloning instances previously meet during thisCloner
lifetime. This is used for preserving reference graph, and also as a safety against infinite recursivity. Keys must be compared using identity comparison, notObject.equals(Object)
.
-
-
Constructor Details
-
Cloner
public Cloner()Creates a newCloner
instance which requires publicclone()
method to be present. -
Cloner
public Cloner(boolean isCloneRequired) Creates a newCloner
instance.- Parameters:
isCloneRequired
- whether aCloneNotSupportedException
should be thrown if no publicclone()
method is found.
-
-
Method Details
-
cloneArray
Clones the given array, then clone all array elements recursively.- Parameters:
array
- the array to clone.componentType
- value ofarray.getClass().getComponentType()
.- Returns:
- the cloned array, potentially with recursively cloned elements.
- Throws:
CloneNotSupportedException
- if an array element cannot be cloned.
-
clone
Clones the given object. If the given object does not provide a publicclone()
method, then there is a choice:- If
isCloneRequired(object)
returnstrue
(the default), then aCloneNotSupportedException
is thrown. - Otherwise the given object is returned.
- Parameters:
object
- the object to clone, ornull
.- Returns:
- a clone of the given object, or
null
ifobject
was null. - Throws:
CloneNotSupportedException
- if the given object cannot be cloned.
- If
-
rethrow
Throws the given exception if it is an instance ofCloneNotSupportedException
or an unchecked exception, or do nothing otherwise. If this method returns normally, then it is caller's responsibility to throw another exception.- Parameters:
cause
- the value ofInvocationTargetException.getCause()
.- Throws:
CloneNotSupportedException
-
fail
Returns an exception telling that the object cannot be cloned because of the given error.- Parameters:
cause
- the cause for the failure to clone an object.type
- the type of object that we failed to clone.- Returns:
- an exception with an error message and the given cause.
-
cloneIfPublic
Clones the given object if itsclone()
method is public, or returns the same object otherwise. This method may be convenient when there is only one object to clone, otherwise instantiating a newCloner
object is more efficient.- Parameters:
object
- the object to clone, ornull
.- Returns:
- the given object (which may be
null
) or a clone of the given object. - Throws:
CloneNotSupportedException
- if the call toObject.clone()
failed.- Since:
- 0.6
-