Java Method.getGenericReturnType()

Syntax

Method.getGenericReturnType() has the following syntax.

public Type getGenericReturnType()

Example

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


import java.lang.reflect.Method;
import java.lang.reflect.Type;
/*from  w  w w .  j  a  v a  2s . c om*/
public class Main<T> {
  private static final String fmt = "%24s: %s%n";

  <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) {
        Type gpType = m.getGenericReturnType();
        System.out.println(gpType);

      }
    } 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