如何利用反射批量修改java类某一属性的代码详解
更新时间:2020-07-25 20:37 文章类目:其它教程 文章来源:开源之家 作者:转载 浏览:180 次
下面看下代码,具体代码如下所示:
package utils.copyProperty; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; public class CopyProperty { public static PropertyDescriptor[] getPropertyDescriptor(Class<?> clz) throws Exception { PropertyDescriptor[] propertyDescriptorsFull = Introspector.getBeanInfo(clz).getPropertyDescriptors(); PropertyDescriptor[] ps = new PropertyDescriptor[propertyDescriptorsFull.length - 1]; int index = 0; for (PropertyDescriptor p : propertyDescriptorsFull) { if (!p.getName().equals("class")) { ps[index++] = p; } } return ps; } public static <T> T setPropertyValue(T t,String propertyName,Object value){ try{ //获取属性描述类 PropertyDescriptor[] pdArr = getPropertyDescriptor(t.getClass()); PropertyDescriptor myPD = null; for (PropertyDescriptor p : pdArr) { //类属性与传入属性对比,为了统一都转小写 if(p.getName().toLowerCase().equals(propertyName.toLowerCase())){ //获取需要修改属性 myPD = p; break; } } //根据需要修改属性,修改属性值 if(myPD!=null){ Method writeMethod = myPD.getWriteMethod(); if(myPD.getPropertyType().getName().equals("java.lang.String")) { writeMethod.invoke(t, value.toString()); }else{ writeMethod.invoke(t, value); } } }catch(Exception e){ e.printStackTrace(); } return t; } public static <T>Collection<T> setPropertyValue(Collection<T> coll,String propertyName,Object value) { if(coll!=null) for(T t : coll){ setPropertyValue(t,propertyName,value); } return coll; } public static void main(String args[]) throws Exception{ ArrayList<Student> students=new ArrayList(); Student student=new Student(); Student student1=new Student(); students.add(student); students.add(student1); for (Student stu:students){ System.out.println("赋值之前:"+stu.getValidStatus()); }//修改validStatus为0 CopyProperty.setPropertyValue(students, "validStatus", "0"); for (Student stu:students){ System.out.println("赋值之后:"+stu.getValidStatus()); } } public static class Student{ private String name ; private String sex; private String validStatus="1"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getValidStatus() { return validStatus; } public void setValidStatus(String validStatus) { this.validStatus = validStatus; } } }
把student的validStatus状态都修改为0,测试效果如下:
转载请注明来源: 如何利用反射批量修改java类某一属性的代码详解
本文永久链接地址: https://www.enboo.cn/CMSjiaocheng/qitajiaocheng/20794.html
文本标签:Java
郑重声明:
本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
如无法链接失效或侵犯版权,请给我们来信:2225329841@qq.com
- 帝国cms和织梦cms哪个好用?优缺点介绍
- JAVA 实现磁盘文件加解密操作的示例代码
- WordPress 使用 PDF Embedder 直接嵌入显示PDF文件
- Maven生成及安装jar包到本地仓库的方法
- Mybatis在sqlite中无法读写byte[]类问题的解决办法
- phpstudy-linux面板(小皮面板)_linux集成环境安装
- php中unserialize返回false的解决方法
- 【网狐荣耀修改教程】第二篇、在房卡红中麻将的基础上增加、东南
- 如何使用大学教育邮箱下载golang等软件(推荐)
- php windows环境安装的方法详解
- WordPress实现中英文数字之间自动加空格排版
- 把数据保存到数据库附加表 `XX_addonarticle` 时出错,Duplicate
- 网狐荣耀MSSQL破解系统存储过程加密的SQL代码
- 简单了解java ibatis #及$的区别和用法
- GoAdminGroup/go-admin的安装和运行的教程详解
- 最新文章
- 热门文章
-
- 微信开发者工具Error: unable to verify the first certificate
- Element PageHeader页头的使用方法
- 帝国cms和织梦cms哪个好用?优缺点介绍
- wps表格下拉数字无法递增怎么办?
- 关于android studio升级4.1 某些插件使用不了的问题(Mac)
- Tomcat启动springboot项目war包报错:启动子级时出错的问题
- 解决IDEA的maven项目中没有新建Servlet文件的选项问题
- Laravel中如何轻松容易的输出完整的SQL语句
- 在函数内部可以通过什么关键字来定义全局变量?
- 基于php+MySql实现学生信息管理系统实例