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!

1
  • 1
    Very good explanation for your first post btw! Commented Jul 6, 2017 at 13:47

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Thank you for your response! I re-checked my annotations and found the mistake. I forgot mark JoinColumn as annotation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.