JavaBeans Introspector无法正确找到具有接口层次结构类型的属性 [英] JavaBeans Introspector does not correctly find property with interface hierarchy type

查看:131
本文介绍了JavaBeans Introspector无法正确找到具有接口层次结构类型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了JavaBeans规范,但没有发现这种现象.这是一个错误吗?

I read the JavaBeans specs but I found nowhere this behavior. Is it a bug ?

  • testPropertyType失败,因为需要Data

testPropertyReadable成功,因为存在DefaultBean.getMyData returning Data方法

testPropertyWritable失败,因为不存在任何DefaultBean.setMyData(Data)方法

testPropertyWritable fails because no DefaultBean.setMyData(Data) method does not exists

在JavaSE 6上测试

Tested on JavaSE 6

import java.beans.BeanInfo;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import org.junit.Test; 

public class DefaultBeanTest {

    @Test
    public void testPropertyType()
        throws Exception
    {
        final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
        for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ("class".equals(property.getName())) {
                continue;
            }

            if ("myData".equals(property.getName())) {
                if (!property.getPropertyType().equals(ModifiableData.class)) {
                    throw new AssertionError("expects " + ModifiableData.class + " but was "
                            + property.getPropertyType());
                }
            }
        }
    }


    @Test
    public void testPropertyReadable()
        throws Exception
    {
        final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
        for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ("class".equals(property.getName())) {
                continue;
            }

            if ("myData".equals(property.getName())) {
                if (property.getReadMethod() == null) {
                    throw new AssertionError("expects read method");
                }
            }
        }
    }


    @Test
    public void testPropertyWritable()
        throws Exception
    {
        final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
        for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ("class".equals(property.getName())) {
                continue;
            }

            if ("myData".equals(property.getName())) {
                if (property.getWriteMethod() == null) {
                    throw new AssertionError("expects write method");
                }
            }
        }
    }


    static interface Data {

    }

    static interface ModifiableData
            extends Data {
    }

    static class DefaultData
            implements ModifiableData {

    }

    static interface Bean {

        Data getMyData();

    }

    static interface ModifiableBean
            extends Bean {

        ModifiableData getMyData();


        void setMyData(
                ModifiableData data);
    }

    static class DefaultBean
            implements ModifiableBean {

        @Override
        public ModifiableData getMyData()
        {
            return this.data;
        }


        @Override
        public void setMyData(
                final ModifiableData data)
        {
            this.data = data;
        }


        private ModifiableData data;

    }

}

推荐答案

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6852569

好消息是针对Java 7的

The good news are for Java 7

这篇关于JavaBeans Introspector无法正确找到具有接口层次结构类型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