在java编程时,经常会使用反射去处理对象,有时会需要知道List的泛型类型:示例操作如下

     obj = pojo.newInstance();
// 首先得到pojo所定义的字段
Field[] fields = pojo.getDeclaredFields();
for (Field curField : fields) {
// 设置字段可访问(必须,否则报错)
curField.setAccessible(true); Class<?> curFieldType = curField.getType();
// 集合List元素
if (curFieldType.equals(List.class)) {
// 当前集合的泛型类型
Type genericType = curField.getGenericType();
if (null == genericType) {
continue;
}
            
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
// 得到泛型里的class类型对象
Class<?> actualTypeArgument = (Class<?>)pt.getActualTypeArguments()[0];
List<Object> curEleList = new ArrayList<>();
Object actualType = actualTypeArgument.newInstance();
//....actualType字段处理
curEleList.add(actualType);
curField.set(obj, curEleList);
}
}
}

最新文章

  1. CentOS 7.0安装配置Vsftp服务器
  2. solution to E: failed to fetch .......
  3. java考核完的心得
  4. caching redirect views leads to memory leak (Spring 3.1)
  5. 一款效果精致的 jQuery 多层滑出菜单插件
  6. 类似material design的电影应用源码
  7. mysql开启binlog
  8. sql 提取数字、字母、汉字
  9. 51nod1175 区间中第K大的数
  10. oracle添加表字段跟修改表字段属性
  11. BestCoder Round #36 (hdu5199)Gunner(水题)
  12. const 修饰成员函数体
  13. Unity User Group 北京站图文报道:《Unity虚拟现实实战技巧》
  14. 《跟我学Shiro》学习笔记 第一章:Shiro简介
  15. 用递归方法求n的阶乘
  16. Class.forname和ClassLoader.loadClass的源码分析
  17. JDK11&amp;12 新特性学习
  18. time模块的学习
  19. Xamarin.Android SharedPreferences的使用方法
  20. 201904Online Human Action Recognition Based on Incremental Learning of Weighted Covariance Descriptors

热门文章

  1. SpringBoot整合AbstractRoutingDataSource实现读写分离
  2. React路由传参的三种方式
  3. [转帖]VMware vSphere 6 序列号大全
  4. 11 IO流(八)——装饰器设计模式,Filter装饰流
  5. redis网文
  6. vs2019中调用本地数据库mdf的相关问题
  7. Luogu4240 毒瘤之神的考验 莫比乌斯反演、根号分治
  8. 全栈项目|小书架|微信小程序-首页水平轮播实现
  9. isolate 通信
  10. Androidx初尝及其新旧包对照表