info.javelot.functionalj
Class ReflectionFunction

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

public abstract class ReflectionFunction
extends Function

Represents a function that uses reflection. If you do not want to use reflection, you can implement the IFunctionN interface and use FunctionN to create a function object that uses your implementation.

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 ReflectionFunction()
          Default constructor.
protected ReflectionFunction(Class p_class)
          Constructor used by ConstructorFunction, since no name is required.
protected ReflectionFunction(Class p_class, Object[] p_parameters)
          Constructor used by ConstructorFunction, since no name is required.
protected ReflectionFunction(Class p_class, String p_name)
          Constructor used by MethodFunction, since a name is required.
protected ReflectionFunction(Class p_class, String p_name, Object[] p_parameters)
          Constructor used by MethodFunction, since a name is required.
protected ReflectionFunction(Class p_class, String p_name, Object p_object, Object[] p_parameters)
          Creates a function that uses reflection to target the method of the name and class, to be invoked on the given object (if applicable), with the specified parameters.
 
Method Summary
 Object call()
          Calls the function by invoking the target using the parameters that have been previously supplied (if any) and returns the result.
protected abstract  Object[] getAllTargets(Class p_class)
          Method for subclasses to specify the targets of the class, which are constructors or methods.
protected abstract  int getInsertIndex(Object p_target)
          Method for subclasses to specify the index at which to insert the target into the list of possible targets, according to priority.
protected abstract  Class[] getParameterTypes(Object p_target)
          Method for subclasses to specify the parameter types of the target, which can be a constructor or a method.
protected abstract  Object invoke(Object p_target, Object p_object, Object[] p_parameters)
          Method to indicate to subclasses to invoke the given target, on the given object (if applicable), and with the given parameters.
 
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

ReflectionFunction

protected ReflectionFunction()
Default constructor.


ReflectionFunction

protected ReflectionFunction(Class p_class)
                      throws FunctionException
Constructor used by ConstructorFunction, since no name is required. See {#ReflectionFunction(Class,String,Object,Object[]) for more information.

See Also:
ReflectionFunction(Class,String,Object,Object[]).

ReflectionFunction

protected ReflectionFunction(Class p_class,
                             Object[] p_parameters)
                      throws FunctionException
Constructor used by ConstructorFunction, since no name is required. See {#ReflectionFunction(Class,String,Object,Object[]) for more information.

See Also:
ReflectionFunction(Class,String,Object,Object[]).

ReflectionFunction

protected ReflectionFunction(Class p_class,
                             String p_name)
                      throws FunctionException
Constructor used by MethodFunction, since a name is required. See {#ReflectionFunction(Class,String,Object,Object[]) for more information.

See Also:
ReflectionFunction(Class,String,Object,Object[]).

ReflectionFunction

protected ReflectionFunction(Class p_class,
                             String p_name,
                             Object[] p_parameters)
                      throws FunctionException
Constructor used by MethodFunction, since a name is required. See {#ReflectionFunction(Class,String,Object,Object[]) for more information.

See Also:
ReflectionFunction(Class,String,Object,Object[]).

ReflectionFunction

protected ReflectionFunction(Class p_class,
                             String p_name,
                             Object p_object,
                             Object[] p_parameters)
                      throws FunctionException
Creates a function that uses reflection to target the method of the name and class, to be invoked on the given object (if applicable), with the specified parameters.

Parameters:
p_class - the target class of the function.
p_name - the name of the method to be invoked on the target class.
p_object - the object on which to invoke the method, in the case of an InstanceFunction.
p_parameters - the method parameters.
Throws:
FunctionException - thrown if no possible target methods satisfying the given conditions were found.
Method Detail

getInsertIndex

protected abstract int getInsertIndex(Object p_target)
Method for subclasses to specify the index at which to insert the target into the list of possible targets, according to priority. Targets at the beginning of the list are considered first.

Parameters:
p_target - the constructor or method for which to obtain the index.
Returns:
the index at which to insert the target into the list, or -1 if the target is not to be inserted into the list.

getAllTargets

protected abstract Object[] getAllTargets(Class p_class)
Method for subclasses to specify the targets of the class, which are constructors or methods.

Parameters:
p_class - the class for which to specify the targets.

getParameterTypes

protected abstract Class[] getParameterTypes(Object p_target)
Method for subclasses to specify the parameter types of the target, which can be a constructor or a method. Both Constructor and Method have a getParameterTypes() method, but this method is not present in their common parent, AccessibleObject.

Parameters:
p_target - the constructor or method for which to obtain the parameter types.
Returns:
the types of the parameters of the target.

invoke

protected abstract Object invoke(Object p_target,
                                 Object p_object,
                                 Object[] p_parameters)
                          throws FunctionException
Method to indicate to subclasses to invoke the given target, on the given object (if applicable), and with the given parameters.

Parameters:
p_target - the target to invoke.
p_object - the object on which to invoke the target, if applicable.
p_parameters - the parameters to use when invoking the target.
Returns:
the result of invoking the target, which is the object created if the target is a constructor, or the returned value if the target is a method.
Throws:
FunctionException - thrown if an exception occurs when invoking the target.

call

public Object call()
            throws FunctionException
Calls the function by invoking the target using the parameters that have been previously supplied (if any) and returns the result. If an exception is thrown by the target call, it is wrapped into a FunctionException.

Specified by:
call in class Function
Throws:
FunctionException - thrown if an exception occurs when invoking the target, if an object on which to invoke the target is required but none has been supplied, or if all of the potential targets expect more parameters than have been supplied.