changeset 40:6552594edb94

indy: convert demos for recent changes (6979327, 6980096)
author jrose
date Fri, 24 Sep 2010 14:27:37 -0700
parents ea314633878b
children 82062030d68e
files netbeans/indy-demo/src/FidgetDemo.java netbeans/indy-demo/src/GetNameDemo.java netbeans/indy-demo/src/Hello.java netbeans/indy-demo/src/JavaDocExamples.java netbeans/indy-demo/src/Main.java netbeans/indy-demo/src/PrintArgsDemo.java netbeans/indy-demo/src/ShouldNotWork.java netbeans/indy-demo/src/VMILDemos.java
diffstat 8 files changed, 113 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/indy-demo/src/FidgetDemo.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/FidgetDemo.java	Fri Sep 24 14:27:37 2010 -0700
@@ -41,7 +41,8 @@
         for (int i = 0; i < 6; i++) {
             System.out.println("--- loop #"+i);
             for (String a : av) {
-                String result = InvokeDynamic.<String>fidget(a);
+                @BootstrapMethod(value=FidgetDemo.class, name="linkDynamic")
+                String result = (String) InvokeDynamic.<String>fidget(a);
                 System.out.println(result);
             }
         }
@@ -57,25 +58,32 @@
     }
 
     // It is easy to build hybrid closure-like method handles out of classes.
-    private static class Guard extends JavaMethodHandle {
+    private static class Guard implements MethodHandleBox {
         MethodHandle target;  // wrapped target
         CSite        site;    // included site
         // constructor folds a sneaky site reference into a direct target
         Guard(CSite site, MethodHandle target) {
-            super(INVOKE);
             this.target = target;
             this.site = site;
         }
 
         String invoke(String x) throws Throwable {
             if ((++site.count & 3) == 0)
-                site.setTarget(new Guard(site, which(site.count)));
-            return target.<String>invokeExact(x);
+                site.setTarget(new Guard(site, which(site.count)).asMethodHandle());
+            return (String) target.<String>invokeExact(x);
         }
-        private static final MethodHandle INVOKE =
-            lookup().findVirtual(Guard.class, "invoke",
-                methodType(String.class, String.class));
-
+        public MethodHandle asMethodHandle() {
+            return INVOKE.bindTo(this);
+        }
+        private static final MethodHandle INVOKE;
+        static {
+            try {
+                INVOKE = lookup().findVirtual(Guard.class, "invoke",
+                    methodType(String.class, String.class));
+            } catch (NoAccessException ex) {
+                throw new RuntimeException(ex);
+            }
+        }
         public String toString() { return "Guard:"+target.toString(); }
     }
 
@@ -85,7 +93,7 @@
         public CSite(Class caller, String name, MethodType type) {
             //?? super(String.class, "gloat", make(void.class));
             System.out.println("[link] new call site: "+this);
-            setTarget(new Guard(this, which(0)));
+            setTarget(new Guard(this, which(0)).asMethodHandle());
         }
         // this is just for the noise value:
         public void setTarget(MethodHandle t) {
@@ -95,7 +103,7 @@
     }
 
     // Set up a class-local bootstrap method.
-    static { Linkage.registerBootstrapMethod("linkDynamic"); }
+    //static { Linkage.registerBootstrapMethod("linkDynamic"); }
     private static CallSite linkDynamic(Class caller, String name, MethodType type) {
         assert((Object)name == "fidget" &&
                 type == methodType(String.class, String.class));
--- a/netbeans/indy-demo/src/GetNameDemo.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/GetNameDemo.java	Fri Sep 24 14:27:37 2010 -0700
@@ -31,6 +31,7 @@
 
 import java.dyn.*;
 
+@BootstrapMethod(value=GetNameDemo.class, name="bootstrapDynamic")
 class GetNameDemo {
     public static void main(String... av) throws Throwable {
         if (av.length == 0)  av = new String[]{ "fred.txt" };
@@ -39,11 +40,11 @@
             System.out.println("--- loop #"+i);
             String result1 = file.getName();
             System.out.println(result1);
-            String result2 = InvokeDynamic.<String>getName(file);
+            String result2 = (String) InvokeDynamic.<String>getName(file);
             System.out.println(result2);
-            String result3 = InvokeDynamic.<String>toString((CharSequence) result1);
+            String result3 = (String) InvokeDynamic.<String>toString((CharSequence) result1);
             System.out.println(result3);
-            String result4 = InvokeDynamic.<String>#"static:\=java\|lang\|Integer:toHexString"(result1.length());
+            String result4 = (String) InvokeDynamic.<String>#"static:\=java\|lang\|Integer:toHexString"(result1.length());
             System.out.println(result4);
         }
     }
@@ -84,7 +85,7 @@
     }
 
     // Set up a class-local bootstrap method.
-    static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
+    //static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
     private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
         MethodHandle target = linkDynamic(caller, name, type);
         System.out.println("[link] set site target to "+target);
--- a/netbeans/indy-demo/src/Hello.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/Hello.java	Fri Sep 24 14:27:37 2010 -0700
@@ -31,6 +31,7 @@
 
 import java.dyn.*;
 
+@BootstrapMethod(value=Hello.class, name="bootstrapDynamic")
 public class Hello {
     public static void main(String... av) throws Throwable {
         if (av.length == 0)  av = new String[] { "world" };
@@ -39,7 +40,7 @@
             greeter.<void>invokeExact(whom);  // strongly typed direct call
             // previous line generates invokevirtual MethodHandle.invokeExact(String)void
             Object x = whom;
-            InvokeDynamic.hail(x);               // weakly typed invokedynamic
+            x = InvokeDynamic.hail(x);      // weakly typed invokedynamic
             // previous line generates invokedynamic hail(Object)Object
         }
     }
@@ -51,7 +52,7 @@
                            MethodType.methodType(void.class, String.class));
 
     // Set up a class-local bootstrap method.
