Package com.google.common.reflect
Class Invokable<T,R>
- java.lang.Object
-
- java.lang.reflect.AccessibleObject
-
- com.google.common.reflect.Element
-
- 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
- Direct Known Subclasses:
Invokable.ConstructorInvokable,Invokable.MethodInvokable
public abstract class Invokable<T,R> extends Element implements java.lang.reflect.GenericDeclaration
Wrapper around either aMethodor aConstructor. Convenience API is provided to make common reflective operation easier to deal with, such asElement.isPublic(),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());- Since:
- 14.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classInvokable.ConstructorInvokable<T>(package private) static classInvokable.MethodInvokable<T>
-
Constructor Summary
Constructors Constructor Description Invokable(M member)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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.(package private) abstract java.lang.reflect.AnnotatedType[]getAnnotatedParameterTypes()abstract java.lang.reflect.AnnotatedTypegetAnnotatedReturnType()java.lang.Class<? super T>getDeclaringClass()ImmutableList<TypeToken<? extends java.lang.Throwable>>getExceptionTypes()Returns all declared exception types of thisInvokable.(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.TypegetGenericReturnType()TypeToken<T>getOwnerType()Returns the type ofT.(package private) abstract java.lang.annotation.Annotation[][]getParameterAnnotations()ImmutableList<Parameter>getParameters()Returns all declared parameters of thisInvokable.TypeToken<? extends R>getReturnType()Returns the return type of thisInvokable.Rinvoke(T receiver, 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.(package private) abstract java.lang.ObjectinvokeInternal(java.lang.Object receiver, java.lang.Object[] args)abstract booleanisOverridable()Returnstrueif this is an overridable method.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.-
Methods inherited from class com.google.common.reflect.Element
equals, getAnnotation, getAnnotations, getDeclaredAnnotations, getModifiers, getName, hashCode, isAbstract, isAccessible, isAnnotationPresent, isFinal, isNative, isPackagePrivate, isPrivate, isProtected, isPublic, isStatic, isSynchronized, isSynthetic, isTransient, isVolatile, setAccessible, toString
-
Methods inherited from class java.lang.reflect.AccessibleObject
canAccess, getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByType, setAccessible, trySetAccessible
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Method Detail
-
from
public static Invokable<?,java.lang.Object> from(java.lang.reflect.Method method)
ReturnsInvokableofmethod.
-
from
public static <T> Invokable<T,T> from(java.lang.reflect.Constructor<T> constructor)
ReturnsInvokableofconstructor.
-
isOverridable
public abstract boolean isOverridable()
Returnstrueif 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()
Returnstrueif this was declared to take a variable number of arguments.
-
invoke
public final R invoke(T receiver, 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 thisConstructorobject 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 thisInvokable.
-
getParameters
public 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.
-
getExceptionTypes
public final ImmutableList<TypeToken<? extends java.lang.Throwable>> getExceptionTypes()
Returns all declared exception types of thisInvokable.
-
returning
public 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);
-
returning
public final <R1 extends R> Invokable<T,R1> returning(TypeToken<R1> returnType)
Explicitly specifies the return type of thisInvokable.
-
getDeclaringClass
public final java.lang.Class<? super T> getDeclaringClass()
- Specified by:
getDeclaringClassin interfacejava.lang.reflect.Member- Overrides:
getDeclaringClassin classElement
-
getOwnerType
public TypeToken<T> getOwnerType()
Returns the type ofT.- Overrides:
getOwnerTypein classElement
-
invokeInternal
abstract java.lang.Object invokeInternal(java.lang.Object receiver, java.lang.Object[] args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException- Throws:
java.lang.reflect.InvocationTargetExceptionjava.lang.IllegalAccessException
-
getGenericParameterTypes
abstract java.lang.reflect.Type[] getGenericParameterTypes()
-
getAnnotatedParameterTypes
abstract java.lang.reflect.AnnotatedType[] getAnnotatedParameterTypes()
-
getGenericExceptionTypes
abstract java.lang.reflect.Type[] getGenericExceptionTypes()
This should never return a type that's not a subtype of Throwable.
-
getParameterAnnotations
abstract java.lang.annotation.Annotation[][] getParameterAnnotations()
-
getGenericReturnType
abstract java.lang.reflect.Type getGenericReturnType()
-
getAnnotatedReturnType
public abstract java.lang.reflect.AnnotatedType getAnnotatedReturnType()
-
-