public static void main(String[] args)throws Exception {
String className = "com.model.FieldNameValue";
Class<?> clazz = Class.forName(className);
Optional<Method> optional = Arrays.stream(clazz.getDeclaredMethods()).filter(item -> item.getName().equals("getFieldName")).findFirst();
if(optional.isPresent()){
//取到方法
Method method = optional.get();
//找到返回值泛型
Type genericType = method.getGenericReturnType();
//类型转换
ParameterizedType pt = (ParameterizedType) genericType;
//得到泛型里的class类型对象 System.out.println(genericType);
Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
System.out.println(genericClazz);
}
}
方便做类型转换、序列化转换、AOP通用自动化处理等 需要获取List或其他集合对应的具体泛型类时使用; - 只是简单例子具体使用需按情况自行修改变化;