Java笔记7 Java基础加强<2>内省

时间:2022-03-26 15:05:12


30_张孝祥_Java基础加强_对JavaBean的简单内省操作.avi

l  代码演示1:

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

 

public class IntroSpectorTest {

 

    public static void main(String[] args)throws Exception {

       ReflectPoint pt1 = new ReflectPoint(3, 5);

      

       String propertyName = "x";

      

       /*把下面代码标注<1>的,但是没声明要注释的代码选中后,

        * 按快捷键ctrl+shift+T,选Extract Methoda可重构一个getProperty方法。

        * */

       PropertyDescriptor pd = new PropertyDescriptor(propertyName,ReflectPoint.class);//<1>

       Class clazz = pd.getPropertyType();//<1>

       System.out.println(clazz.toString());//<1>注释

      

       Method methodW = pd.getWriteMethod();

       System.out.println(methodW);

       Object setVal = methodW.invoke(pt1,6);//因为set方法五返回值,这里invoke方法会返回null

       System.out.println(setVal);

      

       Method methodR = pd.getReadMethod();//<1>

       System.out.println(methodR);//注释

       Object retVal = methodR.invoke(pt1);//注意,x的值改变了,变为:6<1>

       //Object retVal =methodR.invoke(pt1,null);//当方法的形参为空时,形参也可以用null表示。

       System.out.println(retVal);

    }

}

 

l  重构后的方法演示:

public class IntroSpectorTest {

 

    public static void main(String[] args)throws Exception {

       ReflectPoint pt1 = new ReflectPoint(3,5);

      

       String propertyName = "x";

       //"x"-->"X"-->"getX"-->MethodGetX-->

       Object retVal = getProperty(pt1, propertyName);

       System.out.println(retVal);

      

       Object value = 7;

      

       setProperties(pt1, propertyName, value);

 

       System.out.println(BeanUtils.getProperty(pt1,"x").getClass().getName());

       BeanUtils.setProperty(pt1, "x", "9");

       //System.out.println(pt1.getX());

       /*

       //java7的新特性

       Map map = {name:"zxx",age:18};

       BeanUtils.setProperty(map, "name", "lhm");

       */

       BeanUtils.setProperty(pt1, "birthday.time", "111");// //setProperty方法可以设置属性的级联属性(即属性的属性..)

       System.out.println(BeanUtils.getProperty(pt1,"birthday.time"));

      

       PropertyUtils.setProperty(pt1, "x", 9);

       System.out.println(PropertyUtils.getProperty(pt1,"x").getClass().getName());

      

    }

 

    private static void setProperties(Object pt1, String propertyName,

           Object value) throws IntrospectionException,

           IllegalAccessException, InvocationTargetException {

       PropertyDescriptor pd2 = newPropertyDescriptor(propertyName,pt1.getClass());

       Method methodSetX = pd2.getWriteMethod();

       methodSetX.invoke(pt1,value);

    }

 

    private static Object getProperty(Object pt1, String propertyName)

           throws IntrospectionException, IllegalAccessException,

           InvocationTargetException {

       /*PropertyDescriptor pd= new PropertyDescriptor(propertyName,pt1.getClass());

       Method methodGetX = pd.getReadMethod();

       Object retVal = methodGetX.invoke(pt1);*/

      

       BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());

       PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

       Object retVal = null;

       for(PropertyDescriptor pd : pds){

           if(pd.getName().equals(propertyName))

           {

              Method methodGetX = pd.getReadMethod();

              retVal = methodGetX.invoke(pt1);

              break;

           }

       }

       return retVal;

    }

 

}