Nerrve Nerrve - 5 days ago 4x
Groovy Question

Groovy - Unable to use static final string in an annotation

Something is really wierd.
I'm using a static final String in an annotation value.

class Constants {
static final String myConstant = "ting tong"

class Service {
@CacheEvict(cacheNames = Constants.myConstant)
void doSomethingNice() {

However, i just cant get it to compile.

Here's the error message

Attribute 'myConstant' should have type 'java.lang.String'; but found
type 'java.lang.Object' in

Expected 'Constants.getMyConstant()' to be an inline constant of type
java.lang.String in @org.springframework.cache.annotation.CacheEvict

What can be the problem? This works perfectly in Java


Turns out that the problem was due to the automatic getter/setter generation of Groovy. It would generate a getter for my constant and use it within the annotation, and i guess thats not allowed.

To fix, mark the field as public. That would disable the automatic getter generation.

class Constants {
    public static final String myConstant = "ting tong"