標籤:
JAVA中的反射只擷取屬性的get方法
在開發過程中,我們經常需要擷取類中某些屬性的get方法,此時我們需要使用到反射,例如,我們在獲得一個對象後,需要知道該對象的哪些屬性有值,哪些沒有值,方便我們後面的處理。
譬如在我們拼SQL語句時,就需要知道哪些欄位為空白或為NULL,此時我們在拼語句的時候需要剔除掉,若是我們採用一般的判斷的辦法,則會很複雜(需要處理好SQL中的AND關鍵字的有無 ),當然,我們也有另外的解決辦法(例如將非空的鍵和值存入map中,再將map存入list集合中,然後迴圈集合做相應的處理),此處我們通過反射來處理。
Person類
package com.test;public class Person { private int id; private String name; private String empno; private String department; private String mobile; private String email; private String position; private int currentPage; public Person() { super(); } public Person(String name, String empno, String mobile) { super(); this.name = name; this.empno = empno; this.mobile = mobile; } public Person(int id, String name, String empno, String department, String mobile, String email, String position, int currentPage) { super(); this.id = id; this.name = name; this.empno = empno; this.department = department; this.mobile = mobile; this.email = email; this.position = position; this.currentPage = currentPage; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmpno() { return empno; } public void setEmpno(String empno) { this.empno = empno; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; }}
FirstAction類:
package com.test;import java.beans.IntrospectionException;import java.beans.PropertyDescriptor;import java.io.UnsupportedEncodingException;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class FirstAction { public static void main(String[] args) throws UnsupportedEncodingException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, NoSuchFieldException, SecurityException { Person person=new Person("siege","0001","18516100***"); Class<?> clazz=person.getClass(); Field field1=clazz.getDeclaredField("name"); Field field2=clazz.getDeclaredField("empno"); Field field3=clazz.getDeclaredField("department"); Field field4=clazz.getDeclaredField("position"); Field[] fields=new Field[]{field1,field2,field3,field4}; Method[] methods=clazz.getMethods(); int length=fields.length; for(int i=0;i<length;i++){ PropertyDescriptor pd=new PropertyDescriptor(fields[i].getName(),clazz); Method getMethod=pd.getReadMethod(); if(getMethod.invoke(person)!=null){ System.out.println(fields[i].getName()+"----"+getMethod.invoke(person)); } } }}
我們通過PropertyDescriptor 來擷取get方法。
結果如下:
name—-siege
empno—-0001
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
JAVA中的反射只擷取屬性的get方法