欢迎来到代码驿站!

当前位置:首页 >

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

时间:2020-07-25 17:00:59|栏目:|点击:

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

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,测试效果如下:

上一篇:SpringBoot整合模板引擎过程代码实例

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

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

本文地址:http://www.codeinn.net/misctech/2587.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有