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

java.lang.Object
  extended by net.sf.jga.fn.Functor<R>
      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, unless we add more arguments to the constructor.

Copyright © 2003-2005 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
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.
InvokeMethod(java.lang.Class<T> objClass, java.lang.String methName, java.lang.Class<?> argtype)
          Builds an InvokeMethod for a given method, using the given class to find the desired method.
InvokeMethod(java.lang.reflect.Method method)
          Builds an InvokeMethod functor for the given method.
 
Method Summary
 void accept(Visitor v)
          Calls the Visitor's visit(InvokeMethod) method, if it implements the nested Visitor interface.
 R eval(java.lang.Object... args)
          Executes the function and returns the result.
 R fn(T obj, java.lang.Object[] args)
          Executes the function and returns the result.
 java.lang.reflect.Method getMethod()
          Lazy loads the method
 java.lang.String getMethodName()
          Returns the name of the method that this functor invokes.
 java.lang.Class<T> getObjectType()
          Returns the class which supplied the method is invoked.
 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, bind0th, bind1st, bind2nd, bindNth, compose, compose, composeNth, distribute, generate, generate1st, generate2nd, generateNth
 
Methods inherited from class net.sf.jga.fn.Functor
composeNth
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InvokeMethod

public InvokeMethod(java.lang.reflect.Method method)
Builds an InvokeMethod functor for the given method.

NOTE: this constructor has lower type-safety guarantee than the other constructors, in that the compiler cannot check that the method's declaring class is T. When a functor built by this constructor is evaluated, it will throw IllegalArgumentException if given an incompatible argument.

Throws:
java.lang.IllegalArgumentException - if the method is null or if it takes arguments

InvokeMethod

public InvokeMethod(java.lang.Class<T> objClass,
                    java.lang.reflect.Method method)
Builds a InvokeMethod for a given method

Throws:
java.lang.IllegalArgumentException - if the method or class is null; or if the method is not supplied by the given class;

InvokeMethod

public InvokeMethod(java.lang.Class<T> objClass,
                    java.lang.String methName,
                    java.lang.Class<?> argtype)
Builds an InvokeMethod for a given method, using the given class to find the desired method. 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.

Throws:
java.lang.IllegalArgumentException - if class, method name, or argument type array is null; or if the class does not supply the described method.

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 class, method name, or argument type array is null; or if the class does not supply the described method.
Method Detail

getObjectType

public java.lang.Class<T> getObjectType()
Returns the class which supplied the method is invoked.


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


getMethod

public java.lang.reflect.Method getMethod()
                                   throws java.lang.NoSuchMethodException
Lazy loads the method

Throws:
java.lang.NoSuchMethodException

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>

eval

public R eval(java.lang.Object... args)
Description copied from class: BinaryFunctor
Executes the function and returns the result.

Overrides:
eval 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 Functor<R>

toString

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


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