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

来源:互联网 发布:教师培训挂机软件 编辑:程序博客网 时间:2024/05/21 17:40



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类用来操作JavaBeanMethod 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());}//通过抽取出来的,复杂一点的setPropertyprivate 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;}}


原创粉丝点击