Is Byte Buddy intended to be used for instrumenting Kotlin code?

16 views
Skip to first unread message

Guillaume Darmont

unread,
Jun 22, 2025, 4:35:18 AMJun 22
to Byte Buddy
Hey everyone!

I'm currently playing with Byte Buddy to instrument a codebase of mixed Java and Kotlin code.
Instrumentation fails on some Kotlin methods or parameters which have names considered by Byte Buddy as not valid.
A reproducer is available here https://github.com/gdarmont/kotlin-byte-buddy-tests

Example of Kotlin parameter names: <set-?>, <unused var>, <destruct>. Left and right angle brackets are part of the name.
These names are clearly not valid in Java, but are valid for the JVM, as specified in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.2.2 

Hence my question in the title, is Byte Buddy bytecode generation meant to be compatible with Java source code only?

Regards,
Guillaume

Rafael Winterhalter

unread,
Jun 22, 2025, 6:54:16 AMJun 22
to Guillaume Darmont, Byte Buddy

It is not, you can disable type validation entirely:

new ByteBuddy().with(Type validation.DISABLED)

By default, Byte Buddy adheres to the spec, but early Kotlin compilers were not compliant. (The reflection API would struggle on those, too)


--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/byte-buddy/dd07e474-726b-4639-9b19-0e5a798dab70n%40googlegroups.com.

Guillaume Darmont

unread,
Jun 23, 2025, 3:44:04 AMJun 23
to Rafael Winterhalter, Byte Buddy
Thanks for the answer. Disabling validation will help as a quick workaround for now.

Regarding the specification compliance, validation code for Unqualified Names in BB should be updated, as Kotlin 2.1 generated bytecode is definitely legit regarding the Java Virtual Machine Specification, and in fact, is classloaded and runs without any problems in production.
I may have a look into this if I find some time in the next few days.

Thanks again.
Guillaume

Rafael Winterhalter

unread,
Jun 23, 2025, 5:58:33 AMJun 23
to Guillaume Darmont, Byte Buddy
Thanks, very possible that the check is wrong. It is implemented in InstrumentedType#isValidIdentifier.

If you have time to have a look, I would appreciate it!
Reply all
Reply to author
Forward
0 new messages