to top
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
Public Methods
boolean equals(Object object)
Indicates whether or not the specified object is equal to this method.
<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[] 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.
[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

Public Methods

public boolean equals (Object object)

Since: API Level 1

Indicates whether or not the specified 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.

Parameters
object the object to compare
Returns
  • true if the specified object is equal to this method, false otherwise
See Also

public A getAnnotation (Class<A> annotationType)

Since: 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 the type of the annotation to search for
Returns
  • the annotation with the specified type or null

public Annotation[] getDeclaredAnnotations ()

Since: 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
  • an array of annotations declared for this element

public Class<?> getDeclaringClass ()

Since: API Level 1

Returns the class that declares this method.

Returns
  • the declaring class

public Object getDefaultValue ()

Since: API Level 1

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

Returns
  • 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 ()

Since: 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
  • the declared exception classes

public Type[] getGenericExceptionTypes ()

Since: 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
  • 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 ()

Since: 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
  • 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 ()

Since: API Level 1

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

Returns
  • 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 ()

Since: API Level 1

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

Returns
  • the modifiers for this method
See Also

public String getName ()

Since: API Level 1

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

Returns
  • the name of this method

public Annotation[][] getParameterAnnotations ()

Since: 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
  • an array of arrays of Annotation instances

public Class[]<?> getParameterTypes ()

Since: 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
  • the parameter types

public Class<?> getReturnType ()

Since: API Level 1

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

Returns
  • the return type

public TypeVariable[]<Method> getTypeParameters ()

Since: 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
  • the declared type parameters in declaration order

public int hashCode ()

Since: API Level 1

Returns an integer hash code for this method. Objects which are equal return the same value for this method. The hash code for this Method is the hash code of the name of this method.

Returns
  • hash code for this method
See Also

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

Since: 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 the object on which to call this method (or null for static methods)
args the arguments to the method
Returns
  • 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)

Since: API Level 1

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

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

public boolean isBridge ()

Since: API Level 1

Indicates whether or not this method is a bridge.

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

public boolean isSynthetic ()

Since: API Level 1

Indicates whether or not this method is synthetic.

Returns
  • true if this method is synthetic, false otherwise

public boolean isVarArgs ()

Since: API Level 1

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

Returns
  • true if a vararg is declared, false otherwise

public String toGenericString ()

Since: API Level 1

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

Returns
  • the string representation of this method

public String toString ()

Since: 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
  • a printable representation for this method