A KSP plugin for generating compile-time annotated variable list.
plugins {
kotlin("jvm")
id("com.google.devtools.ksp")
application
}
application {
mainClass.set("moe.sdl.test.MainKt")
}
// Makes generated code visible to IDE
kotlin.sourceSets.main {
kotlin.srcDirs(
file("$buildDir/generated/ksp/main/kotlin"),
)
}
dependencies {
implementation("moe.sdl.annolist:annolist-annotations:<VERSION>")
ksp("moe.sdl.annolist:annolist-processor:<VERSION>")
}
- Create an annotation class for annotating variables
// moe.sdl.test.Main
@AnnoList(
listName = "TEST_LIST",
pkg = "moe.sdl.test",
fileName = "Property",
isInternal = true,
)
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.PROPERTY)
annotation class TestList
data class SomeComplexClass<T>(val value: T)
- Annotate variables with the class created before.
// moe.sdl.test.A
@TestList
val test = SomeComplexClass(1)
// moe.sdl.test.B
package moe.sdl.test
@TestList
val test1 = SomeComplexClass(123.123)
// moe.sdl.test.C
package moe.sdl.test
@TestList
val test2 = SomeComplexClass("Hello")
- Compile and you can see the generated source file:
package moe.sdl.test
internal val TEST_LIST = listOf(
moe.sdl.test.test,
moe.sdl.test.test1,
moe.sdl.test.test2,
)
- Use the list:
fun main() {
TEST_LIST.forEach {
println(it.value)
}
}
- Support top-level varaible
- Support
object
andcompanion object
- Better error handling and error message
- Use something like
kotlinpoet
instead of hand-writing to generating source code
Distributed under the Apache 2.0 license.