这个问题似乎很尴尬,但是在检索javabean的PropertyDescriptors时,我们面临一个奇怪的行为。
这是在1.6、1.7和1.8的简单代码的执行结果,这些代码已按照1.6的标准进行了编译。

Java 1.6执行:

java.beans.PropertyDescriptor@4ddc1428 java.beans.IndexedPropertyDescriptor@7174807e

Java 1.7执行:

java.beans.PropertyDescriptor [name = class; propertyType = class java.lang.Class; readMethod = public最终 native java.lang.Class java.lang.Object.getClass()] java.beans.IndexedPropertyDescriptor [name = values; indexedPropertyType = class java.lang.String; indexedReadMethod = public java.lang.String JavaBean.getValues(int)]

Java 1.8执行:

java.beans.PropertyDescriptor [name = class; propertyType = class java.lang.Class; readMethod = public最终 native java.lang.Class java.lang.Object.getClass()] java.beans.PropertyDescriptor [name = values; propertyType =接口(interface)java.util.List; readMethod = public java.util.List JavaBean.getValues()]

为什么改变了?

javabean规范说明了如何使用索引访问属性。使用数组作为索引属性的容器并不是强制性的。我错了吗?

我阅读了规范,第8.3.3章讨论了索引属性的设计模式,而不是严格的规则。

如何在不重构所有应用程序的情况下使以前的行为再次出现?
感谢您的回答,

JavaBean类

import java.util.ArrayList;
import java.util.List;


public class JavaBean {


  private List<String> values = new ArrayList<String>();


  public String getValues(int index) {
  return this.values.get(index);
  }


  public List<String> getValues() {
  return this.values;
  }
}

主类
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

public class Test {
    public static void main(String[] args) throws IntrospectionException {
         PropertyDescriptor[] descs =
         Introspector.getBeanInfo(JavaBean.class).getPropertyDescriptors();
         for (PropertyDescriptor pd : descs) {
         System.out.println(pd);
         }
    }
}

最佳答案

根据JavaBeans 1.01规范的第7.2节“索引属性”:



第8.3节描述了在没有显式BeanInfo的情况下introspection可以识别的设计模式。第8.3.3节说,只有数组属性会触发索引属性的自动识别。

您在技术上是正确的;使用数组不是强制性的。但是,如果不这样做,那么规范说明您必须提供自己的BeanInfo才能将该属性公开为索引属性。

因此,对您的问题的标题的答案是:是的,Java 1.8符合JavaBean规范。

我不确定为什么支持列表属性。也许将来的JavaBeans规范将支持它们,此规范后来被撤消了。

关于最后一个问题:我认为您必须为具有List属性的每个类创建一个BeanInfo类。我希望您可以创建一个通用的父类(super class)来简化它,例如:

public abstract class ListRecognizingBeanInfo
extends SimpleBeanInfo {

    private final BeanDescriptor beanDesc;
    private final PropertyDescriptor[] propDesc;

    protected ListRecognizingBeanInfo(Class<?> beanClass)
    throws IntrospectionException {
        beanDesc = new BeanDescriptor(beanClass);

        List<PropertyDescriptor> desc = new ArrayList<>();

        for (Method method : beanClass.getMethods()) {
            int modifiers = method.getModifiers();
            Class<?> type = method.getReturnType();

            if (Modifier.isPublic(modifiers) &&
                !Modifier.isStatic(modifiers) &&
                !type.equals(Void.TYPE) &&
                method.getParameterCount() == 0) {

                String name = method.getName();
                String remainder;
                if (name.startsWith("get")) {
                    remainder = name.substring(3);
                } else if (name.startsWith("is") &&
                           type.equals(Boolean.TYPE)) {
                    remainder = name.substring(2);
                } else {
                    continue;
                }

                if (remainder.isEmpty()) {
                    continue;
                }

                String propName = Introspector.decapitalize(remainder);

                Method writeMethod = null;
                Method possibleWriteMethod =
                    findMethod(beanClass, "set" + remainder, type);
                if (possibleWriteMethod != null &&
                    possibleWriteMethod.getReturnType().equals(Void.TYPE)) {

                    writeMethod = possibleWriteMethod;
                }

                Class<?> componentType = null;
                if (type.isArray()) {
                    componentType = type.getComponentType();
                } else {
                    Type genType = method.getGenericReturnType();
                    if (genType instanceof ParameterizedType) {
                        ParameterizedType p = (ParameterizedType) genType;
                        if (p.getRawType().equals(List.class)) {
                            Type[] argTypes = p.getActualTypeArguments();
                            if (argTypes[0] instanceof Class) {
                                componentType = (Class<?>) argTypes[0];
                            }
                        }
                    }
                }

                Method indexedReadMethod = null;
                Method indexedWriteMethod = null;

                if (componentType != null) {
                    Method possibleReadMethod =
                        findMethod(beanClass, name, Integer.TYPE);
                    Class<?> idxType = possibleReadMethod.getReturnType();
                    if (idxType.equals(componentType)) {
                        indexedReadMethod = possibleReadMethod;
                    }

                    if (writeMethod != null) {
                        possibleWriteMethod =
                            findMethod(beanClass, writeMethod.getName(),
                                Integer.TYPE, componentType);
                        if (possibleWriteMethod != null &&
                            possibleWriteMethod.getReturnType().equals(
                                Void.TYPE)) {

                            indexedWriteMethod = possibleWriteMethod;
                        }
                    }
                }

                if (indexedReadMethod != null) {
                    desc.add(new IndexedPropertyDescriptor(propName,
                        method, writeMethod,
                        indexedReadMethod, indexedWriteMethod));
                } else {
                    desc.add(new PropertyDescriptor(propName,
                        method, writeMethod));
                }
            }
        }

        propDesc = desc.toArray(new PropertyDescriptor[0]);
    }

    private static Method findMethod(Class<?> cls,
                                     String name,
                                     Class<?>... paramTypes) {
        try {
            Method method = cls.getMethod(name, paramTypes);
            int modifiers = method.getModifiers();
            if (Modifier.isPublic(modifiers) &&
                !Modifier.isStatic(modifiers)) {

                return method;
            }
        } catch (NoSuchMethodException e) {
        }

        return null;
    }

    @Override
    public BeanDescriptor getBeanDescriptor() {
        return beanDesc;
    }

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

09-10 03:14