Search code examples
javareflectionannotationsfieldunivocity

Why getAnnotation(Parsed.class).field() returning String[]?(It should return String, as it is taking input as String)


I have following code.

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

My question is Parsed(field = "Abc"), here field is taking String as input. But when I am getAnnotation(Parsed.class).field() it is returning String[] instead of String. Why this strange behaviour?

Shouldn't getAnnotation(Parsed.class).field() return String?


Solution

  • As per the UniVocity github repo:

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

    There is only 1 method field() whose return type is String[] and not String.

     String[] field() default {};  
    

    EDIT:

    For the second part of the question i.e. why Parsed(field = "Abc") is allowed - this is because:

    If the element type is an array type, then it is not required to use curly braces to specify the element value of the element-value pair

    I have quoted the above statement from this doc which you can refer. Additional reference : SO post.