ForteScarlet / kotlin-suspend-transform-compiler-plugin

A Kotlin compiler plugin for transforming suspend functions to platform-compatible non-suspend functions, such as the JVM Blocking API and CompletableFuture or JS Promise, etc. 🐱
MIT License
34 stars 3 forks source link

Support for configuring `includeProperty` in the IncludeAnnotation #54

Closed ForteScarlet closed 4 months ago

ForteScarlet commented 4 months ago

Before

build:

useJvmDefault()
// or 

addJvmTransformer(
  Transformer(
    ...,
    syntheticFunctionIncludeAnnotations = listOf(
                IncludeAnnotation(jvmApi4JAnnotationClassInfo)
            )
  )
)

Then the source:

class Foo {
    @JvmBlocking(asProperty = true)
    suspend value(): Int = 1
}

the compiled:

class Foo {
    @JvmSynthetic
    suspend value(): Int = 1

    val valueBlocking: Int
        @Api4J // Problem: RequiresOptIn doesn't work on getters.
        get() = runInBlocking { value() }
}

After

build:

useJvmDefault()
// or 

addJvmTransformer(
  Transformer(
    ...,
    syntheticFunctionIncludeAnnotations = listOf(
                IncludeAnnotation(jvmApi4JAnnotationClassInfo).apply {
                    includeProperty = true 
                    // 👆 support for including to the property
                }
            )
  )
)

Then the source:

class Foo {
    @JvmBlocking(asProperty = true)
    suspend value(): Int = 1
}

the compiled:

class Foo {
    @JvmSynthetic
    suspend value(): Int = 1

    @Api4J // Included to this property
    val valueBlocking: Int
        @Api4J
        get() = runInBlocking { value() }
}