-    static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
+    //static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
     private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
         assert(type.parameterCount() == 1 && name.equals("hail"));  // in lieu of MOP
         System.out.println("set target to adapt "+greeter);
--- a/netbeans/indy-demo/src/JavaDocExamples.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/JavaDocExamples.java	Fri Sep 24 14:27:37 2010 -0700
@@ -1,5 +1,5 @@
 /*
- Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ Copyright 2009, 2010 Sun Microsystems, Inc.  All Rights Reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
@@ -55,10 +55,10 @@
 mt = MethodType.methodType(String.class, char.class, char.class);
 mh = lookup.findVirtual(String.class, "replace", mt);
 // (Ljava/lang/String;CC)Ljava/lang/String;
-s = mh.<String>invokeExact("daddy",'d','n');
+s = (String) mh.<String>invokeExact("daddy",'d','n');
 assert(s.equals("nanny"));
 // weakly typed invocation (using MHs.invoke)
-s = (String) mh.invokeVarargs("sappy", 'p', 'v');
+s = (String) mh.<String>invokeVarargs("sappy", 'p', 'v');
 assert(s.equals("savvy"));
 // mt is {Object[] => List}
 mt = MethodType.methodType(java.util.List.class, Object[].class);
@@ -74,7 +74,7 @@
 mt = MethodType.methodType(int.class);
 mh = lookup.findVirtual(java.util.List.class, "size", mt);
 // (Ljava/util/List;)I
-i = mh.<int>invokeExact(java.util.Arrays.asList(1,2,3));
+i = (int) mh.<int>invokeExact(java.util.Arrays.asList(1,2,3));
 assert(i == 3);
 {;;;}
         assert(successful("forMethodHandle"));
@@ -86,13 +86,13 @@
         MethodType mt; MethodHandle mh;
         // for type imports:
         mh = lookup().findVirtual(String.class, "hashCode", methodType(int.class));
-        i = mh.<int>invokeExact("foo");
+        i = (int) mh.<int>invokeExact("foo");
         assert(i == "foo".hashCode());
         mh = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
-        s = mh.<String>invokeExact("foo", "tball");
+        s = (String) mh.<String>invokeExact("foo", "tball");
         assert(s.equals("football"));
         mh = lookup().findVirtual(String.class, "replace", methodType(String.class, char.class, char.class));
-        s = mh.<String>invokeExact("blubber", 'b', 'f');
+        s = (String) mh.<String>invokeExact("blubber", 'b', 'f');
         assert(s.equals("fluffer"));
         assert(successful("forMethodHandle #2"));
     }
@@ -102,68 +102,78 @@
         Greeter greeter = new Greeter("world");
         greeter.run();  // prints "hello, world"
         // Statically typed method handle invocation (most direct):
-        MethodHandle mh = greeter;
+        MethodHandle mh = greeter.asMethodHandle();
         mh.<void>invokeExact();  // also prints "hello, world"
         // Dynamically typed method handle invocation:
-        greeter.invokeVarargs(new Object[0]);  // also prints "hello, world"
+        greeter.asMethodHandle().invokeVarargs(new Object[0]);  // also prints "hello, world"
+    /*!!!
         // We can also do this with symbolic names and/or inner classes:
         new JavaMethodHandle("yow") {
             void yow() { System.out.println("yow, world"); }
         }.invokeVarargs(new Object[0]);
+    */
         assert(successful("forJavaMethodHandle"));
     }
 
     static
