通過PropertyDescriptor和Introspector對JavaBean的簡單內省操作

</pre><pre name="code" class="java">package com.franky.bean;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @描述 利用PropertyDescriptor類或Introspector類
 * 		 進行javabean的內省操作,獲取讀寫方法
 * @作者 franky
 * @日期 2014-12-31 上午11:16:35
 */
public class JavaBeanTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Point point = new Point(4, 5);
		String propertyName = "x";
		//按照javabean的方式來對對象進行內省操作
		
		//獲取讀的方法
		Object retVal = getReadMethod(point, propertyName);
		//另一種獲取讀的方法
		Object retVals = getReadMethod2(point, propertyName);
		System.out.println(retVal);
		
		//獲取寫的方法,並設置值
		Object value = 9;
		getWriteMethod(point, propertyName, value);
		System.out.println(point.getX());

	}

	/**
	 * 獲取bean對象的寫方法,並修改值
	 * @param obj 內省javabean對象
	 * @param propertyName 屬性名
	 * @param value 傳入要修改的值對象
	 */
	private static void getWriteMethod(Object obj, String propertyName,
			Object value) throws IntrospectionException,
			IllegalAccessException, InvocationTargetException {
		PropertyDescriptor property2 = new PropertyDescriptor(propertyName , obj.getClass());
		Method writeMethod = property2.getWriteMethod();
		writeMethod.invoke(obj, value);
	}

	/**
	 * 獲取bean對象的讀方法,並返回屬性的值
	 * @param obj 內省javabean對象
	 * @param propertyName 屬性名
	 * @return 返回屬性值
	 */
	private static Object getReadMethod(Object obj, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		PropertyDescriptor property = new PropertyDescriptor(propertyName , obj.getClass());
		Method readMethod = property.getReadMethod();
		Object retVal = readMethod.invoke(obj, null);
		return retVal;
	}
	/**
	 * 另一種獲取bean對象的讀方法,並返回屬性的值
	 * @param obj 內省javabean對象
	 * @param propertyName 屬性名
	 * @return 返回屬性值
	 */
	private static Object getReadMethod2(Object obj, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
			Object retVal = null; 
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
			for (int i = 0; i < descriptors.length; i++) {
				if(propertyName.equals(descriptors[i].getName())){
					Method readMethod = descriptors[i].getReadMethod();
					retVal = readMethod.invoke(obj, null);
				}
			}
		return retVal;
	}

}

Point類:

package com.franky.bean;

public class Point {
		private int x;
		private int y;
		
		/**
		 * @return the x
		 */
		public int getX() {
			return x;
		}
		/**
		 * @param x the x to set
		 */
		public void setX(int x) {
			this.x = x;
		}
		/**
		 * @return the y
		 */
		public int getY() {
			return y;
		}
		/**
		 * @param y the y to set
		 */
		public void setY(int y) {
			this.y = y;
		}
		public Point(int x, int y) {
			super();
			this.x = x;
			this.y = y;
		}
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章