JavaBeans Introspector 无法正确查找具有接口层次结构类型的属性

发布于 2024-10-07 20:42:00 字数 3149 浏览 0 评论 0原文

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

  • testPropertyType 失败,因为需要 Data

  • testPropertyReadable 成功,因为 DefaultBean.getMyData 返回数据 方法存在

  • testPropertyWritable 失败,因为没有 DefaultBean.setMyData(Data) 方法不存在

在 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;

    }

}

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

  • testPropertyType fails because expects Data class

  • testPropertyReadable succeed because DefaultBean.getMyData returning Data method exists

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

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;

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文