net.sourceforge.sqlunit.utils
Class MethodInvocationUtils

java.lang.Object
  extended by net.sourceforge.sqlunit.utils.MethodInvocationUtils

public final class MethodInvocationUtils
extends java.lang.Object

The MethodInvocationUtils class allow you to instantiate classes and execute method using reflection.

Version:
$Revision: 1.3 $
Author:
Mario Laureti (mlaureti@users.sourceforge.net)

Method Summary
static java.lang.Object[] getArguments(Arg[] args)
          Convenience method to extract the argument values into an array of Objects to pass to Method.invoke().
static java.lang.Class[] getArgumentTypes(Arg[] args)
          Convenience method to extract the argument types into a Class array to pass to Method.invoke().
static java.lang.reflect.Constructor getConstructor(java.lang.String className, java.lang.Class[] ctorArgTypes)
          Returns the constructor method for the specified class and the parameters.
static java.lang.Object getInstance(java.lang.String className)
          Convenience method to instantiate a class without constructor args.
static java.lang.Object getInstance(java.lang.String className, java.lang.Object[] ctorArgs, java.lang.Class[] ctorArgTypes)
          Get an instance of the object whose classname and initialization parameters are supplied.
static java.lang.String getRootCauseMessage(java.lang.Throwable th)
          Climbs the exception tree to find the application specific message that was thrown by the method.
static java.lang.Object invoke(java.lang.Object anObject, java.lang.String methodName)
          Convenience method to invoke a method without arguments.
static java.lang.Object invoke(java.lang.Object anObject, java.lang.String methodName, java.lang.Object[] arguments, java.lang.Class[] argumentsType)
          Invokes the specified method on the specified object and returns the result.
static java.lang.Object invoke(java.lang.String className, java.lang.String staticMethodName)
          Convenience method to invoke a named method on a named class with no arguments.
static java.lang.Object invoke(java.lang.String className, java.lang.String staticMethodName, java.lang.Object[] arguments, java.lang.Class[] argumentsType)
          Invokes a named method on a named class with the specified arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getConstructor

public static java.lang.reflect.Constructor getConstructor(java.lang.String className,
                                                           java.lang.Class[] ctorArgTypes)
                                                    throws SQLUnitException
Returns the constructor method for the specified class and the parameters.

Parameters:
className - name of the class for which to build the Constructor.
ctorArgTypes - an array of constructor parameter types.
Returns:
a Constructor object for the given class and arguments.
Throws:
SQLUnitException - if there was a problem.

getInstance

public static java.lang.Object getInstance(java.lang.String className,
                                           java.lang.Object[] ctorArgs,
                                           java.lang.Class[] ctorArgTypes)
                                    throws SQLUnitException
Get an instance of the object whose classname and initialization parameters are supplied.

Parameters:
className - the name of the class to instantiate.
ctorArgs - an array of constructor argument values.
ctorArgTypes - an array of constructor argument types.
Returns:
an object of the specified class.
Throws:
SQLUnitException - if there was a problem with instantiating.

getInstance

public static java.lang.Object getInstance(java.lang.String className)
                                    throws SQLUnitException
Convenience method to instantiate a class without constructor args.

Parameters:
className - the name of the class to instantiate.
Returns:
an object of the specified class.
Throws:
SQLUnitException - if there was a problem with instantiating.

invoke

public static java.lang.Object invoke(java.lang.Object anObject,
                                      java.lang.String methodName,
                                      java.lang.Object[] arguments,
                                      java.lang.Class[] argumentsType)
                               throws SQLUnitException
Invokes the specified method on the specified object and returns the result.

Parameters:
anObject - the object to invoke the method on.
methodName - the method name to invoke.
arguments - an array of method argument values.
argumentsType - an array of method argument types.
Returns:
the Object returned as a result of the invoke.
Throws:
SQLUnitException - if there was a problem with invoking method.

invoke

public static java.lang.Object invoke(java.lang.Object anObject,
                                      java.lang.String methodName)
                               throws SQLUnitException
Convenience method to invoke a method without arguments.

Parameters:
anObject - the object to invoke the method on.
methodName - the method name to invoke.
Returns:
the Object returned as a result of the invoke.
Throws:
SQLUnitException - if there was a problem with the invocation.

invoke

public static java.lang.Object invoke(java.lang.String className,
                                      java.lang.String staticMethodName,
                                      java.lang.Object[] arguments,
                                      java.lang.Class[] argumentsType)
                               throws SQLUnitException
Invokes a named method on a named class with the specified arguments.

Parameters:
className - the name of the class to invoke the method on.
staticMethodName - the name of the static method to invoke.
arguments - an array of Object arguments to the method.
argumentsType - an array of Class argument types.
Returns:
the Object that is a result of the method invocation.
Throws:
SQLUnitException - if there was a problem.

invoke

public static java.lang.Object invoke(java.lang.String className,
                                      java.lang.String staticMethodName)
                               throws SQLUnitException
Convenience method to invoke a named method on a named class with no arguments.

Parameters:
className - the name of the class to invoke the method on.
staticMethodName - the name of the static method to invoke.
Returns:
an Object that is the result of the method invocation.
Throws:
SQLUnitException - if there was a problem.

getArguments

public static java.lang.Object[] getArguments(Arg[] args)
                                       throws SQLUnitException
Convenience method to extract the argument values into an array of Objects to pass to Method.invoke().

Parameters:
args - an Array of Arg objects.
Returns:
an array of Objects.
Throws:
SQLUnitException - if there was a problem.

getArgumentTypes

public static java.lang.Class[] getArgumentTypes(Arg[] args)
                                          throws SQLUnitException
Convenience method to extract the argument types into a Class array to pass to Method.invoke().

Parameters:
args - an array of Arg objects.
Returns:
an array of Class objects.
Throws:
SQLUnitException - if there was a problem.

getRootCauseMessage

public static java.lang.String getRootCauseMessage(java.lang.Throwable th)
Climbs the exception tree to find the application specific message that was thrown by the method. The method must throw an unchained exception for it to be computed in this way.

Parameters:
th - the Throwable to look at.
Returns:
the Exception message associated with the root cause.