|
| 1 | +# Kotlin Suspend Interface reversal |
| 2 | + |
| 3 | +[English](README.md) | 中文 |
| 4 | + |
| 5 | +基于 KSP,为包含挂起函数的接口或抽象类生成与平台兼容的扩展类型。 |
| 6 | + |
| 7 | +## 简介 |
| 8 | + |
| 9 | +假设:你有一个如下所示的接口: |
| 10 | + |
| 11 | +```kotlin |
| 12 | +interface Foo { |
| 13 | + /** get value */ |
| 14 | + suspend fun value(): Int |
| 15 | + |
| 16 | + /** get name */ |
| 17 | + val name: String |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +现在,你希望此接口由你的用户实现并扩展,但事实上 Java 用户可能很难直接使用它。 |
| 22 | + |
| 23 | +此时,你又提供两个额外的接口来适应阻塞和异步API,如下所示: |
| 24 | + |
| 25 | +```kotlin |
| 26 | +import java.util.concurrent.CompletableFuture |
| 27 | + |
| 28 | +interface Foo { |
| 29 | + /** get value */ |
| 30 | + @JvmSynthetic |
| 31 | + suspend fun value(): Int |
| 32 | + |
| 33 | + /** get name */ |
| 34 | + val name: String |
| 35 | +} |
| 36 | + |
| 37 | +/** 提供给 Java */ |
| 38 | +interface JBlockingFoo : Foo { |
| 39 | + fun valueBlocking(): Int |
| 40 | + |
| 41 | + @JvmSynthetic |
| 42 | + override suspend fun value(): Int = valueBlocking() |
| 43 | +} |
| 44 | + |
| 45 | +/** 提供给 Java */ |
| 46 | +interface JAsyncFoo : Foo { |
| 47 | + fun valueAsync(): CompletableFuture<out Int> |
| 48 | + |
| 49 | + @JvmSynthetic |
| 50 | + override suspend fun value(): Int = valueAsync().await() |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +通过这种方式,Java 用户可以实现它了: |
| 55 | + |
| 56 | +```java |
| 57 | +public class FooImpl implements JBlockingFoo { |
| 58 | + @Override |
| 59 | + public int valueBlocking() { |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String getName() { |
| 65 | + return "name"; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## 使用 |
| 71 | + |
| 72 | +### JVM |
| 73 | + |
| 74 | +```kotlin |
| 75 | +plugins { |
| 76 | + id("org.jetbrains.kotlin.jvm") version "$KOTLIN_VERSION" |
| 77 | + id("com.google.devtools.ksp") version "$KSP_VERSION" |
| 78 | +} |
| 79 | + |
| 80 | +repositories { |
| 81 | + mavenCentral() |
| 82 | +} |
| 83 | + |
| 84 | +dependencies { |
| 85 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$COROUTINES_VERSION") |
| 86 | + // annotation |
| 87 | + compileOnly("love.forte.suspend-interface-reversal:annotations:$VERSION") |
| 88 | + // ksp processor |
| 89 | + ksp("love.forte.suspend-interface-reversal:processor:$VERSION") |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Multiplatform |
| 94 | + |
| 95 | +> [!note] |
| 96 | +> 支持 `JVM` 和 `JS` 目标。 |
| 97 | +
|
| 98 | +```kotlin |
| 99 | +plugins { |
| 100 | + id("org.jetbrains.kotlin.multiplatform") version "$KOTLIN_VERSION" |
| 101 | + id("com.google.devtools.ksp") version "$KSP_VERSION" |
| 102 | +} |
| 103 | + |
| 104 | +repositories { |
| 105 | + mavenCentral() |
| 106 | +} |
| 107 | + |
| 108 | +kotlin { |
| 109 | + jvm { |
| 110 | + // ... |
| 111 | + } |
| 112 | + js(IR) { |
| 113 | + // ... |
| 114 | + } |
| 115 | + |
| 116 | + linuxX64() |
| 117 | + macosX64() |
| 118 | + mingwX64() |
| 119 | + // ... |
| 120 | + sourceSets { |
| 121 | + commonMain.dependencies { |
| 122 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$COROUTINES_VERSION") |
| 123 | + compileOnly("love.forte.suspend-interface-reversal:annotations:$VERSION") |
| 124 | + } |
| 125 | + |
| 126 | + jsMain.dependencies { |
| 127 | + implementation("love.forte.suspend-interface-reversal:annotations:$VERSION") |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// ksp |
| 133 | +dependencies { |
| 134 | + add("kspJvm", "love.forte.suspend-interface-reversal:processor:$VERSION") // process JVM |
| 135 | + add("kspJs", "love.forte.suspend-interface-reversal:processor:$VERSION") // process JS |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### 代码应用 |
| 140 | + |
| 141 | +添加 `@SuspendReversal` 到接口或抽象类上。 |
| 142 | + |
| 143 | +```kotlin |
| 144 | +@SuspendReversal |
| 145 | +interface Foo { |
| 146 | + val age: Int get() = 5 // 会被跳过/忽略 |
| 147 | + |
| 148 | + @JvmSynthetic |
| 149 | + suspend fun run() // 会生成对应的阻塞/异步函数 |
| 150 | + |
| 151 | + @JvmSynthetic |
| 152 | + suspend fun get(): Int // 会生成对应的阻塞/异步函数 |
| 153 | + |
| 154 | + val name: String // 会被跳过/忽略 |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +> [!note] |
| 159 | +> 如果 `SuspendReversal.markJvmSynthetic = true`, |
| 160 | +> 那么必须 (或者说强烈建议) 在原本的挂起函数上标记 `@JvmSynthetic`。 |
| 161 | +
|
| 162 | + |
| 163 | +## 注意事项 |
| 164 | + |
| 165 | +1. 处理器将仅处理 **抽象、挂起** 的函数。 |
| 166 | + |
| 167 | +2. 在 Java 中,返回值为 `Unit` 的函数被转化为 `CompletableFuture<Void?>`(用于生成的异步函数) |
| 168 | + |
| 169 | +3. 会从源函数上复制 `@kotlin.Throws` 和 `@kotlin.jvm.Throws`. |
| 170 | + |
| 171 | +4. 会**粗略**计算生成的函数是否需要标记继承。 |
| 172 | + |
| 173 | +e.g. |
| 174 | +```kotlin |
| 175 | +interface Foo { |
| 176 | + suspend fun run() |
| 177 | + fun runBlocking() { /*...*/ } |
| 178 | +} |
| 179 | + |
| 180 | +// 生成 👇 |
| 181 | + |
| 182 | +interface JBlockingFoo : Foo { |
| 183 | + override fun runBlocking() // 将会标记 `override` |
| 184 | + suspend fun run() { |
| 185 | + runBlocking() |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +> [!warning] |
| 191 | +> 这种判断很粗糙,不是很可靠。 |
| 192 | +> 例如,它不会确定它是否是 `final`、参数是否具有继承关系,等等。 |
| 193 | +
|
| 194 | + |
| 195 | +## 友情链接 |
| 196 | + |
| 197 | +[Kotlin suspend transform compiler plugin](https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin) |
| 198 | +: 用于为Kotlin挂起函数自动生成平台兼容函数的Kotlin编译器插件。 |
0 commit comments