Method Reflection

A Method provides information about a method on a class or interface.

Get annotations

<T extends Annotation>T getAnnotation(Class<T> annotationClass)
Returns this element's annotation for the specified type if such an annotation is present, else null.
Annotation[] getDeclaredAnnotations()
Returns all annotations that are directly present on this element.

Get its class or interface

Class<?> getDeclaringClass()
Returns the Class object representing the class or interface that declares the method represented by this Method object.

Get default value for the annotation member

Object getDefaultValue()
Returns the default value for the annotation member represented by this Method instance.

Get declared exceptions

Class<?>[] getExceptionTypes()
Returns an array of Class objects that represent the types of the exceptions declared to be thrown by the underlying method represented by this Method object.
Type[] getGenericExceptionTypes()
Returns an array of Type objects that represent the exceptions declared to be thrown by this Method object.

Get parameter types

Type[] getGenericParameterTypes()
Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Method object.
Class<?>[] getParameterTypes()
Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method represented by this Method object.

Get return type

Type getGenericReturnType()
Returns a Type object that represents the formal return type of the method represented by this Method object.
Class<?> getReturnType()
Returns a Class object that represents the formal return type of the method represented by this Method object.

Get modifiers and name for this method

int getModifiers()
Returns the Java language modifiers for the method represented by this Method object, as an integer.
String getName()
Returns the name of the method represented by this Method object, as a String.

Get annotations on the formal parameters

Annotation[][] getParameterAnnotations()
Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Method object.

Get the type variables for this method

TypeVariable<Method>[] getTypeParameters()
Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

Invoke this method

Object invoke(Object obj, Object... args)
Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.

Is this method bridge method, synthetic method and has variable number of arguments

boolean isBridge()
Returns true if this method is a bridge method; returns false otherwise.
boolean isSynthetic()
Returns true if this method is a synthetic method; returns false otherwise.
boolean isVarArgs()
Returns true if this method was declared to take a variable number of arguments; returns false otherwise.

Get the string representation of the method

String toGenericString()
Returns a string describing this Method, including type parameters.
String toString()
Returns a string describing this Method.

Revised from Open JDK source code

The following code demonstrates Method's invoke(Object, Object...) method.

 
import java.lang.reflect.Method;

class X {
  public void objectMethod(String arg) {
    System.out.println("Instance method: " + arg);
  }

  public static void classMethod() {
    System.out.println("Class method");
  }
}

public class Main {
  public static void main(String[] args) {
    try {
      Class<?> clazz = Class.forName("X");
      X x = (X) clazz.newInstance();
      Class[] argTypes = { String.class };
      Method method = clazz.getMethod("objectMethod", argTypes);
      Object[] data = { "Hello" };
      method.invoke(x, data); // Output: Instance method: Hello
      method = clazz.getMethod("classMethod", (Class<?>[]) null);
      method.invoke(null, (Object[]) null); // Output: Class method
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}
  

Instance method: Hello
Class method
Home 
  Java Book 
    Reflection  

Method:
  1. Method Reflection
  2. Using reflection to print out all methods
  3. Invoke a method