如何利用反射批量修改java類某一屬性的代碼詳解
下面看下代碼,具體代碼如下所示:
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)文章
圖解Java?ReentrantLock的條件變量Condition機(jī)制
想必大家都使用過wait()和notify()這兩個(gè)方法把,他們主要用于多線程間的協(xié)同處理。而RenentrantLock也支持這樣條件變量的能力,而且相對于synchronized?更加強(qiáng)大,能夠支持多個(gè)條件變量,本文就來詳細(xì)說說2022-10-10淺析javax.servlet.Servlet,ServletContext接口
本篇文章是對javax.servlet.Servlet,ServletContext接口進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下2013-07-07淺談java7增強(qiáng)的try語句關(guān)閉資源
下面小編就為大家?guī)硪黄獪\談java7增強(qiáng)的try語句關(guān)閉資源。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06SpringBoot3整合郵件服務(wù)實(shí)現(xiàn)郵件發(fā)送功能
本文介紹了spring boot整合email服務(wù),實(shí)現(xiàn)發(fā)送驗(yàn)證碼,郵件(普通文本郵件、靜態(tài)資源郵件、附件郵件),文中通過代碼示例介紹的非常詳細(xì),堅(jiān)持看完相信對你有幫助,需要的朋友可以參考下2024-05-05Java中使用Socket發(fā)送Java對象實(shí)例
這篇文章主要介紹了Java中使用Socket發(fā)送Java對象實(shí)例,本文使用對象流直接發(fā)送對象,本文同時(shí)給出代碼實(shí)例,需要的朋友可以參考下2015-05-05Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
Hibernate想要實(shí)現(xiàn)雙向的關(guān)聯(lián)就必須在映射文件的兩端同時(shí)配置<one-to-one>,另外還要在主映射的一端采用foreign外鍵關(guān)聯(lián)屬性,下面我們就一起來看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:2016-06-06Java 數(shù)據(jù)結(jié)構(gòu)之刪除鏈表中重復(fù)的結(jié)點(diǎn)
在一個(gè)排序的鏈表中,會(huì)存在重復(fù)的結(jié)點(diǎn),如何實(shí)現(xiàn)刪除該鏈表中重復(fù)的結(jié)點(diǎn),重復(fù)的結(jié)點(diǎn)不保留,并返回鏈表頭指針呢?接下來小編將帶你詳細(xì)介紹2021-12-12Java接收text/event-stream格式數(shù)據(jù)的詳細(xì)代碼
這篇文章主要介紹了java接收text/event-stream格式數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07