Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin repeatable @annotations don't work on jdk-8

I have declared a repeatable annotation @Parameter in kotlin as below:

@Repeatable
annotation class Parameter(val name: String);

but when I use it as below the compiler reports an Error:

Only annotations with SOURCE retention can be repeated on JVM version before 1.8

@Parameter("foo")
@Parameter("bar")
fun repeat() = 1;

I'm sure I'm working with jdk-8 in kotlin. and the option jvmTarget also is set to 1.8 for kotlin-1.1.2 gradle plugin.

Q: Why it doesn't works fine?

sourceCompatibility = 1.8
targetCompatibility = 1.8

compileKotlin {
    kotlinOptions{
        jvmTarget = "1.8"
    }
}
like image 228
holi-java Avatar asked Jun 14 '17 11:06

holi-java


People also ask

How do I use annotations in Kotlin with Java?

If you need to specify a class as an argument of an annotation, use a Kotlin class ( KClass ). The Kotlin compiler will automatically convert it to a Java class, so that the Java code can access the annotations and arguments normally. Copied! Annotations can also be used on lambdas.

Does Kotlin work with AdoptOpenJDK 8 and 11?

But in general it's possible to use any JDK version. I think AdoptOpenJDK is a specific vendor. Does Kotlin work the same with both JDK 8 and 11? Also, IntelliJ does NOT come with an embedded JDK, right? Because I've had a few people claim that now. Yes, Kotlin works with JDK 8 and 11.

Can I repeat the same annotation in Java 8?

With Java 8, you are able to repeat the same annotation to a declaration or type. For example, to register that one class should only be accessible at runtime by specific roles, you could write something like:

Is Lang instance valid in Kotlin?

In below example, we assume that an Lang instance is valid if the value of the name is either Kotlin or Java. Kotlin also provides certain in-built annotations, that are used to provide more attributes to user-defined annotations. To be precise, these annotations are used to annotate annotations.


1 Answers

If I'm not mistaken, Kotlin compiler currently targets the JDK 1.6 class file format. This means that, on Java, it can't write multiple annotations to the class file.

While conceptually Kotlin supports multiple annotations, until there's proper 1.8 targeting, it can't do so because of the output restrictions.

like image 194
Alexander Romanov Avatar answered Sep 25 '22 03:09

Alexander Romanov