Class 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 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

      All Methods Static Methods Instance Methods Concrete Methods 
      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, java.lang.String typeName)
      Creates a new instance of ConstructorArg for the specified BeanDeclaration and sets the type name explicitly.
      static ConstructorArg forValue​(java.lang.Object value)
      Creates a new instance of ConstructorArg for the specified simple value.
      static ConstructorArg forValue​(java.lang.Object value, java.lang.String typeName)
      Creates a new instance of ConstructorArg for the specified simple value and sets the type name explicitly.
      BeanDeclaration getBeanDeclaration()
      Returns the BeanDeclaration referenced by this constructor argument.
      java.lang.String getTypeName()
      Returns the optional data type name of this constructor argument.
      java.lang.Object getValue()
      Returns the value of this constructor argument.
      boolean isNestedBeanDeclaration()
      Returns a flag whether this constructor argument represents a BeanDeclaration.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • 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:
        java.lang.NullPointerException - if the BeanDeclaration is null
      • forBeanDeclaration

        public static ConstructorArg forBeanDeclaration​(BeanDeclaration beanDeclaration,
                                                        java.lang.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:
        java.lang.NullPointerException - if the BeanDeclaration is null
      • forValue

        public static ConstructorArg forValue​(java.lang.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​(java.lang.Object value,
                                              java.lang.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()
        Returns 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
      • isNestedBeanDeclaration

        public boolean isNestedBeanDeclaration()
        Returns a flag 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:
        a flag whether this constructor argument references a bean declaration
      • getValue

        public java.lang.Object getValue()
        Returns 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
      • getTypeName

        public java.lang.String getTypeName()
        Returns 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 class java.lang.Object
        Returns:
        a string for this object