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.
@Beta public abstract class Invokable<T,R> extends AccessibleObjectimplements GenericDeclaration
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 and TypeToken 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());
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object
|
static <T> Invokable |
from(Constructor
Returns
Invokable of
constructor.
|
static Invokable |
from(Method
Returns
Invokable of
method.
|
<A extends Annotation |
getAnnotation(Class
|
Annotation |
getAnnotations()
|
Annotation |
getDeclaredAnnotations()
|
Class |
getDeclaringClass()
|
ImmutableList |
getExceptionTypes()
Returns all declared exception types of this
Invokable.
|
int |
getModifiers()
|
String |
getName()
|
TypeToken |
getOwnerType()
Returns the type of
T.
|
ImmutableList |
getParameters()
Returns all declared parameters of this
Invokable.
|
TypeToken |
getReturnType()
Returns the return type of this
Invokable.
|
int |
hashCode()
|
R |
invoke(T receiver, Object
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(Class
|
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 |
returning(Class
Explicitly specifies the return type of this
Invokable.
|
<R1 extends R> Invokable |
returning(TypeToken
Explicitly specifies the return type of this
Invokable.
|
void |
setAccessible(boolean flag)
|
String |
toString()
|
getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByType, setAccessibleclone, finalize, getClass, notify, notifyAll, wait, wait, waitgetTypeParametersgetAnnotation, getAnnotations, getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotations, getDeclaredAnnotationsByType, isAnnotationPresentpublic static <T> Invokable<T ,T> from(Constructor <T> constructor)
Invokable of
constructor.
public abstract boolean isOverridable()
true if this is an overridable method. Constructors, private, static or final methods, or methods declared by final classes are not overridable.
public abstract boolean isVarArgs()
true if this was declared to take a variable number of arguments.
public final R invoke(T receiver, Object... args) throws InvocationTargetException , IllegalAccessException
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.
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.
public final TypeToken<? extends R> getReturnType()
Invokable.
public final ImmutableList<Parameter > getParameters()
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.
public final ImmutableList<TypeToken <? extends Throwable >> getExceptionTypes()
Invokable.
public final <R1 extends R> Invokable<T ,R1> returning(Class <R1> returnType)
Invokable. For example:
Method factoryMethod = Person.class.getMethod("create"); Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);
public final <R1 extends R> Invokable<T ,R1> returning(TypeToken <R1> returnType)
Invokable.
public final boolean isAnnotationPresent(Class<? extends Annotation > annotationClass)
public final <A extends Annotation> A getAnnotation(Class <A> annotationClass)
public final Annotation[] getAnnotations()
public final Annotation[] getDeclaredAnnotations()
public final void setAccessible(boolean flag)
throws SecurityException
setAccessible in class
AccessibleObject
SecurityException
public final boolean isAccessible()
public final StringgetName()
public final int getModifiers()
public final boolean isSynthetic()
public final boolean isPublic()
public final boolean isProtected()
public final boolean isPackagePrivate()
public final boolean isPrivate()
public final boolean isStatic()
public final boolean isFinal()
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().
public final boolean isAbstract()
public final boolean isNative()
public final boolean isSynchronized()
public boolean equals(Objectobj)
public int hashCode()
public StringtoString()