public class BasicCloner extends Object implements Cloner, CloneDriver, CloneImplementor
Cloner
interface. This is the most functional Cloner
implementation built in to Jadira Cloning.
The alternative, MinimalCloner
is recommended for situations where only basic
deep-cloning functionality is required and maximum throughput desirable. CloneStrategy
, with implementations available in the
library. UnsafeCloneStrategy
makes use of sun.misc.Unsafe and delivers fast performance,
particularly when using Server VMs with Java 6 and later. However, it requires sun.misc.Unsafe to
be available. VMs providing this class include Oracle and IBM's JVMs. A fully functional Unsafe
implementation is not available on Android. For platforms where UnsafeCloneStrategy is not
suitable, PortableCloneStrategy
should be used instead. In general this delivers good,
but lower performance, giving about half the cloning throughput. Functionality however it is
identical. If you use the PortableCloneStrategy, Objenesis must be included on the classpath. CloneImplementor
s which can be implemented to override the normal cloning behaviour for
particular classes. Using these, must be enabled by setting useCloneImplementors
if
required to be used. setCloneImmutable(boolean)
, whilst non-cloneable classes may never be cloned. cloneTransientFields
to false will prevent the cloning of transient fields which
will be set to their null (or default primitive) values instead of being cloned. Cloneable
and invoke their
clone() method. To enable this function, set useCloneable
. Cloneable
annotation can be used to customise the treatment
of particular classes being cloned. @Cloner can be used to specify a particular method
within a class to be used to fulfil the clone for that specific class. NonCloneable
indicates that a class should not be cloned. Finally Transient
annotation can be used on
any class field to indicate that the field is transient. In the case of this last annotation, use
{cloneTransientAnnotatedFields
to enable or disable the capability (by default these
fields are not cloned). Immutable
or Immutable
provide an alternative
mechanism for indicating that a class is immutable. Constructor and Description |
---|
BasicCloner()
Create a new instance with
UnsafeCloneStrategy , unless it is not available in which
case PortableCloneStrategy will be used. |
BasicCloner(CloneStrategy cloneStrategy)
Creates a new instance with the given
CloneStrategy |
Modifier and Type | Method and Description |
---|---|
boolean |
canClone(Class<?> clazz)
True if this implementor can clone the given class
|
<T> T |
clone(T obj)
Clones the supplied object
|
<T> T |
clone(T obj,
CloneDriver context,
IdentityHashMap<Object,Object> referencesToReuse,
long stackDepth)
Performs a clone.
|
CloneImplementor |
getAnnotationImplementor(Class<?> clazz)
Retrieves the registered implementor for the given class
|
CloneImplementor |
getBuiltInImplementor(Class<?> clazz)
Gets the built in implementor for the given class
|
MethodHandle |
getCloneMethod(Class<?> clazz)
Returns the clone() method for the indicated class
|
Set<Class<?>> |
getImmutableClasses()
A list of classes that should be treated as immutable.
|
CloneImplementor |
getImplementor(Class<?> clazz)
Retrieves the registered implementor for the given class
|
Set<Class<?>> |
getNonCloneableClasses()
A list of classes that should not be cloned.
|
void |
initialiseFor(Class<?> classes)
This method is included simply so that cold starts can be avoided.
|
boolean |
isCloneImmutable()
If false, indicates that classes known to be immutable should be not cloned.
|
boolean |
isCloneSyntheticFields()
Indicates whether synthetic fields should be cloned
|
boolean |
isCloneTransientAnnotatedFields()
If false, indicates that fields annotated by the
Transient annotation should not be
cloned, instead being replaced with null. |
boolean |
isCloneTransientFields()
If false, indicates that fields modified by the transient keyword should not be cloned,
instead being replaced with null.
|
boolean |
isImmutableInstance(Object instance)
Indicates whether the given object instance should be treated as immutable
|
boolean |
isTrackReferences()
Indicates whether to track references
|
boolean |
isTrackReferencesForFlatClasses()
Indicates whether to also track references for flat classes when tracking references.
|
boolean |
isUseCloneable()
If true, then any class that implements
Cloneable will be cloned using the
Object.clone() method. |
boolean |
isUseCloneImplementors()
Indicates whether custom clone implementors are enabled
|
<T> T |
newInstance(Class<T> c)
Create a new, uninitialised instance of the given class (if supported).
|
void |
putAnnotationImplementor(Class<?> clazz,
CloneImplementor implementor)
Put the registered implementor for the given class
|
void |
putCloneMethod(Class<?> clazz,
MethodHandle handle)
Put the clone() method for the indicated class
|
void |
putImmutableInstance(Object instance)
Stores an object instance to be treated as immutable
|
void |
setCloneImmutable(boolean cloneImmutable)
If true, immutable classes should be cloned.
|
void |
setCloneSyntheticFields(boolean cloneSyntheticFields)
If true, synthetic fields should be cloned
|
void |
setCloneTransientAnnotatedFields(boolean cloneTransientAnnotatedFields)
If true, fields annotated with @Transient should be cloned
|
void |
setCloneTransientFields(boolean cloneTransientFields)
If true, fields marked with the transient keyword should be cloned
|
void |
setImmutableClasses(Set<Class<?>> immutableClasses)
Indicates classes which are immutable.
|
void |
setImplementors(Map<Class<?>,CloneImplementor> implementors)
Sets CloneImplementors to be used.
|
void |
setNonCloneableClasses(Set<Class<?>> nonCloneableClasses)
Indicates classes that are not Cloneable.
|
void |
setTrackReferences(boolean trackReferences)
If false, do not track references so that objects are reused if they appear twice
|
void |
setTrackReferencesForFlatClasses(boolean trackReferencesForFlatClasses)
If false, do not track references for flat classes when tracking references
|
void |
setUseCloneable(boolean useCloneable)
If true, the clone() method of classes implementing
Cloneable will be
delegated to where appropriate |
void |
setUseCloneImplementors(boolean useCloneImplementors)
If true, cloning may be delegates to clone implementors for specific tasks.
|
public BasicCloner()
UnsafeCloneStrategy
, unless it is not available in which
case PortableCloneStrategy
will be used.public BasicCloner(CloneStrategy cloneStrategy)
CloneStrategy
cloneStrategy
- CloneStrategy to be usedpublic <T> T clone(T obj)
Cloner
public <T> T newInstance(Class<T> c)
CloneImplementor
newInstance
in interface CloneImplementor
T
- The type of the instance to be constructedc
- Type to create instance ofpublic boolean canClone(Class<?> clazz)
CloneImplementor
canClone
in interface CloneImplementor
clazz
- The classpublic <T> T clone(T obj, CloneDriver context, IdentityHashMap<Object,Object> referencesToReuse, long stackDepth)
CloneImplementor
clone
in interface CloneImplementor
T
- The type of the object to be clonedobj
- Object to clonecontext
- The CloneDriver that initiated the requestreferencesToReuse
- Any references for objects already cloned to ensure reference
identity is preservedstackDepth
- The number of objects already cloned in the current data structurepublic CloneImplementor getBuiltInImplementor(Class<?> clazz)
CloneDriver
getBuiltInImplementor
in interface CloneDriver
clazz
- The classpublic CloneImplementor getImplementor(Class<?> clazz)
CloneDriver
getImplementor
in interface CloneDriver
clazz
- The classpublic CloneImplementor getAnnotationImplementor(Class<?> clazz)
CloneDriver
getAnnotationImplementor
in interface CloneDriver
clazz
- The classpublic void setImplementors(Map<Class<?>,CloneImplementor> implementors)
implementors
- The implementorspublic Set<Class<?>> getImmutableClasses()
CloneDriver
getImmutableClasses
in interface CloneDriver
public void setImmutableClasses(Set<Class<?>> immutableClasses)
immutableClasses
- Classes which should be treated as immutablepublic Set<Class<?>> getNonCloneableClasses()
CloneDriver
getNonCloneableClasses
in interface CloneDriver
public void setNonCloneableClasses(Set<Class<?>> nonCloneableClasses)
nonCloneableClasses
- Set of non-cloneable classespublic boolean isUseCloneable()
CloneDriver
Cloneable
will be cloned using the
Object.clone()
method.isUseCloneable
in interface CloneDriver
public void setUseCloneable(boolean useCloneable)
Cloneable
will be
delegated to where appropriateuseCloneable
- True if the cloneable method should be usedpublic MethodHandle getCloneMethod(Class<?> clazz)
CloneDriver
getCloneMethod
in interface CloneDriver
clazz
- The class to obtain the method forpublic boolean isCloneTransientFields()
CloneDriver
isCloneTransientFields
in interface CloneDriver
public void setCloneTransientFields(boolean cloneTransientFields)
cloneTransientFields
- true if fields decorated with transient keyword should be clonedpublic boolean isCloneTransientAnnotatedFields()
CloneDriver
Transient
annotation should not be
cloned, instead being replaced with null.isCloneTransientAnnotatedFields
in interface CloneDriver
public void setCloneTransientAnnotatedFields(boolean cloneTransientAnnotatedFields)
cloneTransientAnnotatedFields
- True if fields annotated with @Transient should be clonedpublic boolean isCloneImmutable()
CloneDriver
isCloneImmutable
in interface CloneDriver
public void setCloneImmutable(boolean cloneImmutable)
cloneImmutable
- True if immutable classes should be cloned.public void initialiseFor(Class<?> classes)
Cloner
initialiseFor
in interface Cloner
classes
- Classes to perform initialisation for.public boolean isUseCloneImplementors()
CloneDriver
isUseCloneImplementors
in interface CloneDriver
public void setUseCloneImplementors(boolean useCloneImplementors)
useCloneImplementors
- True if CloneImplementors may be delegated topublic boolean isCloneSyntheticFields()
CloneDriver
isCloneSyntheticFields
in interface CloneDriver
public void setCloneSyntheticFields(boolean cloneSyntheticFields)
cloneSyntheticFields
- True if synthetic fields should be clonedpublic void putAnnotationImplementor(Class<?> clazz, CloneImplementor implementor)
CloneDriver
putAnnotationImplementor
in interface CloneDriver
clazz
- The classimplementor
- Implementor to registerpublic void putCloneMethod(Class<?> clazz, MethodHandle handle)
CloneDriver
putCloneMethod
in interface CloneDriver
clazz
- The class to obtain the method forhandle
- The related MethodHandlepublic boolean isImmutableInstance(Object instance)
CloneDriver
isImmutableInstance
in interface CloneDriver
instance
- The instance to check for whether it is registered as immutablepublic void putImmutableInstance(Object instance)
CloneDriver
putImmutableInstance
in interface CloneDriver
instance
- Object to be considered immutablepublic boolean isTrackReferences()
CloneDriver
isTrackReferences
in interface CloneDriver
public void setTrackReferences(boolean trackReferences)
trackReferences
- True if references should be trackedpublic boolean isTrackReferencesForFlatClasses()
CloneDriver
isTrackReferencesForFlatClasses
in interface CloneDriver
public void setTrackReferencesForFlatClasses(boolean trackReferencesForFlatClasses)
trackReferencesForFlatClasses
- False if references should not be tracked for flat classesCopyright © 2010–2018 Jadira Systems. All rights reserved.