Hello! I am exploring KSP to write a compiler plugin, something like Parcelize, so it would implement a predefined interface for annotated classes. Like you have a data class that implements the interface but you don't write the actual implementation code. And then you annotate the class with a marker annotation and the plugin should add the implementation. Is it a doable task for KSP?
j
jw
04/01/2021, 4:38 PM
No. You cannot modify existing source files. Only generate new ones.
a
Arkadii Ivanov
04/01/2021, 4:39 PM
Thanks, that is what I expected, just wanted to confirm.
t
Tyler Hodgkins
04/01/2021, 8:22 PM
If it’s acceptable, an alternate approach is to annotate an interface or abstract class and generate an implementation & a builder for the implementation.
Tyler Hodgkins
04/01/2021, 8:26 PM
Or if you can get away with it, generate extension functions defined for the annotated class that perform your logic. This depends on not needing to fulfill some interface.