-    class Greeter extends JavaMethodHandle {
+    class Greeter implements MethodHandleBox {
         public void run() { System.out.println("hello, "+greetee); }
         private final String greetee;
         Greeter(String greetee) {
-            super(RUN);
             this.greetee = greetee;
         }
+        public MethodHandle asMethodHandle() {
+            return RUN.bindTo(this);
+        }
         // the entry point function is computed once:
-        private static final MethodHandle RUN
-            = MethodHandles.lookup().findVirtual(Greeter.class, "run",
-                  methodType(void.class));
+        private static final MethodHandle RUN;
+        static {
+            try {
+                RUN = MethodHandles.lookup().findVirtual(Greeter.class, "run",
+                          methodType(void.class));
+            } catch (NoAccessException ex) {
+                throw new RuntimeException(ex);
+            }
+        }
     }
 
     /** Code examples from the javadoc in java.dyn.InvokeDynamic. */
+    @BootstrapMethod(value=JavaDocExamples.class, name="bootstrapDynamic")
     static void forInvokeDynamic() throws Throwable {
-        Object x; String s; int i;
-        x = InvokeDynamic.greet("world"); // greet(Ljava/lang/String;)Ljava/lang/Object;
-        s = InvokeDynamic.<String>hail(x); // hail(Ljava/lang/Object;)Ljava/lang/String;
-        InvokeDynamic.<void>cogito(); // cogito()V
-        i = InvokeDynamic.<int>#"op:+"(2, 3); // op:+(II)I
+        exampleForInvokeDynamic();
+    }
+{}  //// BEGIN JAVADOC EXAMPLE
+@BootstrapMethod(value=JavaDocExamples.class, name="bootstrapDynamic")
+static void exampleForInvokeDynamic() throws Throwable {
+    Object x; String s; int i;
+    x = InvokeDynamic.greet("world"); // greet(Ljava/lang/String;)Ljava/lang/Object;
+    s = (String) InvokeDynamic.<String>hail(x); // hail(Ljava/lang/Object;)Ljava/lang/String;
+    InvokeDynamic.<void>cogito(); // cogito()V
+    i = (int) InvokeDynamic.<int>#"op:+"(2, 3); // op:+(II)I
+{}
+        {}
         assert(i == 2+3);
         assert(successful("forInvokeDynamic"));
     }
 
     // Set up a class-local bootstrap method.
-    static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
-    private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
-        MethodHandle target = null;
-        if (name.equals("greet") || name.equals("hail")) {
-            target = new JavaMethodHandle("invoke", type) {
-                private String invoke(Object x) {
-                    System.out.println("howdy, "+x);
-                    return "galaxy";
-                }
-            };
-        }
-        if (name.equals("cogito")) {
-            target = new JavaMethodHandle("invoke") {
-                private void invoke() {
-                    System.out.println("if I only had a brain...");
-                }
-            };
-        }
-        if (name.equals("op:+")) {
-            target = new JavaMethodHandle("invoke") {
-                private int invoke(int x, int y) {
-                    return x + y;
-                }
-            };
-        }
-        return new CallSite(target);
+    private static MethodHandle bootstrapDynamic(Class caller, String name, MethodType type)
+        throws NoSuchMethodException
+    {
+        if (name.contains("op:"))  name = "op";
+        return lookup().findStatic(caller, name+"Handler", type);
+    }
+    private static String hailHandler(Object x) {
+        System.out.println("howdy, "+x);
+        return "galaxy";
+    }
+    private static Object greetHandler(String x) {
+        return hailHandler((Object)x);
+    }
+    private static void cogitoHandler() {
+        System.out.println("if I only had a brain...");
+    }
+    private static int opHandler(int x, int y) {
+        return x + y;
     }
 }
--- a/netbeans/indy-demo/src/Main.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/Main.java	Fri Sep 24 14:27:37 2010 -0700
@@ -51,7 +51,7 @@
             MethodHandle main = lookup().findStatic(c, "main",
                     methodType(void.class, String[].class));
             System.out.println("******** "+c.getName()+"."+main);
