Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Method

public final class Method
extends AccessibleObject implements GenericDeclaration, Member

java.lang.Object
   ↳ java.lang.reflect.AccessibleObject
     ↳ java.lang.reflect.Method


This class represents a method. Information about the method can be accessed, and the method can be invoked dynamically.

Summary

Inherited constants

From interface java.lang.reflect.Member

Fields

protected int accessFlags

Bits encoding access (e.g.

protected Class<?> declaringClass

Method's declaring class

protected Class<?> declaringClassOfOverriddenMethod

Overriden method's declaring class (same as declaringClass unless declaringClass is a proxy class)

protected int dexMethodIndex

The method index of this method within its defining dex file

Public methods

boolean equals(Object other)

Returns true if other has the same declaring class, name, parameters and return type as this method.

<T extends Annotation> T getAnnotation(Class<T> annotationClass)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

<A extends Annotation> A 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[] getAnnotations()

Returns, for this element, an array containing all annotations (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 object.

Equivalent to getDeclaringClass().getName().hashCode() ^ getName().hashCode().

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.

Protected methods

int compareParameters(Class[]<?> params)
boolean equalMethodParameters(Class[]<?> params)

Inherited methods

From class java.lang.reflect.AccessibleObject
From class java.lang.Object
From interface java.lang.reflect.AnnotatedElement
From interface java.lang.reflect.GenericDeclaration
From interface java.lang.reflect.Member

Fields

accessFlags

int accessFlags

Bits encoding access (e.g. public, private) as well as other runtime specific flags

declaringClass

Class<?> declaringClass

Method's declaring class

declaringClassOfOverriddenMethod

Class<?> declaringClassOfOverriddenMethod

Overriden method's declaring class (same as declaringClass unless declaringClass is a proxy class)

dexMethodIndex

int dexMethodIndex

The method index of this method within its defining dex file

Public methods

equals

Added in API level 1
boolean equals (Object other)

Returns true if other has the same declaring class, name, parameters and return type as this method.

Parameters
other Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

getAnnotation

Added in API level 1
T getAnnotation (Class<T> annotationClass)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

Parameters
annotationClass Class: the type of the annotation to search for
Returns
T the annotation with the specified type or null

getAnnotation

Added in API level 14
A 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).

Parameters
annotationType Class: the type of the annotation to search for
Returns
A the annotation with the specified type or null

getAnnotations

Added in API level 1
Annotation[] getAnnotations ()

Returns, for this element, an array containing all annotations (including inherited annotations). If there are no annotations present, this method returns a zero length array.

Returns
Annotation[] an array of all annotations for this element

getDeclaredAnnotations

Added in API level 1
Annotation[] getDeclaredAnnotations ()

Returns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.

Returns
Annotation[] an array of annotations declared for this element

getDeclaringClass

Added in API level 1
Class<?> getDeclaringClass ()

Returns the class that declares this method.

Returns
Class<?>

getDefaultValue

Added in API level 1
Object getDefaultValue ()

Returns the default value for the annotation member represented by this method.

Returns
Object the default value, or null if none
Throws
TypeNotPresentException if this annotation member is of type Class and no definition can be found

getExceptionTypes

Added in API level 1
Class[]<?> getExceptionTypes ()

Returns the exception types as an array of Class instances. If this method has no declared exceptions, an empty array is returned.

Returns
Class[]<?> the declared exception classes

getGenericExceptionTypes

Added in API level 1
Type[] getGenericExceptionTypes ()

Returns the exception types as an array of Type instances. If this method has no declared exceptions, an empty array will be returned.

Returns
Type[] an array of generic exception types
Throws
GenericSignatureFormatError if the generic method signature is invalid
TypeNotPresentException if any exception type points to a missing type
MalformedParameterizedTypeException if any exception type points to a type that cannot be instantiated for some reason

getGenericParameterTypes

Added in API level 1
Type[] getGenericParameterTypes ()

Returns the parameter types as an array of Type instances, in declaration order. If this method has no parameters, an empty array is returned.

Returns
Type[] the parameter types
Throws
GenericSignatureFormatError if the generic method signature is invalid
TypeNotPresentException if any parameter type points to a missing type
MalformedParameterizedTypeException if any parameter type points to a type that cannot be instantiated for some reason

getGenericReturnType

Added in API level 1
Type getGenericReturnType ()

Returns the return type of this method as a Type instance.

Returns
Type the return type of this method
Throws
GenericSignatureFormatError if the generic method signature is invalid
TypeNotPresentException if the return type points to a missing type
MalformedParameterizedTypeException if the return type points to a type that cannot be instantiated for some reason

getModifiers

Added in API level 1
int getModifiers ()

Returns the modifiers for this method. The Modifier class should be used to decode the result.

Returns
int the modifiers for this method

See also:

getName

Added in API level 1
String getName ()

Returns the name of the method represented by this Method instance.

Returns
String the name of this method

getParameterAnnotations

Added in API level 1
Annotation[][] getParameterAnnotations ()

Returns an array of arrays that represent the annotations of the formal parameters of this method. If there are no parameters on this method, then an empty array is returned. If there are no annotations set, then and array of empty arrays is returned.

Returns
Annotation[][] an array of arrays of Annotation instances

getParameterTypes

Added in API level 1
Class[]<?> getParameterTypes ()

Returns an array of Class objects associated with the parameter types of this method. If the method was declared with no parameters, an empty array will be returned.

Returns
Class[]<?> the parameter types

getReturnType

Added in API level 1
Class<?> getReturnType ()

Returns the Class associated with the return type of this method.

Returns
Class<?> the return type

getTypeParameters

Added in API level 1
TypeVariable[]<Method> getTypeParameters ()

Returns the declared type parameters in declaration order. If there are no type parameters, this method returns a zero length array.

Returns
TypeVariable[]<Method> the declared type parameters in declaration order

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Equivalent to getDeclaringClass().getName().hashCode() ^ getName().hashCode().

Returns
int this object's hash code.

invoke

Added in API level 1
Object invoke (Object receiver, 
                Object... args)

Returns the result of dynamically invoking this method. Equivalent to 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.

Parameters
receiver Object: the object on which to call this method (or null for static methods)
args Object: the arguments to the method
Returns
Object the result
Throws
NullPointerException if receiver == null for a non-static method
IllegalAccessException 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 type
InvocationTargetException if an exception was thrown by the invoked method

isAnnotationPresent

Added in API level 1
boolean isAnnotationPresent (Class<? extends Annotation> annotationType)

Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).

