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

