Introspector實現通過反射機制獲取對象的屬性值

實現一個通用的方法的時候,我們有時候需要實現通過反射機制去獲取對應的屬性值。

下面是通過java.beans包實現這個功能的代碼。

首先我們定義一個實體類。

public class Student {

	private Integer age;
	private String name;
	private String address;
	private Integer teacher;
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Integer getTeacher() {
		return teacher;
	}
	public void setTeacher(Integer teacher) {
		this.teacher = teacher;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result + ((age == null) ? 0 : age.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((teacher == null) ? 0 : teacher.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (address == null) {
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (age == null) {
			if (other.age != null)
				return false;
		} else if (!age.equals(other.age))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (teacher == null) {
			if (other.teacher != null)
				return false;
		} else if (!teacher.equals(other.teacher))
			return false;
		return true;
	}

然後,寫一個main方法測試beans下面獲取屬性值得步驟

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo {

	public static void main(String[] args) {
		Student s = new Student();
		s.setAddress("huashan");
		s.setAge(18);
		s.setName("Nic");
		s.setTeacher(3);
		get(s);
	}
	
	public static void get(Object obj) {
		try {
			BeanInfo info = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] arr = info.getPropertyDescriptors();
			for (PropertyDescriptor pd : arr) {
				String key = pd.getName();
				java.lang.reflect.Method g = pd.getReadMethod();//獲取屬性對應的getter方法
				Object v = g.invoke(obj);
				System.out.println(g + " == " + key + ":" + v);
			}
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

最後輸出的結果是:

public java.lang.String com.wc.www.ScBus.entity.Student.getAddress() == address:huashan
public java.lang.Integer com.wc.www.ScBus.entity.Student.getAge() == age:18
public final native java.lang.Class java.lang.Object.getClass() == class:class com.wc.www.ScBus.entity.Student
public java.lang.String com.wc.www.ScBus.entity.Student.getName() == name:Nic
public java.lang.Integer com.wc.www.ScBus.entity.Student.getTeacher() == teacher:3

從結果看可以很好地滿足要求。

 

知識使我快樂

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章