info.javelot.functionalj.reflect
Interface StdReflect

All Known Implementing Classes:
CglibStdReflect, JdkStdReflect, StdReflectImpl

public interface StdReflect

This interface represents the ability to produce functions by using reflection.

Specifying vs. not specifying the types of the parameters

There are two mechanisms for reflection in FunctionalJ: one that requires the types and the number of the parameters, and one that does not. This interface defines "standard" reflection, where the types of the parameters must be specified. The interface that defines "dynamic" reflection, where it is not necessary to provide the types of the parameters, is the DynReflect interface.

Using the version that requires the types of the parameters ensures that exactly one constructor or method is targetted by the produced function. This improves performance, at the cost of the need for the developer to specify the types of the parameters. Another consequence is that if no suitable constructor or method was found, an exception is thrown immediately - making it a fail-fast mechanism.

On the other hand, using the mechanism that does not require the types of the parameters produces a function that will determine dynamically, according to the number and types of the parameters supplied when calling the function, which constructor or method to invoke. This has an impact on performance, in exchange for the convenience of not having to specify the types of the parameters.

Using the dynamic reflection mechanism also dynamically keeps track of the possible target constructors or methods when invoking the other methods of the FunctionN interface.

Finally, with dynamic reflection, an exception is thrown from the method that produces the function only if there is no possible constructor or method. If there is at least one possibility, no exception is thrown here; however, an exception could still be thrown if the types of the parameters that are supplied when the function is called do not match any of the possible constructors or methods, or if there are no possible targets when invoking the other methods of the FunctionN interface.

Types of reflection

Functions are produced from three types of reflection:

Constructors

Targetting a constructor by reflection will produce a function that will return an instance of the target class. The parameters to the function will match those of the target constructor.

Instance methods

An instance method needs an object on which to invoke the method. Therefore, the produced function accepts one more parameter than the number of parameters to the method. That first parameter is the object on which to invoke the method.

If the object on which to invoke the method is known when creating the function using reflection, it can be specified directly to the version of the instanceFunction method which accepts an Object parameter instead of a Class parameter. In that case, the number of parameters expected by produced function will match those of the target instance method, since the object on which to invoke the method is already specified.

Static methods

Static methods do not use an object on which to invoke the method. Therefore, the parameters of the produced function will match those of the target static method.

Author:
Copyright © 2006 Frederic Daoud

Method Summary
 FunctionN constructor(Class p_class, Class... p_types)
          Produces a function that creates an instance of the specified class, using the constructor that accepts parameters of the given types.
 FunctionN constructor(String p_class, Class... p_types)
          Version of constructor(Class,Class...) that accepts a String to specify the class by name.
 FunctionN instanceFunction(Class p_class, String p_name, Class... p_types)
          Produces a function that invokes the instance method of specified class which has the specified name and accepts parameters of the given types.
 FunctionN instanceFunction(Object p_object, String p_name, Class... p_types)
          Produces a function that invokes the instance method on the provided object which has the specified name and accepts parameters of the given types.
 FunctionN instanceFunction(String p_class, String p_name, Class... p_types)
          Version of instanceFunction(Class,String,Class...) that accepts a String to specify the class by name.
 FunctionN staticFunction(Class p_class, String p_name, Class... p_types)
          Produces a function that invokes the static method of specified class which has the specified name and accepts parameters of the given types.
 FunctionN staticFunction(String p_class, String p_name, Class... p_types)
          Version of staticFunction(Class,String,Class...) that accepts a String to specify the class by name.
 

Method Detail

constructor

FunctionN constructor(Class p_class,
                      Class... p_types)
                      throws FunctionException
Produces a function that creates an instance of the specified class, using the constructor that accepts parameters of the given types.

Parameters:
p_class - an instance of this class will be created by the function.
p_types - the number and types of the parameters to the constructor.
Returns:
a function that passes its parameters to the constructor and returns an instance of the class.
Throws:
FunctionException - thrown if a matching constructor was not found.

constructor

FunctionN constructor(String p_class,
                      Class... p_types)
                      throws FunctionException
Version of constructor(Class,Class...) that accepts a String to specify the class by name.

Throws:
FunctionException

instanceFunction

FunctionN instanceFunction(Class p_class,
                           String p_name,
                           Class... p_types)
                           throws FunctionException
Produces a function that invokes the instance method of specified class which has the specified name and accepts parameters of the given types.

Parameters:
p_class - the class which contains the instance method to be invoked by the produced function.
p_name - the name of the method.
p_types - the number and types of the parameters to the instance method.
Returns:
a function that invokes the method on the object that is specified as its first parameter, passing the rest of its parameters to the method and returning the result of the invocation.
Throws:
FunctionException - thrown if a matching instance method was not found.

instanceFunction

FunctionN instanceFunction(String p_class,
                           String p_name,
                           Class... p_types)
                           throws FunctionException
Version of instanceFunction(Class,String,Class...) that accepts a String to specify the class by name.

Throws:
FunctionException

instanceFunction

FunctionN instanceFunction(Object p_object,
                           String p_name,
                           Class... p_types)
                           throws FunctionException
Produces a function that invokes the instance method on the provided object which has the specified name and accepts parameters of the given types.

Parameters:
p_object - the object on which the instance method will be invoked by the produced function.
p_name - the name of the method.
p_types - the number and types of the parameters to the instance method.
Returns:
a function that invokes the method on the object, passing its parameters to the method and returning the result.
Throws:
FunctionException - thrown if a matching instance method was not found.

staticFunction

FunctionN staticFunction(Class p_class,
                         String p_name,
                         Class... p_types)
                         throws FunctionException
Produces a function that invokes the static method of specified class which has the specified name and accepts parameters of the given types.

Parameters:
p_class - the class which contains the static method to be invoked by the produced function.
p_name - the name of the method.
p_types - the number and types of the parameters to the static method.
Returns:
a function that passes its parameters to the static method and returns the result of invoking it.
Throws:
FunctionException - thrown if a matching static method was not found.

staticFunction

FunctionN staticFunction(String p_class,
                         String p_name,
                         Class... p_types)
                         throws FunctionException
Version of staticFunction(Class,String,Class...) that accepts a String to specify the class by name.

Throws:
FunctionException