Class ConstructorUtils


  • @Deprecated(since="2021-04-30")
    public class ConstructorUtils
    extends java.lang.Object
    Deprecated.
    Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.

    Utility reflection methods focussed on constructors, modelled after MethodUtils.

    Known Limitations

    Accessing Public Constructors In A Default Access Superclass

    There is an issue when invoking public constructors contained in a default access superclass. Reflection locates these constructors fine and correctly assigns them as public. However, an IllegalAccessException is thrown if the constructors is invoked.

    ConstructorUtils contains a workaround for this situation. It will attempt to call setAccessible on this constructor. If this call succeeds, then the method can be invoked as normal. This call will only succeed when the application has sufficient security privilages. If this call fails then a warning will be logged and the method may fail.

    Since:
    2.5
    • Constructor Summary

      Constructors 
      Constructor Description
      ConstructorUtils()
      Deprecated.
      ConstructorUtils instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Constructor getAccessibleConstructor​(java.lang.Class cls, java.lang.Class parameterType)
      Deprecated.
      Returns a constructor with single argument.
      static java.lang.reflect.Constructor getAccessibleConstructor​(java.lang.Class cls, java.lang.Class[] parameterTypes)
      Deprecated.
      Returns a constructor given a class and signature.
      static java.lang.reflect.Constructor getAccessibleConstructor​(java.lang.reflect.Constructor ctor)
      Deprecated.
      Returns accessible version of the given constructor.
      static java.lang.reflect.Constructor getMatchingAccessibleConstructor​(java.lang.Class cls, java.lang.Class[] parameterTypes)
      Deprecated.
      Find an accessible constructor with compatible parameters.
      static java.lang.Object invokeConstructor​(java.lang.Class cls, java.lang.Object arg)
      Deprecated.
      Returns new instance of klazz created using the actual arguments args.
      static java.lang.Object invokeConstructor​(java.lang.Class cls, java.lang.Object[] args)
      Deprecated.
      Returns new instance of klazz created using the actual arguments args.
      static java.lang.Object invokeConstructor​(java.lang.Class cls, java.lang.Object[] args, java.lang.Class[] parameterTypes)
      Deprecated.
      Returns new instance of klazz created using constructor with signature parameterTypes and actual arguments args.
      static java.lang.Object invokeExactConstructor​(java.lang.Class cls, java.lang.Object arg)
      Deprecated.
      Returns new instance of klazz created using the actual arguments args.
      static java.lang.Object invokeExactConstructor​(java.lang.Class cls, java.lang.Object[] args)
      Deprecated.
      Returns new instance of klazz created using the actual arguments args.
      static java.lang.Object invokeExactConstructor​(java.lang.Class cls, java.lang.Object[] args, java.lang.Class[] parameterTypes)
      Deprecated.
      Returns new instance of klazz created using constructor with signature parameterTypes and actual arguments args.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConstructorUtils

        public ConstructorUtils()
        Deprecated.

        ConstructorUtils instances should NOT be constructed in standard programming. Instead, the class should be used as ConstructorUtils.invokeConstructor(cls, args).

        This constructor is public to permit tools that require a JavaBean instance to operate.

    • Method Detail

      • invokeConstructor

        public static java.lang.Object invokeConstructor​(java.lang.Class cls,
                                                         java.lang.Object arg)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.InstantiationException
        Deprecated.

        Returns new instance of klazz created using the actual arguments args. The formal parameter types are inferred from the actual values of args. See invokeExactConstructor(Class, Object[], Class[]) for more details.

        The signatures should be assignment compatible.

        Parameters:
        cls - the class to be constructed.
        arg - the actual argument
        Returns:
        new instance of klazz
        Throws:
        java.lang.NoSuchMethodException - If the constructor cannot be found
        java.lang.IllegalAccessException - If an error occurs accessing the constructor
        java.lang.reflect.InvocationTargetException - If an error occurs invoking the constructor
        java.lang.InstantiationException - If an error occurs instantiating the class
        See Also:
        invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
      • invokeConstructor

        public static java.lang.Object invokeConstructor​(java.lang.Class cls,
                                                         java.lang.Object[] args)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.InstantiationException
        Deprecated.

        Returns new instance of klazz created using the actual arguments args. The formal parameter types are inferred from the actual values of args. See invokeExactConstructor(Class, Object[], Class[]) for more details.

        The signatures should be assignment compatible.

        Parameters:
        cls - the class to be constructed.
        args - actual argument array
        Returns:
        new instance of klazz
        Throws:
        java.lang.NoSuchMethodException - If the constructor cannot be found
        java.lang.IllegalAccessException - If an error occurs accessing the constructor
        java.lang.reflect.InvocationTargetException - If an error occurs invoking the constructor
        java.lang.InstantiationException - If an error occurs instantiating the class
        See Also:
        invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
      • invokeConstructor

        public static java.lang.Object invokeConstructor​(java.lang.Class cls,
                                                         java.lang.Object[] args,
                                                         java.lang.Class[] parameterTypes)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.InstantiationException
        Deprecated.

        Returns new instance of klazz created using constructor with signature parameterTypes and actual arguments args.

        The signatures should be assignment compatible.

        Parameters:
        cls - the class to be constructed.
        args - actual argument array
        parameterTypes - parameter types array
        Returns:
        new instance of klazz
        Throws:
        java.lang.NoSuchMethodException - if matching constructor cannot be found
        java.lang.IllegalAccessException - thrown on the constructor's invocation
        java.lang.reflect.InvocationTargetException - thrown on the constructor's invocation
        java.lang.InstantiationException - thrown on the constructor's invocation
        See Also:
        Constructor.newInstance(java.lang.Object...)
      • invokeExactConstructor

        public static java.lang.Object invokeExactConstructor​(java.lang.Class cls,
                                                              java.lang.Object arg)
                                                       throws java.lang.NoSuchMethodException,
                                                              java.lang.IllegalAccessException,
                                                              java.lang.reflect.InvocationTargetException,
                                                              java.lang.InstantiationException
        Deprecated.

        Returns new instance of klazz created using the actual arguments args. The formal parameter types are inferred from the actual values of args. See invokeExactConstructor(Class, Object[], Class[]) for more details.

        The signatures should match exactly.

        Parameters:
        cls - the class to be constructed.
        arg - the actual argument
        Returns:
        new instance of klazz
        Throws:
        java.lang.NoSuchMethodException - If the constructor cannot be found
        java.lang.IllegalAccessException - If an error occurs accessing the constructor
        java.lang.reflect.InvocationTargetException - If an error occurs invoking the constructor
        java.lang.InstantiationException - If an error occurs instantiating the class
        See Also:
        invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
      • invokeExactConstructor

        public static java.lang.Object invokeExactConstructor​(java.lang.Class cls,
                                                              java.lang.Object[] args)
                                                       throws java.lang.NoSuchMethodException,
                                                              java.lang.IllegalAccessException,
                                                              java.lang.reflect.InvocationTargetException,
                                                              java.lang.InstantiationException
        Deprecated.

        Returns new instance of klazz created using the actual arguments args. The formal parameter types are inferred from the actual values of args. See invokeExactConstructor(Class, Object[], Class[]) for more details.

        The signatures should match exactly.

        Parameters:
        cls - the class to be constructed.
        args - actual argument array
        Returns:
        new instance of klazz
        Throws:
        java.lang.NoSuchMethodException - If the constructor cannot be found
        java.lang.IllegalAccessException - If an error occurs accessing the constructor
        java.lang.reflect.InvocationTargetException - If an error occurs invoking the constructor
        java.lang.InstantiationException - If an error occurs instantiating the class
        See Also:
        invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
      • invokeExactConstructor

        public static java.lang.Object invokeExactConstructor​(java.lang.Class cls,
                                                              java.lang.Object[] args,
                                                              java.lang.Class[] parameterTypes)
                                                       throws java.lang.NoSuchMethodException,
                                                              java.lang.IllegalAccessException,
                                                              java.lang.reflect.InvocationTargetException,
                                                              java.lang.InstantiationException
        Deprecated.

        Returns new instance of klazz created using constructor with signature parameterTypes and actual arguments args.

        The signatures should match exactly.

        Parameters:
        cls - the class to be constructed.
        args - actual argument array
        parameterTypes - parameter types array
        Returns:
        new instance of klazz
        Throws:
        java.lang.NoSuchMethodException - if matching constructor cannot be found
        java.lang.IllegalAccessException - thrown on the constructor's invocation
        java.lang.reflect.InvocationTargetException - thrown on the constructor's invocation
        java.lang.InstantiationException - thrown on the constructor's invocation
        See Also:
        Constructor.newInstance(java.lang.Object...)
      • getAccessibleConstructor

        public static java.lang.reflect.Constructor getAccessibleConstructor​(java.lang.Class cls,
                                                                             java.lang.Class parameterType)
        Deprecated.
        Returns a constructor with single argument.
        Parameters:
        cls - the class to be constructed
        parameterType - The constructor parameter type
        Returns:
        null if matching accessible constructor can not be found.
        See Also:
        Class.getConstructor(java.lang.Class<?>...), getAccessibleConstructor(java.lang.reflect.Constructor)
      • getAccessibleConstructor

        public static java.lang.reflect.Constructor getAccessibleConstructor​(java.lang.Class cls,
                                                                             java.lang.Class[] parameterTypes)
        Deprecated.
        Returns a constructor given a class and signature.
        Parameters:
        cls - the class to be constructed
        parameterTypes - the parameter array
        Returns:
        null if matching accessible constructor can not be found
        See Also:
        Class.getConstructor(java.lang.Class<?>...), getAccessibleConstructor(java.lang.reflect.Constructor)
      • getAccessibleConstructor

        public static java.lang.reflect.Constructor getAccessibleConstructor​(java.lang.reflect.Constructor ctor)
        Deprecated.
        Returns accessible version of the given constructor.
        Parameters:
        ctor - prototype constructor object.
        Returns:
        null if accessible constructor can not be found.
        See Also:
        SecurityManager
      • getMatchingAccessibleConstructor

        public static java.lang.reflect.Constructor getMatchingAccessibleConstructor​(java.lang.Class cls,
                                                                                     java.lang.Class[] parameterTypes)
        Deprecated.

        Find an accessible constructor with compatible parameters. Compatible parameters mean that every method parameter is assignable from the given parameters. In other words, it finds constructor that will take the parameters given.

        First it checks if there is constructor matching the exact signature. If no such, all the constructors of the class are tested if their signatures are assignment compatible with the parameter types. The first matching constructor is returned.

        Parameters:
        cls - find constructor for this class
        parameterTypes - find method with compatible parameters
        Returns:
        a valid Constructor object. If there's no matching constructor, returns null.