com.anthonyeden.lib.util
Class MethodUtilities

java.lang.Object
  extended bycom.anthonyeden.lib.util.MethodUtilities

public class MethodUtilities
extends java.lang.Object

Utilities for invoking methods at runtime using reflection.

Author:
Anthony Eden

Method Summary
static java.lang.reflect.Method getDeclaredMethod(java.lang.Class targetClass, java.lang.String name, java.lang.Class paramClass)
          Get the declared method with the given name and with a single argument of the type specified by paramClass.
static java.lang.reflect.Method getDeclaredMethod(java.lang.Class targetClass, java.lang.String name, java.lang.Class[] paramClasses)
          Get the declared method with the given name and with arguments of the types specified by paramClasses.
static java.lang.reflect.Method getMethod(java.lang.Class targetClass, java.lang.String name, java.lang.Class paramClass)
          Get the method with the given name and with a single argument of the type specified by paramClass.
static java.lang.reflect.Method getMethod(java.lang.Class targetClass, java.lang.String name, java.lang.Class[] paramClasses)
          Get the method with the given name and with arguments of the types specified by paramClasses.
static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object arg)
          Invoke the given method.
static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object[] args)
          Invoke the given method.
static java.lang.Object invoke(java.lang.String methodName, java.lang.Object target, java.lang.Class targetClass, java.lang.Object arg)
          Invoke the given method.
static java.lang.Object invoke(java.lang.String methodName, java.lang.Object target, java.lang.Class targetClass, java.lang.Object[] args)
          Invoke the given method.
static java.lang.Object invoke(java.lang.String methodName, java.lang.Object target, java.lang.Object arg)
          Invoke the given method.
static java.lang.Object invoke(java.lang.String methodName, java.lang.Object target, java.lang.Object[] args)
          Invoke the given method.
static java.lang.Object invokeDeclared(java.lang.String methodName, java.lang.Object target, java.lang.Object arg)
          Invoke the given method.
static java.lang.Object invokeDeclared(java.lang.String methodName, java.lang.Object target, java.lang.Object[] args)
          Invoke the given method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

invoke

public static java.lang.Object invoke(java.lang.reflect.Method method,
                                      java.lang.Object target,
                                      java.lang.Object arg)
                               throws java.lang.IllegalAccessException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.reflect.InvocationTargetException
Invoke the given method. This method is a convenience method which creates an array of arguments with the single arg value. This is necessary because the Method.invoke() method only accepts an array of argument objects.

Parameters:
method - The method
target - The target Object
arg - A single argument
Returns:
The return object or null
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException

invoke

public static java.lang.Object invoke(java.lang.reflect.Method method,
                                      java.lang.Object target,
                                      java.lang.Object[] args)
                               throws java.lang.IllegalAccessException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.reflect.InvocationTargetException
Invoke the given method. This method actually invokes the target method with the given arguments.

Parameters:
method - The Method
target - The target Object
args - An array of argument Objects
Returns:
The return value
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException

invoke

public static java.lang.Object invoke(java.lang.String methodName,
                                      java.lang.Object target,
                                      java.lang.Object arg)
                               throws java.lang.IllegalAccessException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.reflect.InvocationTargetException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.SecurityException
Invoke the given method. The target object is used to determine the target class and which is then used to get a Method object. This method is a convenience method which creates an array of arguments with the single arg value. This is necessary because the Method.invoke() method only accepts an array of argument objects.

Parameters:
methodName - The method name
target - The target Object
arg - A single argument
Returns:
The return object or null
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

invoke

public static java.lang.Object invoke(java.lang.String methodName,
                                      java.lang.Object target,
                                      java.lang.Object[] args)
                               throws java.lang.IllegalAccessException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.reflect.InvocationTargetException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.SecurityException
Invoke the given method. The target object is used to determine the target class and which is then used to get a Method object.

Parameters:
methodName - The Method name
target - The target Object
args - An array of argument Objects
Returns:
The return value
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

invoke

