Android APIs
public final class

Method

extends AccessibleObject
implements GenericDeclaration Member
java.lang.Object
   ↳ java.lang.reflect.AccessibleObject
     ↳ java.lang.reflect.Method

Class Overview

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

Summary

[Expand]
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)
[Expand]
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

protected int accessFlags

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

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

public boolean equals (Object other)

Added in API level 1

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.

public T getAnnotation (Class<T> annotationClass)

Added in API level 1

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

public A getAnnotation (Class<A> annotationType)

Added in API level 14

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

public Annotation[] getAnnotations ()

Added in API level 1

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

public Annotation[] getDeclaredAnnotations ()

Added in API level 1

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

public Class<?> getDeclaringClass ()

Added in API level 1

Returns the class that declares this method.

Returns
Class<?>

public Object getDefaultValue ()

Added in API level 1

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

public Class[]<?> getExceptionTypes ()

Added in API level 1

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

public Type[] getGenericExceptionTypes ()

Added in API level 1

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

public Type[] getGenericParameterTypes ()

Added in API level 1

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

public Type getGenericReturnType ()

Added in API level 1

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

public int getModifiers ()

Added in API level 1

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:

public String getName ()

Added in API level 1

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

Returns
String the name of this method

public Annotation[][] getParameterAnnotations ()

Added in API level 1

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

public Class[]<?> getParameterTypes ()

Added in API level 1

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

public Class<?> getReturnType ()

Added in API level 1

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

Returns
Class<?> the return type

public TypeVariable[]<Method> getTypeParameters ()

Added in API level 1

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

public int hashCode ()

Added in API level 1

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.

public Object invoke (Object receiver, Object... args)

Added in API level 1

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

public boolean isAnnotationPresent (Class<? extends Annotation> annotationType)

Added in API level 1

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

public boolean isBridge ()

Added in API level 1

Indicates whether or not this method is a bridge.

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

public boolean isSynthetic ()

Added in API level 1

Indicates whether or not this method is synthetic.

Returns
boolean true if this method is synthetic, false otherwise

public boolean isVarArgs ()

Added in API level 1

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

Returns
boolean true if a vararg is declared, false otherwise

public String toGenericString ()

Added in API level 1

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

Returns
String the string representation of this method

public String toString ()

Added in API level 1

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

protected int compareParameters (Class[]<?> params)

Parameters
params Class
Returns
int

protected boolean equalMethodParameters (Class[]<?> params)

Parameters
params Class
Returns
boolean