Class ReflectionMethodInvoker<T,​R>

  • Type Parameters:
    T - The class type that has the method to be invoked.
    R - The expected return type of the method invocation.

    public class ReflectionMethodInvoker<T,​R>
    extends Object
    This class acts as a proxy to invoke a specific method on objects of a specific class. It will use the JDK's reflection library to find and invoke the method.

    The relatively expensive call to find the correct method on the class is lazy and will not be performed until the first invocation or until the invoker is explicitly initialized. Once found, the method is cached so repeated calls to initialize() or invoke() will not incur the reflection cost of searching for the method on the class.

    Example: ReflectionMethodInvoker<String, Integer> invoker = new ReflectionMethodInvoker<String, Integer>(String.class, Integer.class, "indexOf", String.class, int.class); invoker.invoke("ababab", "ab", 1); // This is equivalent to calling "ababab".indexOf("ab", 1);

    • Constructor Detail

      • ReflectionMethodInvoker

        public ReflectionMethodInvoker​(Class<T> clazz,
                                       Class<R> returnType,
                                       String methodName,
                                       Class<?>... parameterTypes)
        Construct an instance of ReflectionMethodInvoker.

        This constructor will not make any reflection calls as part of initialization; i.e. no validation of the existence of the given method signature will occur.

        Parameters:
        clazz - The class that has the method to be invoked.
        returnType - The expected return class of the method invocation. The object returned by the invocation will be cast to this class.
        methodName - The name of the method to invoke.
        parameterTypes - The classes of the parameters of the method to invoke.
    • Method Detail

      • invoke

        public R invoke​(T obj,
                        Object... args)
                 throws NoSuchMethodException
        Attempt to invoke the method this proxy targets for on the given object with the given arguments. If the invoker has not yet been initialized, an attempt to initialize the invoker will be made first. If the call succeeds the invoker will be in an initialized state after this call and future calls to the same method will not incur an initialization cost. If the call fails because the target method could not be found, the invoker will remain in an uninitialized state after the exception is thrown.
        Parameters:
        obj - The object to invoke the method on.
        args - The arguments to pass to the method. These arguments must match the signature of the method.
        Returns:
        The returned value of the method cast to the 'returnType' class that this proxy was initialized with.
        Throws:
        NoSuchMethodException - if the JVM could not find a method matching the signature specified in the initialization of this proxy.
        RuntimeException - if any other exception is thrown when attempting to invoke the method or by the method itself. The cause of this exception will be the exception that was actually thrown.
      • initialize

        public void initialize()
                        throws NoSuchMethodException
        Initializes the method invoker by finding and caching the target method from the target class that will be used for subsequent invoke operations. If the invoker is already initialized this call will do nothing.
        Throws:
        NoSuchMethodException - if the JVM could not find a method matching the signature specified in the initialization of this proxy.
      • isInitialized

        public boolean isInitialized()
        Gets the initialization state of the invoker.
        Returns:
        true if the invoker has been initialized and the method has been cached for invocation; otherwise false.