Package org.apache.commons.configuration2.beanutils

# Class ConstructorArg

java.lang.Object
org.apache.commons.configuration2.beanutils.ConstructorArg

public final class ConstructorArg extends Object

A class representing an argument for a constructor invocation to be used by a `BeanDeclaration`.

A `BeanDeclaration` can provide a list of instances of this class to define the constructor to be invoked on the bean class. Each constructor argument can either be a simple value or a nested `BeanDeclaration`. In the latter case, the bean is resolved recursively.

The constructor to be invoked on the bean class has to be determined based on the types of the constructor arguments. To avoid ambiguity, the type name can be explicitly provided.

Since:
  2.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static ConstructorArg`
  `forBeanDeclaration(BeanDeclaration decl)`
  Creates a new instance of `ConstructorArg` for the specified `BeanDeclaration`.
  `static ConstructorArg`
  `forBeanDeclaration(BeanDeclaration beanDeclaration, String typeName)`
  Creates a new instance of `ConstructorArg` for the specified `BeanDeclaration` and sets the type name explicitly.
  `static ConstructorArg`
  `forValue(Object value)`
  Creates a new instance of `ConstructorArg` for the specified simple value.
  `static ConstructorArg`
  `forValue(Object value, String typeName)`
  Creates a new instance of `ConstructorArg` for the specified simple value and sets the type name explicitly.
  `BeanDeclaration`
  `getBeanDeclaration()`
  Gets the `BeanDeclaration` referenced by this constructor argument.
  `String`
  `getTypeName()`
  Gets the optional data type name of this constructor argument.
  `Object`
  `getValue()`
  Gets the value of this constructor argument.
  `boolean`
  `isNestedBeanDeclaration()`
  Tests whether this constructor argument represents a `BeanDeclaration`.
  `boolean`
  `matches(Class<?> argCls)`
  Checks whether this constructor argument is compatible with the given class.
  `String`
  `toString()`
  Gets a string representation of this object.

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

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

- ## Method Details

  - ### forBeanDeclaration

    public static ConstructorArg forBeanDeclaration(BeanDeclaration decl)
    Creates a new instance of `ConstructorArg` for the specified `BeanDeclaration`. The actual value of this argument is the resolved `BeanDeclaration`.
    Parameters:
      `decl` - the `BeanDeclaration`
    Returns:
      the newly created instance of this class
    Throws:
      `NullPointerException` - if the `BeanDeclaration` is null

  - ### forBeanDeclaration

    public static ConstructorArg forBeanDeclaration(BeanDeclaration beanDeclaration, String typeName)
    Creates a new instance of `ConstructorArg` for the specified `BeanDeclaration` and sets the type name explicitly. The type name is used to match this argument against the parameter type of a constructor or the bean class.
    Parameters:
      `beanDeclaration` - the `BeanDeclaration`
      `typeName` - the name of the data type of this argument
    Returns:
      the newly created instance of this class
    Throws:
      `NullPointerException` - if the `BeanDeclaration` is null

  - ### forValue

    public static ConstructorArg forValue(Object value)
    Creates a new instance of `ConstructorArg` for the specified simple value. The value is passed to the constructor invocation.
    Parameters:
      `value` - the value of this constructor argument (may be null)
    Returns:
      the newly created instance of this class

  - ### forValue

    public static ConstructorArg forValue(Object value, String typeName)
    Creates a new instance of `ConstructorArg` for the specified simple value and sets the type name explicitly. The type name is used to match this argument against the parameter type of a constructor or the bean class.
    Parameters:
      `value` - the value of this constructor argument (may be null)
      `typeName` - the name of the data type of this argument
    Returns:
      the newly created instance of this class

  - ### getBeanDeclaration

    public BeanDeclaration getBeanDeclaration()
    Gets the `BeanDeclaration` referenced by this constructor argument. A return value of null means that this constructor argument does not have a bean declaration as value; in this case, the value can be queried using the `getValue()` method.
    Returns:
      the referenced `BeanDeclaration` or null

  - ### getTypeName

    public String getTypeName()
    Gets the optional data type name of this constructor argument. The type name can be specified as a hint to select a specific constructor if there are ambiguities. Note that it does not necessarily has to match the data type of this argument's value because a type conversion may be performed before invoking the constructor.
    Returns:
      the data type name of this argument if defined or null otherwise

  - ### getValue

    public Object getValue()
    Gets the value of this constructor argument. This method can be queried if `isNestedBeanDeclaration()` returns false. Note that a return value of null is legal (to pass null to a constructor argument).
    Returns:
      the simple value of this constructor argument

  - ### isNestedBeanDeclaration

    public boolean isNestedBeanDeclaration()
    Tests whether this constructor argument represents a `BeanDeclaration`. If this method returns true, the actual value of this argument can be obtained by resolving the bean declaration returned by `getBeanDeclaration()`. Otherwise, this argument has a simple value which can be queried using `getValue()`.
    Returns:
      whether this constructor argument references a bean declaration

  - ### matches

    public boolean matches(Class<?> argCls)
    Checks whether this constructor argument is compatible with the given class. This method is called to determine a matching constructor. It compares the argument's data type with the class name if it is defined. If no type name has been set, result is true as it is assumed that a type conversion can be performed when calling the constructor. This means that per default only the number of constructor arguments is checked to find a matching constructor. Only if there are multiple constructors with the same number of arguments, explicit type names have to be provided to select a specific constructor.
    Parameters:
      `argCls` - the class of the constructor argument to compare with
    Returns:
      true if this constructor argument is compatible with this class, false otherwise

  - ### toString

    public String toString()
    Gets a string representation of this object. This string contains the value of this constructor argument and the explicit type if provided.
    Overrides:
      `toString` in class `Object`
    Returns:
      a string for this object