org.apache.commons.beanutils
Class ConstructorUtils

java.lang.Object
  extended by org.apache.commons.beanutils.ConstructorUtils

public class ConstructorUtils
extends 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.

Version:
$Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
Author:
Craig R. McClanahan, Ralph Schaer, Chris Audley, Rey Francois, Gregor Rayman, Jan Sorensen, Robert Burrell Donkin, Rodney Waldhoff

Constructor Summary
ConstructorUtils()
           
 
Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConstructorUtils

public ConstructorUtils()
Method Detail

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.

Parameters:
klass - the class to be constructed.
arg - the actual argument
Returns:
new instance of klazz
Throws:
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 class
See Also:
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.

Parameters:
klass - the class to be constructed.
args - actual argument array
Returns:
new instance of klazz
Throws:
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 class
See Also:
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.

Parameters:
klass - the class to be constructed.
args - actual argument array
parameterTypes - parameter types array
Returns:
new instance of klazz
Throws:
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 invocation
See Also:
Constructor.newInstance(java.lang.Object...)

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.

Parameters:
klass - the class to be constructed.
arg - the actual argument
Returns:
new instance of klazz
Throws:
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 class
See Also:
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.

Parameters:
klass - the class to be constructed.
args - actual argument array
Returns:
new instance of klazz
Throws:
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 class
See Also:
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.

Parameters:
klass - the class to be constructed.
args - actual argument array
parameterTypes - parameter types array
Returns:
new instance of klazz
Throws:
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 invocation
See Also:
Constructor.newInstance(java.lang.Object...)

getAccessibleConstructor

public static Constructor getAccessibleConstructor(Class klass,
                                                   Class parameterType)
Returns a constructor with single argument.

Parameters:
klass - the class to be constructed
parameterType - The constructor parameter type
Returns:
null if matching accessible constructor can not be found.
See Also:
Class.getConstructor(java.lang.Class...), getAccessibleConstructor(java.lang.reflect.Constructor)

getAccessibleConstructor

public static Constructor getAccessibleConstructor(Class klass,
                                                   Class[] parameterTypes)
Returns a constructor given a class and signature.

Parameters:
klass - the class to be constructed
parameterTypes - the parameter array
Returns:
null if matching accessible constructor can not be found
See Also:
Class.getConstructor(java.lang.Class...), getAccessibleConstructor(java.lang.reflect.Constructor)

getAccessibleConstructor

public static Constructor getAccessibleConstructor(Constructor ctor)
Returns accessible version of the given constructor.

Parameters:
ctor - prototype constructor object.
Returns:
null if accessible constructor can not be found.
See Also:
SecurityManager


Copyright © 2000-2009 The Apache Software Foundation. All Rights Reserved.