public class ConstructorUtils
extends java.lang.Object
Utility reflection methods focussed on constructors, modelled after
MethodUtils
.
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.
Constructor and Description |
---|
ConstructorUtils()
ConstructorUtils instances should NOT be constructed in standard
programming.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.reflect.Constructor |
getAccessibleConstructor(java.lang.Class cls,
java.lang.Class parameterType)
Returns a constructor with single argument.
|
static java.lang.reflect.Constructor |
getAccessibleConstructor(java.lang.Class cls,
java.lang.Class[] parameterTypes)
Returns a constructor given a class and signature.
|
static java.lang.reflect.Constructor |
getAccessibleConstructor(java.lang.reflect.Constructor ctor)
Returns accessible version of the given constructor.
|
static java.lang.reflect.Constructor |
getMatchingAccessibleConstructor(java.lang.Class cls,
java.lang.Class[] parameterTypes)
Find an accessible constructor with compatible parameters.
|
static java.lang.Object |
invokeConstructor(java.lang.Class cls,
java.lang.Object arg)
Returns new instance of
klazz created using the actual
arguments args . |
static java.lang.Object |
invokeConstructor(java.lang.Class cls,
java.lang.Object[] args)
Returns new instance of
klazz created using the actual
arguments args . |
static java.lang.Object |
invokeConstructor(java.lang.Class cls,
java.lang.Object[] args,
java.lang.Class[] parameterTypes)
Returns new instance of
klazz created using constructor
with signature parameterTypes and actual arguments
args . |
static java.lang.Object |
invokeExactConstructor(java.lang.Class cls,
java.lang.Object arg)
Returns new instance of
klazz created using the actual
arguments args . |
static java.lang.Object |
invokeExactConstructor(java.lang.Class cls,
java.lang.Object[] args)
Returns new instance of
klazz created using the actual
arguments args . |
static java.lang.Object |
invokeExactConstructor(java.lang.Class cls,
java.lang.Object[] args,
java.lang.Class[] parameterTypes)
Returns new instance of
klazz created using constructor
with signature parameterTypes and actual arguments
args . |
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 java.lang.Object invokeConstructor(java.lang.Class cls, java.lang.Object arg) 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.
cls
- the class to be constructed.arg
- the actual argumentklazz
java.lang.NoSuchMethodException
- If the constructor cannot be foundjava.lang.IllegalAccessException
- If an error occurs accessing the constructorjava.lang.reflect.InvocationTargetException
- If an error occurs invoking the constructorjava.lang.InstantiationException
- If an error occurs instantiating the classinvokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static java.lang.Object invokeConstructor(java.lang.Class cls, 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.
cls
- the class to be constructed.args
- actual argument arrayklazz
java.lang.NoSuchMethodException
- If the constructor cannot be foundjava.lang.IllegalAccessException
- If an error occurs accessing the
constructorjava.lang.reflect.InvocationTargetException
- If an error occurs invoking the
constructorjava.lang.InstantiationException
- If an error occurs instantiating the classinvokeConstructor(java.lang.Class, java.lang.Object[],
java.lang.Class[])
public static java.lang.Object invokeConstructor(java.lang.Class cls, 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.
cls
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types arrayklazz
java.lang.NoSuchMethodException
- if matching constructor cannot be foundjava.lang.IllegalAccessException
- thrown on the constructor's invocationjava.lang.reflect.InvocationTargetException
- thrown on the constructor's invocationjava.lang.InstantiationException
- thrown on the constructor's invocationConstructor.newInstance(java.lang.Object...)
public static java.lang.Object invokeExactConstructor(java.lang.Class cls, java.lang.Object arg) 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.
cls
- the class to be constructed.arg
- the actual argumentklazz
java.lang.NoSuchMethodException
- If the constructor cannot be foundjava.lang.IllegalAccessException
- If an error occurs accessing the constructorjava.lang.reflect.InvocationTargetException
- If an error occurs invoking the constructorjava.lang.InstantiationException
- If an error occurs instantiating the classinvokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static java.lang.Object invokeExactConstructor(java.lang.Class cls, 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.
cls
- the class to be constructed.args
- actual argument arrayklazz
java.lang.NoSuchMethodException
- If the constructor cannot be foundjava.lang.IllegalAccessException
- If an error occurs accessing the
constructorjava.lang.reflect.InvocationTargetException
- If an error occurs invoking the
constructorjava.lang.InstantiationException
- If an error occurs instantiating the classinvokeExactConstructor(java.lang.Class, java.lang.Object[],
java.lang.Class[])
public static java.lang.Object invokeExactConstructor(java.lang.Class cls, 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.
cls
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types arrayklazz
java.lang.NoSuchMethodException
- if matching constructor cannot be foundjava.lang.IllegalAccessException
- thrown on the constructor's invocationjava.lang.reflect.InvocationTargetException
- thrown on the constructor's invocationjava.lang.InstantiationException
- thrown on the constructor's invocationConstructor.newInstance(java.lang.Object...)
public static java.lang.reflect.Constructor getAccessibleConstructor(java.lang.Class cls, java.lang.Class parameterType)
cls
- the class to be constructedparameterType
- The constructor parameter typeClass.getConstructor(java.lang.Class<?>...)
,
getAccessibleConstructor(java.lang.reflect.Constructor)
public static java.lang.reflect.Constructor getAccessibleConstructor(java.lang.Class cls, java.lang.Class[] parameterTypes)
cls
- the class to be constructedparameterTypes
- the parameter arrayClass.getConstructor(java.lang.Class<?>...)
,
getAccessibleConstructor(java.lang.reflect.Constructor)
public static java.lang.reflect.Constructor getAccessibleConstructor(java.lang.reflect.Constructor ctor)
ctor
- prototype constructor object.null
if accessible constructor can not be found.SecurityManager
public static java.lang.reflect.Constructor getMatchingAccessibleConstructor(java.lang.Class cls, 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.
cls
- find constructor for this classparameterTypes
- find method with compatible parametersnull
.Copyright © 2010 - 2020 Adobe. All Rights Reserved