org.exolab.jmscts.core
Class MethodInvoker

java.lang.Object
  |
  +--org.exolab.jmscts.core.MethodInvoker

public class MethodInvoker
extends java.lang.Object

A helper class for invoking methods, and verifying that any exception thrown matches that expected.

Version:
$Revision: 1.2 $ $Date: 2004/01/31 13:44:24 $
Author:
Tim Anderson

Constructor Summary
MethodInvoker()
          Construct an instance with that expects no exceptions when a method is invoked
MethodInvoker(java.lang.Class clazz, java.lang.Class exception)
          Construct an instance that expects a single expected exception when a method is invoked.
 
Method Summary
protected  void fail(java.lang.reflect.Method method)
          Helper to raise an exception when a method doesn't throw the expected exception
 java.lang.Object invoke(java.lang.Object object, java.lang.reflect.Method method)
          Invoke a method taking no arguments
 java.lang.Object invoke(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object argument)
          Invoke a method taking a single argument
 java.lang.Object invoke(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object[] arguments)
          Invoke a method taking a list of arguments
 java.lang.Object invoke(java.lang.Object object, java.lang.String method)
          Invoke a method taking no arguments
 java.lang.Object invoke(java.lang.Object object, java.lang.String method, java.lang.Object argument)
          Invoke a method taking a single argument
 java.lang.Object invoke(java.lang.Object object, java.lang.String method, java.lang.Object[] arguments)
          Invoke a method taking a list of arguments
protected  void verify(java.lang.reflect.Method method, java.lang.Exception exception)
          Helper to verify that an exception matches that expected
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodInvoker

public MethodInvoker()
Construct an instance with that expects no exceptions when a method is invoked

MethodInvoker

public MethodInvoker(java.lang.Class clazz,
                     java.lang.Class exception)
Construct an instance that expects a single expected exception when a method is invoked.
Parameters:
clazz - the class to introspect. May be null
exception - the expected exception when methods are invoked, or null if no exceptions are expected.
Method Detail

invoke

public java.lang.Object invoke(java.lang.Object object,
                               java.lang.String method)
                        throws java.lang.Exception,
                               java.lang.NoSuchMethodException
Invoke a method taking no arguments

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is wrapped in an object. If the underlying method return type is void, the invocation returns null.

Parameters:
object - the object to invoke the method on
method - the method name
Returns:
the result of invoking the method, or null, if it is void
Throws:
java.lang.Exception - if an exception is thrown and it doesn't match the expected exception. If no exception is expected, then the exception will be propagated
java.lang.NoSuchMethodException - if the method doesn't exist

invoke

public java.lang.Object invoke(java.lang.Object object,
                               java.lang.reflect.Method method)
                        throws java.lang.Exception
Invoke a method taking no arguments

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is wrapped in an object. If the underlying method return type is void, the invocation returns null.

Parameters:
object - the object to invoke the method on
method - the method to invoke
Returns:
the result of invoking the method, or null, if it is void
Throws:
java.lang.Exception - if an exception is thrown and it doesn't match the expected exception. If no exception is expected, then the exception will be propagated

invoke

public java.lang.Object invoke(java.lang.Object object,
                               java.lang.String method,
                               java.lang.Object argument)
                        throws java.lang.Exception,
                               java.lang.NoSuchMethodException
Invoke a method taking a single argument

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is wrapped in an object. If the underlying method return type is void, the invocation returns null.

Parameters:
object - the object to invoke the method on
method - the method name
argument - the argument to pass to the method
Returns:
the result of invoking the method, or null, if it is void
Throws:
java.lang.Exception - if an exception is thrown and it doesn't match the expected exception. If no exception is expected, then the exception will be propagated
java.lang.NoSuchMethodException - if the method doesn't exist

invoke

public java.lang.Object invoke(java.lang.Object object,
                               java.lang.reflect.Method method,
                               java.lang.Object argument)
                        throws java.lang.Exception
Invoke a method taking a single argument

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is wrapped in an object. If the underlying method return type is void, the invocation returns null.

Parameters:
object - the object to invoke the method on
method - the method to invoke
argument - the argument to pass to the method
Returns:
the result of invoking the method, or null, if it is void
Throws:
java.lang.Exception - if an exception is thrown and it doesn't match the expected exception. If no exception is expected, then the exception will be propagated

invoke

public java.lang.Object invoke(java.lang.Object object,
                               java.lang.String method,
                               java.lang.Object[] arguments)
                        throws java.lang.Exception,
                               java.lang.NoSuchMethodException
Invoke a method taking a list of arguments

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is wrapped in an object. If the underlying method return type is void, the invocation returns null.

Parameters:
object - the object to invoke the method on
method - the method name
arguments - the arguments to pass to the method
Returns:
the result of invoking the method, or null, if it is void
Throws:
java.lang.Exception - if an exception is thrown and it doesn't match the expected exception. If no exception is expected, then the exception will be propagated
java.lang.NoSuchMethodException - if the method doesn't exist

invoke

public java.lang.Object invoke(java.lang.Object object,
                               java.lang.reflect.Method method,
                               java.lang.Object[] arguments)
                        throws java.lang.Exception
Invoke a method taking a list of arguments

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is wrapped in an object. If the underlying method return type is void, the invocation returns null.

Parameters:
object - the object to invoke the method on
method - the method to invoke
arguments - the arguments to pass to the method
Returns:
the result of invoking the method, or null, if it is void
Throws:
java.lang.Exception - if an exception is thrown and it doesn't match the expected exception. If no exception is expected, then the exception will be propagated

fail

protected void fail(java.lang.reflect.Method method)
             throws java.lang.Exception
Helper to raise an exception when a method doesn't throw the expected exception
Parameters:
method - the method
Throws:
java.lang.Exception - details the expected exception

verify

protected void verify(java.lang.reflect.Method method,
                      java.lang.Exception exception)
               throws java.lang.Exception
Helper to verify that an exception matches that expected
Parameters:
method - the method
exception - the exception thrown by the method
Throws:
java.lang.Exception - if exception isn't of the expected type


Copyright © 2001-2004 JMS CTS Team. All Rights Reserved.