Parameters
annotationType Class: the type of the annotation to search for
Returns
boolean true if the annotation exists, false otherwise

isBridge

Added in API level 1
boolean isBridge ()

Indicates whether or not this method is a bridge.

Returns
boolean true if this method is a bridge, false otherwise

isSynthetic

Added in API level 1
boolean isSynthetic ()

Indicates whether or not this method is synthetic.

Returns
boolean true if this method is synthetic, false otherwise

isVarArgs

Added in API level 1
boolean isVarArgs ()

Indicates whether or not this method takes a variable number argument.

Returns
boolean true if a vararg is declared, false otherwise

toGenericString

Added in API level 1
String toGenericString ()

Returns the string representation of the method's declaration, including the type parameters.

Returns
String the string representation of this method

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this method. The format of the string is:

  1. modifiers (if any)
  2. return type or 'void'
  3. declaring class name
  4. '('
  5. parameter types, separated by ',' (if any)
  6. ')'
  7. 'throws' plus exception types, separated by ',' (if any)
For example: public native Object java.lang.Method.invoke(Object,Object) throws IllegalAccessException,IllegalArgumentException ,InvocationTargetException

Returns
String a printable representation for this method

Protected methods

compareParameters

int compareParameters (Class[]<?> params)

Parameters
params Class
Returns
int

equalMethodParameters

boolean equalMethodParameters (Class[]<?> params)

Parameters
params Class
Returns
boolean
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.