public class CopyObjectDefaultHandler extends Object implements DefaultParametersHandler<Object>
A specialized implementation of DefaultParametersHandler
that copies
the properties of a BuilderParameters
object (passed at construction
time) onto the object to be initialized.
Using this handler implementation makes specifying default values pretty straight-forward: Just create a corresponding parameters object, initialize it as desired, and pass it to this class. When invoked the handler uses functionality from Commons BeanUtils to copy all properties defined in the associated parameters object onto the target object. This is based on reflection. Properties not available for the target object are silently ignored. If an exception occurs during the copy operation, it is re-thrown as a runtime exception.
Note that there is no default way to create a defensive copy of the passed in parameters object; therefore, the reference is stored. This makes it possible to change the parameters object later on, and the changes will be effective when initializing objects afterwards. Client code should not rely on this feature.
Constructor and Description |
---|
CopyObjectDefaultHandler(BuilderParameters src)
Creates a new instance of
CopyObjectDefaultHandler and
initializes it with the specified source object. |
Modifier and Type | Method and Description |
---|---|
BuilderParameters |
getSource()
Returns the source object of this handler.
|
void |
initializeDefaults(Object parameters)
Initializes the specified parameters object with default values.
|
public CopyObjectDefaultHandler(BuilderParameters src)
CopyObjectDefaultHandler
and
initializes it with the specified source object. The properties defined
by the source object are copied onto the objects to be initialized.src
- the source object (must not be null)IllegalArgumentException
- if the source object is nullpublic BuilderParameters getSource()
CopyObjectDefaultHandler
public void initializeDefaults(Object parameters)
DefaultParametersHandler
objects registered supporting this
parameters type they are called in the order they have been registered.
So handlers registered later can override initializations done by
handlers registered earlier. This implementation uses
PropertyUtils.copyProperties()
to copy all defined properties
from the source object onto the passed in parameters object. Both the map
with properties (obtained via the getParameters()
method of the
source parameters object) and other properties of the source object are
copied.initializeDefaults
in interface DefaultParametersHandler<Object>
parameters
- the parameters object to be initializedConfigurationRuntimeException
- if an exception occursBuilderParameters.getParameters()
Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.