如何利用反射批量修改java類某一屬性的代碼詳解
更新時間:2020年07月25日 15:34:02 作者:心寒丶
這篇文章主要介紹了如何利用反射批量修改java類某一屬性,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
下面看下代碼,具體代碼如下所示:
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) {
//類屬性與傳入屬性對比,為了統(tǒng)一都轉(zhuǎn)小寫
if(p.getName().toLowerCase().equals(propertyName.toLowerCase())){
//獲取需要修改屬性
myPD = p;
break;
}
}
//根據(jù)需要修改屬性,修改屬性值
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狀態(tài)都修改為0,測試效果如下:

到此這篇關(guān)于如何利用反射批量修改java類某一屬性的文章就介紹到這了,更多相關(guān)批量修改java類某一屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目接收前端參數(shù)的11種方式
在前后端項(xiàng)目交互中,前端傳遞的數(shù)據(jù)可以通過HTTP請求發(fā)送到后端, 后端在Spring Boot中如何接收各種復(fù)雜的前端數(shù)據(jù)呢?這篇文章總結(jié)了11種在Spring Boot中接收前端數(shù)據(jù)的方式,需要的朋友可以參考下2024-12-12
java非遞歸實(shí)現(xiàn)之二叉樹的前中后序遍歷詳解
樹的遍歷順序大體分為三種:前序遍歷(先根遍歷、先序遍歷),中序遍歷(中根遍歷),后序遍歷(后根遍歷),本文將給大家詳細(xì)的介紹,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09
Java使用poi做加自定義注解實(shí)現(xiàn)對象與Excel相互轉(zhuǎn)換
這篇文章主要介紹了Java使用poi做加自定義注解實(shí)現(xiàn)對象與Excel相互轉(zhuǎn)換,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
springboot+vue實(shí)現(xiàn)oss文件存儲的示例代碼
對象存儲服務(wù)是一種海量、安全、低成本、高可靠的云存儲服務(wù),本文主要介紹了springboot+vue實(shí)現(xiàn)oss文件存儲的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02

