T
- the type of the object the method might be on, typically an interface or base classclass OptionalMethod<T>
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
methodName |
private java.lang.Class[] |
methodParams |
private java.lang.Class<?> |
returnType
The return type of the method.
|
Constructor and Description |
---|
OptionalMethod(java.lang.Class<?> returnType,
java.lang.String methodName,
java.lang.Class... methodParams)
Creates an optional method.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.reflect.Method |
getMethod(java.lang.Class<?> clazz)
Perform a lookup for the method.
|
private static java.lang.reflect.Method |
getPublicMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class[] parameterTypes) |
java.lang.Object |
invoke(T target,
java.lang.Object... args)
Invokes the method on
target with args . |
java.lang.Object |
invokeOptional(T target,
java.lang.Object... args)
Invokes the method on
target with args . |
java.lang.Object |
invokeOptionalWithoutCheckedException(T target,
java.lang.Object... args)
Invokes the method on
target . |
java.lang.Object |
invokeWithoutCheckedException(T target,
java.lang.Object... args)
Invokes the method on
target . |
boolean |
isSupported(T target)
Returns true if the method exists on the supplied
target . |
private final java.lang.Class<?> returnType
private final java.lang.String methodName
private final java.lang.Class[] methodParams
OptionalMethod(java.lang.Class<?> returnType, java.lang.String methodName, java.lang.Class... methodParams)
returnType
- the return type to required, null if it does not mattermethodName
- the name of the methodmethodParams
- the method parameter typespublic boolean isSupported(T target)
target
.public java.lang.Object invokeOptional(T target, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException
target
with args
. If the method does not exist or is not
public then null
is returned. See also invokeOptionalWithoutCheckedException(T, java.lang.Object...)
.java.lang.IllegalArgumentException
- if the arguments are invalidjava.lang.reflect.InvocationTargetException
- if the invocation throws an exceptionpublic java.lang.Object invokeOptionalWithoutCheckedException(T target, java.lang.Object... args)
target
. If the method does not exist or is not public then
null
is returned. Any RuntimeException thrown by the method is thrown, checked
exceptions are wrapped in an AssertionError
.java.lang.IllegalArgumentException
- if the arguments are invalidpublic java.lang.Object invoke(T target, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException
target
with args
. Throws an error if the method is not
supported. See also invokeWithoutCheckedException(Object, Object...)
.java.lang.IllegalArgumentException
- if the arguments are invalidjava.lang.reflect.InvocationTargetException
- if the invocation throws an exceptionpublic java.lang.Object invokeWithoutCheckedException(T target, java.lang.Object... args)
target
. Throws an error if the method is not supported. Any
RuntimeException thrown by the method is thrown, checked exceptions are wrapped in an AssertionError
.java.lang.IllegalArgumentException
- if the arguments are invalidprivate java.lang.reflect.Method getMethod(java.lang.Class<?> clazz)
OptionalMethod
was created. If the return
type is specified (i.e. non-null) it must also be compatible. The method must also be public.private static java.lang.reflect.Method getPublicMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)