Class CopyObjectDefaultHandler

java.lang.Object
org.apache.commons.configuration2.builder.CopyObjectDefaultHandler
All Implemented Interfaces:
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 Details

    • CopyObjectDefaultHandler

      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 Details

    • getSource

      Gets 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: