JAVA中的反射只擷取屬性的get方法

來源:互聯網
上載者:User

標籤:

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方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.