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

从结果看可以很好地满足要求。

 

知识使我快乐

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值