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:
AnnotatedElement, Member

public abstract class Invokable<T,R> extends Object implements AnnotatedElement, Member
Wrapper around either a 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 or GenericDeclaration since 31.0)
Author:
Ben Yu
  • Method Details Link icon

    • from Link icon

      public static Invokable<?,Object> from(Method method)
      Returns Invokable of method.
    • from Link icon

      public static <T> Invokable<T,T> from(Constructor<T> constructor)
      Returns Invokable of constructor.
    • isAnnotationPresent Link icon

      public final boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
      Specified by:
      isAnnotationPresent in interface AnnotatedElement
    • getAnnotation Link icon

      public final <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationClass)
      Specified by:
      getAnnotation in interface AnnotatedElement
    • getAnnotations Link icon

      public final Annotation[] getAnnotations()
      Specified by:
      getAnnotations in interface AnnotatedElement
    • getDeclaredAnnotations Link icon

      public final Annotation[] getDeclaredAnnotations()
      Specified by:
      getDeclaredAnnotations in interface AnnotatedElement
    • getTypeParameters Link icon

      public abstract TypeVariable<?>[] getTypeParameters()
    • setAccessible Link icon

      public final void setAccessible(boolean flag)
    • trySetAccessible Link icon

      public final boolean trySetAccessible()
    • isAccessible Link icon

      public final boolean isAccessible()
    • getName Link icon

      public final String getName()
      Specified by:
      getName in interface Member
    • getModifiers Link icon

      public final int getModifiers()
      Specified by:
      getModifiers in interface Member
    • isSynthetic Link icon

      public final boolean isSynthetic()
      Specified by:
      isSynthetic in interface Member
    • isPublic Link icon

      public final boolean isPublic()
      Returns true if the element is public.
    • isProtected Link icon

      public final boolean isProtected()
      Returns true if the element is protected.
    • isPackagePrivate Link icon

      public final boolean isPackagePrivate()
      Returns true if the element is package-private.
    • isPrivate Link icon

      public final boolean isPrivate()
      Returns true if the element is private.
    • isStatic Link icon

      public final boolean isStatic()
      Returns true if the element is static.
    • isFinal Link icon

      public final boolean isFinal()
      Returns 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().

    • isAbstract Link icon

      public final boolean isAbstract()
      Returns true if the method is abstract.
    • isNative Link icon

      public final boolean isNative()
      Returns true if the element is native.
    • isSynchronized Link icon

      public final boolean isSynchronized()
      Returns true if the method is synchronized.
    • equals Link icon

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • hashCode Link icon

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • isOverridable Link icon

      public abstract boolean isOverridable()
      Returns true if this is an overridable method. Constructors, private, static or final methods, or methods declared by final classes are not overridable.
    • isVarArgs Link icon

      public abstract boolean isVarArgs()
      Returns true if this was declared to take a variable number of arguments.
    • invoke Link icon

      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.
      Throws:
      IllegalAccessException - if this Constructor 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 Link icon

      public final TypeToken<? extends R> getReturnType()
      Returns the return type of this Invokable.
    • getParameters Link icon

      public final ImmutableList<Parameter> getParameters()
      Returns all declared parameters of this 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.
    • getExceptionTypes Link icon

      public final ImmutableList<TypeToken<? extends Throwable>> getExceptionTypes()
      Returns all declared exception types of this Invokable.
    • returning Link icon

      public final <R1 extends R> Invokable<T,R1> returning(Class<R1> returnType)
      Explicitly specifies the return type of this Invokable. For example:
      Method factoryMethod = Person.class.getMethod("create");
      Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);
      
    • returning Link icon

      public final <R1 extends R> Invokable<T,R1> returning(TypeToken<R1> returnType)
      Explicitly specifies the return type of this Invokable.
    • getDeclaringClass Link icon

      public final Class<? super T> getDeclaringClass()
      Specified by:
      getDeclaringClass in interface Member
    • getOwnerType Link icon

      public TypeToken<T> getOwnerType()
      Returns the type of T.
    • getAnnotatedReturnType Link icon

      public abstract AnnotatedType getAnnotatedReturnType()
      Returns the AnnotatedType for the return type.
      Since:
      14.0