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
The relatively expensive call to find the correct method on the class is lazy and will not be performed until the first invocation. The result of that getMethod() call is cached for subsequent invocations. If a NoSuchMethodException is thrown, the exception will be cached instead and all subsequent calls to invoke will immediately throw the cached exception.
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 and Description |
---|
ReflectionMethodInvoker(Class<T> clazz,
Class<R> returnType,
String methodName,
Class<?>... parameterTypes)
Construct an instance of
ReflectionMethodInvoker . |
Modifier and Type | Method and Description |
---|---|
R |
invoke(T obj,
Object... args)
Attempt to invoke the method this proxy was initialized for on the given object with the given arguments.
|
public ReflectionMethodInvoker(Class<T> clazz, Class<R> returnType, String methodName, Class<?>... parameterTypes)
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.
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.public R invoke(T obj, Object... args) throws NoSuchMethodException
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.NoSuchMethodException
- if the JVM could not find a method matching the signature specified in the
initialization of this proxy.SdkClientException
- 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.Copyright © 2019. All rights reserved.