如何利用反射批量修改java类某一属性的代码详解

所属分类: 软件编程 / java 阅读数: 105
收藏 0 赞 0 分享

下面看下代码,具体代码如下所示:

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基于反射机制实现全部注解获取的方法示例

这篇文章主要介绍了Java基于反射机制实现全部注解获取的方法,结合实例形式分析了java反射机制获取注解的具体实现方法与操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 信号量Semaphore的实现

这篇文章主要介绍了Java 信号量Semaphore的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

eclipse+maven+spring mvc项目基本搭建过程

这篇文章主要介绍了eclipse+maven+spring mvc项目基本搭建过程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring boot集成swagger2生成接口文档的全过程

这篇文章主要给大家介绍了关于Spring boot集成swagger2生成接口文档的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java冒泡排序法和选择排序法的实现

这篇文章主要介绍了Java冒泡排序法和选择排序法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Cloud Alibaba教程之Sentinel的使用

这篇文章主要介绍了Spring Cloud Alibaba教程之Sentinel的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Josephus环的四种解法(约瑟夫环)基于java详解

这篇文章主要介绍了Josephus环的四种解法(约瑟夫环)基于java详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java继承Thread类创建线程类示例

这篇文章主要介绍了Java继承Thread类创建线程类,结合实例形式分析了java线程操作相关使用技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java使用Callable和Future创建线程操作示例

这篇文章主要介绍了Java使用Callable和Future创建线程操作,结合实例形式分析了java使用Callable接口和Future类创建线程的相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

springBoot使用JdbcTemplate代码实例

这篇文章主要介绍了springBoot使用JdbcTemplate代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多