changeset 45:587e815d5aa9

indy: convert demos for recent changes (6979327, 6980096)
author jrose
date Fri, 24 Sep 2010 16:09:59 -0700
parents da6ec495ff07
children eec0548a4c84
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/PrintArgsDemo.java netbeans/indy-demo/src/ShouldNotWork.java netbeans/indy-demo/src/VMILDemos.java netbeans/indy-demo/src/recipes/FastAndSlow.java netbeans/indy-demo/src/recipes/PlainOldJava.java netbeans/indy-demo/src/recipes/Utensil.java
diffstat 10 files changed, 51 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/indy-demo/src/FidgetDemo.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/FidgetDemo.java	Fri Sep 24 16:09:59 2010 -0700
@@ -52,13 +52,13 @@
     private static String fuss(String x) { return x+" is feeling moody"; }
     private static String bore(String x) { return x+" needs a change of scenery"; }
     private static final String[] NAMES = { "itch", "fuss", "bore" };
-    private static MethodHandle which(int i) {
+    private static MethodHandle which(int i) throws NoAccessException {
         return lookup().findStatic(FidgetDemo.class, NAMES[i % NAMES.length],
                     methodType(String.class, String.class));
     }
 
     // It is easy to build hybrid closure-like method handles out of classes.
-    private static class Guard implements MethodHandleBox {
+    private static class Guard implements MethodHandleProvider {
         MethodHandle target;  // wrapped target
         CSite        site;    // included site
         // constructor folds a sneaky site reference into a direct target
@@ -75,6 +75,9 @@
         public MethodHandle asMethodHandle() {
             return INVOKE.bindTo(this);
         }
+        public MethodHandle asMethodHandle(MethodType type) {
+            return asMethodHandle().asType(type);
+        }
         private static final MethodHandle INVOKE;
         static {
             try {
@@ -90,7 +93,7 @@
     // Use a local call site subclass.  (These are optional but fun.)
     private static class CSite extends CallSite {
         int count;
-        public CSite(Class caller, String name, MethodType type) {
+        public CSite(Class caller, String name, MethodType type) throws NoAccessException {
             //?? super(String.class, "gloat", make(void.class));
             System.out.println("[link] new call site: "+this);
             setTarget(new Guard(this, which(0)).asMethodHandle());
@@ -104,7 +107,7 @@
 
     // Set up a class-local bootstrap method.
     //static { Linkage.registerBootstrapMethod("linkDynamic"); }
-    private static CallSite linkDynamic(Class caller, String name, MethodType type) {
+    private static CallSite linkDynamic(Class caller, String name, MethodType type) throws NoAccessException {
         assert((Object)name == "fidget" &&
                 type == methodType(String.class, String.class));
         return new CSite(caller, name, type);
--- a/netbeans/indy-demo/src/GetNameDemo.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/GetNameDemo.java	Fri Sep 24 16:09:59 2010 -0700
@@ -50,7 +50,7 @@
     }
 
     // Linkage logic for all invokedynamic call sites in this class.
-    private static MethodHandle linkDynamic(Class caller, String name, MethodType type) {
+    private static MethodHandle linkDynamic(Class caller, String name, MethodType type) throws NoAccessException {
         System.out.println("[link] linkDynamic "+caller+" "+name+type);
         MethodHandles.Lookup lookup = MethodHandles.lookup();
         MethodHandle target = null;  // result of call linkage
@@ -86,7 +86,7 @@
 
     // Set up a class-local bootstrap method.
     //static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
-    private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
+    private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) throws NoAccessException {
         MethodHandle target = linkDynamic(caller, name, type);
         System.out.println("[link] set site target to "+target);
         if (!target.type().equals(type)) {
--- a/netbeans/indy-demo/src/Hello.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/Hello.java	Fri Sep 24 16:09:59 2010 -0700
@@ -47,9 +47,14 @@
 
     static void greeter(String x) { System.out.println("Hello, "+x); }
     // intentionally pun between the method and its reified handle:
-    static MethodHandle greeter
+    static final MethodHandle greeter;
+    static { try { greeter
             = MethodHandles.lookup().findStatic(Hello.class, "greeter",
                            MethodType.methodType(void.class, String.class));
+        } catch (NoAccessException ex) {
+            throw new RuntimeException(ex);
+        }
+    }
 
     // Set up a class-local bootstrap method.
     //static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
--- a/netbeans/indy-demo/src/JavaDocExamples.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/JavaDocExamples.java	Fri Sep 24 16:09:59 2010 -0700
@@ -116,7 +116,7 @@
     }
 
     static
-    class Greeter implements MethodHandleBox {
+    class Greeter implements MethodHandleProvider {
         public void run() { System.out.println("hello, "+greetee); }
         private final String greetee;
         Greeter(String greetee) {
@@ -125,6 +125,9 @@
         public MethodHandle asMethodHandle() {
             return RUN.bindTo(this);
         }
+        public MethodHandle asMethodHandle(MethodType type) {
+            return asMethodHandle().asType(type);
+        }
         // the entry point function is computed once:
         private static final MethodHandle RUN;
         static {
@@ -158,7 +161,7 @@
 
     // Set up a class-local bootstrap method.
     private static MethodHandle bootstrapDynamic(Class caller, String name, MethodType type)
-        throws NoSuchMethodException
+        throws NoAccessException
     {
         if (name.contains("op:"))  name = "op";
         return lookup().findStatic(caller, name+"Handler", type);
--- a/netbeans/indy-demo/src/PrintArgsDemo.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/PrintArgsDemo.java	Fri Sep 24 16:09:59 2010 -0700
@@ -54,8 +54,10 @@
 static {
   MethodHandles.Lookup lookup = MethodHandles.lookup();
   Class thisClass = lookup.lookupClass();  // (who am I?)
-  printArgs = lookup.findStatic(thisClass,
-      "printArgs", MethodType.methodType(void.class, Object[].class));
+  try {
+      printArgs = lookup.findStatic(thisClass,
+          "printArgs", MethodType.methodType(void.class, Object[].class));
+  } catch (NoAccessException ex) { throw new RuntimeException(ex); }
 }
 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 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/ShouldNotWork.java	Fri Sep 24 16:09:59 2010 -0700
@@ -45,10 +45,15 @@
         }
     }
     static class Sub extends Super {
-        static final MethodHandle SPECIAL1 = MethodHandles.lookup().findSpecial(Super.class, "foo",
-                MethodType.methodType(void.class, String.class), Sub.class);
-        static final MethodHandle SPECIAL2 = MethodHandles.lookup().findSpecial(Sub.class, "<init>",
-                MethodType.methodType(void.class), Sub.class);
+        static final MethodHandle SPECIAL1, SPECIAL2;
+        static {
+            try {
+                SPECIAL1 = MethodHandles.lookup().findSpecial(Super.class, "foo",
+                    MethodType.methodType(void.class, String.class), Sub.class);
+                SPECIAL2 = MethodHandles.lookup().findSpecial(Sub.class, "<init>",
+                    MethodType.methodType(void.class), Sub.class);
+            } catch (NoAccessException ex) { throw new RuntimeException(ex); }
+        }
         Sub(String x) throws Throwable {
             super(x);
             System.out.println("invoking "+SPECIAL1);
--- a/netbeans/indy-demo/src/VMILDemos.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/VMILDemos.java	Fri Sep 24 16:09:59 2010 -0700
@@ -129,14 +129,14 @@
     }
 {;;;} static
 abstract class AbstractSettableMethodHandle
-    implements MethodHandleBox {
+    implements MethodHandleProvider {
   private final MethodHandle getter, setter;
   public MethodHandle getter()
     { return getter; }
   public MethodHandle setter()
     { return setter; }
   public AbstractSettableMethodHandle(
-      String get, String set) {
+      String get, String set) throws NoAccessException {
     MethodType getType = findGetterType(this.getClass(), get);
     MethodType setType; {{
             int nargs = getType.parameterCount();
@@ -151,6 +151,7 @@
        .bind(this, set, setType);
   }
   {;;;} public MethodHandle asMethodHandle() { return getter(); }
+        public MethodHandle asMethodHandle(MethodType type) { return asMethodHandle().asType(type); }
     static MethodType findGetterType(Class<?> cls, String get) {
       // Should search the methods of cls for get...
       return MethodType.methodType(int.class);
@@ -161,7 +162,7 @@
         int thing1;
         public int getThing1() { return thing1; }
         public void setThing1(int x) { thing1 = x; }
-        CatBox(int x) {
+        CatBox(int x) throws NoAccessException {
             super("getThing1", "setThing1");
             thing1 = x;
         }
--- a/netbeans/indy-demo/src/recipes/FastAndSlow.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/recipes/FastAndSlow.java	Fri Sep 24 16:09:59 2010 -0700
@@ -66,16 +66,19 @@
     }
 
     // ---- MH constants (could be loaded from constant pool, also)
-    static private final MethodHandle
+    static private final MethodHandle fastAdd, slowAdd, bothInts;
+    static { try {
         fastAdd =
             LOOKUP.findStatic(FastAndSlow.class, "fastAdd",
-                methodType(Object.class, int.class, int.class)),
+                methodType(Object.class, int.class, int.class));
         slowAdd =
             LOOKUP.findStatic(FastAndSlow.class, "slowAdd",
-                methodType(Object.class, Object.class, Object.class)),
+                methodType(Object.class, Object.class, Object.class));
         bothInts =
             LOOKUP.findStatic(FastAndSlow.class, "bothInts",
                 methodType(boolean.class, Object.class, Object.class));
+        } catch (NoAccessException ex) { throw new RuntimeException(ex); }
+    }
 
     static MethodHandle makeCheckedFastAdd() {
         MethodHandle fastPath = fastAdd.asType(slowAdd.type());
--- a/netbeans/indy-demo/src/recipes/PlainOldJava.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/recipes/PlainOldJava.java	Fri Sep 24 16:09:59 2010 -0700
@@ -70,7 +70,7 @@
     }
 
     // Linkage logic for all invokedynamic call sites in this class.
-    private static MethodHandle linkDynamic(Class caller, String name, MethodType type) {
+    private static MethodHandle linkDynamic(Class caller, String name, MethodType type) throws NoAccessException {
         System.out.println("[link] linkDynamic "+caller+" "+name+type);
         MethodHandles.Lookup lookup = MethodHandles.lookup();
         MethodHandle target = null;  // result of call linkage
@@ -106,7 +106,7 @@
 
     // Set up a class-local bootstrap method.
     static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
-    private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
+    private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) throws NoAccessException {
         MethodHandle target = linkDynamic(caller, name, type);
         println("[link] set site target to "+target);
         if (!target.type().equals(type)) {
--- a/netbeans/indy-demo/src/recipes/Utensil.java	Fri Sep 24 14:57:45 2010 -0700
+++ b/netbeans/indy-demo/src/recipes/Utensil.java	Fri Sep 24 16:09:59 2010 -0700
@@ -49,7 +49,12 @@
 
     /** Return the identity function from Object to Object. */
     public static MethodHandle identity() {
-        return LOOKUP.findStatic(Utensil.class, "identity", MethodType.genericMethodType(1));
+        try {
+            return LOOKUP.findStatic(Utensil.class, "identity", MethodType.genericMethodType(1));
+        } catch (NoAccessException ex) {
+            // kitchen accident; call 911
+            throw new RuntimeException(ex);
+        }
     }
 
     private static Object identity(Object x) { return x; }