org.apache.commons.configuration2.builder

## Class CopyObjectDefaultHandler

- java.lang.Object
- - org.apache.commons.configuration2.builder.CopyObjectDefaultHandler

- All Implemented Interfaces:
    DefaultParametersHandler<Object>

      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.

  Since:
    2.0

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CopyObjectDefaultHandler(BuilderParameters src)` Creates a new instance of `CopyObjectDefaultHandler` and initializes it with the specified source object.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CopyObjectDefaultHandler

          public CopyObjectDefaultHandler(BuilderParameters src)

      Creates a new instance of `CopyObjectDefaultHandler` and initializes it with the specified source object. The properties defined by the source object are copied onto the objects to be initialized.
      Parameters:
        `src` - the source object (must not be null)
      Throws:
        `IllegalArgumentException` - if the source object is null

  - ### Method Detail

    - #### getSource

          public BuilderParameters getSource()

      Returns the source object of this handler. This is the object whose properties are copied on the objects to be initialized.
      Returns:
        the source object of this `CopyObjectDefaultHandler`

    - #### initializeDefaults

          public void initializeDefaults(Object parameters)

      Initializes the specified parameters object with default values. This method is called after the parameters object was created and before it is passed to the calling code. A concrete implementation can perform arbitrary initializations. Note that if there are multiple `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.
      Specified by:
        `initializeDefaults` in interface `DefaultParametersHandler<Object>`
      Parameters:
        `parameters` - the parameters object to be initialized
      Throws:
        `ConfigurationRuntimeException` - if an exception occurs
      See Also:
        `BuilderParameters.getParameters()`

Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.