Skip to content

Allows you to collect symbols (classes, etc.) into a list or map.

License

Notifications You must be signed in to change notification settings

stashymane/ksp-symbol-collector

Repository files navigation

KSP Symbol Collector

Allows you to define an annotation which collects symbols into a list or map.

Originally made for helping with serialization without reflection.

Current issues

  • KSP2 does not work. not my fault
  • Multiplatform projects work like the example only if there is more than one target. Needs fix from either KMP gradle or KSP.

Setup

Check out the test-proj module for a Kotlin Multiplatform example on how to set everything up. Setup for Kotlin JVM is just like any other KSP project.

Examples

Note - these examples are abbreviated to look good, generated code looks a bit less appealing

Input Output
@CollectSymbols("allClasses")
annotation class CollectToList

@CollectToList
class Foo

@CollectToList
class Bar

symbols/Symbols.kt

val allClasses: Collection<KClass<*>> =
    listOf(Foo::class, Bar::class)
Input Output
@CollectSymbols(
    "symbolMap",
    type = Type.MapByProperty,
    property = "name"
)
annotation class ToMap(val name: String)

@ToMap("foo-class")
class Foo

@ToMap("bar-class")
class Bar

symbols/Symbols.kt

val symbolMap: Map<String, KClass<*>> =
    mapOf(
        "foo-class" to Foo::class,
        "bar-class" to Bar::class
    )

About

Allows you to collect symbols (classes, etc.) into a list or map.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages