I'm currently trying to learn how to create Kotlin Compiler Plugins for the JVM and I'd like to change a method's body as an example, the API requires to manipulate Java byte code using ASM API in order to make any changes to a class' components. The Kotlin compiler plugin provides this class where, if for example, you'd like to modify a class' method body, you'd need to override the newMethod
function, which returns a MethodVisitor
instance, which is the one that you'd need to manually modify using the ASM's API.
I would like to be able of doing so without having to write ASM instructions and I was wondering if there was a way to add instructions straight into a MethodVisitor
object using Byte Buddy's high level API?
Many thanks!
Quite honestly, the API is a bit awkward since it does not follow the standard model of ASM where you wrap a class visitor and since it shades the ASM dependency which makes it non-compatible with any ASM user.
In Byte Buddy, you can register your own visitors so what you can do is to define a custom class in Byte Buddy, and before calling make
, you'd register an AsmVisitorWrapper
which delegates to the visitor Kotlin yields. You'd need to proxy all method calls from Kotlin's ASM namespace to Byte Buddy's ASM namespace, though what you can do using method handles.