-            main.<void>invoke(args);
+            main.<void>invokeExact(args);
         }
     }
     public static void main(String[] args) throws Throwable {
--- a/netbeans/indy-demo/src/PrintArgsDemo.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/PrintArgsDemo.java	Fri Sep 24 14:27:37 2010 -0700
@@ -32,16 +32,21 @@
 import java.dyn.*;
 
 class PrintArgsDemo {
+    @BootstrapMethod(value=PrintArgsDemo.class, name="bootstrapDynamic")
     public static void main(String... av) throws Throwable {
         System.out.println("Printing some argument lists, starting with a empty one:");
-        InvokeDynamic.nothing();
-        InvokeDynamic.foo("foo arg");
-        InvokeDynamic.bar("bar arg", 1);
-        InvokeDynamic.baz("baz arg", 2, 3.14);
+        InvokeDynamic.<void>nothing();
+        InvokeDynamic.<void>foo("foo arg");
+        InvokeDynamic.<void>bar("bar arg", 1);
+        test(); //InvokeDynamic.<void>baz("baz arg", 2, 3.14);
         System.out.println("Done printing argument lists.");
     }
 
     //// BEGIN JAVADOC EXAMPLE
+@BootstrapMethod(value=PrintArgsDemo.class, name="bootstrapDynamic")
+static void test() throws Throwable {
+    InvokeDynamic.<void>baz("baz arg", 2, 3.14);
+}
 private static void printArgs(Object... args) {
   System.out.println(java.util.Arrays.deepToString(args));
 }
@@ -51,7 +56,6 @@
   Class thisClass = lookup.lookupClass();  // (who am I?)
   printArgs = lookup.findStatic(thisClass,
       "printArgs", MethodType.methodType(void.class, Object[].class));
-  Linkage.registerBootstrapMethod("bootstrapDynamic");
 }
 private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
   // ignore caller and name, but match the type:
--- a/netbeans/indy-demo/src/ShouldNotWork.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/ShouldNotWork.java	Fri Sep 24 14:27:37 2010 -0700
@@ -52,9 +52,9 @@
         Sub(String x) throws Throwable {
             super(x);
             System.out.println("invoking "+SPECIAL1);
-            SPECIAL1.<void>invoke((Super)this, x+" via method handle");
+            SPECIAL1.<void>invokeExact((Super)this, x+" via method handle");
             System.out.println("should fail:  invoking "+SPECIAL2);
-            SPECIAL2.<void>invoke(this);
+            SPECIAL2.<void>invokeExact(this);
         }
         @Override
         void foo(String x) {
--- a/netbeans/indy-demo/src/VMILDemos.java	Fri Sep 24 11:41:53 2010 -0700
+++ b/netbeans/indy-demo/src/VMILDemos.java	Fri Sep 24 14:27:37 2010 -0700
@@ -1,5 +1,5 @@
 /*
- Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ Copyright 2009, 2010 Sun Microsystems, Inc.  All Rights Reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
@@ -77,6 +77,7 @@
 {;;;}
     }
     public static void figure_8() throws Throwable {
+    /*!!!
 {;;;} class C {
 MethodHandle greet(final String greeting) {
   return new JavaMethodHandle("run") {
@@ -88,6 +89,7 @@
 {;;;} }; new C().
 greet("hello").<void>invokeExact("world");
 // prints "hello, world"
+    */
 //Figure 8. Anonymous class specifying a method handle.
 {;;;}
     }
@@ -111,7 +113,7 @@
 //*//else if (!(x instanceof Integer))
 //*//  /*S*/ y = site.cacheAndCall(x,1);
 //*//else
-//*//  /*F*/ y = Runtime.add((int)(Integer)x,1);
+//*//  /*F*/ y = Runtime.add((int)x,1);
 //*////Figure 10. Pseudocode for inline cache, at a dynamic call site.
 {;;;}
     }
@@ -127,14 +129,15 @@
     }
 {;;;} static
 abstract class AbstractSettableMethodHandle
-    extends JavaMethodHandle {
-  private final MethodHandle setter;
+    implements MethodHandleBox {
+  private final MethodHandle getter, setter;
+  public MethodHandle getter()
+    { return getter; }
   public MethodHandle setter()
     { return setter; }
   public AbstractSettableMethodHandle(
       String get, String set) {
-    super(get);  // self is the getter
-    MethodType getType = this.type();
+    MethodType getType = findGetterType(this.getClass(), get);
     MethodType setType; {{
             int nargs = getType.parameterCount();
             Class ret = getType.returnType();
@@ -142,10 +145,16 @@
                 .insertParameterTypes(nargs, ret)
                 .changeReturnType(void.class);
         }}
+    this.getter = MethodHandles.publicLookup()
+       .bind(this, get, getType);
     this.setter = MethodHandles.publicLookup()
        .bind(this, set, setType);
   }
-  {;;;} public MethodHandle getter() { return this; }
+  {;;;} public MethodHandle asMethodHandle() { return getter(); }
+    static MethodType findGetterType(Class<?> cls, String get) {
+      // Should search the methods of cls for get...
+      return MethodType.methodType(int.class);
+    }
 }
 {;;;}
     static public class CatBox extends AbstractSettableMethodHandle {
@@ -158,9 +167,9 @@
         }
         static void test() throws Throwable {
             CatBox box = new CatBox(123);
-            System.out.println("thing1 = "+box.getter().<int>invoke());
-            box.setter().<void>invoke(456);
-            System.out.println("thing2 = "+box.getter().<int>invoke());
+            System.out.println("thing1 = "+(int)box.getter().<int>invokeExact());
+            box.setter().<void>invokeExact(456);
+            System.out.println("thing2 = "+(int)box.getter().<int>invokeExact());
         }
     }