Search code examples
javabytecodeinvokedynamic

InvokeDynamic: Is it possible to pass call site bytecode to the bootstrap method and then execute it


Let's say I have a custom Java compiler or bytecode agent.

Is it possible to pass call site bytecode to my bootstrapping handler in a way that won't require me to generate an anonymous class to "host" the bytecode inside a synthetic method?

I.e once I have passed the bytecode I want to execute it with as little overhead as possible (assuming I have already got the stack and local variable array just right for the bytecode to work)


Solution

  • Okay so I finally found what I need:

    http://www.docjar.com/html/api/sun/invoke/anon/AnonymousClassLoader.java.html

    It's not standardised yet and might get ditched but it allows a much lighter touch.

    John Rose talks about it on his blog:

    https://blogs.oracle.com/jrose/entry/anonymous_classes_in_the_vm

    Another option might be to convert the calling byte code to MethodHandles and just pass that through.