Hi folks, question about annotation processing. An...
# announcements
a
Hi folks, question about annotation processing. Anyone knows how to read the annotations of the properties in a interface when processing the interface class?
Copy code
@Model
interface Config {
    @Prop(name = "renamedConfigField")
    val configField: String
}
When processing the Config interface I would like to be able to iterate over it’s properties (which I’m already doing) but I’m not being able to read the fields annotations (e.g. read the @Prop annotation on the
configField
)
I’m working with KotlinPoet by the way
Ultimately what I want to achieve is to generate a class like this:
Copy code
class ConfigModel(val renamedConfigField: String)
t
Have you looked into #C013BA8EQSE? It’s a library/framework that facilitates Kotlin-specific annotation processing (and you can use KotlinPoet, though there isn’t an official binding from KSP to KotlinPoet like there is to kotlin.reflection).
KSP’s libraries allow you to retrieve lots of information about Kotlin source declarations. I’ve used it to build exactly what you describe - an annotation processor capable of deriving a class (with a constructor) from an annotated interface.
a
Hmm I didn’t. I’ll look into it. Thanks man!
t
a
Cool stuff!
Rewriting the processor I had with the #C013BA8EQSE. Much easier… I still didn’t reach the file generation part yet but looks promising.
Also, did you figure out a way to log? I’m having to write into a file as a logging tool
😆 1
t
I'll be honest, I haven't used/looked for a Kotlin logging library, probably should get around to that at some point 😅
Just use file gen to write a Kotlin source file containing the problems found? 😂
a
println
neither the
logger
passed in the
init()
prints in the std out
Yeah that’s what I am doing ahhaha. But seriously the main logic I had is basically done. MUCH MUCH easier it’s like magic
it just works
t
Hmm, it would print to the compiler's output stream, which may be truncated/hidden in a file somewhere
The folks in #C013BA8EQSE may have some better suggestions though!
The maintainers of the project are pretty active :)
a
Really appreciate your help man! thanks a lot!!
t
My pleasure, glad you found KSP useful :D It's a great tool!