Class ConstructorUtils


  • public class ConstructorUtils
    extends java.lang.Object

    Utility reflection methods focused on constructors, modeled 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 privileges. If this call fails then a warning will be logged and the method may fail.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Class<?>[] EMPTY_CLASS_PARAMETERS
      An empty class array
      private static java.lang.Object[] EMPTY_OBJECT_ARRAY
      An empty object array
    • Constructor Summary

      Constructors 
      Constructor Description
      ConstructorUtils()
      Deprecated.
      Will be private in 2.0.
    • Method Summary

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

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_CLASS_PARAMETERS

        private static final java.lang.Class<?>[] EMPTY_CLASS_PARAMETERS
        An empty class array
      • EMPTY_OBJECT_ARRAY

        private static final java.lang.Object[] EMPTY_OBJECT_ARRAY
        An empty object array
    • Constructor Detail

      • ConstructorUtils

        @Deprecated
        public ConstructorUtils()
        Deprecated.
        Will be private in 2.0.
        Deprecated, all methods are static.
    • Method Detail

      • getAccessibleConstructor

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

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

        public static <T> java.lang.reflect.Constructor<T> getAccessibleConstructor​(java.lang.reflect.Constructor<T> ctor)
        Returns accessible version of the given constructor.
        Type Parameters:
        T - the type of the constructor
        Parameters:
        ctor - prototype constructor object.
        Returns:
        null if accessible constructor cannot be found.
        See Also:
        SecurityManager
      • getMatchingAccessibleConstructor

        private static <T> java.lang.reflect.Constructor<T> getMatchingAccessibleConstructor​(java.lang.Class<T> clazz,
                                                                                             java.lang.Class<?>[] parameterTypes)

        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.

        Type Parameters:
        T - the type of the class to be inspected
        Parameters:
        clazz - find constructor for this class
        parameterTypes - find method with compatible parameters
        Returns:
        a valid Constructor object. If there's no matching constructor, returns null.
      • invokeConstructor

        public static <T> T invokeConstructor​(java.lang.Class<T> klass,
                                              java.lang.Object arg)
                                       throws java.lang.NoSuchMethodException,
                                              java.lang.IllegalAccessException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.InstantiationException

        Convenience method returning new instance of klazz using a single argument constructor. The formal parameter type is inferred from the actual values of arg. See invokeExactConstructor(Class, Object[], Class[]) for more details.

        The signatures should be assignment compatible.

        Type Parameters:
        T - the type of the object to be constructed
        Parameters:
        klass - the class to be constructed.
        arg - the actual argument. May be null (this will result in calling the default constructor).
        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 <T> T invokeConstructor​(java.lang.Class<T> klass,
                                              java.lang.Object[] args)
                                       throws java.lang.NoSuchMethodException,
                                              java.lang.IllegalAccessException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.InstantiationException

        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.

        Type Parameters:
        T - the type of the object to be constructed
        Parameters:
        klass - the class to be constructed.
        args - actual argument array. May be null (this will result in calling the default constructor).
        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 <T> T invokeConstructor​(java.lang.Class<T> klass,
                                              java.lang.Object[] args,
                                              java.lang.Class<?>[] parameterTypes)
                                       throws java.lang.NoSuchMethodException,
                                              java.lang.IllegalAccessException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.InstantiationException

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

        The signatures should be assignment compatible.

        Type Parameters:
        T - the type of the object to be constructed
        Parameters:
        klass - the class to be constructed.
        args - actual argument array. May be null (this will result in calling the default constructor).
        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 <T> T invokeExactConstructor​(java.lang.Class<T> klass,
                                                   java.lang.Object arg)
                                            throws java.lang.NoSuchMethodException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.reflect.InvocationTargetException,
                                                   java.lang.InstantiationException

        Convenience method returning new instance of klazz using a single argument constructor. The formal parameter type is inferred from the actual values of arg. See invokeExactConstructor(Class, Object[], Class[]) for more details.

        The signatures should match exactly.

        Type Parameters:
        T - the type of the object to be constructed
        Parameters:
        klass - the class to be constructed.
        arg - the actual argument. May be null (this will result in calling the default constructor).
        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 <T> T invokeExactConstructor​(java.lang.Class<T> klass,
                                                   java.lang.Object[] args)
                                            throws java.lang.NoSuchMethodException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.reflect.InvocationTargetException,
                                                   java.lang.InstantiationException

        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.

        Type Parameters:
        T - the type of the object to be constructed
        Parameters:
        klass - the class to be constructed.
        args - actual argument array. May be null (this will result in calling the default constructor).
        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 <T> T invokeExactConstructor​(java.lang.Class<T> klass,
                                                   java.lang.Object[] args,
                                                   java.lang.Class<?>[] parameterTypes)
                                            throws java.lang.NoSuchMethodException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.reflect.InvocationTargetException,
                                                   java.lang.InstantiationException

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

        The signatures should match exactly.

        Type Parameters:
        T - the type of the object to be constructed
        Parameters:
        klass - the class to be constructed.
        args - actual argument array. May be null (this will result in calling the default constructor).
        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...)
      • toArray

        private static java.lang.Object[] toArray​(java.lang.Object arg)