All-open コンパイラプラグイン
Kotlinでは、クラスとそのメンバーはデフォルトでfinal
です。このため、Spring AOPのようにクラスがopen
であることを要求するフレームワークやライブラリを使用するのが不便になります。all-open
コンパイラプラグインは、Kotlinをこれらのフレームワークの要件に適合させ、特定のAノテーションが付与されたクラスとそのメンバーを、明示的なopen
キーワードなしでopen
にします。
例えば、Springを使用する場合、全てのクラスをopen
にする必要はなく、@Configuration
や@Service
のような特定のアノテーションが付与されたクラスのみで十分です。all-open
プラグインを使用すると、そのようなアノテーションを指定できます。
Kotlinは、完全なIDE統合と共に、GradleとMavenの両方でall-open
プラグインのサポートを提供します。
Springの場合、
kotlin-spring
コンパイラプラグインを使用できます。
Gradle
build.gradle(.kts)
ファイルにプラグインを追加します。
plugins {
kotlin("plugin.allopen") version "2.2.10"
}
plugins {
id "org.jetbrains.kotlin.plugin.allopen" version "2.2.10"
}
次に、クラスをopen
にするアノテーションのリストを指定します。
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サポート
Springを使用している場合、Springアノテーションを手動で指定する代わりに、kotlin-spring
コンパイラプラグインを有効にできます。kotlin-spring
はall-open
のラッパーであり、全く同じように動作します。
build.gradle(.kts)
ファイルにspring
プラグインを追加します。
plugins {
id("org.jetbrains.kotlin.plugin.spring") version "2.2.10"
}
plugins {
id "org.jetbrains.kotlin.plugin.spring" version "2.2.10"
}
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>
このプラグインは以下のAノテーションを指定します。
メタアノテーションのサポートにより、@Configuration
、 @Controller
、 @RestController
、 @Service
、 @Repository
が付与されたクラスは、これらのアノテーションに@Component
がメタアノテーションとして付与されているため、自動的にopen
になります。
もちろん、同じプロジェクトでkotlin-allopen
とkotlin-spring
の両方を使用できます。
start.spring.ioサービスでプロジェクトテンプレートを生成する場合、
kotlin-spring
プラグインはデフォルトで有効になります。
コマンドラインコンパイラ
All-openコンパイラプラグインのJARは、Kotlinコンパイラのバイナリ配布版で利用可能です。-Xplugin
kotlincオプションを使用してJARファイルへのパスを提供することで、プラグインをアタッチできます。
-Xplugin=$KOTLIN_HOME/lib/allopen-compiler-plugin.jar
annotation
プラグインオプションを使用してall-openアノテーションを直接指定するか、_プリセット_を有効にできます。
# プラグインオプションの形式は "-P plugin:<plugin id>:<key>=<value>" です。
# オプションは繰り返し指定できます。
-P plugin:org.jetbrains.kotlin.allopen:annotation=com.my.Annotation
-P plugin:org.jetbrains.kotlin.allopen:preset=spring
all-open
プラグインで利用可能なプリセットは、spring
、micronaut
、quarkus
です。