PropertyDescriptor


PropertyDescriptorクラスを使用するとBean規約に則っている
setter、getterを取得することができる。



main.java

package harada;

import java.lang.Object;
import java.beans.PropertyDescriptor;
import java.lang.reflect.*;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.lang.IllegalAccessException;

/**
 * @author Administrator
 *
 * PropertyDescriptorでの取得テスト
 */
public class main {
	
	public static void main(String[] args) {
		
		System.out.print("Start*");
		
		// propGet pg = new propGet();
		haradaBean bean = new haradaBean();
		
		// 通常の取得
		bean.setAddress("hukuoka");
		System.out.print(",通常での取得=" + bean.getAddress());
		
		// PropertyDescriptorでの取得1
		Object rtnVal =  getProperty (bean,"address");
		System.out.print(",PropertyDescriptorでの取得=" + rtnVal.toString() );
		
		// PropertyDescriptorでの設定2
		String setVal = new String("go");
		setProperty (bean,"firstName",setVal);

		// PropertyDescriptorでの取得2
		rtnVal =  getProperty (bean,"firstName");
		System.out.print(",PropertyDescriptorでの取得=" + rtnVal.toString() );
		
		System.out.print("*End");
	}		
	
	/**
	 * <dd>メソッド名:getProperty
	 * <dd>メソッド説明:プロパティの値を取得する。
	 * <dd>備考:
	 * @param	obj		取得元オブジェクト
	 * @param	prop	    プロパティ名
	 * @return	Object	取得したプロパティ値
	 */
	public static Object getProperty(Object obj, String prop) {
		/** 返値用のオブジェクト */
		Object value = null;
		
		/** プロパティディスクリプタ */
		PropertyDescriptor descriptor = null;
		
		/** Getter取得用メソッド */
		Method method = null;

		try {
			// 対象プロパティのディスクリプタを取得します。
			descriptor = new PropertyDescriptor(prop, obj.getClass());
			
			// Getterを取得します。
			method = descriptor.getReadMethod();
			
			// 取得したGetterを使用してプロパティ値を取得します。
			value = method.invoke(obj, new Object[0]);
		}
		catch ( IntrospectionException e ) {
			System.out.print(e.getMessage());
		}
		catch ( IllegalAccessException e ) {
			System.out.print(e.getMessage());
		}
		catch ( IllegalArgumentException e ) {
			System.out.print(e.getMessage());
		}
		catch ( InvocationTargetException e ) {
			System.out.print(e.getMessage());
		}

		return value;

	}

	/**
	 * <dd>メソッド名:setProperty
	 * <dd>メソッド説明:プロパティを設定する。
	 * <dd>備考:
	 * @param	obj		取得元オブジェクト
	 * @param	prop	    プロパティ名
	 * @param	value   	設定する値
	 */
	public static void setProperty(Object obj, String prop, Object value) {
		
		/** プロパティディスクリプタ */
		PropertyDescriptor descriptor = null;
		
		/** 引数用Object配列 */
		Object args[] = new Object[]{value};
		
		/** Setter取得用メソッド */
		Method method = null;

		try {
			// 対象プロパティのディスクリプタを取得します。
			descriptor = new PropertyDescriptor(prop, obj.getClass());
			
			// Setterを取得します。
			method = descriptor.getWriteMethod();
			
			// 取得したSetterを使用してプロパティ値を設定します。
			method.invoke(obj, args);
			
		}
		catch ( IntrospectionException e ) {
			System.out.print(e.getMessage());
		}
		catch ( IllegalAccessException e ) {
			System.out.print(e.getMessage());
		}
		catch ( IllegalArgumentException e ) {
			System.out.print(e.getMessage());
		}
		catch ( InvocationTargetException e ) {
			System.out.print(e.getMessage());
		}

	}
}


haradaBean.java

package harada;

/**
 * @author Administrator
 *
 * Bean
 * 
 */
public class haradaBean {

    /**
     * address
     */
     private String address;

     public String getAddress() {
         return this.address;
     }

     public void setAddress(String value) {
         this.address = value;
     }
     
     /**
     * firstName
     */
     private String firstName;

     public String getFirstName() {
         return this.firstName;
     }

     public void setFirstName(String value) {
         this.firstName = value;
     }
     
}



実行方法


eclipseより→メニューバー「実行」→プロジェクトに「harada」(package名)、メインクラスに「harada.main」(パッケージ名 + クラス名)を指定して「実行」