1

As the title, I want to check an attribute value is exists or not in a list by annotation. Ex:

private final String[] list = {"a", "b", "c", "d"}
@Includes(list = list)
private String state;

but there is an error Attribute must be constant How can I do it ? Btw, is it a good solution to check this by annotation ? Here is my annotation:

@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Includes {
    String[] list();
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Thach Huynh
  • 1,173
  • 2
  • 12
  • 24

1 Answers1

1

The error says it clearly:

The value for annotation attribute Includes.list must be an array initializer

With non-array variables, the value must be final and static, however, there is no way to do this with arrays. It means you have to do this workaround:

@Includes(list = {"a", "b", "c", "d"})
private String state;

Moreover, your array initialization is wrong, use {} instead of [].

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183