• Java 之 PropertyDescriptor


       PropertyDescriptor 描述了一个JavaBean 属性的一对访问方法即 getter和setter。

    常用的构造方法是PropertyDescriptor(String propertyName,Class<?> beanClass);

    propertyName就是属性的名称,beanClass就是这个属性对应属于哪个对象的Class.

    /**
     *
     * @author zhangwei_david
     * @version $Id: PropertyDescriptorDemo.java, v 0.1 2015年5月25日 下午8:17:59 zhangwei_david Exp $
     */
    public class PropertyDescriptorDemo {
    
        /**
         *
         * @param args
         * @throws IntrospectionException
         * @throws InvocationTargetException
         * @throws IllegalArgumentException
         * @throws IllegalAccessException
         */
        public static void main(String[] args) throws IntrospectionException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {
            // bean的实例
            Form form = new PropertyDescriptorDemo().new Form();
            // 创建属性name 的PropertyDescriptor
            PropertyDescriptor pd = new PropertyDescriptor("name", form.getClass());
            // 获取属性的setter方法
            Method writer = pd.getWriteMethod();
            // 反射调用setter方法设置值
            writer.invoke(form, "TEST");
            // 输入setter以后的结果
            System.out.println(form.getName());
            // 获取getter方法
            Method reader = pd.getReadMethod();
            // 获取属性值
            String value = (String) reader.invoke(form);
            // 获取属性
            String name = pd.getName();
    
            System.out.println(name + "=" + value);
    
        }
    
        /**
         *
         *  测试表单
         *
         * @author zhangwei_david
         * @version $Id: PropertyDescriptorDemo.java, v 0.1 2015年5月25日 下午8:40:29 zhangwei_david Exp $
         */
        class Form {
            /**属性name**/
            private String name;
    
            /**
             * Getter method for property <tt>name</tt>.
             *
             * @return property value of name
             */
            public String getName() {
                return name;
            }
    
            /**
             * Setter method for property <tt>name</tt>.
             *
             * @param name value to be assigned to property name
             */
            public void setName(String name) {
                this.name = name;
            }
    
        }
    }
    

    输出的结果是:

    TEST
    name=TEST

    可以发现,正确调用了setter和getter方法,如果将Form中的getter方法删除后运行的结果是什么呢?

    Exception in thread "main" java.beans.IntrospectionException: Method not found: setName
    	at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:110)
    	at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:70)
    	at com.cathy.demo.reflect.PropertyDescriptorDemo.main(PropertyDescriptorDemo.java:32)
    
  • 相关阅读:
    帮Netpole Review了一下RichEditor控件
    WawaKMv1命名空间.rtf
    WawaKMV1需求变更.rtf
    WawaKMV1技术难点.rtf
    WawaKMV1工具栏设计.rtf
    通讯录相关需求分析
    WawaKMV1Urtal Recall分析.rtf
    WawaKM:关于批量抓图的需求分析及设计
    图片相关的需求分析
    蛙蛙郑重推荐您使用firefox浏览器
  • 原文地址:https://www.cnblogs.com/wei-zw/p/8797778.html
Copyright © 2020-2023  润新知