Package com.rits.cloning
Class Cloner
java.lang.Object
com.rits.cloning.Cloner
Cloner: deep clone objects.
This class is thread safe. One instance can be used by multiple threads on the same time.
- Author:
- kostantinos.kougios 18 Sep 2008
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturn a list of theField
s to include for a given class.protected <T> T
cloneInternal
(T o, Map<Object, Object> clones) protected boolean
considerImmutable
(Class<?> clz) override this to decide if a class is immutable.<T,
E extends T>
voidcopyPropertiesOfInheritedClass
(T src, E dest) copies all properties from src to dest.<T> T
deepClone
(T o) deep clones "o".<T> T
deepCloneDontCloneInstances
(T o, Object... dontCloneThese) void
instances of classes that shouldn't be cloned can be registered using this method.void
dontCloneInstanceOf
(Class<?>... c) protected Object
<T> T
fastCloneOrNewInstance
(Class<T> c) protected Class<?>
boolean
boolean
boolean
boolean
protected <T> T
newInstance
(Class<T> c) creates a new instance of c.void
nullInsteadOfClone
(Class<?>... c) instead of cloning these classes will set the field to nullfinal void
nullInsteadOfCloneFieldAnnotation
(Class<? extends Annotation>... a) instead of cloning, fields annotated with this annotations will be set to nullvoid
registerCloningStrategy
(ICloningStrategy strategy) void
registerConstant
(Class<?> c, String privateFieldName) void
void
registerFastCloner
(Class<?> c, IFastCloner fastCloner) protected void
registers a std set of fast cloners.void
registerImmutable
(Class<?>... c) registers an immutable class.protected void
registerInaccessibleClassToBeFastCloned
(String className, IFastCloner fastCloner) protected void
protected void
registers some known JDK immutable classes.void
registerStaticFields
(Class<?>... classes) registers all static fields of these classes.void
setCloneAnonymousParent
(boolean cloneAnonymousParent) if false, anonymous classes parent class won't be cloned.void
setCloneSynthetics
(boolean cloneSynthetics) void
setCloningEnabled
(boolean cloningEnabled) void
setDontCloneInstanceOf
(Class<?>... c) void
setDumpCloned
(IDumpCloned dumpCloned) provide a cloned classes dumper (so i.e.void
setDumpClonedClasses
(boolean dumpClonedClasses) will println() all cloned classes.void
setExtraImmutables
(Set<Class<?>> set) void
setExtraNullInsteadOfClone
(Set<Class<?>> set) void
setExtraNullInsteadOfCloneFieldAnnotation
(Set<Class<? extends Annotation>> set) void
setExtraStaticFields
(Set<Class<?>> set) spring framework friendly version of registerStaticFieldsvoid
setNullTransient
(boolean nullTransient) this makes the cloner to set a transient field to null upon cloning.<T> T
shallowClone
(T o) shallow clones "o".static Cloner
shared()
static Cloner
standard()
void
unregisterFastCloner
(Class<?> c)
-
Constructor Details
-
Cloner
public Cloner() -
Cloner
-
-
Method Details
-
getDumpCloned
-
setDumpCloned
provide a cloned classes dumper (so i.e. they can be logged or stored in a file instead of the default behaviour which is to println(cloned) )- Parameters:
dumpCloned
- an implementation of the interface which can dump the cloned classes.
-
isNullTransient
public boolean isNullTransient() -
setNullTransient
public void setNullTransient(boolean nullTransient) this makes the cloner to set a transient field to null upon cloning.NOTE: primitive types can't be null-ed. Their value will be set to default, i.e. 0 for int
- Parameters:
nullTransient
- true for transient fields to be nulled
-
setCloneSynthetics
public void setCloneSynthetics(boolean cloneSynthetics) -
registerFastCloners
protected void registerFastCloners()registers a std set of fast cloners. -
registerInaccessibleClassToBeFastCloned
-
fastClone
-
registerConstant
-
registerConstant
-
registerKnownJdkImmutableClasses
protected void registerKnownJdkImmutableClasses()registers some known JDK immutable classes. Override this to register your own list of jdk's immutable classes -
registerKnownConstants
protected void registerKnownConstants() -
registerCloningStrategy
-
registerStaticFields
registers all static fields of these classes. Those static fields won't be cloned when an instance of the class is cloned.This is useful i.e. when a static field object is added into maps or sets. At that point, there is no way for the cloner to know that it was static except if it is registered.
- Parameters:
classes
- array of classes
-
setExtraStaticFields
spring framework friendly version of registerStaticFields- Parameters:
set
- a set of classes which will be scanned for static fields
-
dontClone
instances of classes that shouldn't be cloned can be registered using this method.- Parameters:
c
- The class that shouldn't be cloned. That is, whenever a deep clone for an object is created and c is encountered, the object instance of c will be added to the clone.
-
dontCloneInstanceOf
-
setDontCloneInstanceOf
-
nullInsteadOfClone
instead of cloning these classes will set the field to null- Parameters:
c
- the classes to nullify during cloning
-
setExtraNullInsteadOfClone
-
nullInsteadOfCloneFieldAnnotation
instead of cloning, fields annotated with this annotations will be set to null- Parameters:
a
- the annotations to nullify during cloning
-
setExtraNullInsteadOfCloneFieldAnnotation
-
registerImmutable
registers an immutable class. Immutable classes are not cloned.- Parameters:
c
- the immutable class
-
setExtraImmutables
-
registerFastCloner
-
unregisterFastCloner
-
newInstance
creates a new instance of c. Override to provide your own implementation- Type Parameters:
T
- the type of c- Parameters:
c
- the class- Returns:
- a new instance of c
-
fastCloneOrNewInstance
-
deepClone
public <T> T deepClone(T o) deep clones "o".- Type Parameters:
T
- the type of "o"- Parameters:
o
- the object to be deep-cloned- Returns:
- a deep-clone of "o".
-
deepCloneDontCloneInstances
-
shallowClone
public <T> T shallowClone(T o) shallow clones "o". This means that if c=shallowClone(o) then c!=o. Any change to c won't affect o.- Type Parameters:
T
- the type of o- Parameters:
o
- the object to be shallow-cloned- Returns:
- a shallow clone of "o"
-
considerImmutable
override this to decide if a class is immutable. Immutable classes are not cloned.- Parameters:
clz
- the class under check- Returns:
- true to mark clz as immutable and skip cloning it
-
getImmutableAnnotation
-
cloneInternal
-
copyPropertiesOfInheritedClass
public <T,E extends T> void copyPropertiesOfInheritedClass(T src, E dest) copies all properties from src to dest. Src and dest can be of different class, provided they contain same field names/types- Type Parameters:
T
- Source class typeE
- Destination class type- Parameters:
src
- the source objectdest
- the destination object which must contain as minimum all the fields of src
-
allFields
Return a list of theField
s to include for a given class.This method may be overridden to exclude certain fields from cloning.
- Returns:
- the field list
-
isDumpClonedClasses
public boolean isDumpClonedClasses() -
setDumpClonedClasses
public void setDumpClonedClasses(boolean dumpClonedClasses) will println() all cloned classes. Useful for debugging only. Use setDumpCloned() if you want to control where to print the cloned classes.- Parameters:
dumpClonedClasses
- true to enable printing all cloned classes
-
isCloningEnabled
public boolean isCloningEnabled() -
setCloningEnabled
public void setCloningEnabled(boolean cloningEnabled) -
setCloneAnonymousParent
public void setCloneAnonymousParent(boolean cloneAnonymousParent) if false, anonymous classes parent class won't be cloned. Default is true- Parameters:
cloneAnonymousParent
- Whether to clone anonymous parent classes
-
isCloneAnonymousParent
public boolean isCloneAnonymousParent() -
standard
- Returns:
- a standard cloner instance, will do for most use cases
-