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