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"
}
}
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.
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.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With