Class ConstructorUtils
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 correctly locates these
constructors and assigns them as public. However, an
IllegalAccessException is thrown if the constructor is
invoked.
ConstructorUtils contains a workaround for this situation: it
will attempt to call AccessibleObject.setAccessible(boolean) 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.
- Since:
- 2.5
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Constructor<T> getAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes) Finds a constructor given a class and signature, checking accessibility.static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) Checks if the specified constructor is accessible.static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes) Finds an accessible constructor with compatible parameters.static <T> TinvokeConstructor(Class<T> cls, Object... args) Returns a new instance of the specified class inferring the right constructor from the types of the arguments.static <T> TinvokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) Returns a new instance of the specified class choosing the right constructor from the list of parameter types.static <T> TinvokeExactConstructor(Class<T> cls, Object... args) Returns a new instance of the specified class inferring the right constructor from the types of the arguments.static <T> TinvokeExactConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
-
Constructor Details
-
ConstructorUtils
Deprecated.TODO Make private in 4.0.ConstructorUtils instances should NOT be constructed in standard programming. Instead, the class should be used asConstructorUtils.invokeConstructor(cls, args).This constructor is
publicto permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
getAccessibleConstructor
Finds a constructor given a class and signature, checking accessibility.This finds the constructor and ensures that it is accessible. The constructor signature must match the parameter types exactly.
- Type Parameters:
T- the constructor type- Parameters:
cls- the class to find a constructor for, notnullparameterTypes- the array of parameter types,nulltreated as empty- Returns:
- the constructor,
nullif no matching accessible constructor found - Throws:
NullPointerException- ifclsisnull- See Also:
-
getAccessibleConstructor
Checks if the specified constructor is accessible.This simply ensures that the constructor is accessible.
- Type Parameters:
T- the constructor type- Parameters:
ctor- the prototype constructor object, notnull- Returns:
- the constructor,
nullif no matching accessible constructor found - Throws:
NullPointerException- ifctorisnull- See Also:
-
getMatchingAccessibleConstructor
public static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes) Finds an accessible constructor with compatible parameters.This checks all the constructor and finds one with compatible parameters This requires that every parameter is assignable from the given parameter types. This is a more flexible search than the normal exact matching algorithm.
First it checks if there is a constructor matching the exact signature. If not then all the constructors of the class are checked to see if their signatures are assignment-compatible with the parameter types. The first assignment-compatible matching constructor is returned.
- Type Parameters:
T- the constructor type- Parameters:
cls- the class to find a constructor for, notnullparameterTypes- find method with compatible parameters- Returns:
- the constructor, null if no matching accessible constructor found
- Throws:
NullPointerException- ifclsisnullSecurityException- if an underlying accessible object's method denies the request.- See Also:
-
invokeConstructor
public static <T> T invokeConstructor(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns a new instance of the specified class inferring the right constructor from the types of the arguments.This locates and calls a constructor. The constructor signature must match the argument types by assignment compatibility.
- Type Parameters:
T- the type to be constructed- Parameters:
cls- the class to be constructed, notnullargs- the array of arguments,nulltreated as empty- Returns:
- new instance of
cls, notnull - Throws:
NullPointerException- ifclsisnullNoSuchMethodException- if a matching constructor cannot be foundIllegalAccessException- if invocation is not permitted by securityInvocationTargetException- if an error occurs on invocationInstantiationException- if an error occurs on instantiationSecurityException- if an underlying accessible object's method denies the request.- See Also:
-
invokeConstructor
public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns a new instance of the specified class choosing the right constructor from the list of parameter types.This locates and calls a constructor. The constructor signature must match the parameter types by assignment compatibility.
- Type Parameters:
T- the type to be constructed- Parameters:
cls- the class to be constructed, notnullargs- the array of arguments,nulltreated as emptyparameterTypes- the array of parameter types,nulltreated as empty- Returns:
- new instance of
cls, notnull - Throws:
NullPointerException- ifclsisnullNoSuchMethodException- if a matching constructor cannot be foundIllegalAccessException- if invocation is not permitted by securityInvocationTargetException- if an error occurs on invocationInstantiationException- if an error occurs on instantiationSecurityException- if an underlying accessible object's method denies the request.- See Also:
-
invokeExactConstructor
public static <T> T invokeExactConstructor(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns a new instance of the specified class inferring the right constructor from the types of the arguments.This locates and calls a constructor. The constructor signature must match the argument types exactly.
- Type Parameters:
T- the type to be constructed- Parameters:
cls- the class to be constructed, notnullargs- the array of arguments,nulltreated as empty- Returns:
- new instance of
cls, notnull - Throws:
NullPointerException- ifclsisnullNoSuchMethodException- if a matching constructor cannot be foundIllegalAccessException- if invocation is not permitted by securityInvocationTargetException- if an error occurs on invocationInstantiationException- if an error occurs on instantiation- See Also:
-
invokeExactConstructor
public static <T> T invokeExactConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Returns a new instance of the specified class choosing the right constructor from the list of parameter types.This locates and calls a constructor. The constructor signature must match the parameter types exactly.
- Type Parameters:
T- the type to be constructed- Parameters:
cls- the class to be constructed, notnullargs- the array of arguments,nulltreated as emptyparameterTypes- the array of parameter types,nulltreated as empty- Returns:
- new instance of
cls, notnull - Throws:
NullPointerException- ifclsisnullNoSuchMethodException- if a matching constructor cannot be foundIllegalAccessException- if invocation is not permitted by securityInvocationTargetException- if an error occurs on invocationInstantiationException- if an error occurs on instantiation- See Also:
-