`

43、java.beans.PropertyDescriptor类

阅读更多

一、软件包 java.beans 

 

    包含与开发 beans 有关的类,即基于 JavaBeansTM 架构的组件

 

二、PropertyDescriptor

 

    PropertyDescriptor 描述 Java Bean 通过一对存储器方法导出的一个属性

 

public class PropertyDescriptor extends FeatureDescriptor
{
	//构造方法

	//通过调用 getFoo 和 setFoo 存取方法,为符合标准 Java 约定的属性构造一个 PropertyDescriptor
	public PropertyDescriptor(String propertyName,
                          Class<?> beanClass)
                   throws IntrospectionException{}

	//获得属性的 Class 对象
	public Class<?> getPropertyType(){}

	//获得应该用于读取属性值的方法
	public Method getReadMethod(){}

	//获得应该用于写入属性值的方法
	public Method getWriteMethod(){}
}

 

三、示例

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class IntrospectorDemo {

	public static void main(String[] args)throws Exception {
		Person p = new Person("李四",20);
		
		String propertyName = "name";
		
		Object obj = getProperty(p,propertyName);
		System.out.println(obj);
		
		Object value = "王武";
		setProperty(p, propertyName, value);
		System.out.println(p.getName());
	}

	private static void setProperty(Object obj,String propertyName,Object value)throws Exception {
		PropertyDescriptor pd = new PropertyDescriptor(propertyName,obj.getClass());
		Method setMethod = pd.getWriteMethod();
		setMethod.invoke(obj, value);
	}

	private static Object getProperty(Object obj,String propertyName) throws Exception {
		PropertyDescriptor pd = new PropertyDescriptor(propertyName,obj.getClass());
		Method getMethod = pd.getReadMethod();
		return getMethod.invoke(obj);
	}
}
class Person
{
	private String name;
	private int age;
	Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

四、JavaBean的复杂内省操作

 

  1. 采用便利BeanInfo的所有属性方式来查找和设置某个Person的name属性
  2. 在程序中把一个类当作JavaBean来看
  3. 就是调用Introspector.getBeanInfo方法
  4. 得到的BeanInfo对象封装了把这个类当作JavaBean看的结果信息
/**
 * Introspector
 * Introspector 类为通过工具学习有关受目标 Java Bean 支持的属性、事件和方法的知识提供了一个标准方法
 * 对于这三种信息,Introspector 将分别分析 bean 的类和超类,寻找显式或隐式信息,使用这些信息构建一个全面描述目标 bean 的 BeanInfo 对象。
 * 如果某个类提供有关其自身的显式 BeanInfo,
 * 则将它添加到从分析所有派生类得到的 BeanInfo 信息中,并将显式信息视为当前类及其基类的确定的信息,无需进一步深入超类链进行分析。
 * 如果没有在某个类上发现显式 BeanInfo,
 * 则使用低层次的反射来研究类的方法,并应用标准设计模式来标识属性存储器、事件源或公共方法。
 * 然后深入分析类的超类,从它那里(可能在超类链的顶部)添加信息。
 */
public class Introspector
{
	//没有构造方法,其余方法均为静态的

	//在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件
	public static BeanInfo getBeanInfo(Class<?> beanClass)
                            throws IntrospectionException{}
}

 

/**
 * BeanInfo
 * 希望提供有关其 bean 的显式信息的 bean 实现者可以提供某个 BeanInfo 类,
 * 该类实现此 BeanInfo 接口并提供有关其 bean 的方法、属性、事件等显式信息。
 */
public interface BeanInfo
{
	//获得 beans PropertyDescriptor
	PropertyDescriptor[] getPropertyDescriptors();

	//获得 beans MethodDescriptor
	MethodDescriptor[] getMethodDescriptors();
}

 

//上述示例,用复杂的内省操作,代码如下
private static Object getProperty(Object obj,String propertyName) throws Exception 
{
	BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
	PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
	Object retVal = null;
	for(PropertyDescriptor pd : pds)
	{
		if(pd.getName().equals(propertyName))
			retVal = pd.getReadMethod().invoke(obj);
	}
	return retVal;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics