JavaBeans Introspector does not correctly find property with interface hierarchy type

本秂侑毒 提交于 2019-12-13 02:11:41

问题


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;

    }

}

回答1:


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

The good news are for Java 7



来源:https://stackoverflow.com/questions/4438815/javabeans-introspector-does-not-correctly-find-property-with-interface-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!