public class ConstructorUtils
extends java.lang.Object
Utility reflection methods focused on constructors, modeled after
MethodUtils
.
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.
Constructor and Description |
---|
ConstructorUtils()
ConstructorUtils instances should NOT be constructed in standard
programming.
|
Modifier and Type | Method and Description |
---|---|
static <T> java.lang.reflect.Constructor<T> |
getAccessibleConstructor(java.lang.Class<T> cls,
java.lang.Class<?>... parameterTypes)
Finds a constructor given a class and signature, checking accessibility.
|
static <T> java.lang.reflect.Constructor<T> |
getAccessibleConstructor(java.lang.reflect.Constructor<T> ctor)
Checks if the specified constructor is accessible.
|
static <T> java.lang.reflect.Constructor<T> |
getMatchingAccessibleConstructor(java.lang.Class<T> cls,
java.lang.Class<?>... parameterTypes)
Finds an accessible constructor with compatible parameters.
|
static <T> T |
invokeConstructor(java.lang.Class<T> cls,
java.lang.Object... args)
Returns a new instance of the specified class inferring the right constructor
from the types of the arguments.
|
static <T> T |
invokeConstructor(java.lang.Class<T> cls,
java.lang.Object[] args,
java.lang.Class<?>[] parameterTypes)
Returns a new instance of the specified class choosing the right constructor
from the list of parameter types.
|
static <T> T |
invokeExactConstructor(java.lang.Class<T> cls,
java.lang.Object... args)
Returns a new instance of the specified class inferring the right constructor
from the types of the arguments.
|
static <T> T |
invokeExactConstructor(java.lang.Class<T> cls,
java.lang.Object[] args,
java.lang.Class<?>[] parameterTypes)
Returns a new instance of the specified class choosing the right constructor
from the list of parameter types.
|
public ConstructorUtils()
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.
public static <T> T invokeConstructor(java.lang.Class<T> cls, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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.
T
- the type to be constructedcls
- the class to be constructed, not null
args
- the array of arguments, null
treated as emptycls
, not null
java.lang.NullPointerException
- if cls
is null
java.lang.NoSuchMethodException
- if a matching constructor cannot be foundjava.lang.IllegalAccessException
- if invocation is not permitted by securityjava.lang.reflect.InvocationTargetException
- if an error occurs on invocationjava.lang.InstantiationException
- if an error occurs on instantiationinvokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static <T> T invokeConstructor(java.lang.Class<T> cls, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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.
T
- the type to be constructedcls
- the class to be constructed, not null
args
- the array of arguments, null
treated as emptyparameterTypes
- the array of parameter types, null
treated as emptycls
, not null
java.lang.NullPointerException
- if cls
is null
java.lang.NoSuchMethodException
- if a matching constructor cannot be foundjava.lang.IllegalAccessException
- if invocation is not permitted by securityjava.lang.reflect.InvocationTargetException
- if an error occurs on invocationjava.lang.InstantiationException
- if an error occurs on instantiationConstructor.newInstance(java.lang.Object...)
public static <T> T invokeExactConstructor(java.lang.Class<T> cls, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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.
T
- the type to be constructedcls
- the class to be constructed, not null
args
- the array of arguments, null
treated as emptycls
, not null
java.lang.NullPointerException
- if cls
is null
java.lang.NoSuchMethodException
- if a matching constructor cannot be foundjava.lang.IllegalAccessException
- if invocation is not permitted by securityjava.lang.reflect.InvocationTargetException
- if an error occurs on invocationjava.lang.InstantiationException
- if an error occurs on instantiationinvokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static <T> T invokeExactConstructor(java.lang.Class<T> cls, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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.
T
- the type to be constructedcls
- the class to be constructed, not null
args
- the array of arguments, null
treated as emptyparameterTypes
- the array of parameter types, null
treated as emptycls
, not null
java.lang.NullPointerException
- if cls
is null
java.lang.NoSuchMethodException
- if a matching constructor cannot be foundjava.lang.IllegalAccessException
- if invocation is not permitted by securityjava.lang.reflect.InvocationTargetException
- if an error occurs on invocationjava.lang.InstantiationException
- if an error occurs on instantiationConstructor.newInstance(java.lang.Object...)
public static <T> java.lang.reflect.Constructor<T> getAccessibleConstructor(java.lang.Class<T> cls, java.lang.Class<?>... parameterTypes)
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.
T
- the constructor typecls
- the class to find a constructor for, not null
parameterTypes
- the array of parameter types, null
treated as emptynull
if no matching accessible constructor foundjava.lang.NullPointerException
- if cls
is null
Class.getConstructor(java.lang.Class<?>...)
,
getAccessibleConstructor(java.lang.reflect.Constructor)
public static <T> java.lang.reflect.Constructor<T> getAccessibleConstructor(java.lang.reflect.Constructor<T> ctor)
Checks if the specified constructor is accessible.
This simply ensures that the constructor is accessible.
T
- the constructor typector
- the prototype constructor object, not null
null
if no matching accessible constructor foundjava.lang.NullPointerException
- if ctor
is null
SecurityManager
public static <T> java.lang.reflect.Constructor<T> getMatchingAccessibleConstructor(java.lang.Class<T> cls, java.lang.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.
T
- the constructor typecls
- the class to find a constructor for, not null
parameterTypes
- find method with compatible parametersjava.lang.NullPointerException
- if cls
is null
Copyright © 2010 - 2020 Adobe. All Rights Reserved