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

Séb*_*nec 2 groovy annotations

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

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

但是,这在 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
Run Code Online (Sandbox Code Playgroud)

在 Groovy 中如何做到这一点?

Séb*_*nec 6

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

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