All-openコンパイラプラグイン
Kotlin のクラスとそのメンバーはデフォルトでfinal
であり、クラスがopen
である必要がある Spring AOP のようなフレームワークやライブラリを使用する上で不便です。all-open
コンパイラプラグインは、そのようなフレームワークの要件に Kotlin を適合させ、特定の アノテーション が付与されたクラスとそのメンバーを、明示的なopen
キーワードなしでopen
にします。
例えば、Spring を使用する場合、すべてのクラスをopen
にする必要はなく、@Configuration
や@Service
のような特定の アノテーション が付与されたクラスのみでよいです。all-open
プラグインを使用すると、そのような アノテーション を指定できます。
Kotlin は、Gradle と Maven の両方でall-open
プラグインのサポートを、完全なIDE統合とともに提供しています。
NOTE
Spring の場合、kotlin-spring
コンパイラプラグインを使用できます。
Gradle
build.gradle(.kts)
ファイルにプラグインを追加します。
plugins {
kotlin("plugin.allopen") version "2.1.21"
}
plugins {
id "org.jetbrains.kotlin.plugin.allopen" version "2.1.21"
}
次に、クラスを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
になります。
これはメタアノテーション (meta-annotations) でも機能します。
@com.my.Annotation
annotation class MyFrameworkAnnotation
@MyFrameworkAnnotation
class MyClass // 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.1.21"
}
plugins {
id "org.jetbrains.kotlin.plugin.spring" version "2.1.21"
}
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>
このプラグインは次の アノテーション を指定します。
メタアノテーション (meta-annotations) のサポートにより、@Component
がメタアノテーションとして付与されているため、@Configuration
、@Controller
、@RestController
、@Service
、または @Repository
が付与されたクラスは自動的にopen
になります。
もちろん、同じプロジェクトでkotlin-allopen
とkotlin-spring
の両方を使用できます。
NOTE
start.spring.ioサービスでプロジェクトテンプレートを生成する場合、kotlin-spring
プラグインはデフォルトで有効になります。
コマンドラインコンパイラ
All-open コンパイラプラグインのJARは、Kotlin コンパイラのバイナリディストリビューションで利用できます。プラグインをアタッチするには、kotlinc
の-Xplugin
オプションを使用して、その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
です。