设置一个枚举数组

时间:2014-11-29 16:16:27

标签: java reflection

我有一个小帮助器,它将csv读入pojo。在大多数情况下,它的工作非常好。现在我的枚举问题。

我可以填写:

  • 一个枚举
  • 枚举列表
  • 一个数组

但是我的枚举数组存在问题。以下是一些特殊情况的代码片段:

public void fillPojo(Object pojo) {
    // use setter/getter as well - using beanutils
    for(PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(pojo.getClass())) {
        if(pd.getName().equals("class")|| pd.getReadMethod() == null)
            continue;

        // get the value (based on the property name)
        String value = this.get(pd.getName());
        if(value == null || value.equals("null"))
            continue;
        try {
            // this works for normal lists and list of any enum
            if(pd.getPropertyType().isAssignableFrom(List.class)) {
                List<String> values = new ArrayList<>();
                for(String s : value.split(","))
                    values.add(s);
                pd.getWriteMethod().invoke(pojo, ConvertUtils.convert(values, pd.getPropertyType()));
            }
            else if(pd.getPropertyType().isArray()) {
               ///////////////////////// this throws a conversionException
                List<String> values = new ArrayList<>();
                for(String s : value.split(","))
                    values.add(s);
                Object[] objs = new Object[values.size()];
                for(int i = 0; i < objs.length; i++) {
                    if(StringUtils.isBlank(values.get(i)))
                        objs[i] = null;
                    else {
                        objs[i] = ConvertUtils.convert(values.get(i), pd.getPropertyType().getComponentType());
                    }
                }
                pd.getWriteMethod().invoke(pojo, objs);
               /////////////////////////

            }
            else
            if(pd.getPropertyType().isEnum()) {
                if(StringUtils.isEmpty(value) || "null".equalsIgnoreCase(value))
                    pd.getWriteMethod().invoke(pojo, (Object)null);
                else
                    pd.getWriteMethod().invoke(pojo, Enum.valueOf(pd.getPropertyType().asSubclass(Enum.class), value));
            }
            else
                pd.getWriteMethod().invoke(pojo, ConvertUtils.convert(value, pd.getPropertyType()));
        } catch (NullPointerException | IllegalAccessException | IllegalArgumentException
                | ConversionException | InvocationTargetException e) {
            System.err.println("'" + pojo.getClass().getSimpleName() + "' Problem while setting: " + pd.getName() + " with value " + value + " type: " + pd.getPropertyType().getSimpleName() + ":" + e.getMessage());
            e.printStackTrace();

        }
     }

我尝试了不同的方法来创建枚举,但我似乎无法正确创建枚举列表并设置它们而不会抛出异常。

1 个答案:

答案 0 :(得分:0)

似乎你将一个无效类型的数组传递给setter,例如你有bean setter:

public void setMyEnumArray(MyEnum[] array) {...}
然后你打电话给&#39; pd.getWriteMethod()。invoke(pojo,objs)&#39;类似于:

Object[] objs = new Object[values.size()];
...
pojo.setMyEnumArray(objs);

因此,在调用此setter之前,应将Object []转换为MyEnum []:

Object[] objs = new Object[values.size()];
...
objs = Arrays.copyOf(objs, objs.length, (Class) pd.getPropertyType());
pd.getWriteMethod().invoke(pojo, objs);