2

Java 1.7 の時点で、スーパークラスの BeanInfo から取得された PropertyDescriptorは、プロパティの戻り値の型がジェネリック パラメータを取る場合、その属性マップを失います。以下のテスト コードは、この動作を示しています。

import java.beans.*;
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;

public class BeanInfoTest {

    public interface Super {

        public List<String> getSuperList();

        public void setSuperList(List<String> list);

        public String[] getSuperArray();

        public void setSuperArray(String[] array);
    }

    public interface Sub extends Super {

        public List<String> getSubList();

        public void setSubList(List<String> list);
    }

    public static class SuperBeanInfo extends SimpleBeanInfo {

        private PropertyDescriptor[] props = new PropertyDescriptor[2];

        public SuperBeanInfo() throws IntrospectionException {
            props[0] = new PropertyDescriptor("superList", Super.class);
            props[1] = new PropertyDescriptor("superArray", Super.class);

            props[0].setValue("superListAttribute", new Object());
            props[1].setValue("superArrayAttribute", new Object());
        }

        @Override
        public PropertyDescriptor[] getPropertyDescriptors() {
            return props;
        }
    }

    public static class SubBeanInfo extends SimpleBeanInfo {

        private PropertyDescriptor[] props = new PropertyDescriptor[1];

        public SubBeanInfo() throws IntrospectionException {
            props[0] = new PropertyDescriptor("subList", Sub.class);
            props[0].setValue("subListAttribute", new Object());
        }

        @Override
        public PropertyDescriptor[] getPropertyDescriptors() {
            return props;
        }

        @Override
        public BeanInfo[] getAdditionalBeanInfo() {
            try {
                return new BeanInfo[]{Introspector.getBeanInfo(Super.class)};
            } catch (IntrospectionException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @Test
    public void testBeanInfo() throws IntrospectionException {

        System.out.println(System.getProperty("java.version"));

        PropertyDescriptor[] pds = Introspector.getBeanInfo(
                Sub.class).getPropertyDescriptors();

        List<String> allAttrNames = new ArrayList<String>();
        for (PropertyDescriptor pd : pds)
            allAttrNames.addAll(Collections.list(pd.attributeNames()));


        // always passes
        assertArrayEquals(pds, new PropertyDescriptor[]{
                    new PropertyDescriptor("subList", Sub.class),
                    new PropertyDescriptor("superArray", Super.class),
                    new PropertyDescriptor("superList", Super.class)
                });
        assertTrue(allAttrNames.contains("superArrayAttribute"));
        assertTrue(allAttrNames.contains("subListAttribute"));


        // passes under 1.6_43; fails under 1.7_07
        assertTrue(allAttrNames.contains("superListAttribute"));
    }
}

これはバグレポートのように読めることに気付いたので、ここに私の質問があります:

  • これは本当にバグなのか、仕様の何かを見落としたのか、それともこの状況を完全に回避するためのベスト プラクティスに従わなかったのか?

  • そうでない場合、他の誰かがこの問題に遭遇したか、Java 1.7 でパラメータ化されたプロパティ タイプを引き続き使用できるようにする回避策を知っていますか?

4

1 に答える 1

1

どうやらこれは、私が実行していたバージョン (1.7.0_07) のバグだったようです。1.7.0_17 に更新した後、問題は解決しました。

于 2013-03-14T20:46:03.757 に答える