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:
The Annotation: 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();
}
The java class with the static fields: XPerson.java
package x.y.static.domain;
public class XPerson {
public static String ID;
}
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 marks the “targetAttribute = XPerson.ID” part with:
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
I also tried things like “XPerson.@ID” or defining a getter for the ID field, but nothing helped.
Any hints would be great.
Regards,
michael
Annotation values may only be compile-time constant expressions. Making the field
finalis an option. (With the caveat that the field can’t be initialized in a static initializer/etc. as the snippet implies.)