提问者:小点点

关于IndexedPropertyDescriptor,JRE 1.8是否仍然符合JavaBean规范?


这个问题看起来很尴尬,但是我们在检索javabean的属性描述符时遇到了一个奇怪的行为。下面是一段简单代码的1.6、1.7和1.8上的执行结果,它是根据1.6的合规性编译的。

Java 1.6执行:

JAVA豆。PropertyDescriptor@4ddc1428

Java1.7执行:

JAVA豆。PropertyDescriptor[name=class;propertyType=class java.lang.class;readMethod=public final native java.lang.class java.lang.Object.getClass()]

Java 1.8执行:

JAVA豆。PropertyDescriptor[name=class;propertyType=class java.lang.class;readMethod=public final native java.lang.class java.lang.Object.getClass()]

为什么会改变?

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);
         }
    }
}

共2个答案

匿名用户

来自JavaBeans 1.01规范,第7.2节“索引属性”:

组件还可以将索引属性作为单个数组值公开。

第8.3节描述了在没有明确BeanInfo的情况下,内省识别的设计模式。第8.3节。3表示只有数组属性才会触发索引属性的自动识别。

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

因此,您的问题标题的答案是:是的,Java1.8符合JavaBean规范。

我不确定为什么支持List属性。也许未来的JavaBeans规范会支持它们,但后来被撤回了。

至于最后一个问题:我认为您必须为每个具有列表属性的类创建一个BeanInfo类。我希望您可以创建一个通用的超类来简化它,例如:

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

匿名用户

我也面临同样的问题。我试图从JSP中将StartDate和EndDate保存为列表,但没有保存,值也被删除。在我的项目中,有开始日期和结束日期字段。我调试了BeanUtilsBean,然后发现字段没有writeMethod。我已经为我的类中的每个字段添加了一个setter方法,它很有效。

JAVA豆。PropertyDescriptor[name=StartDateString;propertyType=interface java.util.List;readMethod=public java.util.List com.webapp.tradingpartners.TradingPartnerNewForm.GetStartDateString()]

这是一个JavaBeans规范问题,它只允许设置空值。如果您想要这个返回setter,那么您就不能有JavaBeans兼容性,Lombok对此也无能为力。

理论上,您可以生成两个setter,但是您必须以不同的方式调用它们,每个字段有两个setter实在太糟糕了。

<cc:dateInput property='<%= "startDateStrings[" + row + "]" %>' onchange="setPropertyChangedFlag()"/>                     
<cc:dateInput property='<%= "endDateStrings[" + row + "]" %>' onchange="setPropertyChangedFlag()"/>                     
public List<String> getStartDateStrings() {
  return startDateStrings;
}
public String getStartDateStrings(int index) {
    return startDateStrings.get(index);
  }
public void setStartDateStrings(int index, String value) {
    startDateStrings.set(index, value);
  }
public List<String> getEndDateStrings() {
    return endDateStrings;
  }
public String getEndDateStrings(int index) {
    return endDateStrings.get(index);
  }
public void setEndDateStrings(int index, String value) {
    endDateStrings.set(index, value);