java反射获取集合类的泛型

开门见山直接放代码

Field field;
clazzField = field.getType();
if (clazzField == Map.class) {
     parameterizedType = (ParameterizedType)field.getGenericType();
     Class key = (Class) parameterizedType.getActualTypeArguments()[0];
     Class value = (Class) parameterizedType.getActualTypeArguments()[1];
 }else if (clazzField == List.class) {
      parameterizedType = (ParameterizedType)field.getGenericType();
      Class clazz = (Class) parameterizedType.getActualTypeArguments()[0];
  } else if (clazzField == Set.class) {
       parameterizedType = (ParameterizedType) field.getGenericType();
       Class clazz = (Class) parameterizedType.getActualTypeArguments()[0];
 }

 

你可能感兴趣的:(java反射获取集合类的泛型)