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

steenooo

12/09/2019, 1:27 PM
hey uhm, Im writing a kotlin plugin loader for For minecraft development (spigot). Some features require alot of annoying boilerplate. Since I control the classloader used to load it, would it be advicable to add a feature to generate this boilerplate with ASM when loading the class? (They already modify the bytecode a little bit). Fx their event system requires you to provide a static instance of an object, and override a method returning this instance. Its the same few lines for every class extending class. This makes the code in kotlin very ugly
And I was wondering how I would accomplish this. Is it possible to use something like kotlinpoet? Or do I have to use ASM
m

Mike War

12/09/2019, 2:29 PM
maybe a compiler plugin? (not that I have any experience, but according to things I heard during KotlinConf it sounds like the right approach)
that way you could implement your own annotation, and have it parsed+modified at compile-time
b

Burkhard

12/09/2019, 2:52 PM
Since it’s minecraft your developing using kotlin jvm. You can look into using kapt combined with kotlinpoet. Kapt has some of the same capabilities that a compiler plugin does but is not quite as powerful. The restriction is that you can not modify existing classes. You can only create new classes, functions, extnsion functions. So if that is enough to do your automated work I would suggest using kapt over a compiler plugin. It’s easier to learn and has a stable API. I don’t know anything about ASM so not sure if ASM is the right way to go. It sounds like it can solve your problem, but my guess is that ASM is more complex to use than it is worth to solve this.
g

gildor

12/09/2019, 2:59 PM
Maybe you could show an example of such boilerplate
s

steenooo

12/09/2019, 3:35 PM
image.png
Every event requires that getHandlers method, and the HandlerList instance. It's the same code per event type. But every event type requires their own
g

gildor

12/09/2019, 11:16 PM
You cannot use annotation processing to generate it, because there is no way to modify existing code
Compiler plugin would be huge overkill for such use case imo
21 Views