How do I use the new @BootstrapMethod annotation?

John Rose john.r.rose at oracle.com
Tue Nov 16 12:57:56 PST 2010


assembling.signals, here's an update of your example code that works with my "indify" tool and a recent build of mlvm.

It probably won't work exactly right with the current OpenJDK builds.

The "SHOULD BE" lines are bits that don't work yet with the indify tool.

Note the different in size in the two versions of the class file.  The second version adds new constants, but deletes the MH_, MT_ and INDY_ pattern methods.

Check my blog for references:  http://blogs.sun.com/jrose/entry/a_modest_tool_for_writing

-- John

import java.dyn.*;
import java.dyn.MethodHandles.Lookup;

//OLD// @BootstrapMethod(value = _InvokeDynamic.Bootstrap.class, name = "bootstrap")
public class _InvokeDynamic {

   public final static class Bootstrap {
       //OLD// public static CallSite bootstrap(Class<?> declaring, String name, MethodType type) {
       /*NEW*/ public static CallSite bootstrap(Lookup declaringLookup, String name, MethodType type) {
           /*NEW*/ Class<?> declaring = declaringLookup.lookupClass();
           CallSite cs = new CallSite(/*NEW*/MT_II_I());
           System.out.println(declaring + "." + name + " : " + type);
           cs.setTarget(mhNormal);
           return cs;
       }
   }
/*NEW*/   private static MethodHandle MH_bootstrap() throws ReflectiveOperationException {
/*NEW*/     return MethodHandles.lookup()
                   .findStatic(Bootstrap.class, "bootstrap",
                     //SHOULD BE//MethodType.methodType(CallSite.class, Lookup.class, String.class, MethodType.class)
                     MethodType.fromMethodDescriptorString(
                         "(Ljava/dyn/MethodHandles$Lookup;Ljava/lang/String;Ljava/dyn/MethodType;)Ljava/dyn/CallSite;"
                         , null));
          }

    private final static MethodHandle mhNormal;

   public static int staticMethod(int i1, int i2) {
       return i1 + i2;
   }
/*NEW*/   private static MethodHandle MH_staticMethod() throws ReflectiveOperationException {
/*NEW*/       return MethodHandles.lookup()
                   .findStatic(_InvokeDynamic.class, "staticMethod", MT_II_I());
/*NEW*/   }
/*NEW*/   private static MethodType MT_II_I() {
/*NEW*/       return //SHOULD BE//MethodType.methodType(int.class, int.class, int.class)
                               MethodType.fromMethodDescriptorString("(II)I", null);
/*NEW*/   }

   static {
       try {
           if (false) /*OLD*/
           mhNormal = MethodHandles.lookup().findStatic(
                   _InvokeDynamic.class, "staticMethod",
                   MethodType.methodType(int.class, int.class, int.class));
           else
               mhNormal = MH_staticMethod();
       } catch (Exception ex) {
           throw new Error(ex);
       }
   }

   private static void doDynSyntaxUsual()
           throws Throwable {
       int x;
       //OLD//x = (int) InvokeDynamic.ANY((int) -1, (int) +2);
       x = (int) INDY_ANY().invokeExact((int) -1, (int) +2);
       System.out.println(x);
   }
    private static MethodHandle INDY_ANY() throws Throwable {
        CallSite cs = (CallSite) MH_bootstrap().invokeGeneric(MethodHandles.lookup(),"ANY",MT_II_I());
        return cs.dynamicInvoker();
    }

   public static void main(String... args)
           throws Throwable {
       doDynSyntaxUsual();
   }

}
/* EXAMPLE RUN:
--------
	$JAVA7X_HOME/bin/javac -d Unmanaged Unmanaged/_InvokeDynamic.java
--------
	ls -l Unmanaged/_InvokeDynamic.class
-rw-r--r--  1 jrose  staff  2124 Nov 16 12:51 Unmanaged/_InvokeDynamic.class
--------
	$JAVA7X_HOME/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic -cp Unmanaged _InvokeDynamic
class _InvokeDynamic.ANY : (int,int)int
1
--------
	ls -l Unmanaged/_InvokeDynamic.class
-rw-r--r--  1 jrose  staff  1924 Nov 16 12:51 Unmanaged/_InvokeDynamic.class
--------
	$JAVA7X_HOME/bin/java -jar dist/indify.jar --overwrite --dest Unmanaged Unmanaged/_InvokeDynamic.class 
reading Unmanaged/_InvokeDynamic.class
pattern methods found: [MH_staticMethod()L, MH_bootstrap()L, INDY_ANY()L, MT_II_I()L]
patching _InvokeDynamic.MH_staticMethod()L
8:invokestatic 2 MT_II_I()L => ldc 111:16=40
patching _InvokeDynamic.doDynSyntaxUsual()V
0:invokestatic 13 INDY_ANY()L;...; 5:invokevirtual 14 => invokedynamic 120:18=[115, 119, 0]
patching _InvokeDynamic.INDY_ANY()L
0:invokestatic 17 MH_bootstrap()L => ldc 115:15=[6, 114]
8:invokestatic 2 MT_II_I()L => ldc 111:16=40
patching _InvokeDynamic.access$000()L
0:invokestatic 2 MT_II_I()L => ldc 111:16=40
patching _InvokeDynamic.<clinit>()V
0:invokestatic 22 MH_staticMethod()L => ldc 118:15=[6, 117]
wrote Unmanaged/_InvokeDynamic.class
--------
	$JAVA7X_HOME/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic -cp Unmanaged _InvokeDynamic
class _InvokeDynamic.ANY : (int,int)int
1
--------
*/


-------------- next part --------------
A non-text attachment was scrubbed...
Name: _InvokeDynamic.zip
Type: application/zip
Size: 1422 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20101116/fc23b503/attachment.zip 


More information about the mlvm-dev mailing list