内省Introspector操作 JavaBean的简单实现方式及稍复杂实现方式 主要用到PropertyDescriptor类



package com.base_super;

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;

/**
 * 内省:Introspector
 * 
 * @author zjw
 *
 */
public class Introspector_class {
	public static void main(String[] args) throws Exception {
//		method();
		methid1();
	}
	/*
	 * 简单Introspector实例应用
	 * 
	 * 一般情况获取:
	 * 		String propertyValue="a";
	 * 		"a"--->"A"--->"getA()"--->再通过反射,method获取值
	 * 通过PropertyDescriptor类,进行反射获取
	 * 
	 */
	public static void method() throws Exception{
		JavaBean_class bean=new JavaBean_class(3,4);
		String propertyName="a";//定义JavaBean中的属性
		
		//get,获取JavaBean中的属性值
		Object retVal = getProperty(bean, propertyName);//通过选中,单击右键--》Refactor--》Extract Method就可以抽取为方法
		System.out.println(retVal);
		
		//set,设置JavaBean中的属性值
		Object setProperty=88;
		setPropety(bean, propertyName, setProperty);//同样方法设置,抽取为方法
		System.out.println(bean.getA());
		
		//把上面的三句话合并成一句话
		new PropertyDescriptor(propertyName,bean.getClass()).getWriteMethod().invoke(bean,999);
		System.out.println(bean.getA());
	}
	
	//通过这样抽取为方法,以后可以直接用set/getProperty了,不用老是写,提高代码复用率
	private static void setPropety(JavaBean_class bean, String propertyName,
			Object setProperty) throws IntrospectionException,
			IllegalAccessException, InvocationTargetException {
		PropertyDescriptor pd1=new PropertyDescriptor(propertyName,bean.getClass());
		Method m_set=pd1.getWriteMethod();
		m_set.invoke(bean,setProperty);
	}
	//通过这样抽取为方法,以后可以直接用set/getProperty了,不用老是写,提高代码复用率
	private static Object getProperty(Object bean, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		PropertyDescriptor pd=new PropertyDescriptor(propertyName,bean.getClass());//PropertyDescriptor类用来操作JavaBean
		Method m=pd.getReadMethod();
		Object retVal=m.invoke(bean);//返回bean这个对象的方法
		return retVal;
	}
	
	
	/*
	 * 复杂Introspector应用实例
	 * 
	 * 就是调用Introspector.getBeanInfo方法,得到的BeanInfo对象封装了把这个类当做JavaBean看的结果信息
	 * getBeanInfo方法为Introspector类中的静态方法,所以可以直接调用
	 */
	public static void methid1() throws Exception{
		JavaBean_class bean =new JavaBean_class(11,22);
		String propertyName="a";
//		BeanInfo bi=Introspector.getBeanInfo(bean.getClass());
//		PropertyDescriptor[]pds=bi.getPropertyDescriptors();
//		for (PropertyDescriptor pd : pds) {
//			if(pd.getName().equals(propertyName)){
//				Method m=pd.getReadMethod();
//				m.invoke(bean);
//				break;	
//			}
//		}
		//这个方法就是根据上面注释的语句抽取出来的,为了跟上面的方法不发生冲突,我加了个后缀"_fuza"
		Object getProperty=getProperty_fuza(bean, propertyName);
		System.out.println("直接获取属性a的值:"+getProperty);
		
		Object setProperty=888;
		setProperty(bean, propertyName, setProperty);
		System.out.println("设定属性a值后的:"+bean.getA());
	}
	//通过抽取出来的,复杂一点的setProperty
	private static void setProperty(JavaBean_class bean, String propertyName,
			Object setProperty) throws IntrospectionException,
			IllegalAccessException, InvocationTargetException {
		BeanInfo bi_set=Introspector.getBeanInfo(bean.getClass());
		PropertyDescriptor[] pds_set=bi_set.getPropertyDescriptors();
		for (PropertyDescriptor pd_set : pds_set) {
			if(pd_set.getName().equals(propertyName)){
				Method m_set=pd_set.getWriteMethod();
				m_set.invoke(bean,setProperty);
				break;
			}
		}
	}
	//比上面的方法稍微复杂一点的方式
	private static Object getProperty_fuza(JavaBean_class bean, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		Object getProperty=null;
		BeanInfo bi=Introspector.getBeanInfo(bean.getClass());
		PropertyDescriptor[]pds=bi.getPropertyDescriptors();
		for (PropertyDescriptor pd : pds) {
			if(pd.getName().equals(propertyName)){
				Method m=pd.getReadMethod();
				getProperty=m.invoke(bean);
				break;
			}
		}
		return getProperty;
	}
	
	
}
class JavaBean_class{
	private int a;
	private int b;
	
	public JavaBean_class(int a,int b){
		this.a=a;
		this.b=b;
	}
	
	public int getA() {
		return a;
	}
	public void setA(int a) {
		this.a = a;
	}
	public int getB() {
		return b;
	}
	public void setB(int b) {
		this.b = b;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

King·Forward

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

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

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

打赏作者

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

抵扣说明:

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

余额充值