com.google.common.reflect
Class Invokable<T,R>

java.lang.Object
  extended by java.lang.reflect.AccessibleObject
      extended by 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.GenericDeclaration, java.lang.reflect.Member

@Beta
public abstract class Invokable<T,R>
extends java.lang.reflect.AccessibleObject
implements java.lang.reflect.GenericDeclaration

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());

Since:
14.0

Field Summary
 
Fields inherited from interface java.lang.reflect.Member
DECLARED, PUBLIC
 
Method Summary
 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.
<A extends java.lang.annotation.Annotation>
A
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.
 int getModifiers()
           
 java.lang.String getName()
           
 TypeToken<T> getOwnerType()
          Returns the type of T.
 ImmutableList<Parameter> getParameters()
          Returns all declared parameters of this Invokable.
 TypeToken<? extends R> getReturnType()
          Returns the return type of this Invokable.
 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.
 boolean isAbstract()
          Returns true if the method is abstract.
 boolean 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()
           
abstract  boolean isVarArgs()
          Returns true if this was declared to take a variable number of arguments.
<R1 extends R>
Invokable<T,R1>
returning(java.lang.Class<R1> returnType)
          Explicitly specifies the return type of this Invokable.
<R1 extends R>
Invokable<T,R1>
returning(TypeToken<R1> returnType)
          Explicitly specifies the return type of this Invokable.
 void setAccessible(boolean flag)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.reflect.AccessibleObject
setAccessible
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.lang.reflect.GenericDeclaration
getTypeParameters
 

Method Detail

from

public static Invokable<?,java.lang.Object> from(java.lang.reflect.Method method)
Returns Invokable of method.


from

public static <T> Invokable<T,T> from(java.lang.reflect.Constructor<T> constructor)
Returns Invokable of constructor.


isOverridable

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

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


invoke

public final R invoke(@Nullable
                      T receiver,
                      java.lang.Object... args)
               throws java.lang.reflect.InvocationTargetException,
                      java.lang.IllegalAccessException
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:
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.

getReturnType

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


getParameters

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

public final ImmutableList<TypeToken<? extends java.lang.Throwable>> getExceptionTypes()
Returns all declared exception types of this Invokable.


returning

public final <R1 extends R> Invokable<T,R1> returning(java.lang.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

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


getDeclaringClass

public final java.lang.Class<? super T> getDeclaringClass()
Specified by:
getDeclaringClass in interface java.lang.reflect.Member

getOwnerType

public TypeToken<T> getOwnerType()
Returns the type of T.


isAnnotationPresent

public final boolean isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Specified by:
isAnnotationPresent in interface java.lang.reflect.AnnotatedElement
Overrides:
isAnnotationPresent in class java.lang.reflect.AccessibleObject

getAnnotation

public final <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.Class<A> annotationClass)
Specified by:
getAnnotation in interface java.lang.reflect.AnnotatedElement
Overrides:
getAnnotation in class java.lang.reflect.AccessibleObject

getAnnotations

public final java.lang.annotation.Annotation[] getAnnotations()
Specified by:
getAnnotations in interface java.lang.reflect.AnnotatedElement
Overrides:
getAnnotations in class java.lang.reflect.AccessibleObject

getDeclaredAnnotations

public final java.lang.annotation.Annotation[] getDeclaredAnnotations()
Specified by:
getDeclaredAnnotations in interface java.lang.reflect.AnnotatedElement
Overrides:
getDeclaredAnnotations in class java.lang.reflect.AccessibleObject

setAccessible

public final void setAccessible(boolean flag)
                         throws java.lang.SecurityException
Overrides:
setAccessible in class java.lang.reflect.AccessibleObject
Throws:
java.lang.SecurityException

isAccessible

public final boolean isAccessible()
Overrides:
isAccessible in class java.lang.reflect.AccessibleObject

getName

public final java.lang.String getName()
Specified by:
getName in interface java.lang.reflect.Member

getModifiers

public final int getModifiers()
Specified by:
getModifiers in interface java.lang.reflect.Member

isSynthetic

public final boolean isSynthetic()
Specified by:
isSynthetic in interface java.lang.reflect.Member

isPublic

public final boolean isPublic()
Returns true if the element is public.


isProtected

public final boolean isProtected()
Returns true if the element is protected.


isPackagePrivate

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


isPrivate

public final boolean isPrivate()
Returns true if the element is private.


isStatic

public final boolean isStatic()
Returns true if the element is static.


isFinal

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

public final boolean isAbstract()
Returns true if the method is abstract.


isNative

public final boolean isNative()
Returns true if the element is native.


isSynchronized

public final boolean isSynchronized()
Returns true if the method is synchronized.


equals

public boolean equals(@Nullable
                      java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object