访问注解中的静态字段

问题描述:

我尝试在 Groovy 类中使​​用 Java 注释,但无法将 Java 类的静态字段设置为参数:

Im trying use a Java annotation in a Groovy class but have trouble to set a static field of a java class as a parameter:

注解:Id.java

package x.y.annotations;

import java.lang.annotation.ElementType;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Id {

    public Class<Adapter> adapter();

    public Class<Object> targetType();

    public String targetAttribute();

    public String onDelete();

}

带有静态字段的java类:XPerson.java

The java class with the static fields: XPerson.java

package x.y.static.domain;

public class XPerson {

    public static String ID;

}

还有问题所在的groovy类:Person.groovy

And the groovy class, where the problem occurs: Person.groovy

package x.y.domain

import x.y.annotations.Id
import x.y.static.domain.XPerson

class Person {

    @Id(adapter = Adapter, targetType = XPerson, targetAttribute = XPerson.ID, onDelete = "delete")
    long id
}

Eclipse 将targetAttribute = XPerson.ID"部分标记为:

Eclipse marks the "targetAttribute = XPerson.ID" part with:

Groovy:期望 'x.y.domain.XPerson.ID' 是 java.lang.String 类型的内联常量,而不是 @x.y.annotations.Id 中的属性表达式

Groovy:expected 'x.y.domain.XPerson.ID' to be an inline constant of type java.lang.String not a property expression in @x.y.annotations.Id

我也尝试过诸如XPerson.@ID"或为 ID 字段定义 getter 之类的方法,但没有任何帮助.

I also tried things like "XPerson.@ID" or defining a getter for the ID field, but nothing helped.

任何提示都会很棒.

问候,迈克尔

注解值只能在编译时使用 常量表达式.使字段 final 是一个选项.(需要注意的是,正如代码段所暗示的那样,该字段不能在静态初始值设定项/等中进行初始化.)

Annotation values may only be compile-time constant expressions. Making the field final is an option. (With the caveat that the field can't be initialized in a static initializer/etc. as the snippet implies.)