public final class Method extends AccessibleObject implements GenericDeclaration, Member
Modifier and Type | Field and Description |
---|---|
static Comparator<Method> |
ORDER_BY_SIGNATURE
Orders methods by their name, parameters and return type.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object object)
Indicates whether or not the specified
object is equal to this
method. |
<A extends Annotation> |
getAnnotation(Class<A> annotationType)
Returns, for this element, the annotation with the specified type, or
null if no annotation with the specified type is present
(including inherited annotations). |
Annotation[] |
getDeclaredAnnotations()
Returns, for this element, all annotations that are explicitly declared
(not inherited).
|
Class<?> |
getDeclaringClass()
Returns the class that declares this method.
|
Object |
getDefaultValue()
Returns the default value for the annotation member represented by this
method.
|
Class<?>[] |
getExceptionTypes()
Returns the exception types as an array of
Class instances. |
Type[] |
getGenericExceptionTypes()
Returns the exception types as an array of
Type instances. |
Type[] |
getGenericParameterTypes()
Returns the parameter types as an array of
Type instances, in
declaration order. |
Type |
getGenericReturnType()
Returns the return type of this method as a
Type instance. |
int |
getModifiers()
Returns the modifiers for this method.
|
String |
getName()
Returns the name of the method represented by this
Method
instance. |
Annotation[][] |
getParameterAnnotations()
Returns an array of arrays that represent the annotations of the formal
parameters of this method.
|
Class<?>[] |
getParameterTypes()
Returns an array of
Class objects associated with the parameter
types of this method. |
Class<?> |
getReturnType()
Returns the
Class associated with the return type of this
method. |
TypeVariable<Method>[] |
getTypeParameters()
Returns the declared type parameters in declaration order.
|
int |
hashCode()
Returns an integer hash code for this method.
|
Object |
invoke(Object receiver,
Object... args)
Returns the result of dynamically invoking this method.
|
boolean |
isAnnotationPresent(Class<? extends Annotation> annotationType)
Indicates whether or not this element has an annotation with the
specified annotation type (including inherited annotations).
|
boolean |
isBridge()
Indicates whether or not this method is a bridge.
|
boolean |
isSynthetic()
Indicates whether or not this method is synthetic.
|
boolean |
isVarArgs()
Indicates whether or not this method takes a variable number argument.
|
String |
toGenericString()
Returns the string representation of the method's declaration, including
the type parameters.
|
String |
toString()
Returns a string containing a concise, human-readable description of this
method.
|
getAnnotations, isAccessible, setAccessible, setAccessible
public static final Comparator<Method> ORDER_BY_SIGNATURE
public TypeVariable<Method>[] getTypeParameters()
GenericDeclaration
getTypeParameters
in interface GenericDeclaration
public String toGenericString()
public Type[] getGenericParameterTypes()
Type
instances, in
declaration order. If this method has no parameters, an empty array is
returned.GenericSignatureFormatError
- if the generic method signature is invalidTypeNotPresentException
- if any parameter type points to a missing typeMalformedParameterizedTypeException
- if any parameter type points to a type that cannot be
instantiated for some reasonpublic Type[] getGenericExceptionTypes()
Type
instances. If
this method has no declared exceptions, an empty array will be returned.GenericSignatureFormatError
- if the generic method signature is invalidTypeNotPresentException
- if any exception type points to a missing typeMalformedParameterizedTypeException
- if any exception type points to a type that cannot be
instantiated for some reasonpublic Type getGenericReturnType()
Type
instance.GenericSignatureFormatError
- if the generic method signature is invalidTypeNotPresentException
- if the return type points to a missing typeMalformedParameterizedTypeException
- if the return type points to a type that cannot be
instantiated for some reasonpublic Annotation[] getDeclaredAnnotations()
AnnotatedElement
getDeclaredAnnotations
in interface AnnotatedElement
getDeclaredAnnotations
in class AccessibleObject
public <A extends Annotation> A getAnnotation(Class<A> annotationType)
AnnotatedElement
null
if no annotation with the specified type is present
(including inherited annotations).getAnnotation
in interface AnnotatedElement
getAnnotation
in class AccessibleObject
annotationType
- the type of the annotation to search fornull
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
AnnotatedElement
isAnnotationPresent
in interface AnnotatedElement
isAnnotationPresent
in class AccessibleObject
annotationType
- the type of the annotation to search fortrue
if the annotation exists, false
otherwisepublic Annotation[][] getParameterAnnotations()
Annotation
instancespublic boolean isVarArgs()
true
if a vararg is declared, false
otherwisepublic boolean isBridge()
true
if this method is a bridge, false
otherwisepublic boolean isSynthetic()
isSynthetic
in interface Member
true
if this method is synthetic, false
otherwisepublic Object getDefaultValue()
null
if noneTypeNotPresentException
- if this annotation member is of type Class
and no
definition can be foundpublic boolean equals(Object object)
object
is equal to this
method. To be equal, the specified object must be an instance
of Method
with the same declaring class and parameter types
as this method.equals
in class Object
object
- the object to comparetrue
if the specified object is equal to this
method, false
otherwisehashCode()
public Class<?> getDeclaringClass()
getDeclaringClass
in interface Member
public Class<?>[] getExceptionTypes()
Class
instances. If
this method has no declared exceptions, an empty array is returned.public int getModifiers()
Modifier
class should
be used to decode the result.getModifiers
in interface Member
Modifier
public String getName()
Method
instance.public Class<?>[] getParameterTypes()
Class
objects associated with the parameter
types of this method. If the method was declared with no parameters, an
empty array will be returned.public Class<?> getReturnType()
Class
associated with the return type of this
method.public int hashCode()
hashCode
in class Object
equals(java.lang.Object)
public Object invoke(Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
receiver.methodName(arg1, arg2, ... , argN)
.
If the method is static, the receiver argument is ignored (and may be null).
If the method takes no arguments, you can pass (Object[]) null
instead of
allocating an empty array.
If you're calling a varargs method, you need to pass an Object[]
for the
varargs parameter: that conversion is usually done in javac
, not the VM, and
the reflection machinery does not do this for you. (It couldn't, because it would be
ambiguous.)
Reflective method invocation follows the usual process for method lookup.
If an exception is thrown during the invocation it is caught and wrapped in an InvocationTargetException. This exception is then thrown.
If the invocation completes normally, the return value itself is returned. If the method is declared to return a primitive type, the return value is boxed. If the return type is void, null is returned.
receiver
- the object on which to call this method (or null for static methods)args
- the arguments to the methodNullPointerException
- if receiver == null
for a non-static methodIllegalAccessException
- if this method is not accessible (see AccessibleObject
)IllegalArgumentException
- if the number of arguments doesn't match the number of parameters, the receiver
is incompatible with the declaring class, or an argument could not be unboxed
or converted by a widening conversion to the corresponding parameter typeInvocationTargetException
- if an exception was thrown by the invoked methodpublic String toString()
public native Object
java.lang.Method.invoke(Object,Object) throws
IllegalAccessException,IllegalArgumentException
,InvocationTargetException