Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.commons.beanutils
Class ConstructorUtils  view ConstructorUtils download ConstructorUtils.java

java.lang.Object
  extended byorg.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.

Version:
$Revision: 1.7 $ $Date: 2004/02/28 13:18:33 $

Field Summary
private static java.lang.Class[] emptyClassArray
          An empty class array
private static java.lang.Object[] emptyObjectArray
          An empty object array
 
Constructor Summary
ConstructorUtils()
           
 
Method Summary
static java.lang.reflect.Constructor getAccessibleConstructor(java.lang.Class klass, java.lang.Class parameterType)
          Returns a constructor with single argument.
static java.lang.reflect.Constructor getAccessibleConstructor(java.lang.Class klass, 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.
private static java.lang.reflect.Constructor getMatchingAccessibleConstructor(java.lang.Class clazz, java.lang.Class[] parameterTypes)
          Find an accessible constructor with compatible parameters.
static java.lang.Object invokeConstructor(java.lang.Class klass, java.lang.Object arg)
          Convenience method returning new instance of klazz using a single argument constructor.
static java.lang.Object invokeConstructor(java.lang.Class klass, java.lang.Object[] args)
          Returns new instance of klazz created using the actual arguments args.
static java.lang.Object invokeConstructor(java.lang.Class klass, 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 klass, java.lang.Object arg)
          Convenience method returning new instance of klazz using a single argument constructor.
static java.lang.Object invokeExactConstructor(java.lang.Class klass, java.lang.Object[] args)
          Returns new instance of klazz created using the actual arguments args.
static java.lang.Object invokeExactConstructor(java.lang.Class klass, java.lang.Object[] args, java.lang.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
 

Field Detail

emptyClassArray

private static final java.lang.Class[] emptyClassArray
An empty class array


emptyObjectArray

private static final java.lang.Object[] emptyObjectArray
An empty object array

Constructor Detail

ConstructorUtils

public ConstructorUtils()
Method Detail

invokeConstructor

public static java.lang.Object invokeConstructor(java.lang.Class klass,
                                                 java.lang.Object arg)
                                          throws java.lang.NoSuchMethodException,
                                                 java.lang.IllegalAccessException,
                                                 java.lang.reflect.InvocationTargetException,
                                                 java.lang.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[]) 55 for more details.

The signatures should be assignment compatible.


invokeConstructor

public static java.lang.Object invokeConstructor(java.lang.Class klass,
                                                 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[]) 55 for more details.

The signatures should be assignment compatible.


invokeConstructor

public static java.lang.Object invokeConstructor(java.lang.Class klass,
                                                 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.


invokeExactConstructor

public static java.lang.Object invokeExactConstructor(java.lang.Class klass,
                                                      java.lang.Object arg)
                                               throws java.lang.NoSuchMethodException,
                                                      java.lang.IllegalAccessException,
                                                      java.lang.reflect.InvocationTargetException,
                                                      java.lang.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[]) 55 for more details.

The signatures should match exactly.


invokeExactConstructor

public static java.lang.Object invokeExactConstructor(java.lang.Class klass,
                                                      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[]) 55 for more details.

The signatures should match exactly.


invokeExactConstructor

public static java.lang.Object invokeExactConstructor(java.lang.Class klass,
                                                      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.


getAccessibleConstructor

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


getAccessibleConstructor

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


getAccessibleConstructor

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


getMatchingAccessibleConstructor

private static java.lang.reflect.Constructor getMatchingAccessibleConstructor(java.lang.Class clazz,
                                                                              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.