Module io.github.sugarcubes.cloner
Package io.github.sugarcubes.cloner
Class ReflectionClonerBuilder
java.lang.Object
io.github.sugarcubes.cloner.ReflectionClonerBuilder
Builder for reflection cloners.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Creates an instance of the cloner on the basis of the configuration.Registers clone for the object.copier(Class<?> type, ObjectCopier<?> copier) Registers copier for type.copiers(Map<Class<?>, ObjectCopier<?>> copiers) Registers set of copiers.copyConstructors(boolean enabled) Enables or disables copy constructors which the build tools of the cloner add to classes (enabled by default).executor(ExecutorService executor) Enables parallel mode with given executor service.fieldAction(Class<?> type, String field, CopyAction action) Registers action for field.fieldAction(Field field, CopyAction action) Registers action for field.fieldAction(Predicate<Field> fieldPredicate, CopyAction action) Registers action for field predicate.fieldPolicy(CopyPolicy<Field> fieldPolicy) Sets field copy policy.fieldValue(Class<?> type, String field, Supplier<?> value) Registers the value which clones get in the field, seefieldValue(Field, Supplier).fieldValue(Field field, Supplier<?> value) Registers the value which clones get in the field instead of the copy of the original value.fieldValue(Predicate<Field> fieldPredicate, Supplier<?> value) Registers the value which clones get in the fields matching the predicate, seefieldValue(Field, Supplier).generatedCopiers(boolean enabled) Enables or disables generated copiers.mode(CloningMode mode) Sets cloning mode.objectAction(Object original, CopyAction action) Registers action for object.objectPolicy(CopyPolicy<Object> objectPolicy) Sets object policy.Registers type to be copied in shallow mode, i.e.Registers singleton, i.e.Sets toolset.traversalAlgorithm(TraversalAlgorithm traversalAlgorithm) Sets traversal algorithm for objects graph.typeAction(Class<?> type, CopyAction action) Registers action for type.typeAction(String type, CopyAction action) Registers action for type.typeAction(Predicate<Class<?>> typePredicate, CopyAction action) Registers action for type predicate.typePolicy(CopyPolicy<Class<?>> typePolicy) Sets type copy policy.
-
Constructor Details
-
ReflectionClonerBuilder
public ReflectionClonerBuilder()Creates a builder.
-
-
Method Details
-
toolset
Sets toolset.- Parameters:
toolset- toolset- Returns:
- same builder instance
-
mode
Sets cloning mode.- Parameters:
mode- cloning mode- Returns:
- same builder instance
-
traversalAlgorithm
Sets traversal algorithm for objects graph.- Parameters:
traversalAlgorithm- traversal algorithm- Returns:
- same builder instance
-
executor
Enables parallel mode with given executor service.- Parameters:
executor- executor service- Returns:
- same builder instance
-
objectPolicy
Sets object policy. Using object policy significantly slows down cloning process, thus, type and field policies should be used if possible.- Parameters:
objectPolicy- object policy- Returns:
- same builder instance
-
objectAction
Registers action for object. Usage of object actions significantly slows down cloning process, thus, type and field policies should be used if possible.- Parameters:
original- original objectaction- action- Returns:
- same builder instance
-
typePolicy
Sets type copy policy.- Parameters:
typePolicy- type copy policy- Returns:
- same builder instance
-
typeAction
Registers action for type.- Parameters:
type- object typeaction- action- Returns:
- same builder instance
-
typeAction
Registers action for type.- Parameters:
type- object type nameaction- action- Returns:
- same builder instance
-
typeAction
Registers action for type predicate.- Parameters:
typePredicate- type predicateaction- action- Returns:
- same builder instance
-
fieldPolicy
Sets field copy policy.- Parameters:
fieldPolicy- type copy policy- Returns:
- same builder instance
-
fieldAction
Registers action for field.- Parameters:
field- fieldaction- action- Returns:
- same builder instance
-
fieldAction
Registers action for field.- Parameters:
type- object typefield- declared field nameaction- action- Returns:
- same builder instance
-
fieldAction
Registers action for field predicate.- Parameters:
fieldPredicate- field predicateaction- action- Returns:
- same builder instance
-
fieldValue
Registers the value which clones get in the field instead of the copy of the original value. The supplier is called for every clone, so mutable values are not shared, e.g.fieldValue(field, ArrayList::new). A value has priority over the actions and policies of the field; a primitive field takes a boxed value.- Parameters:
field- instance fieldvalue- supplier of the value- Returns:
- same builder instance
-
fieldValue
Registers the value which clones get in the field, seefieldValue(Field, Supplier).- Parameters:
type- object typefield- declared field namevalue- supplier of the value- Returns:
- same builder instance
-
fieldValue
Registers the value which clones get in the fields matching the predicate, seefieldValue(Field, Supplier). Values of single fields have priority over values of predicates, the predicates are tried in the order of registration.- Parameters:
fieldPredicate- field predicatevalue- supplier of the value- Returns:
- same builder instance
-
copiers
Registers set of copiers.- Parameters:
copiers- copiers- Returns:
- same builder instance
-
copier
Registers copier for type.- Parameters:
type- object typecopier- copier- Returns:
- same builder instance
-
clone
Registers clone for the object.- Parameters:
original- original objectclone- clone- Returns:
- same builder instance
-
singleton
Registers singleton, i.e. the object which must be only copied by reference.- Parameters:
singleton- singleton- Returns:
- same builder instance
-
generatedCopiers
Enables or disables generated copiers. A class copied by its fields gets a generated copier when it is copied for the first time: a class with the private access of the copied class, which reads all fields and writes non-final ones directly, bypassing the field access of the toolset; final fields are written by the toolset. Generated classes are shared by cloners with the same field policies. Needs the ClassFile API (JDK 24+) or ASM on the classpath; classes which cannot get a generated copier (e.g. JDK classes, whose class loader does not see the cloner) are copied as usual. Enabled by default.- Parameters:
enabled- whether copiers are generated- Returns:
- same builder instance
-
copyConstructors
Enables or disables copy constructors which the build tools of the cloner add to classes (enabled by default). A class is cloned by its copy constructor if it has one and the field policy copies all its fields by default.- Parameters:
enabled- whether copy constructors are used- Returns:
- same builder instance
-
shallow
Registers type to be copied in shallow mode, i.e. field values won't be cloned.- Parameters:
type- type- Returns:
- same builder instance
-
build
Creates an instance of the cloner on the basis of the configuration.- Returns:
- cloner
-