I have two enums ```enum class Entity { PROJEC...
# announcements
j
I have two enums
Copy code
enum class Entity {
   PROJECT, WORKSPACE, ...
Copy code
enum class Access {
   UPDATE, REMOVE, GRANT, ...
and i want to write an annotation
ProjectPermission("$PROJECT.$UPDATE", "$PROJECT.$READ")
Copy code
@Repeatable
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class ProjectPermission(vararg val permission: String)
Problem is, the annotation requires a constant, is there a way to convert the String template containing the enums to a constant? The other alternative is to write annotations
@ProjectPermission(PROJECT, UPDATE)
, but i can only have one @ProjectPermission on my method with Runtime retention, so the string route allows for more permissions
m
There is a chance that rephrasing the problem more precisely would help people help you.
j
done, i've updated it to be clearer
m
You can’t concatenate expressions because the compiler will accept only a verifiable compile-time constant, otherwise it can’t determine it. The other solution to use enum values would be the best one. Why using @Repeatable doesn’t solve your problem?
j
if i stack multiple @ProjectPermission on a function, Kotlin complains that it can't repeat @ProjectPermission unless it's Source Retention
Copy code
Repeatable annotations with non-SOURCE retention are not yet supported
I need these to be retained in runtime so that i can pick them up via reflection, which source retention doesn't allow
m
Oh I see… didn’t know this detail. Then you can do this only with annotation arguments:
Copy code
@Retention(AnnotationRetention.RUNTIME)
annotation class Permission(val entity: Entity, val access: Access)

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class ProjectPermission(vararg val permission: Permission)

// then use it on a function:
@ProjectPermission(
        Permission(Entity.PROJECT, Access.UPDATE),
        Permission(Entity.PROJECT, Access.GRANT)
        // etc
)
fun annotated() {
}
j
ah, that could actually work
thanks fo the help!
m
Yes it works, don’t worry. You’re welcome!