Default Transformers
This guide covers the default transformers provided by the plugin and how to use them effectively.
Overviewβ
Transformers are the core components that define how suspend functions are transformed for different platforms. The plugin provides several built-in transformers that cover the most common use cases.
The default transformers depend on the annotation
and runtime
dependencies provided by the plugin.
Make sure you include them in your configuration before using default transformers.
JVM Transformersβ
JVM Blocking Transformerβ
The JVM Blocking transformer generates blocking variants of suspend functions using runBlocking
.
Configurationβ
suspendTransformPlugin {
transformers {
// Way 1: Simple addition
addJvmBlocking()
// Way 2: Using configuration object
addJvm(SuspendTransformConfigurations.jvmBlockingTransformer)
// Way 3: Using platform-specific addition
add(
TargetPlatform.JVM,
SuspendTransformConfigurations.jvmBlockingTransformer
)
}
}
Usageβ
- Source
- Compiled
class ApiService {
@JvmBlocking
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
}
class ApiService {
@JvmSynthetic
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
@Api4J
fun fetchDataBlocking(): String =
`$runInBlocking$` { fetchData() }
}
Key Featuresβ
- Default Generated Function Suffix:
Blocking
- Return Type: Same as the original function
- Runtime Function:
$runInBlocking$
(based onkotlinx.coroutines.runBlocking
)
Mark Annotationβ
@JvmBlocking
provides some properties to change the default values and customize the generated function results.
baseNameβ
baseName
represents the base name of the generated function.
By default, it is empty (""
). If the value is empty, it means using the same value as the original function.
The final function name of the generated function is baseName
+ suffix
.
suffixβ
suffix
represents the suffix of the generated function.
By default, it is Blocking
.
asPropertyβ
asProperty
represents whether to generate a property instead of a function.
By default, it is false
.
suspend fun foo(): T = ...
// Generated
@Api4J
val fooBlocking: T
get() = runInBlocking { foo() }
If asProperty
is true
, the function cannot have parameters.
markNameβ
Refer to Mark Name.
JVM Async Transformerβ
The JVM Async transformer generates asynchronous variants using CompletableFuture
.
Configurationβ
suspendTransformPlugin {
transformers {
// Way 1: Simple addition
addJvmAsync()
// Way 2: Using configuration object
addJvm(SuspendTransformConfigurations.jvmAsyncTransformer)
// Way 3: Using platform-specific addition
add(
TargetPlatform.JVM,
SuspendTransformConfigurations.jvmAsyncTransformer
)
}
}
Usageβ
- Source
- Compiled
class ApiService {
@JvmAsync
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
}
class ApiService {
@JvmSynthetic
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
@Api4J
fun fetchDataAsync(): CompletableFuture<out String> =
`$runInAsync$`(
block = { fetchData() },
scope = this as? CoroutineScope
)
}
Key Featuresβ
- Default Generated Function Suffix:
Async
- Return Type:
CompletableFuture<out T>
where T is the original return type - Runtime Function:
$runInAsync$
- Scope Handling: Uses the current
CoroutineScope
if available, otherwiseGlobalScope
Mark Annotationβ
@JvmAsync
provides some properties to change the default values and customize the generated function results.
baseNameβ
baseName
represents the base name of the generated function.
By default, it is empty (""
). If the value is empty, it means using the same value as the original function.
The final function name of the generated function is baseName
+ suffix
.
suffixβ
suffix
represents the suffix of the generated function.
By default, it is Async
.
asPropertyβ
asProperty
represents whether to generate a property instead of a function.
By default, it is false
.
suspend fun foo(): T = ...
// Generated
@Api4J
val fooAsync: CompletableFuture<out T>
get() = runInAsync { foo() }
If asProperty
is true
, the function cannot have parameters.
markNameβ
Refer to Mark Name.
JavaScript Transformersβ
JS Promise Transformerβ
The JS Promise transformer generates Promise-based variants for JavaScript interoperability.
Configurationβ
suspendTransformPlugin {
transformers {
// Way 1: Simple addition
addJsPromise()
// Way 2: Using configuration object
addJs(SuspendTransformConfigurations.jsPromiseTransformer)
// Way 3: Using platform-specific addition
add(
TargetPlatform.JS,
SuspendTransformConfigurations.jsPromiseTransformer
)
}
}
Usageβ
- Source
- Compiled
class ApiService {
@JsPromise
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
}
class ApiService {
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
@Api4Js
fun fetchDataAsync(): Promise<String> =
`$runInAsync$`(
block = { fetchData() },
scope = this as? CoroutineScope
)
}
Key Featuresβ
- Default Generated Function Suffix:
Async
- Return Type:
Promise<T>
where T is the original return type - Runtime Function:
$runInAsync$
Mark Annotationβ
@JsPromise
provides some properties to change the default values and customize the generated function results.
baseNameβ
baseName
represents the base name of the generated function.
By default, it is empty (""
). If the value is empty, it means using the same value as the original function.
The final function name of the generated function is baseName
+ suffix
.
suffixβ
suffix
represents the suffix of the generated function.
By default, it is Async
.
asPropertyβ
asProperty
represents whether to generate a property instead of a function.
By default, it is false
.
suspend fun foo(): T = ...
// Generated
@Api4Js
val fooAsync: Promise<T>
get() = runInAsync { foo() }
If asProperty
is true
, the function cannot have parameters.
markNameβ
Refer to Mark Name.
Convenience Functionsβ
Combined JVM Transformersβ
suspendTransformPlugin {
transformers {
// Includes both addJvmBlocking() and addJvmAsync()
useJvmDefault()
}
}
This is equivalent to:
suspendTransformPlugin {
transformers {
addJvmBlocking()
addJvmAsync()
}
}
Combined JS Transformersβ
suspendTransformPlugin {
transformers {
// Includes addJsPromise()
useJsDefault()
}
}
All Default Transformersβ
suspendTransformPlugin {
transformers {
// Includes all default transformers for all platforms
useDefault()
}
}
This is equivalent to:
suspendTransformPlugin {
transformers {
useJvmDefault() // JVM Blocking + JVM Async
useJsDefault() // JS Promise
}
}
Combining Multiple Annotationsβ
You can use multiple transformer annotations on the same function:
class ApiService {
@JvmBlocking
@JvmAsync
@JsPromise
suspend fun fetchData(): String {
delay(1000)
return "Data fetched"
}
}
This will generate:
fetchDataBlocking(): String
(JVM)fetchDataAsync(): CompletableFuture<out String>
(JVM)fetchDataAsync(): Promise<String>
(JS)