https://kotlinlang.org logo
#compiler
Title
# compiler
s

SecretX

08/06/2022, 8:34 PM
I decided to build a Kotlin Plugin compiler to learn more about bytecode. I planned to build a plugin that adds a
serialVersionUID
(example below) to Kotlin classes that implement (or inherit from)
java.io.Serializable
. I know the compiler already creates those, but I wanted to simulate that (and maybe change the value to experiment with it). I got the Gradle plugin and subplugin finished, alongside the
CommandLineProcessor
and
ComponentRegistrar
. The only part left is to figure out how to: - Detect/find if the class already has a
serialVersionUID
variable declared - Add the
serialVersionUID
exactly as it is below to the declaration of the class And this last task is the one I can't figure out how to do. Should I use a
ClassBuilderInterceptorExtension
, a
ExpressionCodegenExtension
, or something else entire (because I saw there are bunchs of IR-somethings extensions to do a lot of things), what of those would be more appropriate for this purpose? And second, how to actually add fields to a class using these extensions?
Copy code
private static final long serialVersionUID = 1L;
t

Tóth István Zoltán

08/07/2022, 6:05 AM
I would write an IR transformer, override the
visitClassNew
method, call the transformer from the generation extension. TL;DR check https://github.com/spxbhuhb/zakadabar-stack/blob/rui/plugin/rui/rui-kotlin-plugin/[…]in/kotlin/zakadabar/rui/kotlin/plugin/RuiGenerationExtension.kt When I try to figure out what to do I usually follow these steps: 1. In IDEA I use the "Find Usages" function on IR classes/functions to see if there is an example somewhere in the compiler. 2. I check the source code of the plugin Compose uses (see link below). 3. I check the source code of the plugin kotlinx.serialization uses (see link below). 4. I start to read the source code of the Kotlin compiler to figure out what's going on. 5. I ask here. :) Compose source code kotlin.serializtion (and other plugins) source code I don't know if you've seen this already, but it is very helpful: https://bnorm.medium.com/writing-your-second-kotlin-compiler-plugin-part-1-project-setup-7b05c7d93f6c I've been working on a reactive UI plugin for a while, you can check it for examples. It generates classes from functions, you might find an example for almost everything: Rui. Please note, that I'm a hobbyist in plugin development, so take everything I say with a grain of salt.
8 Views