下面的代码很简单,编译也没有问题,但是执行报错,小弟我能力有限,发现不了原因,请各位大大帮忙,多谢!//----------------------------------
import java.beans.Introspector;public class TestIndexProperty extends Base {    public static void main(String[] args) throws Throwable {
        Introspector.getBeanInfo(TestIndexProperty.class);
    }    public Integer[] getProp() {
        return null;
    }    public Integer getProp(int i) {
        return null;
    }}class Base {
    public Number[] getProp() {
        return null;
    }    public Number getProp(int i) {
        return null;
    }
}
//----------------------------------
异常如下:
Exception in thread "main" java.lang.AssertionError: java.beans.IntrospectionException: type mismatch between indexed and non-indexed methods: prop
at java.beans.IndexedPropertyDescriptor.<init>(IndexedPropertyDescriptor.java:425)
at java.beans.Introspector.processPropertyDescriptors(Introspector.java:674)
at java.beans.Introspector.getTargetPropertyInfo(Introspector.java:612)
at java.beans.Introspector.getBeanInfo(Introspector.java:404)
at java.beans.Introspector.getBeanInfo(Introspector.java:168)
at TestIndexProperty.main(TestIndexProperty.java:6)
Caused by: java.beans.IntrospectionException: type mismatch between indexed and non-indexed methods: prop
at java.beans.IndexedPropertyDescriptor.findIndexedPropertyType(IndexedPropertyDescriptor.java:341)
at java.beans.IndexedPropertyDescriptor.setIndexedReadMethod(IndexedPropertyDescriptor.java:177)
at java.beans.IndexedPropertyDescriptor.<init>(IndexedPropertyDescriptor.java:416)
... 5 more

解决方案 »

  1.   


    import java.beans.Introspector;public class TestIndexProperty extends Base { public static void main(String[] args) throws Throwable {
    Introspector.getBeanInfo(TestIndexProperty.class);
    } public Integer[] getProp() {
    return null;
    } public Integer[] getProp(int i) {
    return null;
    }}class Base {
    public Number[] getProp() {
    return null;
    } public Number[] getProp(int i) {
    return null;
    }}bean的类型不匹配。
    一个用Number[] , 一个用Number;但这两个是同一个bean属性 prop
      

  2.   

    我怀疑是不是JDK的Bug,在继承关系上判断属性的类型出现了错误
      

  3.   

    没有出错;你一个返回数组;一个返回Number; 但使用的是同一个 属性prop
    所以匹配不上
      

  4.   

    但是我的子类能保证返回的一定是Number的子类Integer,这样在使用时能更方便一些,不用转型了。
    我觉得在代码编写上应该是没有什么问题的,而且也会存在这种场景,只是jdk的处理可能有bug
      

  5.   

    不是Numbre转Integer的问题。这个能转的。
    是Number 与 Number[]的问题; 这是两个完全不同的类型。后一个是数组
      

  6.   

    是Number 与 Number[]不是问题,本来JavaBean规范中就支持这两种属性,普通属性和索引属性,如果将上述结构中的继承关系去掉,下面的代码不会报错,也就是说,如果没有继承,这种关系JDK是能处理好的,但是为什么有继承之后就处理不好了呢?import java.beans.Introspector;public class TestIndexProperty {    public static void main(String[] args) throws Throwable {
            Introspector.getBeanInfo(TestIndexProperty.class);
        }    public Integer[] getProp() {
            return null;
        }    public Integer getProp(int i) {
            return null;
        }}