All-open compiler plugin
Kotlin ではクラスとそのメンバーはデフォルトで final
になっています。そのため、クラスが open
であることを要求する Spring AOP などのフレームワークやライブラリを使用する際に不便です。all-open
コンパイラープラグインは、Kotlin をこれらのフレームワークの要件に適合させ、特定のアノテーションが付いたクラスとそのメンバーを、明示的な open
キーワードなしで open にします。
たとえば、Spring を使用する場合、すべてのクラスを open にする必要はなく、@Configuration
や @Service
などの特定のアノテーションが付いたクラスのみを open にする必要があります。all-open
プラグインを使用すると、このようなアノテーションを指定できます。
Kotlin は、完全な IDE 統合により、Gradle と Maven の両方で all-open
プラグインのサポートを提供します。
Spring の場合、kotlin-spring
コンパイラープラグインを使用できます。
Gradle
build.gradle(.kts)
ファイルにプラグインを追加します。
- Kotlin
- Groovy
plugins {
kotlin("plugin.allopen") version "2.1.20"
}
plugins {
id "org.jetbrains.kotlin.plugin.allopen" version "2.1.20"
}
次に、クラスを open にするアノテーションのリストを指定します。
- Kotlin
- Groovy
allOpen {
annotation("com.my.Annotation")
// annotations("com.another.Annotation", "com.third.Annotation")
}
allOpen {
annotation("com.my.Annotation")
// annotations("com.another.Annotation", "com.third.Annotation")
}
クラス (またはそのスーパークラスのいずれか) に com.my.Annotation
が付いている場合、クラス自体とそのすべてのメンバーが open になります。
メタアノテーションでも同様に動作します。
@com.my.Annotation
annotation class MyFrameworkAnnotation
@MyFrameworkAnnotation
class MyClass // will be all-open
MyFrameworkAnnotation
は all-open メタアノテーション com.my.Annotation
でアノテーションされているため、all-open アノテーションにもなります。
Maven
pom.xml
ファイルにプラグインを追加します。
<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>
all-open アノテーションの仕組みの詳細については、Gradle セクションを参照してください。
Spring support
Spring を使用している場合、Spring アノテーションを手動で指定する代わりに、kotlin-spring
コンパイラープラグインを有効にできます。kotlin-spring
は all-open
の上に構築されたラッパーであり、まったく同じように動作します。
build.gradle(.kts)
ファイルに spring
プラグインを追加します。
- Kotlin
- Groovy
plugins {
id("org.jetbrains.kotlin.plugin.spring") version "2.1.20"
}
plugins {
id "org.jetbrains.kotlin.plugin.spring" version "2.1.20"
}
Maven では、spring
プラグインは kotlin-maven-allopen
プラグインの依存関係によって提供されるため、pom.xml
ファイルで有効にするには次の手順を実行します。
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
このプラグインは、次のアノテーションを指定します。
メタアノテーションのサポートのおかげで、@Configuration
、
@Controller
、
@RestController
、
@Service
または @Repository
でアノテーションされたクラスは、これらのアノテーションが
@Component
でメタアノテーションされているため、自動的に open になります。
もちろん、同じプロジェクトで kotlin-allopen
と kotlin-spring
の両方を使用できます。
start.spring.io
サービスでプロジェクトテンプレートを生成する場合、kotlin-spring
プラグインはデフォルトで有効になります。
Command-line compiler
All-open コンパイラープラグイン JAR は、Kotlin コンパイラーのバイナリ配布に含まれています。-Xplugin
kotlinc オプションを使用して、その JAR ファイルへのパスを指定することにより、プラグインをアタッチできます。
-Xplugin=$KOTLIN_HOME/lib/allopen-compiler-plugin.jar
annotation
プラグインオプションを使用して all-open アノテーションを直接指定するか、preset を有効にすることができます。
# The plugin option format is: "-P plugin:<plugin id>:<key>=<value>".
# Options can be repeated.
-P plugin:org.jetbrains.kotlin.allopen:annotation=com.my.Annotation
-P plugin:org.jetbrains.kotlin.allopen:preset=spring
all-open
プラグインで使用可能なプリセットは、spring
、micronaut
、および quarkus
です。