Get generic type of java.util.List

后端 未结 14 2306
广开言路
广开言路 2020-11-22 02:06

I have;

List stringList = new ArrayList();
List integerList = new ArrayList();

Is

14条回答
  •  温柔的废话
    2020-11-22 02:21

    You can do the same for method parameters as well:

    Method method = someClass.getDeclaredMethod("someMethod");
    Type[] types = method.getGenericParameterTypes();
    //Now assuming that the first parameter to the method is of type List
    ParameterizedType pType = (ParameterizedType) types[0];
    Class clazz = (Class) pType.getActualTypeArguments()[0];
    System.out.println(clazz); //prints out java.lang.Integer
    

提交回复
热议问题