在Java中,可以将常量字符串作为参数传递给注解,但我不知道如何在Groovy中实现相同的效果。
例如:
例如:
@Retention(RetentionPolicy.RUNTIME)
@Target(value=[ElementType.METHOD])
public @interface MyGroovyAnnotation {
String value()
}
class MyGroovyClass {
public static final String VALUE = "Something"
@MyGroovyAnnotation(value=VALUE)
public String myMethod(String value) {
return value
}
}
在这里,如果我给方法myMethod
加上注解@MyGroovyAnnotation
,并传递一个字符串字面量,如@MyGroovyAnnotation(value="Something")
,它可以完美地工作。但是,如果我像上面的示例一样尝试传递VALUE
,就会出现以下错误:
来自Eclipse:
Groovy:Expected 'VALUE' to be an inline constant of type java.lang.String in @MyGroovyAnnotation
从GroovyConsole运行:
expected 'VALUE' to be an inline constant of type java.lang.String not a field expression in @MyGroovyAnnotation
at line: 20, column: 31
Attribute 'value' should have type 'java.lang.String'; but found type 'java.lang.Object' in @MyGroovyAnnotation
at line: -1, column: -1
有人知道我需要做什么才能让这个工作,或者它是否可能吗?感谢您提供任何帮助或洞察力。