如何为 Groovy 中的多个目标定义注释?

How to define an annotation for more than one target in Groovy?

在Java中,要为多个目标定义注释,可以使用花括号:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface AnnotExample {
    String name();
}

但是,这在 Groovy 中不起作用:

$ groovyc AnnotExample.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
AnnotExample.groovy: 8: expecting '}', found ',' @ line 8, column 26.
   @Target({ElementType.TYPE, ElementType.FIELD})
                            ^

1 error

如何在 Groovy 中完成?

在 Groovy 中,语法是列表一——即带方括号:

@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE, ElementType.FIELD])
public @interface AnnotExample {
    String name()
}