`
zhangwei_david
  • 浏览: 469142 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java 之 PropertyDescriptor

    博客分类:
  • Java
 
阅读更多

   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)

 

 

0
0
分享到:
评论

相关推荐

    Java通过PropertyDescriptor反射调用set和get方法

    主要为大家详细介绍了Java通过PropertyDescriptor反射调用set和get方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    java基础类库文件rt.jar

    Java基础类库,也就是Java doc里面看到的所有的类的class文件。

    openbeans1.0.zip

    //import java.beans.PropertyDescriptor; import com.googlecode.openbeans.BeanInfo; import com.googlecode.openbeans.IntrospectionException; import com.googlecode.openbeans.Introspector; import ...

    Java中的反射机制

    import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * */ /** * @ClassName: ReflectTest * @Description: TODO(这里用一句话描述这个类的作用...

    android-logging-log4j-1.0.2.jar

    PropertyDescriptor. Not all classes of this package are supported in Android. See javadoc of Android's java.beans package. There is a project called android-logging-log4j, which provides a ...

    根据数据库表自动产生javabean

    PropertyDescriptor pd = new PropertyDescriptor(propertyName, clazz); Method mt = pd.getWriteMethod(); Type t [] = ms[i].getParameterTypes(); Object [] params = new Object[t.length]; for(int j...

    beanutils-1.9.3-bin

    在由JDK提供的默认的API中,有java.beans包,里面有诸如Introspector,BeanInfo,PropertyDescriptor等用来操作JavaBean的方法, 但是由Apache公司开发的BeanUtils会更常用,同时,BeanUtils还需要配合第三方日志...

    C# dataGridVeiw多维表头

    C#自定义控件,DataGridView多维表头.

    openbeans.rar

    在android studio 中无法使用 BeanInfo,IntrospectionException,Introspector, PropertyDescriptor 这些类,只需要导入这个jar 库就可以了。 只不过,要把java.beans.BeanInfo; 替换为 ...

    C# WinForm 利用NPOI 自定义[表头|列表|表尾]设计导出Execl格式源码【原创】【可直接运行】

    C# WinForm 工作中遇到一个需要将界面表格数据按照设定的格式[表头|...完整版还有自动反射字段中文名称方便客户自己编辑 时间匆忙就懒得分离代码上传 了 原理很简单 字段自定义属性[PropertyDescriptor] 然后反射就好了

    learnTS:TypeScript项目开发

    * @param propertyKey 元素名称 * @param descriptor 要应用装饰器的方法的描述符 */function Admin ( target : any , propertyKey : string | symbol , descriptor : PropertyDescriptor ) { let originalMethod = ...

    HibernateValidatorJSR303的参考实现使用指南.pdf

    6.2. PropertyDescriptor 6.3. ElementDescriptor 6.4. ConstraintDescriptor 7. 与其他框架集成 7.1. OSGi 7.2. 与数据库集成校验 7.3. ORM集成 7.3.1. 基于Hibernate事件模型的校验 7.3.2. JPA 7.4. 展示层校验 8....

    从JavaScript属性描述器剖析Vue.js响应式视图

    对于对象内的属性,JavaScript提供了一个属性描述器接口PropertyDescriptor,大部分开发者并不需要直接使用它,但是很多框架和类库内部实现使用了它,如avalon.js,Vue.js,本篇介绍属性描述器及相关应用。...

Global site tag (gtag.js) - Google Analytics