T
- the type that owns this method or constructor.R
- the return type of (or supertype thereof) the method or the declaring type of the
constructor.public abstract class Invokable<T,R>
extends java.lang.Object
implements java.lang.reflect.AnnotatedElement, java.lang.reflect.Member
Method
or a Constructor
. Convenience API is provided to
make common reflective operation easier to deal with, such as isPublic()
, getParameters()
etc.
In addition to convenience methods, TypeToken.method(java.lang.reflect.Method)
and TypeToken.constructor(java.lang.reflect.Constructor<?>)
will resolve the type parameters of the method or constructor in the context of the owner type,
which may be a subtype of the declaring class. For example:
Method getMethod = List.class.getMethod("get", int.class);
Invokable<List<String>, ?> invokable = new TypeToken<List<String>>() {}.method(getMethod);
assertEquals(TypeToken.of(String.class), invokable.getReturnType()); // Not Object.class!
assertEquals(new TypeToken<List<String>>() {}, invokable.getOwnerType());
Note: earlier versions of this class inherited from AccessibleObject
and GenericDeclaration
. Since version 31.0 that is no longer
the case. However, most methods from those types are present with the same signature in this
class.
AccessibleObject
or GenericDeclaration
since
31.0)Modifier and Type | Class and Description |
---|---|
(package private) static class |
Invokable.ConstructorInvokable<T> |
(package private) static class |
Invokable.MethodInvokable<T> |
Modifier and Type | Field and Description |
---|---|
private java.lang.reflect.AccessibleObject |
accessibleObject |
private java.lang.reflect.Member |
member |
Constructor and Description |
---|
Invokable(M member) |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object obj) |
static <T> Invokable<T,T> |
from(java.lang.reflect.Constructor<T> constructor)
Returns
Invokable of constructor . |
static Invokable<?,java.lang.Object> |
from(java.lang.reflect.Method method)
Returns
Invokable of method . |
(package private) abstract java.lang.reflect.AnnotatedType[] |
getAnnotatedParameterTypes() |
abstract java.lang.reflect.AnnotatedType |
getAnnotatedReturnType() |
<A extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.Class<A> annotationClass) |
java.lang.annotation.Annotation[] |
getAnnotations() |
java.lang.annotation.Annotation[] |
getDeclaredAnnotations() |
java.lang.Class<? super T> |
getDeclaringClass() |
ImmutableList<TypeToken<? extends java.lang.Throwable>> |
getExceptionTypes()
Returns all declared exception types of this
Invokable . |
(package private) abstract java.lang.reflect.Type[] |
getGenericExceptionTypes()
This should never return a type that's not a subtype of Throwable.
|
(package private) abstract java.lang.reflect.Type[] |
getGenericParameterTypes() |
(package private) abstract java.lang.reflect.Type |
getGenericReturnType() |
int |
getModifiers() |
java.lang.String |
getName() |
TypeToken<T> |
getOwnerType()
Returns the type of
T . |
(package private) abstract java.lang.annotation.Annotation[][] |
getParameterAnnotations() |
ImmutableList<Parameter> |
getParameters()
Returns all declared parameters of this
Invokable . |
TypeToken<? extends R> |
getReturnType()
Returns the return type of this
Invokable . |
abstract java.lang.reflect.TypeVariable<?>[] |
getTypeParameters()
See
GenericDeclaration.getTypeParameters() . |
int |
hashCode() |
R |
invoke(T receiver,
java.lang.Object... args)
Invokes with
receiver as 'this' and args passed to the underlying method and
returns the return value; or calls the underlying constructor with args and returns the
constructed instance. |
(package private) abstract java.lang.Object |
invokeInternal(java.lang.Object receiver,
java.lang.Object[] args) |
boolean |
isAbstract()
Returns true if the method is abstract.
|
boolean |
isAccessible()
See
AccessibleObject.isAccessible() . |
boolean |
isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass) |
boolean |
isFinal()
Returns
true if this method is final, per Modifier.isFinal(getModifiers()) . |
boolean |
isNative()
Returns true if the element is native.
|
abstract boolean |
isOverridable()
Returns
true if this is an overridable method. |
boolean |
isPackagePrivate()
Returns true if the element is package-private.
|
boolean |
isPrivate()
Returns true if the element is private.
|
boolean |
isProtected()
Returns true if the element is protected.
|
boolean |
isPublic()
Returns true if the element is public.
|
boolean |
isStatic()
Returns true if the element is static.
|
boolean |
isSynchronized()
Returns true if the method is synchronized.
|
boolean |
isSynthetic() |
(package private) boolean |
isTransient()
Returns true if the field is transient.
|
abstract boolean |
isVarArgs()
Returns
true if this was declared to take a variable number of arguments. |
(package private) boolean |
isVolatile()
Returns true if the field is volatile.
|
<R1 extends R> |
returning(java.lang.Class<R1> returnType)
Explicitly specifies the return type of this
Invokable . |
<R1 extends R> |
returning(TypeToken<R1> returnType)
Explicitly specifies the return type of this
Invokable . |
void |
setAccessible(boolean flag)
See
AccessibleObject.setAccessible(boolean) . |
java.lang.String |
toString() |
boolean |
trySetAccessible()
See
java.lang.reflect.AccessibleObject#trySetAccessible() . |
private final java.lang.reflect.AccessibleObject accessibleObject
private final java.lang.reflect.Member member
public static Invokable<?,java.lang.Object> from(java.lang.reflect.Method method)
Invokable
of method
.public static <T> Invokable<T,T> from(java.lang.reflect.Constructor<T> constructor)
Invokable
of constructor
.public final boolean isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
isAnnotationPresent
in interface java.lang.reflect.AnnotatedElement
@CheckForNull public final <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.Class<A> annotationClass)
getAnnotation
in interface java.lang.reflect.AnnotatedElement
public final java.lang.annotation.Annotation[] getAnnotations()
getAnnotations
in interface java.lang.reflect.AnnotatedElement
public final java.lang.annotation.Annotation[] getDeclaredAnnotations()
getDeclaredAnnotations
in interface java.lang.reflect.AnnotatedElement
public abstract java.lang.reflect.TypeVariable<?>[] getTypeParameters()
GenericDeclaration.getTypeParameters()
.public final void setAccessible(boolean flag)
AccessibleObject.setAccessible(boolean)
.public final boolean trySetAccessible()
java.lang.reflect.AccessibleObject#trySetAccessible()
.public final boolean isAccessible()
AccessibleObject.isAccessible()
.public final java.lang.String getName()
getName
in interface java.lang.reflect.Member
public final int getModifiers()
getModifiers
in interface java.lang.reflect.Member
public final boolean isSynthetic()
isSynthetic
in interface java.lang.reflect.Member
public final boolean isPublic()
public final boolean isProtected()
public final boolean isPackagePrivate()
public final boolean isPrivate()
public final boolean isStatic()
public final boolean isFinal()
true
if this method is final, per Modifier.isFinal(getModifiers())
.
Note that a method may still be effectively "final", or non-overridable when it has no
final
keyword. For example, it could be private, or it could be declared by a final
class. To tell whether a method is overridable, use isOverridable()
.
public final boolean isAbstract()
public final boolean isNative()
public final boolean isSynchronized()
final boolean isVolatile()
final boolean isTransient()
public boolean equals(@CheckForNull java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public abstract boolean isOverridable()
true
if this is an overridable method. Constructors, private, static or final
methods, or methods declared by final classes are not overridable.public abstract boolean isVarArgs()
true
if this was declared to take a variable number of arguments.@CheckForNull public final R invoke(@CheckForNull T receiver, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
receiver
as 'this' and args
passed to the underlying method and
returns the return value; or calls the underlying constructor with args
and returns the
constructed instance.java.lang.IllegalAccessException
- if this Constructor
object enforces Java language access
control and the underlying method or constructor is inaccessible.java.lang.IllegalArgumentException
- if the number of actual and formal parameters differ; if an
unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a
parameter value cannot be converted to the corresponding formal parameter type by a method
invocation conversion.java.lang.reflect.InvocationTargetException
- if the underlying method or constructor throws an exception.public final TypeToken<? extends R> getReturnType()
Invokable
.public final ImmutableList<Parameter> getParameters()
Invokable
. Note that if this is a constructor
of a non-static inner class, unlike Constructor.getParameterTypes()
, the hidden this
parameter of the enclosing class is excluded from the returned parameters.public final ImmutableList<TypeToken<? extends java.lang.Throwable>> getExceptionTypes()
Invokable
.public final <R1 extends R> Invokable<T,R1> returning(java.lang.Class<R1> returnType)
Invokable
. For example:
Method factoryMethod = Person.class.getMethod("create");
Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);
public final <R1 extends R> Invokable<T,R1> returning(TypeToken<R1> returnType)
Invokable
.public final java.lang.Class<? super T> getDeclaringClass()
getDeclaringClass
in interface java.lang.reflect.Member
@CheckForNull abstract java.lang.Object invokeInternal(@CheckForNull java.lang.Object receiver, java.lang.Object[] args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
abstract java.lang.reflect.Type[] getGenericParameterTypes()
abstract java.lang.reflect.AnnotatedType[] getAnnotatedParameterTypes()
abstract java.lang.reflect.Type[] getGenericExceptionTypes()
abstract java.lang.annotation.Annotation[][] getParameterAnnotations()
abstract java.lang.reflect.Type getGenericReturnType()
public abstract java.lang.reflect.AnnotatedType getAnnotatedReturnType()