org.spicefactory.cinnamon.reflect
Class MethodInvoker

java.lang.Object
  extended by org.spicefactory.cinnamon.reflect.MethodInvoker

public class MethodInvoker
extends Object

MethodInvoker instances allow the reflective invocation of Methods with automatic parameter conversion. Instances of MethodInvoker will be created with ReflectionFactory instances. For parameter conversion they will use any Converter instances registered in that factory. Like all classes in this package this class takes into account the "context" of the method invocation. That means that it will resolve (if possible) any TypeVariables of the declaring class of the method from the perspective of the context class. Example: Consider a generic class MyClass<E> with a method setProperty(E value). If you invoke this method on a subclass like MySubclass extends MyClass<String> that does not override the setProperty method, normal reflection would still report Object (the erased type) for the parameter type, which is not accurate. This class will correctly report String as the parameter type and use this type information for any required parameter conversions.

Author:
Jens Halm

Method Summary
 Method getMethod()
          Returns the Method that this MethodInvoker wraps.
 int getParameterCount()
          Returns the number of parameters this MethodInvoker expects.
 int getRank(Object[] methodParams)
          Returns a numeric rank for the given method parameters.
 Object invoke(Object target, Object[] methodParams)
          Invokes this method on the given target instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getMethod

public Method getMethod()
Returns the Method that this MethodInvoker wraps.

Returns:
the Method that this MethodInvoker wraps

getRank

public int getRank(Object[] methodParams)
Returns a numeric rank for the given method parameters. This method can be useful if you have to deal with overloaded methods and want to find out for which one the given array of parameters is the best match. This method will return 0 if the given parameters are not applicable and trying to use them with this MethodInvoker will likely result in an Exception. Otherwise a positive int will be returned, the higher the more closely the parameters match. A parameter that has the same type like as the required method parameter will score higher than parameters that need to be converted first.

Parameters:
methodParams - the method parameters to analyze
Returns:
a positive integer or 0 depending on how closely the given parameters match the required parameters

getParameterCount

public int getParameterCount()
Returns the number of parameters this MethodInvoker expects.

Returns:
the number of parameters this MethodInvoker expects

invoke

public Object invoke(Object target,
                     Object[] methodParams)
              throws MethodInvocationException
Invokes this method on the given target instance. Any required parameter conversion will be done before invoking the method using any Converter instances registered in the ReflectionFactory at the time this MethodInvoker was created.

Parameters:
target - the target instance this method should be invoked on
methodParams - the parameters to be passed to the method
Returns:
the value returned by the target method
Throws:
MethodInvocationException - if the method cannot be invoked, the target method threw an Exception or parameter conversion failed