简体   繁体   中英

Default array value in Groovy annotation

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[]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM