Java Method.getTypeParameters()

Syntax

Method.getTypeParameters() has the following syntax.

public TypeVariable <Method>[] getTypeParameters()

Example

In the following code shows how to use Method.getTypeParameters() method.


/* w ww.j a va 2 s . c om*/
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;

public class Main<T> {

  <E extends RuntimeException> T genericThrow(T t) throws E {
    return t;
  }

  public static void main(String... args) {
    try {
      Class<?> c = Class.forName("Main");
      Method[] allMethods = c.getDeclaredMethods();
      for (Method m : allMethods) {
        TypeVariable[] types = m.getTypeParameters();
        for(TypeVariable t:types){
          System.out.println(t);
        }
        

      }
    } catch (ClassNotFoundException x) {
      x.printStackTrace();
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable