English | 简体中文
Kotlin compiler plugin for generating platform-compatible functions for suspend functions.
class Foo {
@JvmBlocking
@JvmAsync
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
compiled 👇
class Foo {
// Hide from Java
@JvmSynthetic
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4J // RequiresOptIn annotation, provide warnings to Kotlin
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' from the runtime provided by the plugin
@Api4J // RequiresOptIn annotation, provide warnings to Kotlin
fun waitAndGetAsync(): CompletableFuture<out String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin
}
class Foo {
@JsPromise
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
compiled 👇
class Foo {
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4Js // RequiresOptIn annotation, provide warnings to Kotlin
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin
}
JS platform target not supported yet. see: KT-53993JS has been supported since 0.6.0! See the process at KT-53993, and the final winning shot at #39!
Warning
Since v0.6.0
, In experiments, immature and unstable
class Foo {
@JsPromise
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
// Some functions or types customised by **you**...
// They are not included in the runtime.
// Since there are a lot of restrictions on the use of various types in WasmJS...
// so I'm not sure how to handle them perfectly yet.
// Until then, you can customise functions and types to control the behaviour of the compiler plugin yourself.
// just like you can customise other platforms.
fun <T> runInAsync(block: suspend () -> T): AsyncResult<T> = AsyncResult(block)
class AsyncResult<T>(val block: suspend () -> T) {
@OptIn(DelicateCoroutinesApi::class)
fun toPromise(): Promise<JsAny?> {
return GlobalScope.promise { block() }
}
}
compiled 👇
class Foo {
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4Js // RequiresOptIn annotation, provide warnings to Kotlin
fun waitAndGetAsync(): AsyncResult<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin
// AsyncResult is a custom type by **you**
}
Before 0.9.0
(included), the naming convention for versions was x.y.z
.
But it seems that the contents of the Kotlin compiler may find changes with each version,
and such version numbers do not reflect the corresponding Kotlin version,
and may lead to some confusion as a result.
Therefore, starting after 0.9.0
, versions will be named in the form $Kotlin-$plugin
,
e.g. 2.0.20-0.9.1
.
The first half is the version of Kotlin used for the build, while the second half is the version of this plugin.
If the version is less than or equal to 0.9.0
, you can refer to this comparison table:
Kotlin version | plugin version |
---|---|
2.0.0 |
0.8.0-beta1 ~ 0.9.0 |
1.9.22 |
0.7.0-beta1 |
1.9.21 |
0.6.0 |
1.9.10 |
0.5.1 |
1.9.0 |
0.5.0 |
1.8.21 |
0.3.1 ~ 0.4.0 |
Note
I haven't documented in detail the compiler plugin compatibility between each Kotlin version.
From my memory and guess, Kotlin versions have a higher probability of incompatibility when minor is added (e.g. 1.8.0
-> 1.9.0
),
and a smaller probability of incompatibility when patch is added (e.g. 1.9.21
-> 1.9.22
).
Using the plugins DSL:
build.gradle.kts
plugins {
kotlin("jvm") version "$KOTLIN_VERSION" // or multiplatform
id("love.forte.plugin.suspend-transform") version "$PLUGIN_VERSION"
// other...
}
// other...
// config it.
suspendTransformPlugin {
// Config the SuspendTransformPluginExtension ...
}
Using legacy plugin application:
build.gradle.kts
buildscript {
repositories {
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:$GRADLE_PLUGIN_VERSION")
}
}
plugins {
id("org.jetbrains.kotlin.jvm") // or multiplatform?
id("love.forte.plugin.suspend-transform")
// other...
}
// other...
// config it.
suspendTransformPlugin {
// Config the SuspendTransformPluginExtension ...
}
Enable the Kotlin compiler plugin.
Default value is true
.
suspendTransformPlugin {
enabled = true
}
If you wish to use the Transformer we provide, then you may need to add the annotation
and runtime
dependencies.
You can add them automatically via configuration.
suspendTransformPlugin {
// include the annotation
// Default is `true`
includeAnnotation = true
// The default can be left unconfigured and the default values are used exclusively.
annotationDependency {
// Default is `compileOnly`
configurationName = "compileOnly"
// Default is same as the plugin version
version = "<ANNOTATION_VERSION>"
}
// Include the runtime
// Default is `true`
includeRuntime = true
// The default can be left unconfigured and the default values are used exclusively.
runtimeDependency {
// Default is `implementation`
configurationName = "implementation"
// Default is same as the plugin version
version = "<RUNTIME_VERSION>"
}
}
You can also disable them and add dependencies manually.
plugin {
kotlin("jvm") version "..." // Take the Kotlin/JVM as an example
id("love.forte.plugin.suspend-transform") version "2.1.20-0.12.0"
}
dependencies {
// annotation
compileOnly("love.forte.plugin.suspend-transform:suspend-transform-annotation:<VERSION>")
// runtime
implementation("love.forte.plugin.suspend-transform:suspend-transform-runtime:<VERSION>")
}
suspendTransformPlugin {
// Disable them
includeAnnotation = false
includeRuntime = false
}
Transformer
is the type used to describe how the suspend function is transformed.
You need to add some Transformer
s to make the compiler plugin actually work.
suspendTransformPlugin {
// Config the transformers
transformers {
add(TargetPlatform.JVM) { // this: TransformerSpec
// Config the TransformerSpec...
}
addJvm { // this: TransformerSpec
// Config the TransformerSpec...
}
// Use a default transformer we provided from `SuspendTransformConfigurations`
add(TargetPlatform.JVM, SuspendTransformConfigurations.jvmBlockingTransformer)
addJvm { // this: TransformerSpec
// Modify and adjust from a Transformer
from(SuspendTransformConfigurations.jvmBlockingTransformer)
// Further configurations...
}
}
}
First, we provide some simple and commonly used implementations. You can use them simply and quickly through configuration.
Note
The default Transformer
s depend on the annotation
and runtime
we provide.
Make sure you include them before using it.
JVM blocking
suspendTransformPlugin {
transformers {
// The 1st way:
addJvmBlocking()
// Or the 2ed way:
addJvm(SuspendTransformConfigurations.jvmBlockingTransformer)
// Or use transformers.add(TargetPlatform.JVM, jvmBlockingTransformer), etc.
}
}
JvmBlocking
allows you to mark @JvmBlocking
on the suspend function,
which generates a xxxBlocking
function.
class Cat {
@JvmBlocking
suspend fun meow() {
// ...
}
// Generated:
fun meowBlocking() {
`$runInBlocking$` { meow() }
}
}
The $runInBlocking$
based on kotlinx.coroutines.runBlocking
。
JVM Async
suspendTransformPlugin {
transformers {
// The 1st way:
addJvmAsync()
// Or the 2ed way:
addJvm(SuspendTransformConfigurations.jvmAsyncTransformer)
// Or use transformers.add(TargetPlatform.JVM, jvmAsyncTransformer), etc.
}
}
JvmAsync
allows you to mark @JvmAsync
on the suspend function,
which generates a xxxAsync
function.
class Cat {
@JvmBlocking
suspend fun meow(): String = "Meow!"
// Generated:
fun meowAsync(): CompletableFuture<out String> {
`$runInAsync$`(block = { meow() }, scope = this as? CoroutineScope)
}
}
The block
is the original suspend function that needs to be executed
and the scope
is the CoroutineScope
that will be used.
If the current scope is a CoroutineScope
, it takes precedence over itself.
Otherwise, GlobalScope
is used internally.
Why use GlobalScope
: When using an internal scope, this scope qualifies:
- global.
- is never visible externally, so it is not artificially closed.
- is not intended for IO and does not require a custom dispatcher.
We believe GlobalScope
meets these conditions.
Have a different point? Feel free to create issue!
JS Promise
suspendTransformPlugin {
transformers {
// The 1st way:
addJsPromise()
// Or the 2ed way:
addJs(SuspendTransformConfigurations.jsPromiseTransformer)
// Or use transformers.add(TargetPlatform.JS, jsPromiseTransformer), etc.
}
}
class Cat {
@JsPromise
suspend fun meow(): String = "Meow!"
// Generated:
fun meowAsync(): Promise<String> {
`$runInAsync$`(block = { meow() }, scope = this as? CoroutineScope)
}
}
The block
is the original suspend function that needs to be executed
and the scope
is the CoroutineScope
that will be used.
The addJvmBlocking()
and addJvmAsync()
may be combined as useJvmDefault()
.
suspendTransformPlugin {
transformers {
// Includes addJvmBlocking() and addJvmAsync()
useJvmDefault()
}
}
The addJsPromise()
may be combined as useJsDefault()
.
suspendTransformPlugin {
transformers {
// Includes addJsPromise()
useJsDefault()
}
}
The useJvmDefault()
and useJsDefault()
may be combined as useDefault()
.
suspendTransformPlugin {
transformers {
// Includes useJvmDefault() and useJsDefault()
useDefault()
}
}
You can also customize your Transformer
if the default Transformer
s don't meet your needs,
e.g. if you want to fully implement blocking logic and don't want to use kotlinx.coroutines.runBlocking
.
A fully customized implementation of JVM Blocking/Async Transformers reference: https://github.com/simple-robot/simpler-robot/blob/v4-main/simbot-commons/simbot-common-suspend-runner/src/jvmMain/kotlin/love/forte/simbot/suspendrunner/BlockingRunner.kt
suspendTransformPlugin {
// If customized, then you may not use the annotation and runtime we provide.
includeAnnotation = false
includeRuntime = false
transformer {
// See below for details
}
}
As an example, you intend to create a custom annotation: @JBlock
,
which is executed via the function inBlock
when the suspend function uses this annotation.
// Your annotation
annotation class JBlock(...)
// Your top-level transform function
fun <T> inBlock(block: suspend () -> T): T {
TODO("Your impl")
}
First, let's agree that the following properties should be included in the annotation:
baseName
: The generated function's base name. When the value of this property is empty, the name of the original function is used by default.@JBlock(baseName = "") suspend fun meow1() // Generated function name: ${baseName}${suffix} -> meow1Blocking @JBlock(baseName = "meow999") suspend fun meow2() // Generated function name: ${baseName}${suffix} -> meow999Blocking
suffix
: The generated function name's suffix.asProperty
: Make the generated function a property. Can be used in cases where the original function has no arguments.@JBlock(asProperty = true) suspend fun value(): Int // Generated: val valueBlocking: Int get() = inBlock { value() }
So your annotation should look like this:
annotation class JBlock(
val baseName: String = "",
val suffix: String = "Blocking",
val asProperty: Boolean = false
)
The configuration:
suspendTransformPlugin {
includeAnnotation = false
includeRuntime = false
transformers {
addJvm {
markAnnotation {
// Your annotation class's info.
classInfo {
packageName = "com.example"
className = "JBlock"
}
// The property names.
baseNameProperty = "baseName" // Default is `baseName`
suffixProperty = "suffix" // Default is `suffix`
asPropertyProperty = "asProperty" // Default is `asProperty`
// The compiler plugin doesn't seem to be able to get the default values for annotations
// (or I haven't found a way to do it yet).
// So here you need to configure the default value of the annotation, which needs to be consistent with your definition.
defaultSuffix = "Blocking"
defaultAsProperty = false // For the same reasons as above.
}
}
}
}
However, the property names do not have to be the same as these three, as long as the function and type correspond. So we can adjust it like this:
annotation class JBlock(
val myBaseName: String = "",
val mySuffix: String = "Blocking",
val myAsProperty: Boolean = false
)
The configuration:
suspendTransformPlugin {
includeAnnotation = false
includeRuntime = false
transformers {
addJvm {
markAnnotation {
// Your annotation class's info.
classInfo {
packageName = "com.example"
className = "JBlock"
}
// The property names.
baseNameProperty = "myBaseName"
suffixProperty = "mySuffix"
asPropertyProperty = "myAsProperty"
// The default values.
defaultSuffix = "Blocking"
defaultAsProperty = false
}
}
}
}
Then configure the information for your transform function.
// Your top-level transform function
fun <T> inBlock(block: suspend () -> T): T {
TODO("Your impl")
}
The configuration:
suspendTransformPlugin {
includeAnnotation = false
includeRuntime = false
transformers {
addJvm {
markAnnotation {
// ...
}
// The function info
transformFunctionInfo {
packageName = "com.example"
functionName = "inBlock"
}
// The return type configs
// The return type.
// If `null` it means the same type as the original function return.
// If you return a specific type (e.g. `CompletableFuture`) you need to configure that type.
//
// Default value is null.
transformReturnType = null
// Whether the returned type contains a generic type that is of the same type as the original function.
// e.g. CompletableFuture<T>, The `T` represents the value returned by the original function.
// In this case it is set to `true`.
//
// Set to `false` if the return type is of a specific type,
// but without a generic (a rare case, an example: `Job`).
// Valid if `transformReturnType` is not null.
//
// Default value is false.
transformReturnTypeGeneric = false
}
}
}
Finally, in the process of generating the function, we allow some manipulation of the annotations.
- Copy annotations from original function to generated synthetic function.
- exclude some annotations from copying.
- Include some annotations to original function.
- Include some annotations to generated synthetic function.
Now let's assume:
- We want to add
@JvmSynthetic
to the original function. - We want to add
@JApi
to the generated synthetic function. - Copy the annotations without copying
@JvmSynthetic
(exclude@JvmSynthetic
).
The @JApi
:
@RequiresOptIn(message = "Api for Java", level = RequiresOptIn.Level.WARNING)
@Retention(AnnotationRetention.BINARY)
annotation class JApi
The configuration:
suspendTransformPlugin {
includeAnnotation = false
includeRuntime = false
transformers {
addJvm {
markAnnotation {
// ...
}
transformFunctionInfo {
// ...
}
// Enabling annotated copies
// Default is FALSE
copyAnnotationsToSyntheticFunction = true
// If the generated synthetic function is property (asProperty=true),
// Copy annotations to the property.
// Otherwise, copy to the property's getter function.
// Default is FALSE
copyAnnotationsToSyntheticProperty = true
// Include `@kotlin.jvm.JvmSynthetic` to original function.
addOriginFunctionIncludeAnnotation {
// Some common types are defined in SuspendTransformConfigurations. See below.
classInfo {
packageName = "kotlin.jvm"
className = "JvmSynthetic"
}
// Default is false
repeatable = false
}
// Include `@com.example.JApi` to generated synthetic function
addSyntheticFunctionIncludeAnnotation {
classInfo {
packageName = "com.example"
className = "JApi"
}
// Marks whether this annotation supports being added to a property.
// Default is FALSE
includeProperty = true
}
// Exclude `@kotlin.jvm.JvmSynthetic` when copying.
addCopyAnnotationExclude {
// SuspendTransformConfigurations provides a small number of
// common annotations or type definitions that can be used directly.
from(SuspendTransformConfigurations.jvmSyntheticClassInfo)
}
}
}
}
The full example:
Code:
annotation class JBlock(
val myBaseName: String = "",
val mySuffix: String = "Blocking",
val myAsProperty: Boolean = false
)
@RequiresOptIn(message = "Api for Java", level = RequiresOptIn.Level.WARNING)
@Retention(AnnotationRetention.BINARY)
annotation class JApi
fun <T> inBlock(block: suspend () -> T): T {
TODO("Your impl")
}
Configuration:
suspendTransformPlugin {
includeAnnotation = false
includeRuntime = false
transformers {
addJvm {
markAnnotation {
classInfo {
packageName = "com.example"
className = "JBlock"
}
baseNameProperty = "myBaseName"
suffixProperty = "mySuffix"
asPropertyProperty = "myAsProperty"
defaultSuffix = "Blocking"
defaultAsProperty = false
}
transformFunctionInfo {
packageName = "com.example"
functionName = "inBlock"
}
copyAnnotationsToSyntheticFunction = true
copyAnnotationsToSyntheticProperty = true
addOriginFunctionIncludeAnnotation {
classInfo {
from(SuspendTransformConfigurations.jvmSyntheticClassInfo)
}
repeatable = false
}
addSyntheticFunctionIncludeAnnotation {
classInfo {
packageName = "com.example"
className = "JApi"
}
includeProperty = true
}
addCopyAnnotationExclude {
from(SuspendTransformConfigurations.jvmSyntheticClassInfo)
}
}
}
}
Note
Since the property name is configurable, the same annotation can be reused on multiple transformers. Annotation:
annotation class JTrans(
val blockingBaseName: String = "",
val blockingSuffix: String = "Blocking",
val blockingAsProperty: Boolean = false,
val asyncBaseName: String = "",
val asyncSuffix: String = "Async",
val asyncAsProperty: Boolean = false
)
Configuration:
suspendTransformPlugin {
includeAnnotation = false
includeRuntime = false
transformers {
// For blocking
addJvm {
markAnnotation {
classInfo {
packageName = "com.example"
className = "JTrans"
}
baseNameProperty = "blockingBaseName"
suffixProperty = "blockingSuffix"
asPropertyProperty = "blockingAsProperty"
defaultSuffix = "Blocking"
defaultAsProperty = false
}
transformFunctionInfo {
packageName = "com.example"
functionName = "inBlock"
}
// other config...
}
// For async
addJvm {
markAnnotation {
classInfo {
packageName = "com.example"
className = "JTrans"
}
baseNameProperty = "asyncBaseName"
suffixProperty = "asyncSuffix"
asPropertyProperty = "asyncAsProperty"
defaultSuffix = "Async"
defaultAsProperty = false
}
transformFunctionInfo {
packageName = "com.example"
functionName = "inAsync"
}
}
}
}
Gradle JVM must be JDK11+
K2 is supported since v0.7.0
.
If you want to use @JsExport
with default configuration in JS,
try this:
build.gradle.kts
import love.forte.plugin.suspendtrans.configuration.SuspendTransformConfigurations
plugins {
// ...
}
suspendTransformPlugin {
transformers {
addJsPromise {
addCopyAnnotationExclude {
// The generated function does not include (copy) `@JsExport.Ignore`.
from(kotlinJsExportIgnoreClassInfo)
}
}
}
}
@file:OptIn(ExperimentalJsExport::class)
@JsExport
class Foo {
@JsPromise
@JsExport.Ignore
suspend fun run(): Int = ...
}
source:
import love.forte.plugin.suspendtrans.annotation.JvmAsync
import love.forte.plugin.suspendtrans.annotation.JvmBlocking
@JvmBlocking
@JvmAsync
interface Foo {
suspend fun name(): String
suspend fun age(def: Int = 5): Int
@JvmBlocking(asProperty = true)
suspend fun self(): Foo
}
@JvmBlocking
@JvmAsync
class FooImpl : Foo {
suspend fun size(): Long = 666
override suspend fun name(): String = "forte"
override suspend fun age(def: Int): Int = def
@JvmBlocking(asProperty = true) // must be 'asProperty=true'
override suspend fun self(): FooImpl = this
}
class Bar {
@JvmBlocking
@JvmAsync
suspend fun bar(): String = ""
suspend fun noTrans(): Int = 1
}
compiled:
Simplified from decompiled results.
import love.forte.plugin.suspendtrans.annotation.JvmAsync
import love.forte.plugin.suspendtrans.annotation.JvmBlocking
import love.forte.plugin.suspendtrans.annotation.Generated
import love.forte.plugin.suspendtrans.annotation.Api4J
import kotlin.jvm.JvmSynthetic
@JvmBlocking
@JvmAsync
interface Foo {
@Generated
@Api4J
val selfBlocking: Foo /* compiled code */
suspend fun age(def: Int /* = compiled code */): Int
@Generated
@Api4J
fun ageAsync(def: Int /* = compiled code */): java.util.concurrent.CompletableFuture<Int> { /* compiled code */ }
@Generated
@Api4J
fun ageBlocking(def: Int /* = compiled code */): Int { /* compiled code */ }
suspend fun name(): String
@Generated
@Api4J
fun nameAsync(): java.util.concurrent.CompletableFuture<out String> { /* compiled code */ }
@Generated
@Api4J
fun nameBlocking(): String { /* compiled code */ }
@JvmBlocking
suspend fun self(): Foo
@Generated
@Api4J
fun selfAsync(): java.util.concurrent.CompletableFuture<out Foo> { /* compiled code */ }
}
@JvmBlocking
@JvmAsync
class FooImpl : Foo {
@Generated
@Api4J
open val selfBlocking: FooImpl /* compiled code */
@JvmSynthetic
open suspend fun age(def: Int): Int { /* compiled code */ }
@Generated
@Api4J
open fun ageAsync(def: Int): java.util.concurrent.CompletableFuture<Int> { /* compiled code */ }
@Generated
@Api4J
open fun ageBlocking(def: Int): Int { /* compiled code */ }
@JvmSynthetic
open suspend fun name(): String { /* compiled code */ }
@Generated
@Api4J
open fun nameAsync(): java.util.concurrent.CompletableFuture<out String> { /* compiled code */ }
@Generated
@Api4J
open fun nameBlocking(): String { /* compiled code */ }
@JvmSynthetic
@JvmBlocking
suspend fun self(): FooImpl { /* compiled code */ }
@Generated
@Api4J
fun selfAsync(): java.util.concurrent.CompletableFuture<out FooImpl> { /* compiled code */ }
@JvmSynthetic
suspend fun size(): Long { /* compiled code */ }
@Generated
@Api4J
fun sizeAsync(): java.util.concurrent.CompletableFuture<Long> { /* compiled code */ }
@Generated
@Api4J
fun sizeBlocking(): Long { /* compiled code */ }
}
class Bar {
@JvmSynthetic
@JvmBlocking
@JvmAsync
suspend fun bar(): String { /* compiled code */ }
@Generated
@Api4J
fun barAsync(): java.util.concurrent.CompletableFuture<out String> { /* compiled code */ }
@Generated
@Api4J
fun barBlocking(): String { /* compiled code */ }
fun noTrans(): Int { /* compiled code */ }
}
- Simple Robot Frameworks (Fully customized)
see LICENSE .
Copyright (c) 2022 ForteScarlet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.