I'm generating bytecode roughly equivalent to a class like:
final class MyCls { final MethodHandle handle1; final MethodHandle handle2; // and so on // This needs to invoke `handle1`, `handle2`, etc. in it somehow final static myMethod() { // ... }}
The class is fairly long-lived and I wish to call the MethodHandle
s from inside other methods, with ideally as little overhead as possible. What would be the best way to do this? The two ideas that come to mind are:
- Generating explicit
MethodHandle.invokeExact
calls on the fields - Using
invokedynamic
somehow (although I think I'd still need theexactInvoker
?)
The handles will vary in signatures (although their use-sites should all use the right signatures - I can detect/enforce that at codegen time).
Update
Here's some extra context on what I'm actually doing. The classes represent compiled WASM modules, the method handles are imported functions, and each instance of the class in another instance of the WASM module.
Using MethodHandle
to represent imported functions isn't a necessity here - I could also accept something like a java.util.function.Function
or maybe even just a virtual method invocation. I do need a MethodHandle
representation sometimes, but I could summon one up from a virtual method too (and I could implement a virtual method manually calling a Function
too).
The module class instances themselves might end up being stored in static fields but that's not guaranteed. If there is a way to speed up that case, I could recommend users use that.