跳到主要内容

Kotlin Compiler Plugin - Multi Receiver

自动收集自 lib/kcp/multireceiver/README.md

A Kotlin compiler plugin that transforms function parameters into receivers or context parameters.

Features

Single Parameter → Extension Receiver

When a function annotated with @GenerateExtension has exactly one source-level value parameter, the plugin generates an extension wrapper with that parameter as the receiver.

Before:

@GenerateExtension
fun process(data: String): Result {
// ...
}

After (generated):

fun String.process(): Result {
// ...
}

Multiple Receivers → Context Parameters

When a function has one or more parameters annotated with @Receiver, those parameters become context parameters in the generated wrapper.

Before:

@GenerateExtension
fun combine(@Receiver foo: Foo, @Receiver bar: Bar, value: Int): Result {
// ...
}

After (generated):

context(foo: Foo, bar: Bar)
fun combine(value: Int): Result {
// ...
}

JVM Compatibility

Generated wrappers keep the same Kotlin source name, but they are emitted with unique @JvmName(...) values to avoid JVM signature clashes with the original declaration.

Usage

  1. Apply the Gradle subplugin:
plugins {
id("site.addzero.kcp.multireceiver")
}
  1. The subplugin will:
  • add site.addzero:kcp-multireceiver-annotations
  • add -Xcontext-parameters
  • wire the compiler plugin artifact automatically
  1. Annotate your functions:
import site.addzero.kcp.annotations.GenerateExtension
import site.addzero.kcp.annotations.Receiver

@GenerateExtension
fun wrap(value: MyType) { }

@GenerateExtension
fun render(@Receiver scope: Scope, value: Int) { }

Modules

  • kcp-multireceiver-annotations - Runtime annotations
  • kcp-multireceiver-plugin - Compiler plugin implementation
  • kcp-multireceiver-gradle-plugin - Gradle subplugin integration
  • kcp-multireceiver-idea-plugin - IntelliJ IDEA companion plugin

Verification

  • Compiler plugin integration tests prove top-level/member extension wrappers and context wrappers compile and run.
  • Gradle smoke tests prove a consumer project can apply site.addzero.kcp.multireceiver and call generated APIs.
  • IDEA plugin builds successfully with :lib:kcp:multireceiver:kcp-multireceiver-idea-plugin:buildPlugin.