Class ConstructorArg
- java.lang.Object
-
- org.apache.commons.configuration2.beanutils.ConstructorArg
-
public final class ConstructorArg extends java.lang.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 nestedBeanDeclaration
. 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ConstructorArg
forBeanDeclaration(BeanDeclaration decl)
Creates a new instance ofConstructorArg
for the specifiedBeanDeclaration
.static ConstructorArg
forBeanDeclaration(BeanDeclaration beanDeclaration, java.lang.String typeName)
Creates a new instance ofConstructorArg
for the specifiedBeanDeclaration
and sets the type name explicitly.static ConstructorArg
forValue(java.lang.Object value)
Creates a new instance ofConstructorArg
for the specified simple value.static ConstructorArg
forValue(java.lang.Object value, java.lang.String typeName)
Creates a new instance ofConstructorArg
for the specified simple value and sets the type name explicitly.BeanDeclaration
getBeanDeclaration()
Returns theBeanDeclaration
referenced by this constructor argument.java.lang.String
getTypeName()
Gets the optional data type name of this constructor argument.java.lang.Object
getValue()
Gets the value of this constructor argument.boolean
isNestedBeanDeclaration()
Tests whether this constructor argument represents aBeanDeclaration
.boolean
matches(java.lang.Class<?> argCls)
Checks whether this constructor argument is compatible with the given class.java.lang.String
toString()
Returns a string representation of this object.
-
-
-
Method Detail
-
forBeanDeclaration
public static ConstructorArg forBeanDeclaration(BeanDeclaration decl)
Creates a new instance ofConstructorArg
for the specifiedBeanDeclaration
. The actual value of this argument is the resolvedBeanDeclaration
.- Parameters:
decl
- theBeanDeclaration
- Returns:
- the newly created instance of this class
- Throws:
java.lang.NullPointerException
- if theBeanDeclaration
is null
-
forBeanDeclaration
public static ConstructorArg forBeanDeclaration(BeanDeclaration beanDeclaration, java.lang.String typeName)
Creates a new instance ofConstructorArg
for the specifiedBeanDeclaration
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
- theBeanDeclaration
typeName
- the name of the data type of this argument- Returns:
- the newly created instance of this class
- Throws:
java.lang.NullPointerException
- if theBeanDeclaration
is null
-
forValue
public static ConstructorArg forValue(java.lang.Object value)
Creates a new instance ofConstructorArg
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(java.lang.Object value, java.lang.String typeName)
Creates a new instance ofConstructorArg
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()
Returns theBeanDeclaration
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 thegetValue()
method.- Returns:
- the referenced
BeanDeclaration
or null
-
isNestedBeanDeclaration
public boolean isNestedBeanDeclaration()
Tests whether this constructor argument represents aBeanDeclaration
. If this method returns true, the actual value of this argument can be obtained by resolving the bean declaration returned bygetBeanDeclaration()
. Otherwise, this argument has a simple value which can be queried usinggetValue()
.- Returns:
- whether this constructor argument references a bean declaration
-
getValue
public java.lang.Object getValue()
Gets the value of this constructor argument. This method can be queried ifisNestedBeanDeclaration()
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
-
getTypeName
public java.lang.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
-
matches
public boolean matches(java.lang.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 java.lang.String toString()
Returns a string representation of this object. This string contains the value of this constructor argument and the explicit type if provided.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string for this object
-
-