Class Invokable<T,R>
- java.lang.Object
- 
- com.google.common.reflect.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:
- java.lang.reflect.AnnotatedElement,- java.lang.reflect.Member
 
 public abstract class Invokable<T,R> extends java.lang.Object implements java.lang.reflect.AnnotatedElement, java.lang.reflect.Member Wrapper around either aMethodor aConstructor. Convenience API is provided to make common reflective operation easier to deal with, such asisPublic(),getParameters()etc.In addition to convenience methods, TypeToken.method(java.lang.reflect.Method)andTypeToken.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 AccessibleObjectandGenericDeclaration. 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 AccessibleObjectorGenericDeclarationsince 31.0)
- Author:
- Ben Yu
 
- 
- 
Method SummaryAll Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)static <T> Invokable<T,T>from(java.lang.reflect.Constructor<T> constructor)ReturnsInvokableofconstructor.static Invokable<?,java.lang.Object>from(java.lang.reflect.Method method)ReturnsInvokableofmethod.<A extends java.lang.annotation.Annotation>
 AgetAnnotation(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 thisInvokable.intgetModifiers()java.lang.StringgetName()TypeToken<T>getOwnerType()Returns the type ofT.ImmutableList<Parameter>getParameters()Returns all declared parameters of thisInvokable.TypeToken<? extends R>getReturnType()Returns the return type of thisInvokable.abstract java.lang.reflect.TypeVariable<?>[]getTypeParameters()SeeGenericDeclaration.getTypeParameters().inthashCode()Rinvoke(T receiver, @Nullable java.lang.Object... args)Invokes withreceiveras 'this' andargspassed to the underlying method and returns the return value; or calls the underlying constructor withargsand returns the constructed instance.booleanisAbstract()Returns true if the method is abstract.booleanisAccessible()SeeAccessibleObject.isAccessible().booleanisAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)booleanisFinal()Returnstrueif this method is final, perModifier.isFinal(getModifiers()).booleanisNative()Returns true if the element is native.abstract booleanisOverridable()Returnstrueif this is an overridable method.booleanisPackagePrivate()Returns true if the element is package-private.booleanisPrivate()Returns true if the element is private.booleanisProtected()Returns true if the element is protected.booleanisPublic()Returns true if the element is public.booleanisStatic()Returns true if the element is static.booleanisSynchronized()Returns true if the method is synchronized.booleanisSynthetic()abstract booleanisVarArgs()Returnstrueif this was declared to take a variable number of arguments.<R1 extends R>
 Invokable<T,R1>returning(TypeToken<R1> returnType)Explicitly specifies the return type of thisInvokable.<R1 extends R>
 Invokable<T,R1>returning(java.lang.Class<R1> returnType)Explicitly specifies the return type of thisInvokable.voidsetAccessible(boolean flag)SeeAccessibleObject.setAccessible(boolean).java.lang.StringtoString()booleantrySetAccessible()SeeAccessibleObject.trySetAccessible().
 
- 
- 
- 
Method Detail- 
frompublic static Invokable<?,java.lang.Object> from(java.lang.reflect.Method method) ReturnsInvokableofmethod.
 - 
frompublic static <T> Invokable<T,T> from(java.lang.reflect.Constructor<T> constructor) ReturnsInvokableofconstructor.
 - 
isAnnotationPresentpublic final boolean isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass) - Specified by:
- isAnnotationPresentin interface- java.lang.reflect.AnnotatedElement
 
 - 
getAnnotation@CheckForNull public final <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.Class<A> annotationClass) - Specified by:
- getAnnotationin interface- java.lang.reflect.AnnotatedElement
 
 - 
getAnnotationspublic final java.lang.annotation.Annotation[] getAnnotations() - Specified by:
- getAnnotationsin interface- java.lang.reflect.AnnotatedElement
 
 - 
getDeclaredAnnotationspublic final java.lang.annotation.Annotation[] getDeclaredAnnotations() - Specified by:
- getDeclaredAnnotationsin interface- java.lang.reflect.AnnotatedElement
 
 - 
