info.javelot.functionalj
Class MethodFunction

java.lang.Object
  extended byinfo.javelot.functionalj.Function
      extended byinfo.javelot.functionalj.ReflectionFunction
          extended byinfo.javelot.functionalj.MethodFunction
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ComposedFunction, InstanceFunction, StaticFunction

public abstract class MethodFunction
extends ReflectionFunction

Represents a function that targets a method by name, either a static method (StaticFunction) or an instance method (InstanceFunction). In the latter case, an object is required on which to invoke the method.

Name matching:

Method names are matched either exactly or by interpreting the name as a property and matching on the corresponding getter and setter methods. Thus, for the name "person", besides exact matches, the methods named "getPerson", "isPerson" and "setPerson" will also be considered matches. Exact matches have priority in the list of possible targets.

Author:
Copyright © 2006 Frederic Daoud
See Also:
Serialized Form

Field Summary
 
Fields inherited from class info.javelot.functionalj.Function
m_class, m_name, m_object, m_parameters, m_targets
 
Constructor Summary
protected MethodFunction()
          Default constructor which is only used by ComposedFunction.
protected MethodFunction(Class p_class, String p_name)
          Creates a function that targets the methods of the given class which match the given name.
protected MethodFunction(Class p_class, String p_name, Object[] p_parameters)
          Creates a function that targets the methods of the given class which match the given name and parameters.
protected MethodFunction(Class p_class, String p_name, Object p_object, Object[] p_parameters)
          Creates a function that targets the methods of the given class which match the given name and parameters, to be invoked on the given object.
 
Method Summary
protected  Object[] getAllTargets(Class p_class)
          Returns the methods of the target class and its superclasses.
protected  int getInsertIndex(Object p_target)
          Determines if the given method's name matches the target name, and returns the index at which to insert the method accordingly.
protected  Class[] getParameterTypes(Object p_target)
          Returns the types of the parameters accepted by the method.
protected  Object invoke(Object p_target, Object p_object, Object[] p_parameters)
          Invokes the Method.invoke(Object,Object[]) method using the supplied object, which may be null in the case of a static method, and the supplied parameters.
 
Methods inherited from class info.javelot.functionalj.ReflectionFunction
call
 
Methods inherited from class info.javelot.functionalj.Function
addParameter, addParameters, addParameters, addParameters, addParameters, addParameters, getName, getObject, getParameters, getTargetClass, getTargets, isObjectPresent, isObjectRequired, newInstance, object, objectOrParameter, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MethodFunction

protected MethodFunction()
Default constructor which is only used by ComposedFunction.


MethodFunction

protected MethodFunction(Class p_class,
                         String p_name)
                  throws FunctionException
Creates a function that targets the methods of the given class which match the given name.

Parameters:
p_class - the class of the target.
p_name - the name to match, either exactly or by interpreting the name as a property and matching on the corresponding getter and setter methods.
Throws:
FunctionException - thrown if no methods of the target class match the given name.

MethodFunction

protected MethodFunction(Class p_class,
                         String p_name,
                         Object[] p_parameters)
Creates a function that targets the methods of the given class which match the given name and parameters.

Parameters:
p_class - the class of the target.
p_name - the name to match, either exactly or by interpreting the name as a property and matching on the corresponding getter and setter methods.
p_parameters - the parameters to be supplied to the method.
Throws:
FunctionException - thrown if no methods of the target class match the given name and parameters.

MethodFunction

protected MethodFunction(Class p_class,
                         String p_name,
                         Object p_object,
                         Object[] p_parameters)
Creates a function that targets the methods of the given class which match the given name and parameters, to be invoked on the given object. Thus, this constructor is used by InstanceFunction only.

Parameters:
p_class - the class of the target.
p_name - the name to match, either exactly or by interpreting the name as a property and matching on the corresponding getter and setter methods.
p_parameters - the parameters to be supplied to the method.
Throws:
FunctionException - thrown if no methods of the target class match the given name and parameters.
Method Detail

getAllTargets

protected Object[] getAllTargets(Class p_class)
Returns the methods of the target class and its superclasses.

Specified by:
getAllTargets in class ReflectionFunction
Parameters:
p_class - the target class.
Returns:
the methods of the target class and its superclasses.

getParameterTypes

protected Class[] getParameterTypes(Object p_target)
Returns the types of the parameters accepted by the method.

Specified by:
getParameterTypes in class ReflectionFunction
Parameters:
p_target - the target method for which to determine the types of the parameters.
Returns:
the types of the parameters accepted by the method.

getInsertIndex

protected int getInsertIndex(Object p_target)
Determines if the given method's name matches the target name, and returns the index at which to insert the method accordingly.

Specified by:
getInsertIndex in class ReflectionFunction
Parameters:
p_target - the method for which to obtain the index.
Returns:
the index at which to insert the target into the list, which is 0 in the case of an exact match; the end of the list in the case of a match on a getter or setter; or -1 if the method's name does not match the target name.

invoke

protected Object invoke(Object p_target,
                        Object p_object,
                        Object[] p_parameters)
                 throws FunctionException
Invokes the Method.invoke(Object,Object[]) method using the supplied object, which may be null in the case of a static method, and the supplied parameters.

Specified by:
invoke in class ReflectionFunction
Parameters:
p_target - the target method to invoke.
p_object - the object on which to invoke the method, or null if the target method is static.
p_parameters - the parameters to pass to the method.
Returns:
the result returned by the method.
Throws:
FunctionException - thrown if an exception occurs when invoking the method.