Classes and their members are final in Kotlin, making it difficult to use frameworks and libraries like Spring AOP, which demand that classes be open. By making classes and their members open without the explicit “open” keyword, the all-open compiler plugin modifies Kotlin to meet the needs of those frameworks.
For instance, only classes with specific annotations like @Configuration or @Service must be open when using Spring. Such annotations can be specified with All-open.
Let's move on to creating our first plugin.
First, we have to add the plugin artifact to help build script dependencies and then apply the plugin:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: "kotlin-allopen"
We can also you can enable it using the plugins block:
plugins {
id "org.jetbrains.kotlin.plugin.allopen" version "1.7.10"
}
Next, specify the list of annotations that will make classes open:
allOpen {
annotation("com.my.Annotation")
// annotations("com.another.Annotation", "com.third.Annotation")
}
Note: If we annotate a class with com.my.Annotation, then the class and all its members will become open.
Moreover, it also works with meta-annotations:
@com.my.Annotation
annotation class MyFrameworkAnnotation
@MyFrameworkAnnotation
class MyClass // will be all-open
MyFrameworkAnnotation is annotated with the all-open meta-annotation com.my.Annotation, so it becomes an all-open annotation as well.
Note: Commonly asked Kotlin interview questions.