getTypeParameterspublic abstract java.lang.reflect.TypeVariable<?>[] getTypeParameters() SeeGenericDeclaration.getTypeParameters().
 - 
setAccessiblepublic final void setAccessible(boolean flag) SeeAccessibleObject.setAccessible(boolean).
 - 
trySetAccessiblepublic final boolean trySetAccessible() SeeAccessibleObject.trySetAccessible().
 - 
isAccessiblepublic final boolean isAccessible() SeeAccessibleObject.isAccessible().
 - 
getNamepublic final java.lang.String getName() - Specified by:
- getNamein interface- java.lang.reflect.Member
 
 - 
getModifierspublic final int getModifiers() - Specified by:
- getModifiersin interface- java.lang.reflect.Member
 
 - 
isSyntheticpublic final boolean isSynthetic() - Specified by:
- isSyntheticin interface- java.lang.reflect.Member
 
 - 
isPublicpublic final boolean isPublic() Returns true if the element is public.
 - 
isProtectedpublic final boolean isProtected() Returns true if the element is protected.
 - 
isPackagePrivatepublic final boolean isPackagePrivate() Returns true if the element is package-private.
 - 
isPrivatepublic final boolean isPrivate() Returns true if the element is private.
 - 
isStaticpublic final boolean isStatic() Returns true if the element is static.
 - 
isFinalpublic final boolean isFinal() Returnstrueif this method is final, perModifier.isFinal(getModifiers()).Note that a method may still be effectively "final", or non-overridable when it has no finalkeyword. For example, it could be private, or it could be declared by a final class. To tell whether a method is overridable, useisOverridable().
 - 
isAbstractpublic final boolean isAbstract() Returns true if the method is abstract.
 - 
isNativepublic final boolean isNative() Returns true if the element is native.
 - 
isSynchronizedpublic final boolean isSynchronized() Returns true if the method is synchronized.
 - 
equalspublic boolean equals(@CheckForNull java.lang.Object obj) - Overrides:
- equalsin class- java.lang.Object
 
 - 
hashCodepublic int hashCode() - Overrides:
- hashCodein class- java.lang.Object
 
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 - 
isOverridablepublic abstract boolean isOverridable() Returnstrueif this is an overridable method. Constructors, private, static or final methods, or methods declared by final classes are not overridable.
 - 
isVarArgspublic abstract boolean isVarArgs() Returnstrueif this was declared to take a variable number of arguments.
 - 
invoke@CanIgnoreReturnValue @CheckForNull public final R invoke(@CheckForNull T receiver, @Nullable java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException Invokes withreceiveras 'this' andargspassed to the underlying method and returns the return value; or calls the underlying constructor withargsand returns the constructed instance.- Throws:
- java.lang.IllegalAccessException- if this- Constructorobject 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.
 
 - 
getReturnTypepublic final TypeToken<? extends R> getReturnType() Returns the return type of thisInvokable.
 - 
getParameterspublic final ImmutableList<Parameter> getParameters() Returns all declared parameters of thisInvokable. Note that if this is a constructor of a non-static inner class, unlikeConstructor.getParameterTypes(), the hiddenthisparameter of the enclosing class is excluded from the returned parameters.
 - 
getExceptionTypespublic final ImmutableList<TypeToken<? extends java.lang.Throwable>> getExceptionTypes() Returns all declared exception types of thisInvokable.
 - 
returningpublic final <R1 extends R> Invokable<T,R1> returning(java.lang.Class<R1> returnType) Explicitly specifies the return type of thisInvokable. For example:Method factoryMethod = Person.class.getMethod("create"); Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);
 - 
returningpublic final <R1 extends R> Invokable<T,R1> returning(TypeToken<R1> returnType) Explicitly specifies the return type of thisInvokable.
 - 
getDeclaringClasspublic final java.lang.Class<? super T> getDeclaringClass() - Specified by:
- getDeclaringClassin interface- java.lang.reflect.Member
 
 - 
getOwnerTypepublic TypeToken<T> getOwnerType() Returns the type ofT.
 
- 
 
-