Default array value in Groovy annotation

Viewed 118

I have enum and annotation like:

enum Permission { user, superuser, admin }

@CompileStatic
@interface Anno {
  Permission[] value
}

Now I want to supply a default value to value, which should be [ Permission.user ].

I tried different groovy tricks:

  Permission[] value default [user]
  Permission[] value default ([user] as Permission[])
  Permission[] value default (Permission[])[user]
  ...

but none of them worked, so as a workaround I implemented the annotation in j4v4:

public @interface Anno {
  Permission[] value default { user };
}

What am I missing?

UPDATE:

The solution was to remove @CompileStatic from the annotation. With @CompileStatic the compile threw:

[Static type checking] - Cannot return value of type java.util.List <io.domain.Permission> on method returning type io.domain.Permission[]

1 Answers

It's a current naming limitation which should be removed but for now you need either:

Permission[] value() default [Permission.user]

or before you use user:

import static Permission.user

and then your original (with missing brackets added) is fine:

Permission[] value() default [user]
Related