Class ConstructorUtils
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
ConstructorsConstructorDescriptionDeprecated.ConstructorUtils instances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic Constructor
getAccessibleConstructor
(Class cls, Class parameterType) Deprecated.Returns a constructor with single argument.static Constructor
getAccessibleConstructor
(Class cls, Class[] parameterTypes) Deprecated.Returns a constructor given a class and signature.static Constructor
Deprecated.Returns accessible version of the given constructor.static Constructor
getMatchingAccessibleConstructor
(Class cls, Class[] parameterTypes) Deprecated.Find an accessible constructor with compatible parameters.static Object
invokeConstructor
(Class cls, Object arg) Deprecated.Returns new instance ofklazz
created using the actual argumentsargs
.static Object
invokeConstructor
(Class cls, Object[] args) Deprecated.Returns new instance ofklazz
created using the actual argumentsargs
.static Object
invokeConstructor
(Class cls, Object[] args, Class[] parameterTypes) Deprecated.Returns new instance ofklazz
created using constructor with signatureparameterTypes
and actual argumentsargs
.static Object
invokeExactConstructor
(Class cls, Object arg) Deprecated.Returns new instance ofklazz
created using the actual argumentsargs
.static Object
invokeExactConstructor
(Class cls, Object[] args) Deprecated.Returns new instance ofklazz
created using the actual argumentsargs
.static Object
invokeExactConstructor
(Class cls, Object[] args, Class[] parameterTypes) Deprecated.Returns new instance ofklazz
created using constructor with signatureparameterTypes
and actual argumentsargs
.
-
Constructor Details
-
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 Details
-
invokeConstructor
public static Object invokeConstructor(Class cls, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Deprecated.Returns new instance of
klazz
created using the actual argumentsargs
. The formal parameter types are inferred from the actual values ofargs
. SeeinvokeExactConstructor(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:
NoSuchMethodException
- If the constructor cannot be foundIllegalAccessException
- If an error occurs accessing the constructorInvocationTargetException
- If an error occurs invoking the constructorInstantiationException
- If an error occurs instantiating the class- See Also:
-
invokeConstructor
public static Object invokeConstructor(Class cls, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Deprecated.Returns new instance of
klazz
created using the actual argumentsargs
. The formal parameter types are inferred from the actual values ofargs
. SeeinvokeExactConstructor(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:
NoSuchMethodException
- If the constructor cannot be foundIllegalAccessException
- If an error occurs accessing the constructorInvocationTargetException
- If an error occurs invoking the constructorInstantiationException
- If an error occurs instantiating the class- See Also:
-
invokeConstructor
public static Object invokeConstructor(Class cls, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Deprecated.Returns new instance of
klazz
created using constructor with signatureparameterTypes
and actual argumentsargs
.The signatures should be assignment compatible.
- Parameters:
cls
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types array- Returns:
- new instance of
klazz
- Throws:
NoSuchMethodException
- if matching constructor cannot be foundIllegalAccessException
- thrown on the constructor's invocationInvocationTargetException
- thrown on the constructor's invocationInstantiationException
- thrown on the constructor's invocation- See Also:
-
invokeExactConstructor
public static Object invokeExactConstructor(Class cls, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Deprecated.Returns new instance of
klazz
created using the actual argumentsargs
. The formal parameter types are inferred from the actual values ofargs
. SeeinvokeExactConstructor(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:
NoSuchMethodException
- If the constructor cannot be foundIllegalAccessException
- If an error occurs accessing the constructorInvocationTargetException
- If an error occurs invoking the constructorInstantiationException
- If an error occurs instantiating the class- See Also:
-
invokeExactConstructor
public static Object invokeExactConstructor(Class cls, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Deprecated.Returns new instance of
klazz
created using the actual argumentsargs
. The formal parameter types are inferred from the actual values ofargs
. SeeinvokeExactConstructor(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:
NoSuchMethodException
- If the constructor cannot be foundIllegalAccessException
- If an error occurs accessing the constructorInvocationTargetException
- If an error occurs invoking the constructorInstantiationException
- If an error occurs instantiating the class- See Also:
-
invokeExactConstructor
public static Object invokeExactConstructor(Class cls, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Deprecated.Returns new instance of
klazz
created using constructor with signatureparameterTypes
and actual argumentsargs
.The signatures should match exactly.
- Parameters:
cls
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types array- Returns:
- new instance of
klazz
- Throws:
NoSuchMethodException
- if matching constructor cannot be foundIllegalAccessException
- thrown on the constructor's invocationInvocationTargetException
- thrown on the constructor's invocationInstantiationException
- thrown on the constructor's invocation- See Also:
-
getAccessibleConstructor
Deprecated.Returns a constructor with single argument.- Parameters:
cls
- the class to be constructedparameterType
- The constructor parameter type- Returns:
- null if matching accessible constructor can not be found.
- See Also:
-
getAccessibleConstructor
Deprecated.Returns a constructor given a class and signature.- Parameters:
cls
- the class to be constructedparameterTypes
- the parameter array- Returns:
- null if matching accessible constructor can not be found
- See Also:
-
getAccessibleConstructor
Deprecated.Returns accessible version of the given constructor.- Parameters:
ctor
- prototype constructor object.- Returns:
null
if accessible constructor can not be found.- See Also:
-
getMatchingAccessibleConstructor
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 classparameterTypes
- find method with compatible parameters- Returns:
- a valid Constructor object. If there's no matching constructor,
returns
null
.
-