代码拉取完成,页面将自动刷新
English | 简体中文
用于为Kotlin挂起函数自动生成平台兼容函数的Kotlin编译器插件。
class Foo {
@JvmBlocking
@JvmAsync
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
编译后 👇
class Foo {
// 对Java隐藏
@JvmSynthetic
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4J // RequiresOptIn 注解, 向Kotlin开发者提供警告
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' 来自于插件提供的运行时依赖
@Api4J // RequiresOptIn 注解, 向Kotlin开发者提供警告
fun waitAndGetAsync(): CompletableFuture<out String> = runInAsync { waitAndGet() } // 'runInAsync' 来自于插件提供的运行时依赖
}
class Foo {
@JsPromise
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
编译后 👇
class Foo {
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4Js // RequiresOptIn 注解, 向Kotlin开发者提供警告
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' 来自于插件提供的运行时依赖
}
JS 目标平台暂不支持。原因参考: KT-53993
[!warning] 从
v0.6.0
开始支持,实验中,不成熟、不稳定。
class Foo {
@JsPromise
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
// 一些由**你**自定义的函数或类型...
// 它们不包含在 runtime 中。由于在 WasmJS 中,对于各种类型的使用会有很多限制,
// 因此我还不清楚如何完美地处理它们。
// 在那之前,你可以自定义函数和类型来自行控制编译器插件的行为,
// 就像自定义其他平台那样。
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
}
通过 plugins DSL 使用:
build.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm") version "$KOTLIN_VERSION" // or js? or multiplatform?
id("love.forte.plugin.suspend-transform") version "$PLUGIN_VERSION"
// other...
}
// other...
// config it.
suspendTransform {
enabled = true // default: true
includeRuntime = true // default: true
includeAnnotation = true // default: true
/*
* 相当于同时使用 `useJvmDefault` 和 `useJsDefault`.
* 需要包含 runtime 和 annotation
*/
// useDefault()
/*
* 使用JVM平台的默认配置
* 相当于:
* addJvmTransformers(
* SuspendTransformConfiguration.jvmBlockingTransformer,
* SuspendTransformConfiguration.jvmAsyncTransformer,
* )
*
* 需要包含 runtime 和 annotation
*/
useJvmDefault()
// 或者由你自定义
jvm {
// ...
}
// 或者由你自定义
addJvmTransformers(...)
/*
* 使用JS平台的默认配置
* 相当于:
* addJvmTransformers(
* SuspendTransformConfiguration.jsPromiseTransformer,
* )
*
* 需要包含 runtime 和 annotation
*/
useJsDefault()
// 或者由你自定义
js {
// ...
}
// 或者由你自定义
addJsTransformers(...)
}
通过 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") // 或 js? 或 multiplatform?
id("love.forte.plugin.suspend-transform")
// 其他...
}
// 其他...
// 配置
suspendTransform {
enabled = true // default: true
includeRuntime = true // default: true
includeAnnotation = true // default: true
/*
* 相当于同时使用 `useJvmDefault` 和 `useJsDefault`.
* 需要包含 runtime 和 annotation
*/
// useDefault()
/*
* 使用JVM平台的默认配置
* 相当于:
* addJvmTransformers(
* SuspendTransformConfiguration.jvmBlockingTransformer,
* SuspendTransformConfiguration.jvmAsyncTransformer,
* )
*
* 需要包含 runtime 和 annotation
*/
useJvmDefault()
// 或者由你自定义
jvm {
// ...
}
// 或者由你自定义
addJvmTransformers(...)
/*
* 使用JS平台的默认配置
* 相当于:
* addJvmTransformers(
* SuspendTransformConfiguration.jsPromiseTransformer,
* )
*
* 需要包含 runtime 和 annotation
*/
useJsDefault()
// 或者由你自定义
js {
// ...
}
// 或者由你自定义
addJsTransformers(...)
}
Gradle JVM 必须满足 JDK11+
K2 编译器从 v0.7.0
开始支持。
[!warning] 实验中。
如果你打算在默认配置的情况下使用 @JsExport
, 可以尝试以下代码:
build.gradle.kts
plugins {
...
}
suspendTransform {
addJsTransformers(
SuspendTransformConfiguration.jsPromiseTransformer.copy(
copyAnnotationExcludes = listOf(
// 生成的函数将不会包含 `@JsExport.Ignore`
ClassInfo("kotlin.js", "JsExport.Ignore")
)
)
)
}
@file:OptIn(ExperimentalJsExport::class)
@JsExport
class Foo {
@JsPromise
@JsExport.Ignore
suspend fun run(): Int = ...
}
源代码:
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) // asProperty 必须为 true
override suspend fun self(): FooImpl = this
}
class Bar {
@JvmBlocking
@JvmAsync
suspend fun bar(): String = ""
suspend fun noTrans(): Int = 1
}
编译结果:
简化自反编译结果
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 */ }
}
plugin {
id("love.forte.plugin.suspend-transform") version "$VERSION"
}
suspendTransform {
// enabled suspend transform plugin
enabled = true
// include 'love.forte.plugin.suspend-transform:suspend-transform-runtime' to the runtime environment
includeRuntime = true
// the configuration name for including 'love.forte.plugin.suspend-transform:suspend-transform-runtime'
runtimeConfigurationName = "implementation"
val customJvmTransformer = Transformer(
// mark annotation info, e.g. `@JvmBlocking`
markAnnotation = MarkAnnotation(
classInfo = ClassInfo("love.forte.plugin.suspendtrans.annotation", "JvmBlocking"), // class info for this annotation
baseNameProperty = "baseName", // The property used to represent the 'base name' in the annotation, e.g. `@JvmBlocking(baseName = ...)`
suffixProperty = "suffix", // The property used to represent the 'suffix' in the annotation, e.g. `@JvmBlocking(suffix = ...)`
asPropertyProperty = "asProperty", // The property used to represent the 'asProperty' in the annotation, e.g. `@JvmBlocking(asProperty = true|false)`
defaultSuffix = "Blocking", // Default value used when property 'suffix' (the value of suffixProperty) does not exist (when not specified by the user) (the compiler plugin cannot detect property defaults directly, so the default value must be specified from here)
// e.g. @JvmBlocking(suffix = "Abc"), the suffix is 'Abc', but `@JvmBlocking()`, the suffix is null in compiler plugin, so use the default suffix value.
defaultAsProperty = false, // Default value used when property 'suffix' (the value of suffixProperty) does not exist (Similar to defaultSuffix)
),
// the transform function, e.g.
// 👇 `love.forte.plugin.suspendtrans.runtime.$runInBlocking$`
// it will be like
// ```
// @JvmBlocking suspend fun runXxx() { ... }
// fun runXxxBlocking() = `$runInBlocking$` { runXxx() /* suspend */ } // generated function
// ```
transformFunctionInfo = FunctionInfo(
packageName = "love.forte.plugin.suspendtrans.runtime",
className = null, // null if top-level function
functionName = "\$runInBlocking\$"
),
transformReturnType = null, // return type, or null if return the return type of origin function, e.g. `ClassInfo("java.util.concurrent", "CompletableFuture")`
transformReturnTypeGeneric = false, // if you return like `CompletableFuture<T>`, make it `true`
originFunctionIncludeAnnotations = listOf(IncludeAnnotation(ClassInfo("kotlin.jvm", "JvmSynthetic"))), // include into origin function
copyAnnotationsToSyntheticFunction = true,
copyAnnotationExcludes = listOf(ClassInfo("kotlin.jvm", "JvmSynthetic")), // do not copy from origin function
syntheticFunctionIncludeAnnotations = listOf(IncludeAnnotation(jvmApi4JAnnotationClassInfo)) // include into synthetic function
)
addJvmTransformers(
customJvmTransformer, ...
)
// or addJsTransformers(...)
}
Kotlin Suspend Interface reversal : 基于 KSP,为包含挂起函数的接口或抽象类生成与平台兼容的扩展类型。
参考 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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。