public class CloneUtil extends Object
| Constructor and Description |
|---|
CloneUtil() |
| Modifier and Type | Method and Description |
|---|---|
protected static Object |
cloneArray(Object arrayToClone,
Map<Object,Object> cloneCache)
Clones the given array and all it's elements.
|
protected static void |
cloneFields(Class<?> clazz,
Object instanceToClone,
Object clonedInstance,
Map<Object,Object> cloneCache)
Clones all values in all fields of the given class and superclasses.
|
protected static Object |
cloneObject(Object instanceToClone,
Map<Object,Object> cloneCache)
Actual implementation of the cloning.
|
static <T> T |
createDeepClone(T object)
Creates a deep clone of the given object.
|
protected static Object |
createInstanceUsingClone(Object instanceToClone)
If the given value is cloneable and the cloning succeeds, the clone is returned, else null is returned.
|
protected static Object |
createInstanceUsingObjenesis(Object instanceToClone)
Tries to create an instance of the same type as the given value using Objenesis.
|
protected static boolean |
isImmutable(Object instanceToClone) |
protected static boolean |
isJdkClass(Object instanceToClone) |
public static <T> T createDeepClone(T object)
object - The object to cloneprotected static Object cloneObject(Object instanceToClone, Map<Object,Object> cloneCache) throws Throwable
cloneArray(java.lang.Object, java.util.Map<java.lang.Object, java.lang.Object>) method.
Finally it will see whether the object is cloneable and the clone method can be used. If not, Objenisis is
used to create the instance. The last step is to recursively do the same operation for the inner fields.
An object is cloned once. All created clones are put in a cache and if an object is to be cloned a second time,
the cached instance is used. This way the object graph is preserved.instanceToClone - The instance, not nullcloneCache - The cached clones, not nullThrowableprotected static boolean isImmutable(Object instanceToClone)
instanceToClone - The instance, not nullprotected static boolean isJdkClass(Object instanceToClone)
instanceToClone - The instance, not nullprotected static Object createInstanceUsingClone(Object instanceToClone)
instanceToClone - The instance, not nullprotected static Object createInstanceUsingObjenesis(Object instanceToClone)
instanceToClone - The instance, not nullprotected static void cloneFields(Class<?> clazz, Object instanceToClone, Object clonedInstance, Map<Object,Object> cloneCache) throws Throwable
clazz - The current classinstanceToClone - The instance, not nullclonedInstance - The clone, not nullcloneCache - The cached clones, not nullThrowableCopyright © 2016. All Rights Reserved.