# compiler


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
(example below) to Kotlin classes that implement (or inherit from)
. 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
. The only part left is to figure out how to: - Detect/find if the class already has a
variable declared - Add the
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
, a
, 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óth István Zoltán

08/07/2022, 6:05 AM
I would write an IR transformer, override the
method, call the transformer from the generation extension. TL;DR check[…]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: 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.