4

I have some problem with creating my own annotations in Kotlin. I have to create some annotations and in some of them i need to declare values with array type. In java we can do this:

public @interface JoinTable {
...
    JoinColumn[] inverseJoinColumns() default {};
...
}

Where JoinColumn is also an annotation type.

I want to do something like that in Kotlin:

annotation class JoinTable(
    val name: String,
    val joinColumns: Array<JoinColumn>
)

I also tried to do this:

annotation class JoinTable(
    val name: String,
    val joinColumns: List<JoinColumn>
)

But my IDe says:

Invalid type of annotation member

What should i do?

Thank you!

mfulton26
  • 29,956
  • 6
  • 64
  • 88
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34

2 Answers2

4

So, it was my big fault. I didn't notice that JoinColumn in my realization isn't an annotation.

class JoinColumn()

Well, it fixed ^_^:

annotation class JoinColumn()
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
1

As in java, the values for annotations must be available at compile time. That means that val joinColumns: List<JoinColumn> is not possible if the JoinColumn is a usual class or data-class. If it's an enum class (enum class JoinColumn), than it's possible to use it.

See also the official kotlin documentation for allowed types https://kotlinlang.org/docs/reference/annotations.html

Allowed parameter types are:

  • types that correspond to Java primitive types (Int, Long etc.);
  • strings;
  • classes (Foo::class);
  • enums;
  • other annotations;
  • arrays of the types listed above.

Annotation parameters cannot have nullable types, because the JVM does not support storing null as a value of an annotation attribute.

guenhter
  • 11,255
  • 3
  • 35
  • 66