1

我有以下代码。

import com.univocity.parsers.annotations.Parsed;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.lang.reflect.Field;

public class MyClass {
    public static void main(String args[]) {
        try{
            for (Field field : FieldUtils.getAllFieldsList(SpendsInputBean.class)) {
                String[] headerName = field.getAnnotation(Parsed.class).field();
//              ^
//              |____________(Shouldn't this be String)


                .
                .
                .
            }
        }catch(Exception ex){
            System.out.println(ex);
        }
    }
}

class X {
    @Parsed(field = "Abc")
    private String abc;
}

我的问题是Parsed(field = "Abc"),这里的字段String作为输入。但是当我在时getAnnotation(Parsed.class).field()它正在返回String[]而不是String. 为什么会出现这种奇怪的行为?

不应该getAnnotation(Parsed.class).field()回来String吗?

4

1 回答 1

4

根据 UniVocity github repo:

https://github.com/uniVocity/univocity-parsers/blob/master/src/main/java/com/univocity/parsers/annotations/Parsed.java

只有 1 个方法field()的返回类型是String[]and not String

 String[] field() default {};  

编辑:

对于问题的第二部分,即为什么Parsed(field = "Abc")允许 - 这是因为:

如果元素类型是数组类型,则不需要使用花括号来指定元素值对的元素值

我已经从这个文档中引用了上述声明,您可以参考。附加参考:SO 帖子

于 2019-05-02T11:21:07.917 回答