org.apache.commons.beanutils
Class ConstructorUtils
java.lang.Object
org.apache.commons.beanutils.ConstructorUtils
public class ConstructorUtils
extends java.lang.Object
Utility reflection methods focussed on constructors, modelled after
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 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.
$Revision: 1.7 $ $Date: 2004/02/28 13:18:33 $- Craig R. McClanahan
- Ralph Schaer
- Chris Audley
- Rey Fran?ois
- Gregor Ra?man
- Jan Sorensen
- Robert Burrell Donkin
- Rodney Waldhoff
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.
|
private static Constructor | getMatchingAccessibleConstructor(Class clazz, Class[] parameterTypes) - Find an accessible constructor with compatible parameters.
|
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 .
|
emptyClassArray
private static final Class[] emptyClassArray
An empty class array
emptyObjectArray
private static final Object[] emptyObjectArray
An empty object array
getAccessibleConstructor
public static Constructor getAccessibleConstructor(Class klass,
Class parameterType)
Returns a constructor with single argument.
klass
- the class to be constructed
- null if matching accessible constructor can not be found.
Class.getConstructor
, getAccessibleConstructor(java.lang.reflect.Constructor)
getAccessibleConstructor
public static Constructor getAccessibleConstructor(Class klass,
Class[] parameterTypes)
Returns a constructor given a class and signature.
klass
- the class to be constructedparameterTypes
- the parameter array
- null if matching accessible constructor can not be found
Class.getConstructor
, getAccessibleConstructor(java.lang.reflect.Constructor)
getAccessibleConstructor
public static Constructor getAccessibleConstructor(Constructor ctor)
Returns accessible version of the given constructor.
ctor
- prototype constructor object.
null
if accessible constructor can not be found.
java.lang.SecurityManager
getMatchingAccessibleConstructor
private static Constructor getMatchingAccessibleConstructor(Class clazz,
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.
clazz
- find constructor for this classparameterTypes
- find method with compatible parameters
- a valid Constructor object. If there's no matching constructor, returns
null
.
invokeConstructor
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 argument
invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
invokeConstructor
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 array
invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
invokeConstructor
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 array
invokeExactConstructor
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 argument
invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
invokeExactConstructor
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 array
invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
invokeExactConstructor
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 array
Copyright (c) 2001-2004 - Apache Software Foundation