|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.lang.reflect.ConstructorUtils
public class ConstructorUtils
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 Summary | |
---|---|
ConstructorUtils()
ConstructorUtils instances should NOT be constructed in standard programming. |
Method Summary | |
---|---|
static Constructor |
getAccessibleConstructor(Class cls,
Class parameterType)
Returns a constructor with single argument. |
static Constructor |
getAccessibleConstructor(Class cls,
Class[] parameterTypes)
Returns a constructor given a class and signature. |
static Constructor |
getAccessibleConstructor(Constructor ctor)
Returns accessible version of the given constructor. |
static Constructor |
getMatchingAccessibleConstructor(Class cls,
Class[] parameterTypes)
Find an accessible constructor with compatible parameters. |
static Object |
invokeConstructor(Class cls,
Object arg)
Convenience method returning new instance of klazz using a single argument constructor. |
static Object |
invokeConstructor(Class cls,
Object[] args)
Returns new instance of klazz created using the actual arguments args . |
static Object |
invokeConstructor(Class cls,
Object[] args,
Class[] parameterTypes)
Returns new instance of klazz created using constructor
with signature parameterTypes and actual arguments args . |
static Object |
invokeExactConstructor(Class cls,
Object arg)
Convenience method returning new instance of klazz using a single argument constructor. |
static Object |
invokeExactConstructor(Class cls,
Object[] args)
Returns new instance of klazz created using the actual arguments args . |
static Object |
invokeExactConstructor(Class cls,
Object[] args,
Class[] parameterTypes)
Returns new instance of klazz created using constructor
with signature parameterTypes and actual arguments
args . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
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.
Method Detail |
---|
public static Object invokeConstructor(Class cls, 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.
cls
- the class to be constructed.arg
- the actual argument
klazz
NoSuchMethodException
- If the constructor cannot be found
IllegalAccessException
- If an error occurs accessing the constructor
InvocationTargetException
- If an error occurs invoking the constructor
InstantiationException
- If an error occurs instantiating the classinvokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeConstructor(Class cls, 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.
cls
- the class to be constructed.args
- actual argument array
klazz
NoSuchMethodException
- If the constructor cannot be found
IllegalAccessException
- If an error occurs accessing the constructor
InvocationTargetException
- If an error occurs invoking the constructor
InstantiationException
- If an error occurs instantiating the classinvokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeConstructor(Class cls, 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.
cls
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types array
klazz
NoSuchMethodException
- if matching constructor cannot be found
IllegalAccessException
- thrown on the constructor's invocation
InvocationTargetException
- thrown on the constructor's invocation
InstantiationException
- thrown on the constructor's invocationConstructor.newInstance(java.lang.Object...)
public static Object invokeExactConstructor(Class cls, 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.
cls
- the class to be constructed.arg
- the actual argument
klazz
NoSuchMethodException
- If the constructor cannot be found
IllegalAccessException
- If an error occurs accessing the constructor
InvocationTargetException
- If an error occurs invoking the constructor
InstantiationException
- If an error occurs instantiating the classinvokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeExactConstructor(Class cls, 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.
cls
- the class to be constructed.args
- actual argument array
klazz
NoSuchMethodException
- If the constructor cannot be found
IllegalAccessException
- If an error occurs accessing the constructor
InvocationTargetException
- If an error occurs invoking the constructor
InstantiationException
- If an error occurs instantiating the classinvokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
public static Object invokeExactConstructor(Class cls, 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.
cls
- the class to be constructed.args
- actual argument arrayparameterTypes
- parameter types array
klazz
NoSuchMethodException
- if matching constructor cannot be found
IllegalAccessException
- thrown on the constructor's invocation
InvocationTargetException
- thrown on the constructor's invocation
InstantiationException
- thrown on the constructor's invocationConstructor.newInstance(java.lang.Object...)
public static Constructor getAccessibleConstructor(Class cls, Class parameterType)
cls
- the class to be constructedparameterType
- The constructor parameter type
Class.getConstructor(java.lang.Class...)
,
getAccessibleConstructor(java.lang.reflect.Constructor)
public static Constructor getAccessibleConstructor(Class cls, Class[] parameterTypes)
cls
- the class to be constructedparameterTypes
- the parameter array
Class.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
public static Constructor getMatchingAccessibleConstructor(Class cls, 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 parameters
null
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |