全开放编译器插件

Kotlin has classes and their members final by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be open. The all-open compiler plugin adapts Kotlin to the requirements of those frameworks and makes classes annotated with a specific annotation and their members open without the explicit open keyword.

For instance, when you use Spring, you don’t need all the classes to be open, but only classes annotated with specific annotations like @Configuration or @Service. All-open allows to specify such annotations.

We provide all-open plugin support both for Gradle and Maven with the complete IDE integration.

For Spring, you can use the kotlin-spring compiler plugin (see below).

全开放编译器插件 - 图1

Gradle

Add the plugin using Gradle’s plugins DSL:

  1. plugins {
  2. id "org.jetbrains.kotlin.plugin.allopen" version "1.9.10"
  3. }

Then specify the list of annotations that will make classes open:

  1. allOpen {
  2. annotation("com.my.Annotation")
  3. // annotations("com.another.Annotation", "com.third.Annotation")
  4. }

If the class (or any of its superclasses) is annotated with com.my.Annotation, the class itself and all its members will become open.

It also works with meta-annotations:

  1. @com.my.Annotation
  2. annotation class MyFrameworkAnnotation
  3. @MyFrameworkAnnotation
  4. 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.

Maven

Here’s how to use all-open with Maven:

  1. <plugin>
  2. <artifactId>kotlin-maven-plugin</artifactId>
  3. <groupId>org.jetbrains.kotlin</groupId>
  4. <version>${kotlin.version}</version>
  5. <configuration>
  6. <compilerPlugins>
  7. <!-- Or "spring" for the Spring support -->
  8. <plugin>all-open</plugin>
  9. </compilerPlugins>
  10. <pluginOptions>
  11. <!-- Each annotation is placed on its own line -->
  12. <option>all-open:annotation=com.my.Annotation</option>
  13. <option>all-open:annotation=com.their.AnotherAnnotation</option>
  14. </pluginOptions>
  15. </configuration>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.jetbrains.kotlin</groupId>
  19. <artifactId>kotlin-maven-allopen</artifactId>
  20. <version>${kotlin.version}</version>
  21. </dependency>
  22. </dependencies>
  23. </plugin>

Please refer to the Gradle section for the detailed information about how all-open annotations work.

Spring support

If you use Spring, you can enable the kotlin-spring compiler plugin instead of specifying Spring annotations manually. The kotlin-spring is a wrapper on top of all-open, and it behaves exactly the same way.

Add the plugin using Gradle’s plugins DSL:

  1. plugins {
  2. id "org.jetbrains.kotlin.plugin.spring" version "1.9.10"
  3. }

In Maven, the spring plugin is provided by the kotlin-maven-allopen plugin dependency, so to enable it:

  1. <compilerPlugins>
  2. <plugin>spring</plugin>
  3. </compilerPlugins>
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.jetbrains.kotlin</groupId>
  7. <artifactId>kotlin-maven-allopen</artifactId>
  8. <version>${kotlin.version}</version>
  9. </dependency>
  10. </dependencies>

The plugin specifies the following annotations:

Thanks to meta-annotations support, classes annotated with @Configuration, @Controller, @RestController, @Service or @Repository are automatically opened since these annotations are meta-annotated with @Component.

Of course, you can use both kotlin-allopen and kotlin-spring in the same project.

Note that if you use the project template generated by the start.spring.io service, the kotlin-spring plugin will be enabled by default.

Command-line compiler

All-open compiler plugin JAR is available in the binary distribution of the Kotlin compiler. You can attach the plugin by providing the path to its JAR file using the Xplugin kotlinc option:

  1. -Xplugin=$KOTLIN_HOME/lib/allopen-compiler-plugin.jar

You can specify all-open annotations directly, using the annotation plugin option, or enable the “preset”. The presets available now for all-open are spring, micronaut, and quarkus.

  1. # The plugin option format is: "-P plugin:<plugin id>:<key>=<value>".
  2. # Options can be repeated.
  3. -P plugin:org.jetbrains.kotlin.allopen:annotation=com.my.Annotation
  4. -P plugin:org.jetbrains.kotlin.allopen:preset=spring