package pkg;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD})
public @interface Tag {
String value() default "";
}
Additional test we could add to AnnotationTest and improve:
void testUsingClassStaticPublicFieldsAsAnnotationParameterValue() { assertScript """ import java.lang.annotation.* @Retention(RetentionPolicy.RUNTIME) @Target([ElementType.METHOD, ElementType.FIELD]) @interface Tag { String value() default "" String[] array() default [] } class TagType { public static final String TAG_ONE = "1" public static final String[] TAG_TWO = ["2a", "2b"] } class GroovyClassWithAnnotationsAndConstants { @Tag(TagType.TAG_ONE) private String annotatedField @Tag(value = TagType.TAG_ONE) void annotatedMethod() { } @Tag(array = TagType.TAG_TWO) String annotatedProperty @Tag(array = TagType.TAG_TWO) int otherAnnotatedMethod(int i) { i } @Tag(array = TagType.TAG_ONE) String helloAnnotatedMethod() { "hello" } @Tag(array = [TagType.TAG_ONE]) String hiAnnotatedMethod() { "hi" } @Tag(array = [TagType.TAG_ONE, TagType.TAG_ONE]) String byeAnnotatedMethod() { "bye" } } assertEquals TagType.TAG_ONE, GroovyClassWithAnnotationsAndConstants.class.getDeclaredField('annotatedField').annotations[0].value() // more asserts here """ }