Hey I was wondering if it was possible to put properties on a class via annotation?
Example being
Copy code
annotation class Example(val a: String)
@Example("t")
class Foo()
// to
@Example("t")
class Foo(val: a = "t")
a
Ayfri
01/03/2023, 7:03 AM
Yes it's possible but with an annotation processor, for example KSP
t
Tech
01/03/2023, 10:55 AM
Ah, I’ve been looking into this for a bit and I’ve seen KSP but it’s something I didn’t really want to learn how to implement yet but if it’s the only way I’ll just learn it.
Thank you!
j
Javier
01/03/2023, 12:38 PM
ksp
allows transformations?
Javier
01/03/2023, 12:38 PM
I don't think so, you probably will need your own compiler plugin
t
Tech
01/03/2023, 1:27 PM
🤔
I’m not home at the moment but when I was looking into it I believe I saw you could write into classes and stuff so you could probably add properties onto it
Tech
01/03/2023, 1:27 PM
Not too sure though.
s
Stephan Schröder
01/04/2023, 10:23 AM
Javier is correct. KSP can only add code, but not modify it. See https://kotlinlang.org/docs/ksp-overview.html#overview :
1. Processors read and analyze source programs and resources.
2. Processors generate code or other forms of output.
3. The Kotlin compiler compiles the source programs together with the generated code.
Unlike a full-fledged compiler plugin, processors cannot modify the code. A compiler plugin that changes language semantics can sometimes be very confusing. KSP avoids that by treating the source programs as read-only.