public static java.lang.Object invoke(java.lang.String methodName,
                                      java.lang.Object target,
                                      java.lang.Class targetClass,
                                      java.lang.Object[] args)
                               throws java.lang.IllegalAccessException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.reflect.InvocationTargetException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.SecurityException
Invoke the given method. The target object may be null in which case the target class is used and the method must be a static method.

Parameters:
methodName - The method name
target - The target Object
targetClass - The Class of the target object
args - An array of argument Objects
Returns:
The return value
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

invoke

public static java.lang.Object invoke(java.lang.String methodName,
                                      java.lang.Object target,
                                      java.lang.Class targetClass,
                                      java.lang.Object arg)
                               throws java.lang.IllegalAccessException,
                                      java.lang.IllegalArgumentException,
                                      java.lang.reflect.InvocationTargetException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.SecurityException
Invoke the given method. This method is a convenience method for invoking single argument methods.

Parameters:
methodName - The method name
target - The target Object
targetClass - The Class of the target object
arg - A single argument
Returns:
The return value
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

invokeDeclared

public static java.lang.Object invokeDeclared(java.lang.String methodName,
                                              java.lang.Object target,
                                              java.lang.Object arg)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.IllegalArgumentException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.SecurityException
Invoke the given method. This method is a convenience method for invoking single argument methods. The argument's class is obtained from the argument object.

Parameters:
methodName - The method name
target - The target Object
arg - A single argument
Returns:
The return value
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

invokeDeclared

public static java.lang.Object invokeDeclared(java.lang.String methodName,
                                              java.lang.Object target,
                                              java.lang.Object[] args)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.IllegalArgumentException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.SecurityException
Invoke the given method. This method is a convenience method for invoking single argument methods. The argument's class is obtained from the argument object.

Parameters:
methodName - The method name
target - The target Object
args - An array of arguments
Returns:
The return value
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class targetClass,
                                                 java.lang.String name,
                                                 java.lang.Class paramClass)
                                          throws java.lang.NoSuchMethodException,
                                                 java.lang.SecurityException
Get the method with the given name and with a single argument of the type specified by paramClass. The paramClass may be null in which case the method should have no arguments.

Parameters:
targetClass - The target class
name - The method name
paramClass - The single parameter class (may be null)
Returns:
The Method
Throws:
java.lang.SecurityException
java.lang.NoSuchMethodException

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class targetClass,
                                                 java.lang.String name,
                                                 java.lang.Class[] paramClasses)
                                          throws java.lang.NoSuchMethodException,
                                                 java.lang.SecurityException
Get the method with the given name and with arguments of the types specified by paramClasses. If the method is not found then a method which accepts superclasses of the current arguments will be returned, if possible.

Parameters:
targetClass - The target class
name - The method name
paramClasses - An array of parameter classes
Returns:
The Method
Throws:
java.lang.SecurityException
java.lang.NoSuchMethodException

getDeclaredMethod

public static java.lang.reflect.Method getDeclaredMethod(java.lang.Class targetClass,
                                                         java.lang.String name,
                                                         java.lang.Class paramClass)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.SecurityException
Get the declared method with the given name and with a single argument of the type specified by paramClass. The paramClass may be null in which case the method should have no arguments.

Parameters:
targetClass - The target class
name - The method name
paramClass - The single parameter class (may be null)
Returns:
The declared Method
Throws:
java.lang.SecurityException
java.lang.NoSuchMethodException

getDeclaredMethod

public static java.lang.reflect.Method getDeclaredMethod(java.lang.Class targetClass,
                                                         java.lang.String name,
                                                         java.lang.Class[] paramClasses)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.SecurityException
Get the declared method with the given name and with arguments of the types specified by paramClasses.

Parameters:
targetClass - The target class
name - The method name
paramClasses - An array of parameter classes
Returns:
The declared Method
Throws:
java.lang.SecurityException
java.lang.NoSuchMethodException


Copyright (c) 2000-2003 Anthony Eden. All Rights Reserved.