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!

+3


source to share


2 answers


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.

+1


source


So this was my big mistake. I didn't notice that JoinColumn in my implementation is not annotated.

class JoinColumn()

      



Well that's fixed ^ _ ^:

annotation class JoinColumn()

      

+3


source







All Articles