net.sf.jga.fn.property
Class InvokeMethod<T,R>

java.lang.Object
  extended by net.sf.jga.fn.BinaryFunctor<T,java.lang.Object[],R>
      extended by net.sf.jga.fn.property.InvokeMethod<T,R>
All Implemented Interfaces:
java.io.Serializable, Visitable

public class InvokeMethod<T,R>
extends BinaryFunctor<T,java.lang.Object[],R>

Functor that invokes a method described at construction and returns the result or null if the result is void. The arguments must be passed to the functor in an array, and the values must be assignable to the corresponding classes given at construction.

Note that declaring the return type incorrectly can result in ClassCastExceptions being thrown when the functor is invoked: the compiler cannot check the return type of a reflectively loaded method.

Copyright © 2003 David A. Hall

Author:
David A. Hall
See Also:
Serialized Form

Nested Class Summary
static interface InvokeMethod.Visitor
          Interface for classes that may interpret a InvokeMethod function.
 
Constructor Summary
InvokeMethod(java.lang.Class<T> objClass, java.lang.reflect.Method method)
          Builds a InvokeMethod for a given method, using the given class to find the desired method.
InvokeMethod(java.lang.Class<T> objClass, java.lang.String methName, java.lang.Class argtype)
          Builds a InvokeMethod for a given method, using the given class to find the desired method.
InvokeMethod(java.lang.Class<T> objClass, java.lang.String methName, java.lang.Class[] argtypes)
          Builds a InvokeMethod for the given method, using the given class array to find the desired method.
 
Method Summary
 void accept(Visitor v)
          Calls the Visitor's visit(InvokeMethod) method, if it implements the nested Visitor interface.
 R fn(T obj, java.lang.Object[] args)
          Executes the function and returns the result.
 java.lang.String getMethodName()
          Returns the name of the method that this functor invokes.
 java.lang.Class<R> getReturnType()
          Returns the type of the method
 java.lang.String toString()
           
 
Methods inherited from class net.sf.jga.fn.BinaryFunctor
bind, bind1st, bind2nd, compose, compose, distribute, generate, generate1st, generate2nd
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InvokeMethod

public InvokeMethod(java.lang.Class<T> objClass,
                    java.lang.reflect.Method method)
Builds a InvokeMethod for a given method, using the given class to find the desired method.

Throws:
java.lang.IllegalArgumentException - if the method name is null or empty, or if the argument type array is null.

InvokeMethod

public InvokeMethod(java.lang.Class<T> objClass,
                    java.lang.String methName,
                    java.lang.Class argtype)
Builds a InvokeMethod for a given method, using the given class to find the desired method. Note that this is a convenience constructor for a common case: the case where the method takes a single argument. It is still necessary for the argument passed to the fn method to be an array, in this case it must be an array of length 1 containing a value of of type argtype. A ClassCastException may be thrown if this functor is called incorrectly: if the line number in the associated stack dump points to the class statement (on or about line 45), then the argument was likely passed without being wrapped in an array.

Throws:
java.lang.IllegalArgumentException - if the method name is null or empty, or if the argument type array is null.

InvokeMethod

public InvokeMethod(java.lang.Class<T> objClass,
                    java.lang.String methName,
                    java.lang.Class[] argtypes)
Builds a InvokeMethod for the given method, using the given class array to find the desired method.

Throws:
java.lang.IllegalArgumentException - if the method name is null or empty, or if the argument type array is null.
Method Detail

getMethodName

public java.lang.String getMethodName()
Returns the name of the method that this functor invokes.


getReturnType

public java.lang.Class<R> getReturnType()
Returns the type of the method


fn

public R fn(T obj,
            java.lang.Object[] args)
Description copied from class: BinaryFunctor
Executes the function and returns the result.

Specified by:
fn in class BinaryFunctor<T,java.lang.Object[],R>

accept

public void accept(Visitor v)
Calls the Visitor's visit(InvokeMethod) method, if it implements the nested Visitor interface.

Specified by:
accept in interface Visitable
Overrides:
accept in class BinaryFunctor<T,java.lang.Object[],R>

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2002-2005 David A. Hall. All Rights Reserved.