前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kotlin Compiler Plugins : All-open , Kotlin-spring,No-arg,Kotlin-jpaCompiler Plugins

Kotlin Compiler Plugins : All-open , Kotlin-spring,No-arg,Kotlin-jpaCompiler Plugins

作者头像
一个会写诗的程序员
发布2018-08-17 14:17:04
1.5K0
发布2018-08-17 14:17:04
举报

Compiler Plugins

All-open compiler plugin

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. The all-open plugin allows to specify these annotations.

We provide all-open plugin support both for Gradle and Maven, as well as the IDE integration. For Spring you can use the kotlin-spring compiler plugin (see below).

How to use all-open plugin

Add the plugin in build.gradle:

代码语言:javascript
复制
buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
}

apply plugin: "kotlin-allopen"

Or, if you use the Gradle plugins DSL, add it to the plugins block:

代码语言:javascript
复制
plugins {
  id "org.jetbrains.kotlin.plugin.allopen" version "{{ site.data.releases.latest.version }}"
}

Then specify the annotations that will make the class open:

代码语言:javascript
复制
allOpen {
    annotation("com.my.Annotation")
}

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:

代码语言:javascript
复制
@com.my.Annotation
annotation class MyFrameworkAnnotation

@MyFrameworkAnnotation
class MyClass // will be all-open

MyFrameworkAnnotation is also the annotation that makes the class open, because it's annotated with com.my.Annotation.

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

代码语言:javascript
复制
<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>

    <configuration>
        <compilerPlugins>
            <!-- Or "spring" for the Spring support -->
            <plugin>all-open</plugin>
        </compilerPlugins>

        <pluginOptions>
            <!-- Each annotation is placed on its own line -->
            <option>all-open:annotation=com.my.Annotation</option>
            <option>all-open:annotation=com.their.AnotherAnnotation</option>
        </pluginOptions>
    </configuration>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>

Kotlin-spring compiler plugin

You don't need to specify Spring annotations by hand, you can use the kotlin-spring plugin, which automatically configures the all-open plugin according to the requirements of Spring:

代码语言:javascript
复制
buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
}

apply plugin: "kotlin-spring"

Or using the Gradle plugins DSL:

代码语言:javascript
复制
plugins {
  id "org.jetbrains.kotlin.plugin.spring" version "{{ site.data.releases.latest.version }}"
}

The Maven example is similar to the one above.

The plugin specifies the following annotations: @Component, @Async, @Transactional, @Cacheable. 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 start.spring.io the kotlin-spring plugin will be enabled by default.

No-arg compiler plugin

The no-arg compiler plugin generates an additional zero-argument constructor for classes with a specific annotation. The generated constructor is synthetic so it can’t be directly called from Java or Kotlin, but it can be called using reflection. This allows the Java Persistence API (JPA) to instantiate the data class although it doesn't have the no-arg constructor from Kotlin or Java point of view (see the description of kotlin-jpa plugin below).

How to use no-arg plugin

The usage is pretty similar to all-open. You add the plugin and specify the list of annotations that must lead to generating a no-arg constructor for the annotated classes.

How to use no-arg in Gradle:

代码语言:javascript
复制
buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
}

apply plugin: "kotlin-noarg"

Or using the Gradle plugins DSL:

代码语言:javascript
复制
plugins {
  id "org.jetbrains.kotlin.plugin.noarg" version "{{ site.data.releases.latest.version }}"
}

Then specify the annotation types:

代码语言:javascript
复制
noArg {
    annotation("com.my.Annotation")
}

Enable invokeInitializers option if you want the plugin to run the initialization logic from the synthetic constructor. Starting from Kotlin 1.1.3-2, it is disabled by default because of KT-18667 and KT-18668 which will be addressed in the future:

代码语言:javascript
复制
noArg {
    invokeInitializers = true
}

How to use no-arg in Maven:

代码语言:javascript
复制
<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>

    <configuration>
        <compilerPlugins>
            <!-- Or "jpa" for JPA support -->
            <plugin>no-arg</plugin>
        </compilerPlugins>

        <pluginOptions>
            <option>no-arg:annotation=com.my.Annotation</option>
            <!-- Call instance initializers in the synthetic constructor -->
            <!-- <option>no-arg:invokeInitializers=true</option> -->
        </pluginOptions>
    </configuration>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>

Kotlin-jpa compiler plugin

The plugin specifies @Entity and @Embeddable annotations as markers that no-arg constructor should be generated for a class. That's how you add the plugin in Gradle:

代码语言:javascript
复制
buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
}

apply plugin: "kotlin-jpa"

Or using the Gradle plugins DSL:

代码语言:javascript
复制
plugins {
  id "org.jetbrains.kotlin.plugin.jpa" version "{{ site.data.releases.latest.version }}"
}

The Maven example is similar to the one above.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.10.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Compiler Plugins
    • All-open compiler plugin
      • How to use all-open plugin
      • Kotlin-spring compiler plugin
    • No-arg compiler plugin
      • How to use no-arg plugin
      • Kotlin-jpa compiler plugin
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档