How to declare array value in Kotlin annotations?
I have some problem creating my own annotations in Kotlin. I have to create some annotations and in some of them I need to declare the values ββwith array type. In java, we can do this:
public @interface JoinTable {
...
JoinColumn[] inverseJoinColumns() default {};
...
}
If JoinColumn is also an annotation type.
I want to do something like this in Kotlin:
annotation class JoinTable(
val name: String,
val joinColumns: Array<JoinColumn>
)
I also tried this:
annotation class JoinTable(
val name: String,
val joinColumns: List<JoinColumn>
)
But my IDe says:
Invalid annotation element type
What should I do?
Thank!
source to share
As in java, annotation values ββmust be available at compile time. This means that val joinColumns: List<JoinColumn>
it is not possible if it JoinColumn
is a regular or data class. If it's an enum ( enum class JoinColumn
) class , then you can use it.
See also the official kotlin documentation for allowed types https://kotlinlang.org/docs/reference/annotations.html
Valid parameter types:
- which correspond to Java primitive types (Int, Long, etc.);
- strings;
- classes (Foo :: class);
- transfers;
- other annotations;
- arrays of the types listed above.
Annotation parameters cannot be nullable types because the JVM does not support storing null as the value of an annotation attribute.
source to share