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の複雑な内省操作
 
  • 便利BeanInfoのすべての属性方式を採用してあるPersonのname属性
  • を検索して設定する
  • プログラムの中で1つのクラスをJavaBeanとして見て
  • はIntrospectorを呼び出す.getBeanInfoメソッド
  • で得られた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;
    }