JDK1.5新特性--内省(Introspector)

</pre><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">定义:java语言对bean类属性,时间的一种缺省的处理方法。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">正常处理方式:类A中有属性name,那么我们可以通过getName/setName来得到name的值或设置新值,通过 getName/setName来访问name属<span style="white-space:pre">	</span>性,这是默认规则。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">内省处理方式:java中提供了一套API用来访问某个属性的getter/setter方法,通过这些API可以使你不需要了解这 些规则,这些API存放在包java.bean<span style="white-space:pre">	</span>中,一般的做法是通过类Introspector来获取某个对象的BeanInfo信息, 通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属<span style="white-space:pre">	</span>性描述器就可以获取某个属性对应的 getter/setter方法,然后我们就可以通过反射来调用这些方法</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">PropertyDescriptor类:表示javaBean类通过存储器导出一个属性,主要方法:</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre">	</span>1、getPropertyType(),获得属性的Class对象。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre">	</span>2、getReadMethod(),获得用于读取属性值得方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre">	</span>3、getWriteMethod(),获得用于写入属性值得方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre">	</span>4、hashCode(),获取对象的哈希值;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre">	</span>5、setReadMethod(Method readMethod),设置读取属性值的方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="white-space:pre">	</span>6、setWriteMethod(Method writeMethod),设置用于写入属性值的方法;</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;"><span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;">实例代码如下:</span></div><pre name="code" class="java"><pre name="code" class="java">package test.com;

class UserInfo {
	private String username;

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}
}

public class PropertyDescriptorDemo {
	public static void main(String[] args) {
		UserInfo userInfo = new UserInfo();
		setProperty(userInfo, "username");
		getProperty(userInfo, "username");
	}

	public static void setProperty(User user, String username) {
		PropertyDescriptor pd = new PropertyDescriptor(username, User.class);
		Method method = pd.getWriteMethod();
		method.invoke(user, "fanjunkai");
		System.out.println("username ==:" + user.getUsername());
	}

	public static void getProperty(User user, String username) {
		PropertyDescriptor pd = new PropertyDescriptor(username, User.class);
		Method method = pd.getReadMethod();
		Object obj = method.invoke(user);
		System.out.println(obj.toString());
	}
}
 
<div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">Introspector类:</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">1、将JavaBean中的属性封装起来进行操作,程序一个类当做JavaBean来看,就是调用 Introspector.getBeanInfo()方法,BeanInfo对象把这个类当做JavaBean来看。</div><div style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 21px; font-size: 14px;">2、getPropertyDescriptors()获得属性的描述,可以采用遍历BeanInfo的方法,来查找、设置类的属性。</div>
public static void setPropertyByIntrospector(UserInfo userInfo, String username) throws Exception {
		BeanInfo beaninfo = Introspector.getBeanInfo(UserInfo.class);
		PropertyDescriptor[] pds = beaninfo.getPropertyDescriptors();
		if (pds != null && pds.length > 0) {
			for (PropertyDescriptor pd : pds) {
				if (pd.getName().equals(username)) {
					Method method = pd.getWriteMethod();
					method.invoke(userinfo, "fanjunkai");
				}
			}
		}
	}

	public static void getPropertyByIntrospector(UserInfo userinfo, String username) throws Exception {
		BeanInfo beaninfo = Introspector.getBeanInfo(UserInfo.class);
		PropertyDescriptor[] pds = beaninfo.getPropertyDescriptors();
		if (pds != null && pds.length) {
			for (PropertyDescriptor pd : pds) {
				if (pd.getName().equals(username)) {
					Method method = pd.getReadMethod();
					Object obj = method.invoke(username);
					System.out.println(obj.toString());
				}
			}
		}
	}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fjkxyl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值