public class ConstructorUtils extends 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() |
Modifier and Type | Method and Description |
---|---|
static Constructor |
getAccessibleConstructor(Class klass,
Class parameterType)
Returns a constructor with single argument.
|
static Constructor |
getAccessibleConstructor(Class klass,
Class[] parameterTypes)
Returns a constructor given a class and signature.
|
static Constructor |
getAccessibleConstructor(Constructor ctor)
Returns accessible version of the given constructor.
|
static Object |
invokeConstructor(Class klass,
Object arg)
Convenience method returning new instance of
klazz using a single argument constructor. |
static Object |
invokeConstructor(Class klass,
Object[] args)
Returns new instance of
klazz created using the actual arguments args . |
static Object |
invokeConstructor(Class klass,
Object[] args,
Class[] parameterTypes)
Returns new instance of
klazz created using constructor
with signature parameterTypes and actual arguments args . |
static Object |
invokeExactConstructor(Class klass,
Object arg)
Convenience method returning new instance of
klazz using a single argument constructor. |
static Object |
invokeExactConstructor(Class klass,
Object[] args)
Returns new instance of
klazz created using the actual arguments args . |
static Object |
invokeExactConstructor(Class klass,
Object[] args,
Class[] parameterTypes)
Returns new instance of
klazz created using constructor
with signature parameterTypes and actual arguments
args . |
public static Object invokeConstructor(Class klass, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
Convenience method returning new instance of klazz
using a single argument constructor.
The formal parameter type is inferred from the actual values of arg
.
See invokeExactConstructor(Class, Object[], Class[])
for more details.
The signatures should be assignment compatible.
klass
- the class to be constructed.arg
- the actual argumentklazz
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 classinvokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeConstructor(Class klass, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, 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.
klass
- the class to be constructed.args
- actual argument arrayklazz
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 classinvokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeConstructor(Class klass, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
Returns new instance of klazz
created using constructor
with signature parameterTypes
and actual arguments args
.
The signatures should be assignment compatible.
klass
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types arrayklazz
NoSuchMethodException
- if matching constructor cannot be foundIllegalAccessException
- thrown on the constructor's invocationInvocationTargetException
- thrown on the constructor's invocationInstantiationException
- thrown on the constructor's invocationConstructor.newInstance(java.lang.Object...)
public static Object invokeExactConstructor(Class klass, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
Convenience method returning new instance of klazz
using a single argument constructor.
The formal parameter type is inferred from the actual values of arg
.
See invokeExactConstructor(Class, Object[], Class[])
for more details.
The signatures should match exactly.
klass
- the class to be constructed.arg
- the actual argumentklazz
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 classinvokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeExactConstructor(Class klass, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, 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.
klass
- the class to be constructed.args
- actual argument arrayklazz
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 classinvokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeExactConstructor(Class klass, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
Returns new instance of klazz
created using constructor
with signature parameterTypes
and actual arguments
args
.
The signatures should match exactly.
klass
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types arrayklazz
NoSuchMethodException
- if matching constructor cannot be foundIllegalAccessException
- thrown on the constructor's invocationInvocationTargetException
- thrown on the constructor's invocationInstantiationException
- thrown on the constructor's invocationConstructor.newInstance(java.lang.Object...)
public static Constructor getAccessibleConstructor(Class klass, Class parameterType)
klass
- the class to be constructedparameterType
- The constructor parameter typeClass.getConstructor(java.lang.Class<?>...)
,
getAccessibleConstructor(java.lang.reflect.Constructor)
public static Constructor getAccessibleConstructor(Class klass, Class[] parameterTypes)
klass
- the class to be constructedparameterTypes
- the parameter arrayClass.getConstructor(java.lang.Class<?>...)
,
getAccessibleConstructor(java.lang.reflect.Constructor)
public static Constructor getAccessibleConstructor(Constructor ctor)
ctor
- prototype constructor object.null
if accessible constructor can not be found.SecurityManager
Copyright © 2000-2017 The Apache Software Foundation. All Rights Reserved.