Class Invokable<T,R>
- Type Parameters:
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.
- All Implemented Interfaces:
AnnotatedElement
,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.
- Since:
- 14.0 (no longer implements
AccessibleObject
orGenericDeclaration
since 31.0) - Author:
- Ben Yu
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
static <T> Invokable
<T, T> from
(Constructor<T> constructor) ReturnsInvokable
ofconstructor
.ReturnsInvokable
ofmethod
.abstract AnnotatedType
Returns theAnnotatedType
for the return type.final <A extends Annotation>
@Nullable AgetAnnotation
(Class<A> annotationClass) final Annotation[]
final Annotation[]
final ImmutableList
<TypeToken<? extends Throwable>> Returns all declared exception types of thisInvokable
.final int
final String
getName()
Returns the type ofT
.final ImmutableList
<Parameter> Returns all declared parameters of thisInvokable
.Returns the return type of thisInvokable
.abstract TypeVariable<?>[]
int
hashCode()
Invokes withreceiver
as 'this' andargs
passed to the underlying method and returns the return value; or calls the underlying constructor withargs
and returns the constructed instance.final boolean
Returns true if the method is abstract.final boolean
final boolean
isAnnotationPresent
(Class<? extends Annotation> annotationClass) final boolean
isFinal()
Returnstrue
if this method is final, perModifier.isFinal(getModifiers())
.final boolean
isNative()
Returns true if the element is native.abstract boolean
Returnstrue
if this is an overridable method.final boolean
Returns true if the element is package-private.final boolean
Returns true if the element is private.final boolean
Returns true if the element is protected.final boolean
isPublic()
Returns true if the element is public.final boolean
isStatic()
Returns true if the element is static.final boolean
Returns true if the method is synchronized.final boolean
abstract boolean
Returnstrue
if this was declared to take a variable number of arguments.Explicitly specifies the return type of thisInvokable
.Explicitly specifies the return type of thisInvokable
.final void
setAccessible
(boolean flag) toString()
final boolean
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.reflect.AnnotatedElement
getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByType
Methods inherited from interface java.lang.reflect.Member
accessFlags
-
Method Details
-
from
-
from
ReturnsInvokable
ofconstructor
. -
isAnnotationPresent
- Specified by:
isAnnotationPresent
in interfaceAnnotatedElement
-
getAnnotation
- Specified by:
getAnnotation
in interfaceAnnotatedElement
-
getAnnotations
- Specified by:
getAnnotations
in interfaceAnnotatedElement
-
getDeclaredAnnotations
- Specified by:
getDeclaredAnnotations
in interfaceAnnotatedElement
-
getTypeParameters
-
setAccessible
-
trySetAccessible
-
isAccessible
-
getName
-
getModifiers
- Specified by:
getModifiers
in interfaceMember
-
isSynthetic
- Specified by:
isSynthetic
in interfaceMember
-
isPublic
Returns true if the element is public. -
isProtected
Returns true if the element is protected. -
isPackagePrivate
Returns true if the element is package-private. -
isPrivate
Returns true if the element is private. -
isStatic
Returns true if the element is static. -
isFinal
Returnstrue
if this method is final, perModifier.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, useisOverridable()
. -
isAbstract
Returns true if the method is abstract. -
isNative
Returns true if the element is native. -
isSynchronized
Returns true if the method is synchronized. -
equals
-
hashCode
-
toString
-
isOverridable
Returnstrue
if this is an overridable method. Constructors, private, static or final methods, or methods declared by final classes are not overridable. -
isVarArgs
Returnstrue
if this was declared to take a variable number of arguments. -
invoke
@CanIgnoreReturnValue public final @Nullable R invoke(@Nullable T receiver, @Nullable Object... args) throws InvocationTargetException, IllegalAccessException Invokes withreceiver
as 'this' andargs
passed to the underlying method and returns the return value; or calls the underlying constructor withargs
and returns the constructed instance.- Throws:
IllegalAccessException
- if thisConstructor
object enforces Java language access control and the underlying method or constructor is inaccessible.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.InvocationTargetException
- if the underlying method or constructor throws an exception.
-
getReturnType
Returns the return type of thisInvokable
. -
getParameters
Returns all declared parameters of thisInvokable
. Note that if this is a constructor of a non-static inner class, unlikeConstructor.getParameterTypes()
, the hiddenthis
parameter of the enclosing class is excluded from the returned parameters. -
getExceptionTypes
Returns all declared exception types of thisInvokable
. -
returning
-
returning
-
getDeclaringClass
- Specified by:
getDeclaringClass
in interfaceMember
-
getOwnerType
Returns the type ofT
. -
getAnnotatedReturnType
Returns theAnnotatedType
for the return type.- Since:
- 14.0
-