内省

 

内省(Introspector)是Java语言对Bean类属性、事件的一种缺省方法。

  Java中提供了一套API用来访问某个属性的getter/setter方法,这些API存在于Java.beans中。

  一般的做法是通过类Introspector的getBeanInfo方法来获取某个对象的BeanInfo信息,然后通过BeanInfo来获取属性的描述器(PropertyDescriptor),然后通过这个属性描述器就可以获取某个属性对应的getter/setter方法,然后我们可以通过反射机制来调用这些方法。

  第二种做法是直接通过PropertyDescriptor类操作Bean对象。

 

方法一代码示例

/*
        内省获取某个属性
     */
    @Test
    public void getFileds(){
        try {
            Person person = new Person("小明",6);
            BeanInfo beanInfo = Introspector.getBeanInfo(person.getClass());
            //获取类中所有 属性的描述器
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                //获取属性类型
                Class<?> propertyType = propertyDescriptors[i].getPropertyType();
                //获取属性名字
                String name = propertyDescriptors[i].getName();
                if (name.equals("name")){
                    //获取 name属性对应的 getName()方法
                    Method readMethod = propertyDescriptors[i].getReadMethod();
                    //通过反射调用 person对象de getName()方法
                    String s = (String)readMethod.invoke(person);
                    System.out.println(s);
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

  

/*
        内省设置属性的值
     */
    @Test
    public void setFileds(){
        Person person = new Person();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(person.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {

                if (propertyDescriptors[i].getName().equals("name")){
                    // 获取person对象的  setName() 方法
                    Method writeMethod = propertyDescriptors[i].getWriteMethod();
                    //  反射调用person对象的  setName() 方法
                    Object invoke = writeMethod.invoke(person, "小王");
                }
                if (propertyDescriptors[i].getName().equals("age")){
                    // 获取person对象的  setAge() 方法
                    Method writeMethod = propertyDescriptors[i].getWriteMethod();
                    //  反射调用person对象的  setAge() 方法
                    Object invoke = writeMethod.invoke(person, 8);
                }
            }
            System.out.println(person.toString());
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

 

方法二代码示例

 @Test
    public void getFilds2(){
        Person person = new Person("小周", 9);
        try {
            //使用属性描述器类获取 name属性的描述信息
            PropertyDescriptor pdName = new PropertyDescriptor("name",person.getClass());
            //获取name属性对应的 getName() 方法
            Method readMethod = pdName.getReadMethod();
            //反射调用 name属性的 getName()方法
            Object invoke = readMethod.invoke(person);
            System.out.println(invoke);
            
            //获取属性name对应的 setName() 方法
            Method writeMethod = pdName.getWriteMethod();
            Object invoke1 = writeMethod.invoke(person, "老周");

            System.out.println(person.toString());
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

 

posted @ 2022-10-23 22:05  藤原豆腐渣渣  阅读(11)  评论(0编辑  收藏  举报