Search |
||
ASM now supports invokedynamicPosted by forax on June 11, 2009 at 10:42 PM PDT
Great news, ASM 3.2 is released
(Extended changelog).
ASM supports of invokedynamic
From ASM point of view, invokedynamic is a
method call instruction emittable/trappable by
calling/overriding visitInsnMethod
of a MethodVisitor
like any other invoke* bytecode instructions.
Example This simple code transforms all invokevirtual to invokedynamic, registers a boostrap method and configure invokedynamic call site to perform an invokevirtual.
public class Invokedynamiker extends ClassAdapter {
boolean isStaticInitPatched;
public Invokedynamiker(ClassVisitor cv) {
super(cv);
}
@Override
public MethodVisitor visitMethod(int access, final String name, final String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
return new MethodAdapter(mv) {
@Override
public void visitCode() {
super.visitCode();
if ("
The boostrap method creates a call site and links the call site target to a method handles performing an invokevirtual.
public static CallSite bootstrapMethod(Class<?> callerClass, String methodName, MethodType methodType) {
MethodType argumentType = methodType.dropParameterType(0);
MethodHandle mh = MethodHandles.lookup().findVirtual(methodType.parameterType(0), methodName, argumentType);
CallSite site = new CallSite(callerClass, methodName, methodType);
site.setTarget(mh);
return site;
}
The code of the bootstrap method can be changed to, by example,
log the arguments before calling the destination method using
method handle adapters :
convertArguments, spreadArguments, collectArguments
and a java.dyn.JavaMethodHandle.
The whole code is available as a zip :
indy-asm-sample.zip.
cheers,
»
Related Topics >>
Open JDK Comments
Comments are listed in date ascending order (oldest first)
Submitted by jrose on Fri, 2009-06-12 14:42.
That's a very good example. With a similar transformation of invokespecial (except ) and invokestatic, it could provide a strong stress test for the JSR 292 core.
|
||
|
|