Class ConstructorUtils
 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.
 
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> Constructor<T> getAccessibleConstructor(Class<T> klass, Class<?> parameterType) Returns a constructor with single argument.static <T> Constructor<T> getAccessibleConstructor(Class<T> klass, Class<?>[] parameterTypes) Returns a constructor given a class and signature.static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) Returns accessible version of the given constructor.static <T> TinvokeConstructor(Class<T> klass, Object arg) Convenience method returning new instance ofklazzusing a single argument constructor.static <T> TinvokeConstructor(Class<T> klass, Object[] args) Returns new instance ofklazz</code> created using the actual arguments <code>args.static <T> TinvokeConstructor(Class<T> klass, Object[] args, Class<?>[] parameterTypes) Returns new instance ofklazzcreated using constructor with signatureparameterTypes</code> and actual arguments <code>args.static <T> TinvokeExactConstructor(Class<T> klass, Object arg) Convenience method returning new instance ofklazzusing a single argument constructor.static <T> TinvokeExactConstructor(Class<T> klass, Object[] args) Returns new instance ofklazz</code> created using the actual arguments <code>args.static <T> TinvokeExactConstructor(Class<T> klass, Object[] args, Class<?>[] parameterTypes) Returns new instance ofklazzcreated using constructor with signatureparameterTypesand actual argumentsargs.static <T> T[]Delegates toArray.newInstance(Class, int).
- 
Method Details- 
getAccessibleConstructorReturns 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:
 
- 
getAccessibleConstructorpublic static <T> Constructor<T> getAccessibleConstructor(Class<T> klass, 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:
 
- 
getAccessibleConstructorReturns accessible version of the given constructor.- Type Parameters:
- T- the type of the constructor
- Parameters:
- ctor- prototype constructor object.
- Returns:
- nullif accessible constructor cannot be found.
- See Also:
 
- 
invokeConstructorpublic static <T> T invokeConstructor(Class<T> klass, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Convenience method returning new instance of klazzusing a single argument constructor. The formal parameter type is inferred from the actual values ofarg. SeeinvokeExactConstructor(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:
- NoSuchMethodException- If the constructor cannot be found
- IllegalAccessException- If an error occurs accessing the constructor
- InvocationTargetException- If an error occurs invoking the constructor
- InstantiationException- If an error occurs instantiating the class
- See Also:
 
- 
invokeConstructorpublic static <T> T invokeConstructor(Class<T> klass, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns new instance of klazz</code> created using the actual arguments <code>args. The formal parameter types are inferred from the actual values ofargs. SeeinvokeExactConstructor(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:
- NoSuchMethodException- If the constructor cannot be found
- IllegalAccessException- If an error occurs accessing the constructor
- InvocationTargetException- If an error occurs invoking the constructor
- InstantiationException- If an error occurs instantiating the class
- See Also:
 
- 
invokeConstructorpublic static <T> T invokeConstructor(Class<T> klass, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns new instance of klazzcreated using constructor with signatureparameterTypes</code> and actual arguments <code>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:
- NoSuchMethodException- if matching constructor cannot be found
- IllegalAccessException- thrown on the constructor's invocation
- InvocationTargetException- thrown on the constructor's invocation
- InstantiationException- thrown on the constructor's invocation
- See Also:
 
- 
invokeExactConstructorpublic static <T> T invokeExactConstructor(Class<T> klass, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Convenience method returning new instance of klazzusing a single argument constructor. The formal parameter type is inferred from the actual values ofarg. SeeinvokeExactConstructor(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:
- NoSuchMethodException- If the constructor cannot be found
- IllegalAccessException- If an error occurs accessing the constructor
- InvocationTargetException- If an error occurs invoking the constructor
- InstantiationException- If an error occurs instantiating the class
- See Also:
 
- 
invokeExactConstructorpublic static <T> T invokeExactConstructor(Class<T> klass, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns new instance of klazz</code> created using the actual arguments <code>args. The formal parameter types are inferred from the actual values ofargs. SeeinvokeExactConstructor(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:
- NoSuchMethodException- If the constructor cannot be found
- IllegalAccessException- If an error occurs accessing the constructor
- InvocationTargetException- If an error occurs invoking the constructor
- InstantiationException- If an error occurs instantiating the class
- See Also:
 
- 
invokeExactConstructorpublic static <T> T invokeExactConstructor(Class<T> klass, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns new instance of klazzcreated using constructor with signatureparameterTypesand actual argumentsargs.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:
- NoSuchMethodException- if matching constructor cannot be found
- IllegalAccessException- thrown on the constructor's invocation
- InvocationTargetException- thrown on the constructor's invocation
- InstantiationException- thrown on the constructor's invocation
- See Also:
 
- 
newArrayDelegates toArray.newInstance(Class, int).- Type Parameters:
- T- Component type.
- Parameters:
- componentType- See- Array.newInstance(Class, int).
- length- See- Array.newInstance(Class, int).
- Returns:
- See Array.newInstance(Class, int).
 
 
-