changeset 36:1c7351d7192f

meth, indy: update 292 API; support ldc of MH
author jrose
date Wed, 24 Mar 2010 18:57:41 -0700
parents 1ded1692e7e0
children 6801a67b2bfd
files netbeans/indy-demo/build.xml netbeans/indy-demo/nbproject/project.properties netbeans/indy-demo/src/FidgetDemo.java netbeans/indy-demo/src/GetNameDemo.java netbeans/indy-demo/src/Hello.java netbeans/indy-demo/src/Main.java netbeans/indy-demo/src/ShouldNotWork.java netbeans/indy-demo/src/recipes/Curry.java netbeans/indy-demo/src/recipes/FastAndSlow.java netbeans/indy-demo/src/recipes/PlainOldJava.java netbeans/indy-demo/src/recipes/Utensil.java netbeans/meth/build.xml netbeans/meth/nbproject/build-impl.xml netbeans/meth/nbproject/genfiles.properties netbeans/meth/nbproject/project.properties netbeans/meth/nbproject/project.xml netbeans/meth/old-test/jdk/java/dyn/MethodHandlesTest.java netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java
diffstat 18 files changed, 1963 insertions(+), 1938 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/indy-demo/build.xml	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/build.xml	Wed Mar 24 18:57:41 2010 -0700
@@ -2,6 +2,29 @@
 <project name="indy-demo" default="default" basedir=".">
     <description>Builds, tests, and runs the project indy-demo.</description>
     <import file="nbproject/build-impl.xml"/>
+
+    <target name="-pre-compile">
+        <echo>Relevant attributes and properties to javac:
+    srcdir:         ${src.dir}
+    destdir:        ${build.classes.dir}
+    compilerarg:    ${javac.compilerargs}
+    compilerarg:    ${endorsed.classpath.cmd.line.arg}
+    classpath:      ${javac.classpath}
+    includes:       ${includes}
+    excludes:       ${excludes}
+    debug=          ${javac.debug}
+    sourcepath:     ${empty.dir}
+    gensrcdir:      ${empty.dir}
+    deprecation=    ${javac.deprecation}
+    encoding=       ${source.encoding}
+    executable=     ${platform.javac}
+    source=         ${javac.source}
+    target=         ${javac.target}
+    tempdir=        ${java.io.tmpdir}
+    other attrs:    fork=yes includeantruntime=false
+        </echo>
+    </target>
+
     <target name="-post-jar">
         <delete file="dist/README.TXT"/>
 	<copy file="README.txt" todir="dist"/>
--- a/netbeans/indy-demo/nbproject/project.properties	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/nbproject/project.properties	Wed Mar 24 18:57:41 2010 -0700
@@ -26,8 +26,9 @@
 javac.classpath=\
     ${reference.meth.jar}
 # Space-separated list of extra javac options
-javac.compilerargs=-XDinvokedynamic \
-    -Xbootclasspath/p:"${reference.meth.jar}"
+javac.compilerargs=-XDinvokedynamic -Xlint:deprecation
+#    -J-ea -J-esa -J-Xbootclasspath/p:${reference.langtools}/build/bootstrap/classes
+#    -Xbootclasspath/p:"${reference.meth.jar}"
 javac.deprecation=false
 javac.source=1.5
 javac.target=1.5
@@ -56,6 +57,7 @@
 file.reference.projects=${project.davinci}/patches/netbeans
 project.meth=${file.reference.projects}/meth
 reference.meth.jar=${project.meth}/dist/meth.jar
+reference.langtools=${project.davinci}/sources/langtools
 run.classpath=\
     ${javac.classpath}:\
     ${build.classes.dir}
--- a/netbeans/indy-demo/src/FidgetDemo.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/FidgetDemo.java	Wed Mar 24 18:57:41 2010 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2009-2010 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
  */
 
 import java.dyn.*;
+import static java.dyn.MethodType.*;
 import static java.dyn.MethodHandles.*;
 
 class FidgetDemo {
@@ -69,7 +70,7 @@
         String invoke(String x) throws Throwable {
             if ((++site.count & 3) == 0)
                 site.setTarget(new Guard(site, which(site.count)));
-            return target.<String>invoke(x);
+            return target.<String>invokeExact(x);
         }
         private static final MethodHandle INVOKE =
             lookup().findVirtual(Guard.class, "invoke",
@@ -82,7 +83,6 @@
     private static class CSite extends CallSite {
         int count;
         public CSite(Class caller, String name, MethodType type) {
-            super(caller, name, type);
             //?? super(String.class, "gloat", make(void.class));
             System.out.println("[link] new call site: "+this);
             setTarget(new Guard(this, which(0)));
--- a/netbeans/indy-demo/src/GetNameDemo.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/GetNameDemo.java	Wed Mar 24 18:57:41 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
@@ -49,11 +49,9 @@
     }
 
     // Linkage logic for all invokedynamic call sites in this class.
-    private static MethodHandle linkDynamic(CallSite site) {
-        System.out.println("[link] linkDynamic "+site);
+    private static MethodHandle linkDynamic(Class caller, String name, MethodType type) {
+        System.out.println("[link] linkDynamic "+caller+" "+name+type);
         MethodHandles.Lookup lookup = MethodHandles.lookup();
-        String       name = site.name();
-        MethodType   type = site.type();  // static type of call
         MethodHandle target = null;  // result of call linkage
         Class<?>     recvType = null;  // receiver (1st arg) type if any
         if (type.parameterCount() >= 1) {
@@ -61,12 +59,12 @@
             if (recvType.isPrimitive())  recvType = null;
         }
         if (target == null && recvType != null) {
-            MethodType dropRecvType = type.dropParameterType(0);
+            MethodType dropRecvType = type.dropParameterTypes(0, 1);
             target = lookup.findVirtual(recvType, name, dropRecvType);
         }
         if (target == null && recvType != null && name.startsWith("special:")) {
-            MethodType dropRecvType = type.dropParameterType(0);
-            target = lookup.findSpecial(recvType, name, dropRecvType, site.callerClass());
+            MethodType dropRecvType = type.dropParameterTypes(0, 1);
+            target = lookup.findSpecial(recvType, name, dropRecvType, caller);
         }
         if (target == null && name.startsWith("static:\\=")) {
             int cbeg = name.indexOf('=')+1;
@@ -80,7 +78,7 @@
             }
         }
         if (target == null) {
-            throw new InvokeDynamicBootstrapError("linkage failed: "+site);
+            throw new InvokeDynamicBootstrapError("linkage failed for "+name);
         }
         return target;
     }
@@ -88,16 +86,14 @@
     // Set up a class-local bootstrap method.
     static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
     private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
-        CallSite site = new CallSite(caller, name, type);
-        MethodHandle target = linkDynamic(site);
+        MethodHandle target = linkDynamic(caller, name, type);
         System.out.println("[link] set site target to "+target);
-        if (target.type() != site.type()) {
-            target = MethodHandles.convertArguments(target, site.type());
+        if (!target.type().equals(type)) {
+            target = MethodHandles.convertArguments(target, type);
             System.out.println("[link]   with conversions "+target);
         }
-        site.setTarget(target);
-        System.out.println("[link] site is linked; this is the last time for "+site.name());
-        return site;
+        System.out.println("[link] site is linked; this is the last time for "+name);
+        return new CallSite(target);
     }
 }
 
--- a/netbeans/indy-demo/src/Hello.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/Hello.java	Wed Mar 24 18:57:41 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
@@ -36,11 +36,11 @@
         if (av.length == 0)  av = new String[] { "world" };
         greeter(av[0] + " (from a statically linked call site)");
         for (String whom : av) {
-            greeter.<void>invoke(whom);  // strongly typed direct call
-            // previous line generates invokevirtual MethodHandle.invoke(String)void
+            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
-            // previous line generates invokedynamic MethodHandle.invoke(Dynamic)Dynamic
+            // previous line generates invokedynamic MethodHandle.invokeExact(Object)Object
         }
     }
 
@@ -48,16 +48,13 @@
     // intentionally pun between the method and its reified handle:
     static MethodHandle greeter
             = MethodHandles.lookup().findStatic(Hello.class, "greeter",
-                           MethodType.make(void.class, String.class));
+                           MethodType.methodType(void.class, String.class));
 
     // Set up a class-local bootstrap method.
     static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
     private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
         assert(type.parameterCount() == 1 && (Object)name == "hail");  // in lieu of MOP
         System.out.println("set target to adapt "+greeter);
-        MethodHandle target = MethodHandles.convertArguments(greeter, type);
-        CallSite site = new CallSite(caller, name, type);
-        site.setTarget(target);
-        return site;
+        return new CallSite(greeter.asType(type));
     }
 }
--- a/netbeans/indy-demo/src/Main.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/Main.java	Wed Mar 24 18:57:41 2010 -0700
@@ -32,10 +32,12 @@
 import java.dyn.*;
 import java.util.*;
 import static java.dyn.MethodHandles.*;
+import static java.dyn.MethodType.*;
 
 public class Main {
     static final Class[] CLASSES = {
         Hello.class,
+        PrintArgsDemo.class,
         JavaDocExamples.class,
         GetNameDemo.class,
         FidgetDemo.class,
--- a/netbeans/indy-demo/src/ShouldNotWork.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/ShouldNotWork.java	Wed Mar 24 18:57:41 2010 -0700
@@ -46,9 +46,9 @@
     }
     static class Sub extends Super {
         static final MethodHandle SPECIAL1 = MethodHandles.lookup().findSpecial(Super.class, "foo",
-                MethodType.make(void.class, String.class), Sub.class);
+                MethodType.methodType(void.class, String.class), Sub.class);
         static final MethodHandle SPECIAL2 = MethodHandles.lookup().findSpecial(Sub.class, "<init>",
-                MethodType.make(void.class), Sub.class);
+                MethodType.methodType(void.class), Sub.class);
         Sub(String x) throws Throwable {
             super(x);
             System.out.println("invoking "+SPECIAL1);
--- a/netbeans/indy-demo/src/recipes/Curry.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/recipes/Curry.java	Wed Mar 24 18:57:41 2010 -0700
@@ -33,6 +33,7 @@
 
 import java.dyn.*;
 import static java.dyn.MethodHandles.*;
+import static java.dyn.MethodType.*;
 import static recipes.Utensil.*;
 
 public class Curry {
@@ -41,13 +42,13 @@
 
         MethodHandle list2 = Utensil.list(2);
         println(list2);  // list2 = {(x,y) => Arrays.asList(x,y)}
-        println(invokeVarargs(list2, "chicken", "rice"));  // [chicken, rice]
+        println(list2.invokeVarargs("chicken", "rice"));  // [chicken, rice]
 
         // curry with chicken or rice:
         MethodHandle partialApp = insertArguments(list2, 0, "curry");
         println(partialApp); // partialApp = {x => list2("curry", x)}
-        println(invokeVarargs(partialApp, "chicken"));  // [curry, chicken]
-        println(invokeVarargs(partialApp, "rice"));     // [curry, rice]
+        println(partialApp.invokeVarargs("chicken"));  // [curry, chicken]
+        println(partialApp.invokeVarargs("rice"));     // [curry, rice]
 
         // ----
 
@@ -57,7 +58,7 @@
         MethodHandle partialApp2 = insertArguments(list3, 0, "curry");
         // partialApp2 = {(x, y) => list3("curry", x, y)}
         println(partialApp2);
-        println(invokeVarargs(partialApp2, "chicken", "rice"));
+        println(partialApp2.invokeVarargs("chicken", "rice"));
                                             // [curry, chicken, rice]
 
         // ----
@@ -66,13 +67,13 @@
         MethodHandle pa3 = insertArguments(list3, 0, "curry", "chutney");
         // pa3 = {x => list3("curry", "chutney", x)}
         println(pa3);
-        println(invokeVarargs(pa3, "tofu")); //[curry, chutney, tofu]
+        println(pa3.invokeVarargs("tofu")); //[curry, chutney, tofu]
 
         // triple curry:
         MethodHandle pa4 = insertArguments(pa3, 0, "yak");
         // pa4 = { => list3("curry", "chutney", "yak")}
         println(pa4);
-        println(invokeVarargs(pa4));  // [curry, chutney, yak]
+        println(pa4.invokeVarargs());  // [curry, chutney, yak]
     }
 
     static void stronglyTyped() throws Throwable {
@@ -80,14 +81,14 @@
         println(list2);  // list2 = {(x,y) => Arrays.asList(x,y)}
         MethodType s2t = methodType(Object.class, String.class, String.class);
         //list2 = convertArguments(list2, s2t);
-        println(list2.invoke("chicken", "rice"));
+        println(list2.invokeExact("chicken", "rice"));
 
         // curry with chicken or rice:
         MethodHandle partialApp
                 = MethodHandles.insertArguments(list2, 0, "curry");
         println(partialApp); // partialApp = {x => list2("curry", x)}
-        println(partialApp.invoke("chicken"));  // [curry, chicken]
-        println(partialApp.invoke("rice"));     // [curry, rice]
+        println(partialApp.invokeExact("chicken"));  // [curry, chicken]
+        println(partialApp.invokeExact("rice"));     // [curry, rice]
 
         // curry with everything:
         MethodHandle list3 = Utensil.list(3);
@@ -98,20 +99,20 @@
                 = MethodHandles.insertArguments(list3, 0, "curry");
         // partialApp2 = {(x, y) => list3("curry", x, y)}
         println(partialApp2);
-        println(partialApp2.invoke("chicken", "rice"));  // [curry, chicken, rice]
+        println(partialApp2.invokeExact("chicken", "rice"));  // [curry, chicken, rice]
 
         // double curry:
         MethodHandle partialApp3
                 = MethodHandles.insertArguments(list3, 0, "curry", "chutney");
         // partialApp3 = {x => list3("curry", "chutney", x)}
         println(partialApp3);
-        println(partialApp3.invoke("tofu"));  // [curry, chutney, tofu]
+        println(partialApp3.invokeExact("tofu"));  // [curry, chutney, tofu]
 
         // triple curry:
         MethodHandle partialApp4
                 = MethodHandles.insertArguments(partialApp3, 0, "yak");
         // partialApp4 = { => list3("curry", "chutney", "yak")}
         println(partialApp4);
-        println(partialApp4.invoke());  // [curry, chutney, yak]
+        println(partialApp4.invokeExact());  // [curry, chutney, yak]
     }
 }
--- a/netbeans/indy-demo/src/recipes/FastAndSlow.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/recipes/FastAndSlow.java	Wed Mar 24 18:57:41 2010 -0700
@@ -33,6 +33,7 @@
 
 import java.dyn.*;
 import static java.dyn.MethodHandles.*;
+import static java.dyn.MethodType.*;
 import static recipes.Utensil.*;
 
 public class FastAndSlow {
@@ -76,9 +77,9 @@
                 methodType(boolean.class, Object.class, Object.class));
         fastAdd = convertArguments(fastAdd, slowAdd.type());
         MethodHandle combo = guardWithTest(bothInts, fastAdd, slowAdd);
-        println(invokeVarargs(combo, 2, 3));
-        println(invokeVarargs(combo, 2.1, 3.1));
-        println(invokeVarargs(combo, Integer.MAX_VALUE, -1));
-        println(invokeVarargs(combo, Integer.MAX_VALUE, 1));
+        println(combo.invokeVarargs(2, 3));
+        println(combo.invokeVarargs(2.1, 3.1));
+        println(combo.invokeVarargs(Integer.MAX_VALUE, -1));
+        println(combo.invokeVarargs(Integer.MAX_VALUE, 1));
     }
 }
--- a/netbeans/indy-demo/src/recipes/PlainOldJava.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/recipes/PlainOldJava.java	Wed Mar 24 18:57:41 2010 -0700
@@ -49,15 +49,15 @@
         println(file.getName());
         MethodHandle getName =
                 LOOKUP.findVirtual(file.getClass(), "getName",
-                            MethodType.make(String.class));
-        println(getName.<String>invoke(file));
+                            MethodType.methodType(String.class));
+        println(getName.<String>invokeExact(file));
 
         // ---
 
         MethodHandle charAt =
                 LOOKUP.findVirtual(String.class, "charAt",
-                            MethodType.make(char.class, int.class));
-        println(charAt.<char>invoke("foam", 3));
+                            MethodType.methodType(char.class, int.class));
+        println(charAt.<char>invokeExact("foam", 3));
 
         // ---
         
@@ -70,11 +70,9 @@
     }
 
     // Linkage logic for all invokedynamic call sites in this class.
-    private static MethodHandle linkDynamic(CallSite site) {
-        println("[link] linkDynamic "+site);
+    private static MethodHandle linkDynamic(Class caller, String name, MethodType type) {
+        System.out.println("[link] linkDynamic "+caller+" "+name+type);
         MethodHandles.Lookup lookup = MethodHandles.lookup();
-        String       name = site.name();
-        MethodType   type = site.type();  // static type of call
         MethodHandle target = null;  // result of call linkage
         Class<?>     recvType = null;  // receiver (1st arg) type if any
         if (type.parameterCount() >= 1) {
@@ -82,12 +80,12 @@
             if (recvType.isPrimitive())  recvType = null;
         }
         if (target == null && recvType != null) {
-            MethodType dropRecvType = type.dropParameterType(0);
+            MethodType dropRecvType = type.dropParameterTypes(0, 1);
             target = lookup.findVirtual(recvType, name, dropRecvType);
         }
         if (target == null && recvType != null && name.startsWith("special:")) {
-            MethodType dropRecvType = type.dropParameterType(0);
-            target = lookup.findSpecial(recvType, name, dropRecvType, site.callerClass());
+            MethodType dropRecvType = type.dropParameterTypes(0, 1);
+            target = lookup.findSpecial(recvType, name, dropRecvType, caller);
         }
         if (target == null && name.startsWith("static:\\=")) {
             int cbeg = name.indexOf('=')+1;
@@ -101,7 +99,7 @@
             }
         }
         if (target == null) {
-            throw new InvokeDynamicBootstrapError("linkage failed: "+site);
+            throw new InvokeDynamicBootstrapError("linkage failed for "+name);
         }
         return target;
     }
@@ -109,15 +107,13 @@
     // Set up a class-local bootstrap method.
     static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
     private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
-        CallSite site = new CallSite(caller, name, type);
-        MethodHandle target = linkDynamic(site);
+        MethodHandle target = linkDynamic(caller, name, type);
         println("[link] set site target to "+target);
-        if (target.type() != site.type()) {
-            target = MethodHandles.convertArguments(target, site.type());
+        if (!target.type().equals(type)) {
+            target = MethodHandles.convertArguments(target, type);
             println("[link]   with conversions "+target);
         }
-        site.setTarget(target);
-        println("[link] site is linked; this is the last time for "+site.name());
-        return site;
+        println("[link] site is linked; this is the last time for "+name);
+        return new CallSite(target);
     }
 }
--- a/netbeans/indy-demo/src/recipes/Utensil.java	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/indy-demo/src/recipes/Utensil.java	Wed Mar 24 18:57:41 2010 -0700
@@ -49,7 +49,7 @@
 
     /** Return the identity function from Object to Object. */
     public static MethodHandle identity() {
-        return LOOKUP.findStatic(Utensil.class, "identity", MethodType.makeGeneric(1));
+        return LOOKUP.findStatic(Utensil.class, "identity", MethodType.genericMethodType(1));
     }
 
     private static Object identity(Object x) { return x; }
@@ -74,7 +74,7 @@
      */
     public static MethodHandle array(int nargs) {
         try {
-            MethodType type = MethodType.makeGeneric(nargs).changeReturnType(Object[].class);
+            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
             return LOOKUP.findStatic(Utensil.class, "array", type);
         } catch (NoAccessException ex) {
             // kitchen accident; call 911
@@ -102,7 +102,7 @@
      */
     public static MethodHandle list(int nargs) {
         try {
-            MethodType type = MethodType.makeGeneric(nargs).changeReturnType(List.class);
+            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
             return LOOKUP.findStatic(Utensil.class, "list", type);
         } catch (NoAccessException ex) {
             // kitchen accident; call 911
--- a/netbeans/meth/build.xml	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/meth/build.xml	Wed Mar 24 18:57:41 2010 -0700
@@ -8,7 +8,7 @@
 
     <property name="javadoc.includes" value="java/dyn/*.java"/>
 
-    <!--- build a bundle of the tests -->
+    <!-- build a bundle of the tests -->
     <target name="-post-compile-test">
         <echo message="make build/test-classes.jar from ${build.test.classes.dir}"/>
         <jar compress="${jar.compress}" jarfile="${build.dir}/test-classes.jar">
--- a/netbeans/meth/nbproject/build-impl.xml	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/meth/nbproject/build-impl.xml	Wed Mar 24 18:57:41 2010 -0700
@@ -141,6 +141,7 @@
         <condition property="have.tests">
             <or>
                 <available file="${test.src.dir}"/>
+                <available file="${test.tests-from-jdk.dir}"/>
             </or>
         </condition>
         <condition property="have.sources">
@@ -200,6 +201,7 @@
     <target depends="-pre-init,-init-private,-init-user,-init-project,-do-init" name="-init-check">
         <fail unless="src.src.dir">Must set src.src.dir</fail>
         <fail unless="test.src.dir">Must set test.src.dir</fail>
+        <fail unless="test.tests-from-jdk.dir">Must set test.tests-from-jdk.dir</fail>
         <fail unless="build.dir">Must set build.dir</fail>
         <fail unless="dist.dir">Must set dist.dir</fail>
         <fail unless="build.classes.dir">Must set build.classes.dir</fail>
@@ -286,6 +288,9 @@
                         <fileset dir="${test.src.dir}" excludes="@{excludes},${excludes}" includes="@{includes}">
                             <filename name="@{testincludes}"/>
                         </fileset>
+                        <fileset dir="${test.tests-from-jdk.dir}" excludes="@{excludes},${excludes}" includes="@{includes}">
+                            <filename name="@{testincludes}"/>
+                        </fileset>
                     </batchtest>
                     <classpath>
                         <path path="${run.test.classpath}"/>
@@ -681,12 +686,13 @@
         <!-- You can override this target in the ../build.xml file. -->
     </target>
     <target if="do.depend.true" name="-compile-test-depend">
-        <j2seproject3:depend classpath="${javac.test.classpath}" destdir="${build.test.classes.dir}" srcdir="${test.src.dir}"/>
+        <j2seproject3:depend classpath="${javac.test.classpath}" destdir="${build.test.classes.dir}" srcdir="${test.src.dir}:${test.tests-from-jdk.dir}"/>
     </target>
     <target depends="init,compile,-pre-pre-compile-test,-pre-compile-test,-compile-test-depend" if="have.tests" name="-do-compile-test">
-        <j2seproject3:javac classpath="${javac.test.classpath}" debug="true" destdir="${build.test.classes.dir}" srcdir="${test.src.dir}"/>
+        <j2seproject3:javac classpath="${javac.test.classpath}" debug="true" destdir="${build.test.classes.dir}" srcdir="${test.src.dir}:${test.tests-from-jdk.dir}"/>
         <copy todir="${build.test.classes.dir}">
             <fileset dir="${test.src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
+            <fileset dir="${test.tests-from-jdk.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
         </copy>
     </target>
     <target name="-post-compile-test">
@@ -701,9 +707,10 @@
     <target depends="init,compile,-pre-pre-compile-test,-pre-compile-test-single" if="have.tests" name="-do-compile-test-single">
         <fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail>
         <j2seproject3:force-recompile destdir="${build.test.classes.dir}"/>
-        <j2seproject3:javac classpath="${javac.test.classpath}" debug="true" destdir="${build.test.classes.dir}" excludes="" includes="${javac.includes}" sourcepath="${test.src.dir}" srcdir="${test.src.dir}"/>
+        <j2seproject3:javac classpath="${javac.test.classpath}" debug="true" destdir="${build.test.classes.dir}" excludes="" includes="${javac.includes}" sourcepath="${test.src.dir}:${test.tests-from-jdk.dir}" srcdir="${test.src.dir}:${test.tests-from-jdk.dir}"/>
         <copy todir="${build.test.classes.dir}">
             <fileset dir="${test.src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
+            <fileset dir="${test.tests-from-jdk.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
         </copy>
     </target>
     <target name="-post-compile-test-single">
--- a/netbeans/meth/nbproject/genfiles.properties	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/meth/nbproject/genfiles.properties	Wed Mar 24 18:57:41 2010 -0700
@@ -1,5 +1,5 @@
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=1b4417fc
-nbproject/build-impl.xml.script.CRC32=cef958d5
+nbproject/build-impl.xml.data.CRC32=a09bc78f
+nbproject/build-impl.xml.script.CRC32=222725db
 nbproject/build-impl.xml.stylesheet.CRC32=576378a2@1.32.1.45
--- a/netbeans/meth/nbproject/project.properties	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/meth/nbproject/project.properties	Wed Mar 24 18:57:41 2010 -0700
@@ -30,17 +30,16 @@
 #file.reference.bsd-i586-classes=../davinci/sources/build/bsd-i586/classes
 file.reference.test-classes=build/test/classes
 file.reference.jdk-tests=${file.reference.davinci.sources.jdk}/test
-includes=java/dyn/**,jdk/java/dyn/**,sun/dyn/**
+includes=java/dyn/**,jdk/java/dyn/**,sun/dyn/**,test/java/dyn/**
 #includes=**
 jar.compress=true
 javac.classpath=
 #    ${file.reference.bsd-i586-classes}
 # Space-separated list of extra javac options
 # assuming full JDK_7X:
-#javac.compilerargs=-Xlint:unchecked -XDinvokedynamic -nowarn -Xlint:none
+javac.compilerargs=-Xlint:unchecked -XDinvokedynamic -nowarn -Xlint:none
 # without full JDK_7X:
-javac.compilerargs=-Xlint:unchecked -J-Xbootclasspath/p:${reference.langtools-javac.jar} -XDinvokedynamic \
- -Xbootclasspath/p:${build.classes.dir}
+#javac.compilerargs=-Xlint:unchecked -J-Xbootclasspath/p:${reference.langtools-javac.jar} -XDinvokedynamic -Xbootclasspath/p:${build.classes.dir}
 javac.deprecation=false
 javac.source=1.6
 javac.target=1.6
@@ -90,9 +89,8 @@
 # One or more refs probably need fixing:
 file.reference.projects=${user.home}/Projects
 file.reference.davinci.sources.jdk=${file.reference.projects}/davinci/sources/jdk
-reference.langtools-javac.jar=${platforms.JDK_7X.home}/lib/tools.jar
-#was: reference.langtools-javac.jar=${project.langtools}/dist/bootstrap/lib/javac.jar
-#was: reference.langtools-javac.jar=${project.langtools}/dist/bootstrap/lib/javac.jar
+#was: reference.langtools-javac.jar=${platforms.JDK_7X.home}/lib/tools.jar
+reference.langtools-javac.jar=${project.langtools}/dist/bootstrap/lib/javac.jar
 src.src.dir=${file.reference.davinci.sources.jdk}/src/share/classes
 test.src.dir=test
 project.license=openjdk
@@ -107,3 +105,4 @@
 #project.davinci=${user.home}/Projects/davinci/j2sdk-image
 libs.junit_4.classpath=${user.home}/env/jars/junit-4.5.jar
 platform.java=${platforms.JDK_7X.home}/bin/java
+test.tests-from-jdk.dir=${file.reference.davinci.sources.jdk}
--- a/netbeans/meth/nbproject/project.xml	Wed Jan 13 23:26:51 2010 -0800
+++ b/netbeans/meth/nbproject/project.xml	Wed Mar 24 18:57:41 2010 -0700
@@ -11,6 +11,7 @@
             </source-roots>
             <test-roots>
                 <root id="test.src.dir"/>
+                <root id="test.tests-from-jdk.dir" name="JDK Test Packages"/>
             </test-roots>
         </data>
     </configuration>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netbeans/meth/old-test/jdk/java/dyn/MethodHandlesTest.java	Wed Mar 24 18:57:41 2010 -0700
@@ -0,0 +1,1855 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @summary unit tests for java.dyn.MethodHandles
+ * @compile -XDinvokedynamic MethodHandlesTest.java
+ * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic jdk.java.dyn.MethodHandlesTest
+ */
+
+package jdk.java.dyn;
+
+import java.dyn.*;
+import java.dyn.MethodHandles.Lookup;
+import java.lang.reflect.*;
+import java.util.*;
+import org.junit.*;
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+
+/**
+ *
+ * @author jrose
+ */
+public class MethodHandlesTest {
+    // How much output?
+    static int verbosity = 1;
+
+    // Set this true during development if you want to fast-forward to
+    // a particular new, non-working test.  Tests which are known to
+    // work (or have recently worked) test this flag and return on true.
+    static boolean CAN_SKIP_WORKING = false;
+    //static { CAN_SKIP_WORKING = true; }
+
+    // Set true to test more calls.  If false, some tests are just
+    // lookups, without exercising the actual method handle.
+    static boolean DO_MORE_CALLS = false;
+
+
+    @Test
+    public void testFirst() throws Throwable {
+        verbosity += 9; try {
+            // left blank for debugging
+        } finally { verbosity -= 9; }
+    }
+
+    // current failures
+    @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
+    public void testFail_1() throws Throwable {
+        testSpreadArguments(int.class, 0, 6);
+    }
+    @Test @Ignore("failure in JVM when expanding the stack")
+    public void testFail_2() throws Throwable {
+        // if CONV_OP_IMPLEMENTED_MASK includes OP_SPREAD_ARGS, this crashes:
+        testSpreadArguments(Object.class, 0, 2);
+    }
+    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
+    public void testFail_3() throws Throwable {
+        testSpreadArguments(int.class, 1, 2);
+    }
+    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
+    public void testFail_4() throws Throwable {
+        testCollectArguments(int.class, 1, 2);
+    }
+    @Test @Ignore("cannot collect leading primitive types")
+    public void testFail_5() throws Throwable {
+        testInvokers(MethodType.genericMethodType(2).changeParameterType(0, int.class));
+    }
+    @Test @Ignore("should not insert arguments beyond MethodHandlePushLimit")
+    public void testFail_6() throws Throwable {
+        testInsertArguments(0, 0, MAX_ARG_INCREASE+1);
+    }
+    static final int MAX_ARG_INCREASE = 3;
+
+    public MethodHandlesTest() {
+    }
+
+    @Before
+    public void checkImplementedPlatform() {
+        boolean platformOK = false;
+        Properties properties = System.getProperties();
+        String vers = properties.getProperty("java.vm.version");
+        String name = properties.getProperty("java.vm.name");
+        String arch = properties.getProperty("os.arch");
+        if (arch.equals("i386") &&
+            (name.contains("Client") || name.contains("Server"))
+            ) {
+            platformOK = true;
+        } else {
+            System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch));
+        }
+        assumeTrue(platformOK);
+    }
+
+    String testName;
+    int posTests, negTests;
+    @After
+    public void printCounts() {
+        if (verbosity >= 1 && (posTests | negTests) != 0) {
+            System.out.println();
+            if (posTests != 0)  System.out.println("=== "+testName+": "+posTests+" positive test cases run");
+            if (negTests != 0)  System.out.println("=== "+testName+": "+negTests+" negative test cases run");
+        }
+    }
+    void countTest(boolean positive) {
+        if (positive) ++posTests;
+        else          ++negTests;
+    }
+    void countTest() { countTest(true); }
+    void startTest(String name) {
+        if (testName != null)  printCounts();
+        if (verbosity >= 0)
+            System.out.println(name);
+        posTests = negTests = 0;
+        testName = name;
+    }
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+        calledLog.clear();
+        calledLog.add(null);
+        nextArg = 1000000;
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    static List<Object> calledLog = new ArrayList<Object>();
+    static Object logEntry(String name, Object... args) {
+        return Arrays.asList(name, Arrays.asList(args));
+    }
+    static Object called(String name, Object... args) {
+        Object entry = logEntry(name, args);
+        calledLog.add(entry);
+        return entry;
+    }
+    static void assertCalled(String name, Object... args) {
+        Object expected = logEntry(name, args);
+        Object actual   = calledLog.get(calledLog.size() - 1);
+        if (expected.equals(actual))  return;
+        System.out.println("assertCalled "+name+":");
+        System.out.println("expected:   "+expected);
+        System.out.println("actual:     "+actual);
+        System.out.println("ex. types:  "+getClasses(expected));
+        System.out.println("act. types: "+getClasses(actual));
+        assertEquals("previous method call types", expected, actual);
+        assertEquals("previous method call", expected, actual);
+    }
+    static void printCalled(MethodHandle target, String name, Object... args) {
+        if (verbosity >= 2)
+            System.out.println("calling "+logEntry(name, args)+" on "+target);
+    }
+
+    static Object castToWrapper(Object value, Class<?> dst) {
+        Object wrap = null;
+        if (value instanceof Number)
+            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
+        if (value instanceof Character)
+            wrap = castToWrapperOrNull((char)(Character)value, dst);
+        if (wrap != null)  return wrap;
+        return dst.cast(value);
+    }
+
+    static Object castToWrapperOrNull(long value, Class<?> dst) {
+        if (dst == int.class || dst == Integer.class)
+            return (int)(value);
+        if (dst == long.class || dst == Long.class)
+            return (long)(value);
+        if (dst == char.class || dst == Character.class)
+            return (char)(value);
+        if (dst == short.class || dst == Short.class)
+            return (short)(value);
+        if (dst == float.class || dst == Float.class)
+            return (float)(value);
+        if (dst == double.class || dst == Double.class)
+            return (double)(value);
+        if (dst == byte.class || dst == Byte.class)
+            return (byte)(value);
+        if (dst == boolean.class || dst == boolean.class)
+            return ((value % 29) & 1) == 0;
+        return null;
+    }
+
+    static int nextArg;
+    static Object randomArg(Class<?> param) {
+        Object wrap = castToWrapperOrNull(nextArg, param);
+        if (wrap != null) {
+            nextArg++;
+            return wrap;
+        }
+//        import sun.dyn.util.Wrapper;
+//        Wrapper wrap = Wrapper.forBasicType(dst);
+//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
+//            wrap = Wrapper.forWrapperType(dst);
+//        if (wrap != Wrapper.OBJECT)
+//            return wrap.wrap(nextArg++);
+        if (param.isInterface() || param.isAssignableFrom(String.class))
+            return "#"+(nextArg++);
+        else
+            try {
+                return param.newInstance();
+            } catch (InstantiationException ex) {
+            } catch (IllegalAccessException ex) {
+            }
+        return null;  // random class not Object, String, Integer, etc.
+    }
+    static Object[] randomArgs(Class<?>... params) {
+        Object[] args = new Object[params.length];
+        for (int i = 0; i < args.length; i++)
+            args[i] = randomArg(params[i]);
+        return args;
+    }
+    static Object[] randomArgs(int nargs, Class<?> param) {
+        Object[] args = new Object[nargs];
+        for (int i = 0; i < args.length; i++)
+            args[i] = randomArg(param);
+        return args;
+    }
+
+    static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
+        return Arrays.copyOf(a, a.length, atype);
+    }
+    static <T> T[] cat(T[] a, T... b) {
+        int alen = a.length, blen = b.length;
+        if (blen == 0)  return a;
+        T[] c = Arrays.copyOf(a, alen + blen);
+        System.arraycopy(b, 0, c, alen, blen);
+        return c;
+    }
+    static Integer[] boxAll(int... vx) {
+        Integer[] res = new Integer[vx.length];
+        for (int i = 0; i < res.length; i++) {
+            res[i] = vx[i];
+        }
+        return res;
+    }
+    static Object getClasses(Object x) {
+        if (x == null)  return x;
+        if (x instanceof String)  return x;  // keep the name
+        if (x instanceof List) {
+            // recursively report classes of the list elements
+            Object[] xa = ((List)x).toArray();
+            for (int i = 0; i < xa.length; i++)
+                xa[i] = getClasses(xa[i]);
+            return Arrays.asList(xa);
+        }
+        return x.getClass().getSimpleName();
+    }
+
+    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
+        return changeArgTypes(target, 0, 999, argType);
+    }
+    static MethodHandle changeArgTypes(MethodHandle target,
+            int beg, int end, Class<?> argType) {
+        MethodType targetType = target.type();
+        end = Math.min(end, targetType.parameterCount());
+        ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
+        Collections.fill(argTypes.subList(beg, end), argType);
+        MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
+        return MethodHandles.convertArguments(target, ttype2);
+    }
+
+    // This lookup is good for all members in and under MethodHandlesTest.
+    static final Lookup PRIVATE = MethodHandles.lookup();
+    // This lookup is good for package-private members but not private ones.
+    static final Lookup PACKAGE = PackageSibling.lookup();
+    // This lookup is good only for public members.
+    static final Lookup PUBLIC  = MethodHandles.publicLookup();
+
+    // Subject methods...
+    static class Example implements IntExample {
+        final String name;
+        public Example() { name = "Example#"+(nextArg++); }
+        protected Example(String name) { this.name = name; }
+        protected Example(int x) { this(); called("protected <init>", this, x); }
+        @Override public String toString() { return name; }
+
+        public void            v0()     { called("v0", this); }
+        void                   pkg_v0() { called("pkg_v0", this); }
+        private void           pri_v0() { called("pri_v0", this); }
+        public static void     s0()     { called("s0"); }
+        static void            pkg_s0() { called("pkg_s0"); }
+        private static void    pri_s0() { called("pri_s0"); }
+
+        public Object          v1(Object x) { return called("v1", this, x); }
+        public Object          v2(Object x, Object y) { return called("v2", this, x, y); }
+        public Object          v2(Object x, int    y) { return called("v2", this, x, y); }
+        public Object          v2(int    x, Object y) { return called("v2", this, x, y); }
+        public Object          v2(int    x, int    y) { return called("v2", this, x, y); }
+        public static Object   s1(Object x) { return called("s1", x); }
+        public static Object   s2(int x)    { return called("s2", x); }
+        public static Object   s3(long x)   { return called("s3", x); }
+        public static Object   s4(int x, int y) { return called("s4", x, y); }
+        public static Object   s5(long x, int y) { return called("s5", x, y); }
+        public static Object   s6(int x, long y) { return called("s6", x, y); }
+        public static Object   s7(float x, double y) { return called("s7", x, y); }
+    }
+    public static class PubExample extends Example {
+    }
+    static class SubExample extends Example {
+        @Override public void  v0()     { called("Sub/v0", this); }
+        @Override void         pkg_v0() { called("Sub/pkg_v0", this); }
+        private      SubExample(int x)  { called("<init>", this, x); }
+        public SubExample() { super("SubExample#"+(nextArg++)); }
+    }
+    public static interface IntExample {
+        public void            v0();
+        static class Impl implements IntExample {
+            public void        v0()     { called("Int/v0", this); }
+            final String name;
+            public Impl() { name = "Example#"+(nextArg++); }
+        }
+    }
+
+    static final Object[][][] ACCESS_CASES = {
+        { { false, PUBLIC }, { false, PACKAGE }, { false, PRIVATE } },
+        { { false, PUBLIC }, { false, PACKAGE }, { true,  PRIVATE } },
+        { { false, PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE } },
+        { { true,  PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE } },
+    };
+
+    static Object[][] accessCases(Class<?> defc, String name) {
+        if (name.contains("pri_")) {
+            return ACCESS_CASES[1]; // PRIVATE only
+        } else if (name.contains("pkg_")) {
+            return ACCESS_CASES[2]; // not PUBLIC
+        } else {
+            assertTrue(name.indexOf('_') < 0);
+            boolean pubc = Modifier.isPublic(defc.getModifiers());
+            if (pubc)
+                return ACCESS_CASES[3]; // all access levels
+            return ACCESS_CASES[2]; // PACKAGE but not PUBLIC
+        }
+    }
+
+    @Test
+    public void testFindStatic() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("findStatic");
+        testFindStatic(PubExample.class, void.class, "s0");
+        testFindStatic(Example.class, void.class, "s0");
+        testFindStatic(Example.class, void.class, "pkg_s0");
+        testFindStatic(Example.class, void.class, "pri_s0");
+
+        testFindStatic(Example.class, Object.class, "s1", Object.class);
+        testFindStatic(Example.class, Object.class, "s2", int.class);
+        testFindStatic(Example.class, Object.class, "s3", long.class);
+        testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
+        testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
+        testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
+        testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
+
+        testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
+    }
+
+    void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        for (Object[] ac : accessCases(defc, name)) {
+            testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
+        }
+    }
+    void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        testFindStatic(true, lookup, defc, ret, name, params);
+    }
+    void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        countTest(positive);
+        MethodType type = MethodType.methodType(ret, params);
+        MethodHandle target = null;
+        RuntimeException noAccess = null;
+        try {
+            target = lookup.findStatic(defc, name, type);
+        } catch (NoAccessException ex) {
+            noAccess = ex;
+        }
+        if (verbosity >= 2)
+            System.out.println("findStatic "+lookup+": "+defc+"."+name+"/"+type+" => "+target
+                    +(noAccess == null ? "" : " !! "+noAccess));
+        if (positive && noAccess != null)  throw noAccess;
+        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
+        if (!positive)  return; // negative test failed as expected
+        assertEquals(type, target.type());
+        assertTrue(target.toString().contains(name));  // rough check
+        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
+        Object[] args = randomArgs(params);
+        printCalled(target, name, args);
+        target.invokeVarargs(args);
+        assertCalled(name, args);
+        System.out.print(':');
+    }
+
+    @Test
+    public void testFindVirtual() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("findVirtual");
+        testFindVirtual(Example.class, void.class, "v0");
+        testFindVirtual(Example.class, void.class, "pkg_v0");
+        testFindVirtual(Example.class, void.class, "pri_v0");
+        testFindVirtual(Example.class, Object.class, "v1", Object.class);
+        testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
+        testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
+        testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
+        testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
+        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
+        // test dispatch
+        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/v0");
+        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/v0");
+        testFindVirtual(SubExample.class,      IntExample.class, void.class, "Sub/v0");
+        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/pkg_v0");
+        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/pkg_v0");
+        testFindVirtual(Example.class,         IntExample.class, void.class, "v0");
+        testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
+    }
+
+    void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        Class<?> rcvc = defc;
+        testFindVirtual(rcvc, defc, ret, name, params);
+    }
+    void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        for (Object[] ac : accessCases(defc, name)) {
+            testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params);
+        }
+    }
+    void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
+    }
+    void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        countTest(positive);
+        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
+        MethodType type = MethodType.methodType(ret, params);
+        MethodHandle target = null;
+        RuntimeException noAccess = null;
+        try {
+            target = lookup.findVirtual(defc, methodName, type);
+        } catch (NoAccessException ex) {
+            noAccess = ex;
+        }
+        if (verbosity >= 2)
+            System.out.println("findVirtual "+lookup+": "+defc+"."+name+"/"+type+" => "+target
+                    +(noAccess == null ? "" : " !! "+noAccess));
+        if (positive && noAccess != null)  throw noAccess;
+        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
+        if (!positive)  return; // negative test failed as expected
+        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
+        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
+        MethodType ttype = target.type();
+        ttype = ttype.changeParameterType(0, defc); // FIXME: test this
+        assertEquals(typeWithSelf, ttype);
+        assertTrue(target.toString().contains(methodName));  // rough check
+        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
+        Object[] argsWithSelf = randomArgs(paramsWithSelf);
+        if (rcvc != defc)  argsWithSelf[0] = randomArg(rcvc);
+        printCalled(target, name, argsWithSelf);
+        target.invokeVarargs(argsWithSelf);
+        assertCalled(name, argsWithSelf);
+        System.out.print(':');
+    }
+
+    @Test
+    public void testFindSpecial() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("findSpecial");
+        testFindSpecial(Example.class, void.class, "v0");
+        testFindSpecial(Example.class, void.class, "pkg_v0");
+        testFindSpecial(false, PRIVATE, Example.class, void.class, "<init>", int.class);
+        testFindSpecial(false, PRIVATE, Example.class, void.class, "bogus");
+    }
+
+    void testFindSpecial(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        testFindSpecial(true,  PRIVATE, defc, ret, name, params);
+        testFindSpecial(false, PACKAGE, defc, ret, name, params);
+        testFindSpecial(false, PUBLIC,  defc, ret, name, params);
+    }
+    void testFindSpecial(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        countTest(positive);
+        MethodType type = MethodType.methodType(ret, params);
+        MethodHandle target = null;
+        RuntimeException noAccess = null;
+        try {
+            target = lookup.findSpecial(defc, name, type, defc);
+        } catch (NoAccessException ex) {
+            noAccess = ex;
+        }
+        if (verbosity >= 2)
+            System.out.println("findSpecial "+defc+"."+name+"/"+type+" => "+target
+                    +(noAccess == null ? "" : " !! "+noAccess));
+        if (positive && noAccess != null)  throw noAccess;
+        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
+        if (!positive)  return; // negative test failed as expected
+        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
+        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
+        MethodType ttype = target.type();
+        ttype = ttype.changeParameterType(0, defc); // FIXME: test this
+        assertEquals(typeWithSelf, ttype);
+        assertTrue(target.toString().contains(name));  // rough check
+        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
+        Object[] args = randomArgs(paramsWithSelf);
+        printCalled(target, name, args);
+        target.invokeVarargs(args);
+        assertCalled(name, args);
+        System.out.print(':');
+    }
+
+    @Test
+    public void testBind() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("bind");
+        testBind(Example.class, void.class, "v0");
+        testBind(Example.class, void.class, "pkg_v0");
+        testBind(Example.class, void.class, "pri_v0");
+        testBind(Example.class, Object.class, "v1", Object.class);
+        testBind(Example.class, Object.class, "v2", Object.class, Object.class);
+        testBind(Example.class, Object.class, "v2", Object.class, int.class);
+        testBind(Example.class, Object.class, "v2", int.class, Object.class);
+        testBind(Example.class, Object.class, "v2", int.class, int.class);
+        testBind(false, PRIVATE, Example.class, void.class, "bogus");
+        testBind(SubExample.class, void.class, "Sub/v0");
+        testBind(SubExample.class, void.class, "Sub/pkg_v0");
+        testBind(IntExample.Impl.class, void.class, "Int/v0");
+    }
+
+    void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        for (Object[] ac : accessCases(defc, name)) {
+            testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
+        }
+    }
+
+    void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        countTest(positive);
+        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
+        MethodType type = MethodType.methodType(ret, params);
+        Object receiver = randomArg(defc);
+        MethodHandle target = null;
+        RuntimeException noAccess = null;
+        try {
+            target = lookup.bind(receiver, methodName, type);
+        } catch (NoAccessException ex) {
+            noAccess = ex;
+        }
+        if (verbosity >= 2)
+            System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
+                    +(noAccess == null ? "" : " !! "+noAccess));
+        if (positive && noAccess != null)  throw noAccess;
+        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
+        if (!positive)  return; // negative test failed as expected
+        assertEquals(type, target.type());
+        Object[] args = randomArgs(params);
+        printCalled(target, name, args);
+        target.invokeVarargs(args);
+        Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
+        assertCalled(name, argsWithReceiver);
+        System.out.print(':');
+    }
+
+    @Test
+    public void testUnreflect() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("unreflect");
+        testUnreflect(Example.class, true, void.class, "s0");
+        testUnreflect(Example.class, true, void.class, "pkg_s0");
+        testUnreflect(Example.class, true, void.class, "pri_s0");
+
+        testUnreflect(Example.class, true, Object.class, "s1", Object.class);
+        testUnreflect(Example.class, true, Object.class, "s2", int.class);
+        //testUnreflect(Example.class, true, Object.class, "s3", long.class);
+        //testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class);
+        //testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class);
+        //testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class);
+
+        testUnreflect(Example.class, false, void.class, "v0");
+        testUnreflect(Example.class, false, void.class, "pkg_v0");
+        testUnreflect(Example.class, false, void.class, "pri_v0");
+        testUnreflect(Example.class, false, Object.class, "v1", Object.class);
+        testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
+        testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
+        testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
+        testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
+    }
+
+    void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        for (Object[] ac : accessCases(defc, name)) {
+            testUnreflect((Boolean)ac[0], (Lookup)ac[1], defc, isStatic, ret, name, params);
+        }
+    }
+    void testUnreflect(boolean positive, Lookup lookup, Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
+        countTest(positive);
+        MethodType type = MethodType.methodType(ret, params);
+        Method rmethod = null;
+        MethodHandle target = null;
+        RuntimeException noAccess = null;
+        try {
+            rmethod = defc.getDeclaredMethod(name, params);
+        } catch (NoSuchMethodException ex) {
+            throw new NoAccessException(ex);
+        }
+        assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
+        try {
+            target = lookup.unreflect(rmethod);
+        } catch (NoAccessException ex) {
+            noAccess = ex;
+        }
+        if (verbosity >= 2)
+            System.out.println("unreflect "+defc+"."+name+"/"+type+" => "+target
+                    +(noAccess == null ? "" : " !! "+noAccess));
+        if (positive && noAccess != null)  throw noAccess;
+        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
+        if (!positive)  return; // negative test failed as expected
+        Class<?>[] paramsMaybeWithSelf = params;
+        if (!isStatic) {
+            paramsMaybeWithSelf = cat(array(Class[].class, (Class)defc), params);
+        }
+        MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
+        MethodType ttype = target.type();
+        if (!isStatic)
+            ttype = ttype.changeParameterType(0, defc); // FIXME: test this
+        assertEquals(typeMaybeWithSelf, ttype);
+        Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
+        printCalled(target, name, argsMaybeWithSelf);
+        target.invokeVarargs(argsMaybeWithSelf);
+        assertCalled(name, argsMaybeWithSelf);
+        System.out.print(':');
+    }
+
+    @Test @Ignore("unimplemented")
+    public void testUnreflectSpecial() throws Throwable {
+        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
+        startTest("unreflectSpecial");
+        Method m = null;
+        MethodHandle expResult = null;
+        MethodHandle result = lookup.unreflectSpecial(m, Example.class);
+        assertEquals(expResult, result);
+        fail("The test case is a prototype.");
+    }
+
+    public static class HasFields {
+        boolean fZ = false;
+        byte fB = (byte)'B';
+        short fS = (short)'S';
+        char fC = 'C';
+        int fI = 'I';
+        long fJ = 'J';
+        float fF = 'F';
+        double fD = 'D';
+        static boolean sZ = true;
+        static byte sB = 1+(byte)'B';
+        static short sS = 1+(short)'S';
+        static char sC = 1+'C';
+        static int sI = 1+'I';
+        static long sJ = 1+'J';
+        static float sF = 1+'F';
+        static double sD = 1+'D';
+
+        Object fL = 'L';
+        String fR = "R";
+        static Object sL = 'M';
+        static String sR = "S";
+
+        static final Object[][] CASES;
+        static {
+            ArrayList<Object[]> cases = new ArrayList<Object[]>();
+            Object types[][] = {
+                {'L',Object.class}, {'R',String.class},
+                {'I',int.class}, {'J',long.class},
+                {'F',float.class}, {'D',double.class},
+                {'Z',boolean.class}, {'B',byte.class},
+                {'S',short.class}, {'C',char.class},
+            };
+            HasFields fields = new HasFields();
+            for (Object[] t : types) {
+                for (int kind = 0; kind <= 1; kind++) {
+                    boolean isStatic = (kind != 0);
+                    char btc = (Character)t[0];
+                    String name = (isStatic ? "s" : "f") + btc;
+                    Class<?> type = (Class<?>) t[1];
+                    Object value;
+                    Field field;
+                    try {
+                        field = HasFields.class.getDeclaredField(name);
+                    } catch (Exception ex) {
+                        throw new InternalError("no field HasFields."+name);
+                    }
+                    try {
+                        value = field.get(fields);
+                    } catch (Exception ex) {
+                        throw new InternalError("cannot fetch field HasFields."+name);
+                    }
+                    if (type == float.class) {
+                        float v = 'F';
+                        if (isStatic)  v++;
+                        assert(value.equals(v));
+                    }
+                    assert(name.equals(field.getName()));
+                    assert(type.equals(field.getType()));
+                    assert(isStatic == (Modifier.isStatic(field.getModifiers())));
+                    cases.add(new Object[]{ field, value });
+                }
+            }
+            CASES = cases.toArray(new Object[0][]);
+        }
+    }
+
+    @Test
+    public void testUnreflectGetter() throws Throwable {
+        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
+        startTest("unreflectGetter");
+        for (Object[] c : HasFields.CASES) {
+            Field f = (Field)c[0];
+            Object value = c[1];
+            Class<?> type = f.getType();
+            if (type.isPrimitive() && type != int.class)
+                continue;  //FIXME
+            testUnreflectGetter(lookup, f, type, value);
+        }
+    }
+    public void testUnreflectGetter(MethodHandles.Lookup lookup,
+            Field f, Class<?> type, Object value) throws Throwable {
+        countTest(true);
+        boolean isStatic = Modifier.isStatic(f.getModifiers());
+        MethodType expType = MethodType.methodType(type, HasFields.class);
+        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
+        MethodHandle mh = lookup.unreflectGetter(f);
+        assertSame(mh.type(), expType);
+        assertEquals(mh.toString(), f.getName());
+        HasFields fields = new HasFields();
+        Object sawValue;
+        Class<?> rtype = type;
+        if (type != int.class)  rtype = Object.class;
+        mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(rtype));
+        Object expValue = value;
+        for (int i = 0; i <= 1; i++) {
+            if (isStatic) {
+                if (type == int.class)
+                    sawValue = mh.<int>invoke();  // do these exactly
+                else
+                    sawValue = mh.invoke();
+            } else {
+                if (type == int.class)
+                    sawValue = mh.<int>invoke((Object) fields);
+                else
+                    sawValue = mh.invoke((Object) fields);
+            }
+            assertEquals(sawValue, expValue);
+            Object random = randomArg(type);
+            f.set(fields, random);
+            expValue = random;
+        }
+        f.set(fields, value);  // put it back
+    }
+
+
+    @Test
+    public void testUnreflectSetter() throws Throwable {
+        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
+        startTest("unreflectSetter");
+        for (Object[] c : HasFields.CASES) {
+            Field f = (Field)c[0];
+            Object value = c[1];
+            Class<?> type = f.getType();
+            if (type.isPrimitive() && type != int.class)
+                continue;  //FIXME
+            testUnreflectSetter(lookup, f, type, value);
+        }
+    }
+    public void testUnreflectSetter(MethodHandles.Lookup lookup,
+            Field f, Class<?> type, Object value) throws Throwable {
+        countTest(true);
+        boolean isStatic = Modifier.isStatic(f.getModifiers());
+        MethodType expType = MethodType.methodType(void.class, HasFields.class, type);
+        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
+        MethodHandle mh = lookup.unreflectSetter(f);
+        assertSame(mh.type(), expType);
+        assertEquals(mh.toString(), f.getName());
+        HasFields fields = new HasFields();
+        Object sawValue;
+        Class<?> vtype = type;
+        if (type != int.class)  vtype = Object.class;
+        int last = mh.type().parameterCount() - 1;
+        mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(void.class).changeParameterType(last, vtype));
+        assertEquals(f.get(fields), value);  // clean to start with
+        for (int i = 0; i <= 1; i++) {
+            Object putValue = randomArg(type);
+            if (isStatic) {
+                if (type == int.class)
+                    mh.<void>invoke((int)(Integer)putValue);  // do these exactly
+                else
+                    mh.<void>invoke(putValue);
+            } else {
+                if (type == int.class)
+                    mh.<void>invoke((Object) fields, (int)(Integer)putValue);
+                else
+                    mh.<void>invoke((Object) fields, putValue);
+            }
+            assertEquals(f.get(fields), putValue);
+        }
+        f.set(fields, value);  // put it back
+    }
+
+    @Test
+    public void testArrayElementGetter() throws Throwable {
+        startTest("arrayElementGetter");
+        testArrayElementGetterSetter(new Object[10], false);
+        testArrayElementGetterSetter(new String[10], false);
+        testArrayElementGetterSetter(new int[10], false);
+        // FIXME: Do the other primitive types.
+        //testArrayElementGetterSetter(new float[10], false);
+    }
+
+    @Test
+    public void testArrayElementSetter() throws Throwable {
+        startTest("arrayElementSetter");
+        testArrayElementGetterSetter(new Object[10], true);
+        testArrayElementGetterSetter(new String[10], true);
+        testArrayElementGetterSetter(new int[10], true);
+        // FIXME: Do the other primitive types.
+        //testArrayElementGetterSetter(new float[10], true);
+    }
+
+    public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable {
+        countTest(true);
+        Class<?> arrayType = array.getClass();
+        Class<?> elemType = arrayType.getComponentType();
+        MethodType expType = !testSetter
+                ? MethodType.methodType(elemType,   arrayType, int.class)
+                : MethodType.methodType(void.class, arrayType, int.class, elemType);
+        MethodHandle mh = !testSetter
+                ? MethodHandles.arrayElementGetter(arrayType)
+                : MethodHandles.arrayElementSetter(arrayType);
+        assertSame(mh.type(), expType);
+        //assertEquals(mh.toString(), f.getName());
+        Object sawValue, expValue;
+        List<Object> model = array2list(array);
+        int length = Array.getLength(array);
+        for (int i = 0; i < length; i++) {
+            // update array element
+            Object random = randomArg(elemType);
+            model.set(i, random);
+            if (testSetter) {
+                if (elemType == int.class)
+                    mh.<void>invoke((int[]) array, i, (int)(Integer)random);
+                else
+                    mh.invokeGeneric(array, i, random);
+                assertEquals(model, array2list(array));
+            } else {
+                Array.set(array, i, random);
+
+            }
+            // observe array element
+            sawValue = Array.get(array, i);
+            if (!testSetter) {
+                expValue = sawValue;
+                if (elemType == int.class)
+                    sawValue = mh.<int>invoke((int[]) array, i);
+                else
+                    sawValue = mh.invokeGeneric(array, i);
+                assertEquals(sawValue, expValue);
+                assertEquals(model, array2list(array));
+            }
+        }
+    }
+
+    List<Object> array2list(Object array) {
+        int length = Array.getLength(array);
+        ArrayList<Object> model = new ArrayList<Object>(length);
+        for (int i = 0; i < length; i++)
+            model.add(Array.get(array, i));
+        return model;
+    }
+
+    static class Callee {
+        static Object id() { return called("id"); }
+        static Object id(Object x) { return called("id", x); }
+        static Object id(Object x, Object y) { return called("id", x, y); }
+        static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
+        static Object id(Object... vx) { return called("id", vx); }
+        static MethodHandle ofType(int n) {
+            return ofType(Object.class, n);
+        }
+        static MethodHandle ofType(Class<?> rtype, int n) {
+            if (n == -1)
+                return ofType(MethodType.methodType(rtype, Object[].class));
+            return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
+        }
+        static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
+            return ofType(MethodType.methodType(rtype, ptypes));
+        }
+        static MethodHandle ofType(MethodType type) {
+            Class<?> rtype = type.returnType();
+            String pfx = "";
+            if (rtype != Object.class)
+                pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
+            String name = pfx+"id";
+            return PRIVATE.findStatic(Callee.class, name, type);
+        }
+    }
+
+    @Test
+    public void testConvertArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("convertArguments");
+        testConvert(Callee.ofType(1), null, "id", int.class);
+        testConvert(Callee.ofType(1), null, "id", String.class);
+        testConvert(Callee.ofType(1), null, "id", Integer.class);
+        testConvert(Callee.ofType(1), null, "id", short.class);
+    }
+
+    void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
+        testConvert(true, id, rtype, name, params);
+    }
+
+    void testConvert(boolean positive, MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
+        countTest(positive);
+        MethodType idType = id.type();
+        if (rtype == null)  rtype = idType.returnType();
+        for (int i = 0; i < params.length; i++) {
+            if (params[i] == null)  params[i] = idType.parameterType(i);
+        }
+        // simulate the pairwise conversion
+        MethodType newType = MethodType.methodType(rtype, params);
+        Object[] args = randomArgs(newType.parameterArray());
+        Object[] convArgs = args.clone();
+        for (int i = 0; i < args.length; i++) {
+            Class<?> src = newType.parameterType(i);
+            Class<?> dst = idType.parameterType(i);
+            if (src != dst)
+                convArgs[i] = castToWrapper(convArgs[i], dst);
+        }
+        Object convResult = id.invokeVarargs(convArgs);
+        {
+            Class<?> dst = newType.returnType();
+            Class<?> src = idType.returnType();
+            if (src != dst)
+                convResult = castToWrapper(convResult, dst);
+        }
+        MethodHandle target = null;
+        RuntimeException error = null;
+        try {
+            target = MethodHandles.convertArguments(id, newType);
+        } catch (RuntimeException ex) {
+            error = ex;
+        }
+        if (verbosity >= 2)
+            System.out.println("convert "+id+ " to "+newType+" => "+target
+                    +(error == null ? "" : " !! "+error));
+        if (positive && error != null)  throw error;
+        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
+        if (!positive)  return; // negative test failed as expected
+        assertEquals(newType, target.type());
+        printCalled(target, id.toString(), args);
+        Object result = target.invokeVarargs(args);
+        assertCalled(name, convArgs);
+        assertEquals(convResult, result);
+        System.out.print(':');
+    }
+
+    @Test
+    public void testPermuteArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("permuteArguments");
+        testPermuteArguments(4, Integer.class,  2, String.class,  0);
+        //testPermuteArguments(6, Integer.class,  0, null,         30);
+        //testPermuteArguments(4, Integer.class,  1, int.class,     6);
+    }
+    public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
+        if (verbosity >= 1)
+            System.out.println("permuteArguments "+max+"*"+type1.getName()
+                    +(t2c==0?"":"/"+t2c+"*"+type2.getName())
+                    +(dilution > 0 ? " with dilution "+dilution : ""));
+        int t2pos = t2c == 0 ? 0 : 1;
+        for (int inargs = t2pos+1; inargs <= max; inargs++) {
+            Class<?>[] types = new Class<?>[inargs];
+            Arrays.fill(types, type1);
+            if (t2c != 0) {
+                // Fill in a middle range with type2:
+                Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
+            }
+            Object[] args = randomArgs(types);
+            int numcases = 1;
+            for (int outargs = 0; outargs <= max; outargs++) {
+                if (outargs - inargs >= MAX_ARG_INCREASE)  continue;
+                int[] reorder = new int[outargs];
+                int casStep = dilution + 1;
+                // Avoid some common factors:
+                while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
+                       (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
+                    casStep++;
+                for (int cas = 0; cas < numcases; cas += casStep) {
+                    for (int i = 0, c = cas; i < outargs; i++) {
+                        reorder[i] = c % inargs;
+                        c /= inargs;
+                    }
+                    testPermuteArguments(args, types, reorder);
+                }
+                numcases *= inargs;
+                if (dilution > 10 && outargs >= 4) {
+                    // Do some special patterns, which we probably missed.
+                    // Replication of a single argument or argument pair.
+                    for (int i = 0; i < inargs; i++) {
+                        Arrays.fill(reorder, i);
+                        testPermuteArguments(args, types, reorder);
+                        for (int d = 1; d <= 2; d++) {
+                            if (i + d >= inargs)  continue;
+                            for (int j = 1; j < outargs; j += 2)
+                                reorder[j] += 1;
+                            testPermuteArguments(args, types, reorder);
+                            testPermuteArguments(args, types, reverse(reorder));
+                        }
+                    }
+                    // Repetition of a sequence of 3 or more arguments.
+                    for (int i = 1; i < inargs; i++) {
+                        for (int len = 3; len <= inargs; len++) {
+                            for (int j = 0; j < outargs; j++)
+                                reorder[j] = (i + (j % len)) % inargs;
+                            testPermuteArguments(args, types, reorder);
+                            testPermuteArguments(args, types, reverse(reorder));
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    static int[] reverse(int[] reorder) {
+        reorder = reorder.clone();
+        for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
+            int j = reorder.length - 1 - i;
+            int tem = reorder[i];
+            reorder[i] = reorder[j];
+            reorder[j] = tem;
+        }
+        return reorder;
+    }
+
+    void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
+        countTest();
+        if (args == null && types == null) {
+            int max = 0;
+            for (int j : reorder) {
+                if (max < j)  max = j;
+            }
+            args = randomArgs(max+1, Integer.class);
+        }
+        if (args == null) {
+            args = randomArgs(types);
+        }
+        if (types == null) {
+            types = new Class<?>[args.length];
+            for (int i = 0; i < args.length; i++)
+                types[i] = args[i].getClass();
+        }
+        int inargs = args.length, outargs = reorder.length;
+        assert(inargs == types.length);
+        if (verbosity >= 2)
+            System.out.println("permuteArguments "+Arrays.toString(reorder));
+        Object[] permArgs = new Object[outargs];
+        Class<?>[] permTypes = new Class<?>[outargs];
+        for (int i = 0; i < outargs; i++) {
+            permArgs[i] = args[reorder[i]];
+            permTypes[i] = types[reorder[i]];
+        }
+        if (verbosity >= 3) {
+            System.out.println("in args:   "+Arrays.asList(args));
+            System.out.println("out args:  "+Arrays.asList(permArgs));
+            System.out.println("in types:  "+Arrays.asList(types));
+            System.out.println("out types: "+Arrays.asList(permTypes));
+        }
+        MethodType inType  = MethodType.methodType(Object.class, types);
+        MethodType outType = MethodType.methodType(Object.class, permTypes);
+        MethodHandle target = MethodHandles.convertArguments(ValueConversions.varargsList(outargs), outType);
+        MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
+        Object result = newTarget.invokeVarargs(args);
+        Object expected = Arrays.asList(permArgs);
+        assertEquals(expected, result);
+    }
+
+
+    @Test
+    public void testSpreadArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("spreadArguments");
+        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
+            if (verbosity >= 2)
+                System.out.println("spreadArguments "+argType);
+            for (int nargs = 0; nargs < 10; nargs++) {
+                if (argType == int.class && nargs >= 6)  continue; // FIXME Fail_1
+                for (int pos = 0; pos < nargs; pos++) {
+                    if (argType == int.class && pos > 0)  continue; // FIXME Fail_3
+                    testSpreadArguments(argType, pos, nargs);
+                }
+            }
+        }
+    }
+    public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable {
+        countTest();
+        MethodHandle target = ValueConversions.varargsArray(nargs);
+        MethodHandle target2 = changeArgTypes(target, argType);
+        if (verbosity >= 2)
+            System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
+        Object[] args = randomArgs(target2.type().parameterArray());
+        // make sure the target does what we think it does:
+        if (pos == 0 && nargs < 5) {
+            Object[] check = (Object[]) target.invokeVarargs(args);
+            assertArrayEquals(args, check);
+            switch (nargs) {
+                case 0:
+                    check = target.<Object[]>invoke();
+                    assertArrayEquals(args, check);
+                    break;
+                case 1:
+                    check = target.<Object[]>invoke(args[0]);
+                    assertArrayEquals(args, check);
+                    break;
+                case 2:
+                    check = target.<Object[]>invoke(args[0], args[1]);
+                    assertArrayEquals(args, check);
+                    break;
+            }
+        }
+        List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList());
+        {   // modify newParams in place
+            List<Class<?>> spreadParams = newParams.subList(pos, nargs);
+            spreadParams.clear(); spreadParams.add(Object[].class);
+        }
+        MethodType newType = MethodType.methodType(Object.class, newParams);
+        MethodHandle result = MethodHandles.spreadArguments(target2, newType);
+        Object[] returnValue;
+        if (pos == 0) {
+            returnValue = (Object[]) result.invoke(args);
+        } else {
+            Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
+            args1[pos] = Arrays.copyOfRange(args, pos, args.length);
+            returnValue = (Object[]) result.invokeVarargs(args1);
+        }
+        assertArrayEquals(args, returnValue);
+    }
+
+    @Test
+    public void testCollectArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("collectArguments");
+        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
+            if (verbosity >= 2)
+                System.out.println("collectArguments "+argType);
+            for (int nargs = 0; nargs < 10; nargs++) {
+                for (int pos = 0; pos < nargs; pos++) {
+                    if (argType == int.class)  continue; // FIXME Fail_4
+                    testCollectArguments(argType, pos, nargs);
+                }
+            }
+        }
+    }
+    public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable {
+        countTest();
+        // fake up a MH with the same type as the desired adapter:
+        MethodHandle fake = ValueConversions.varargsArray(nargs);
+        fake = changeArgTypes(fake, argType);
+        MethodType newType = fake.type();
+        Object[] args = randomArgs(newType.parameterArray());
+        // here is what should happen:
+        Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
+        collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
+        // here is the MH which will witness the collected argument tail:
+        MethodHandle target = ValueConversions.varargsArray(pos+1);
+        target = changeArgTypes(target, 0, pos, argType);
+        target = changeArgTypes(target, pos, pos+1, Object[].class);
+        if (verbosity >= 2)
+            System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
+        MethodHandle result = MethodHandles.collectArguments(target, newType);
+        Object[] returnValue = (Object[]) result.invokeVarargs(args);
+//        assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
+//        returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
+//        collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
+        assertArrayEquals(collectedArgs, returnValue);
+    }
+
+    @Test
+    public void testInsertArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("insertArguments");
+        for (int nargs = 0; nargs <= 4; nargs++) {
+            for (int ins = 0; ins <= 4; ins++) {
+                if (ins > MAX_ARG_INCREASE)  continue;  // FIXME Fail_6
+                for (int pos = 0; pos <= nargs; pos++) {
+                    testInsertArguments(nargs, pos, ins);
+                }
+            }
+        }
+    }
+
+    void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
+        countTest();
+        MethodHandle target = ValueConversions.varargsArray(nargs + ins);
+        Object[] args = randomArgs(target.type().parameterArray());
+        List<Object> resList = Arrays.asList(args);
+        List<Object> argsToPass = new ArrayList<Object>(resList);
+        List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
+        if (verbosity >= 2)
+            System.out.println("insert: "+argsToInsert+" into "+target);
+        MethodHandle target2 = MethodHandles.insertArguments(target, pos,
+                (Object[]) argsToInsert.toArray());
+        argsToInsert.clear();  // remove from argsToInsert
+        Object res2 = target2.invokeVarargs(argsToPass);
+        Object res2List = Arrays.asList((Object[])res2);
+        if (verbosity >= 2)
+            System.out.println("result: "+res2List);
+        //if (!resList.equals(res2List))
+        //    System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
+        assertEquals(resList, res2List);
+    }
+
+    @Test
+    public void testFilterArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("filterArguments");
+        for (int nargs = 1; nargs <= 6; nargs++) {
+            for (int pos = 0; pos < nargs; pos++) {
+                testFilterArguments(nargs, pos);
+            }
+        }
+    }
+
+    void testFilterArguments(int nargs, int pos) throws Throwable {
+        countTest();
+        MethodHandle target = ValueConversions.varargsList(nargs);
+        MethodHandle filter = ValueConversions.varargsList(1);
+        filter = MethodHandles.convertArguments(filter, filter.type().generic());
+        Object[] argsToPass = randomArgs(nargs, Object.class);
+        if (verbosity >= 2)
+            System.out.println("filter "+target+" at "+pos+" with "+filter);
+        MethodHandle[] filters = new MethodHandle[pos*2+1];
+        filters[pos] = filter;
+        MethodHandle target2 = MethodHandles.filterArguments(target, filters);
+        // Simulate expected effect of filter on arglist:
+        Object[] filteredArgs = argsToPass.clone();
+        filteredArgs[pos] = filter.invoke(filteredArgs[pos]);
+        List<Object> expected = Arrays.asList(filteredArgs);
+        Object result = target2.invokeVarargs(argsToPass);
+        if (verbosity >= 2)
+            System.out.println("result: "+result);
+        if (!expected.equals(result))
+            System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+argsToPass+" => "+result);
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testFoldArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("foldArguments");
+        for (int nargs = 0; nargs <= 4; nargs++) {
+            for (int fold = 0; fold <= nargs; fold++) {
+                for (int pos = 0; pos <= nargs; pos++) {
+                    testFoldArguments(nargs, pos, fold);
+                }
+            }
+        }
+    }
+
+    void testFoldArguments(int nargs, int pos, int fold) throws Throwable {
+        if (pos != 0)  return;  // can fold only at pos=0 for now
+        countTest();
+        MethodHandle target = ValueConversions.varargsList(1 + nargs);
+        MethodHandle combine = ValueConversions.varargsList(fold);
+        List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class));
+        if (verbosity >= 2)
+            System.out.println("fold "+target+" with "+combine);
+        MethodHandle target2 = MethodHandles.foldArguments(target, combine);
+        // Simulate expected effect of combiner on arglist:
+        List<Object> expected = new ArrayList<Object>(argsToPass);
+        List<Object> argsToFold = expected.subList(pos, pos + fold);
+        if (verbosity >= 2)
+            System.out.println("fold: "+argsToFold+" into "+target2);
+        Object foldedArgs = combine.invokeVarargs(argsToFold);
+        argsToFold.add(0, foldedArgs);
+        Object result = target2.invokeVarargs(argsToPass);
+        if (verbosity >= 2)
+            System.out.println("result: "+result);
+        if (!expected.equals(result))
+            System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result);
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testDropArguments() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("dropArguments");
+        for (int nargs = 0; nargs <= 4; nargs++) {
+            for (int drop = 1; drop <= 4; drop++) {
+                for (int pos = 0; pos <= nargs; pos++) {
+                    testDropArguments(nargs, pos, drop);
+                }
+            }
+        }
+    }
+
+    void testDropArguments(int nargs, int pos, int drop) throws Throwable {
+        countTest();
+        MethodHandle target = ValueConversions.varargsArray(nargs);
+        Object[] args = randomArgs(target.type().parameterArray());
+        MethodHandle target2 = MethodHandles.dropArguments(target, pos,
+                Collections.nCopies(drop, Object.class).toArray(new Class[0]));
+        List<Object> resList = Arrays.asList(args);
+        List<Object> argsToDrop = new ArrayList<Object>(resList);
+        for (int i = drop; i > 0; i--) {
+            argsToDrop.add(pos, "blort#"+i);
+        }
+        Object res2 = target2.invokeVarargs(argsToDrop);
+        Object res2List = Arrays.asList((Object[])res2);
+        //if (!resList.equals(res2List))
+        //    System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
+        assertEquals(resList, res2List);
+    }
+
+    @Test
+    public void testInvokers() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
+        // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
+        Set<MethodType> done = new HashSet<MethodType>();
+        for (int i = 0; i <= 6; i++) {
+            MethodType gtype = MethodType.genericMethodType(i);
+            for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
+                for (int j = -1; j < i; j++) {
+                    MethodType type = gtype;
+                    if (j < 0)
+                        type = type.changeReturnType(argType);
+                    else if (argType == void.class)
+                        continue;
+                    else
+                        type = type.changeParameterType(j, argType);
+                    if (argType.isPrimitive() && j != i-1)  continue; // FIXME Fail_5
+                    if (done.add(type))
+                        testInvokers(type);
+                    MethodType vtype = type.changeReturnType(void.class);
+                    if (done.add(vtype))
+                        testInvokers(vtype);
+                }
+            }
+        }
+    }
+
+    public void testInvokers(MethodType type) throws Throwable {
+        if (verbosity >= 2)
+            System.out.println("test invokers for "+type);
+        int nargs = type.parameterCount();
+        boolean testRetCode = type.returnType() != void.class;
+        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
+                                MethodType.genericMethodType(0, true));
+        target = MethodHandles.collectArguments(target, type);
+        Object[] args = randomArgs(type.parameterArray());
+        List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args));
+        targetPlusArgs.add(0, target);
+        int code = (Integer) invokee(args);
+        Object log = logEntry("invokee", args);
+        assertEquals(log.hashCode(), code);
+        assertCalled("invokee", args);
+        MethodHandle inv;
+        Object result;
+        // exact invoker
+        countTest();
+        calledLog.clear();
+        inv = MethodHandles.exactInvoker(type);
+        result = inv.invokeVarargs(targetPlusArgs);
+        if (testRetCode)  assertEquals(code, result);
+        assertCalled("invokee", args);
+        // generic invoker
+        countTest();
+        inv = MethodHandles.genericInvoker(type);
+        if (nargs <= 3) {
+            calledLog.clear();
+            switch (nargs) {
+            case 0:
+                result = inv.invoke(target);
+                break;
+            case 1:
+                result = inv.invoke(target, args[0]);
+                break;
+            case 2:
+                result = inv.invoke(target, args[0], args[1]);
+                break;
+            case 3:
+                result = inv.invoke(target, args[0], args[1], args[2]);
+                break;
+            }
+            if (testRetCode)  assertEquals(code, result);
+            assertCalled("invokee", args);
+        }
+        calledLog.clear();
+        result = inv.invokeVarargs(targetPlusArgs);
+        if (testRetCode)  assertEquals(code, result);
+        assertCalled("invokee", args);
+        // varargs invoker #0
+        calledLog.clear();
+        inv = MethodHandles.varargsInvoker(type, 0);
+        result = inv.invoke(target, args);
+        if (testRetCode)  assertEquals(code, result);
+        assertCalled("invokee", args);
+        if (nargs >= 1) {
+            // varargs invoker #1
+            calledLog.clear();
+            inv = MethodHandles.varargsInvoker(type, 1);
+            result = inv.invoke(target, args[0], Arrays.copyOfRange(args, 1, nargs));
+            if (testRetCode)  assertEquals(code, result);
+            assertCalled("invokee", args);
+        }
+        if (nargs >= 2) {
+            // varargs invoker #2
+            calledLog.clear();
+            inv = MethodHandles.varargsInvoker(type, 2);
+            result = inv.invoke(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
+            if (testRetCode)  assertEquals(code, result);
+            assertCalled("invokee", args);
+        }
+        if (nargs >= 3) {
+            // varargs invoker #3
+            calledLog.clear();
+            inv = MethodHandles.varargsInvoker(type, 3);
+            result = inv.invoke(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
+            if (testRetCode)  assertEquals(code, result);
+            assertCalled("invokee", args);
+        }
+        for (int k = 0; k <= nargs; k++) {
+            // varargs invoker #0..N
+            countTest();
+            calledLog.clear();
+            inv = MethodHandles.varargsInvoker(type, k);
+            List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs);
+            List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
+            Object[] tail = tailList.toArray();
+            tailList.clear(); tailList.add(tail);
+            result = inv.invokeVarargs(targetPlusVarArgs);
+            if (testRetCode)  assertEquals(code, result);
+            assertCalled("invokee", args);
+        }
+        // dynamic invoker
+        countTest();
+        CallSite site = new CallSite(MethodHandlesTest.class, "foo", type);
+        inv = MethodHandles.dynamicInvoker(site);
+        site.setTarget(target);
+        calledLog.clear();
+        result = inv.invokeVarargs(args);
+        if (testRetCode)  assertEquals(code, result);
+        assertCalled("invokee", args);
+    }
+
+    static Object invokee(Object... args) {
+        return called("invokee", args).hashCode();
+    }
+
+    private static final String MISSING_ARG = "missingArg";
+    static Object targetIfEquals() {
+        return called("targetIfEquals");
+    }
+    static Object fallbackIfNotEquals() {
+        return called("fallbackIfNotEquals");
+    }
+    static Object targetIfEquals(Object x) {
+        assertEquals(x, MISSING_ARG);
+        return called("targetIfEquals", x);
+    }
+    static Object fallbackIfNotEquals(Object x) {
+        assertFalse(x.toString(), x.equals(MISSING_ARG));
+        return called("fallbackIfNotEquals", x);
+    }
+    static Object targetIfEquals(Object x, Object y) {
+        assertEquals(x, y);
+        return called("targetIfEquals", x, y);
+    }
+    static Object fallbackIfNotEquals(Object x, Object y) {
+        assertFalse(x.toString(), x.equals(y));
+        return called("fallbackIfNotEquals", x, y);
+    }
+    static Object targetIfEquals(Object x, Object y, Object z) {
+        assertEquals(x, y);
+        return called("targetIfEquals", x, y, z);
+    }
+    static Object fallbackIfNotEquals(Object x, Object y, Object z) {
+        assertFalse(x.toString(), x.equals(y));
+        return called("fallbackIfNotEquals", x, y, z);
+    }
+
+    @Test
+    public void testGuardWithTest() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("guardWithTest");
+        for (int nargs = 0; nargs <= 3; nargs++) {
+            if (nargs != 2)  continue;  // FIXME: test more later
+            testGuardWithTest(nargs, Object.class);
+            testGuardWithTest(nargs, String.class);
+        }
+    }
+    void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
+        countTest();
+        MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
+        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
+        MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
+        while (test.type().parameterCount() < nargs)
+            test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
+        while (test.type().parameterCount() > nargs)
+            test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
+        if (argClass != Object.class) {
+            test = changeArgTypes(test, argClass);
+            target = changeArgTypes(target, argClass);
+            fallback = changeArgTypes(fallback, argClass);
+        }
+        MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
+        assertEquals(target.type(), mh.type());
+        Object[][] argLists = {
+            { },
+            { "foo" }, { MISSING_ARG },
+            { "foo", "foo" }, { "foo", "bar" },
+            { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
+        };
+        for (Object[] argList : argLists) {
+            if (argList.length != nargs)  continue;
+            boolean equals;
+            switch (nargs) {
+            case 0:   equals = true; break;
+            case 1:   equals = MISSING_ARG.equals(argList[0]); break;
+            default:  equals = argList[0].equals(argList[1]); break;
+            }
+            String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
+            if (verbosity >= 2)
+                System.out.println(logEntry(willCall, argList));
+            Object result = mh.invokeVarargs(argList);
+            assertCalled(willCall, argList);
+        }
+    }
+
+    @Test
+    public void testCatchException() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("catchException");
+        for (int nargs = 2; nargs <= 6; nargs++) {
+            for (int ti = 0; ti <= 1; ti++) {
+                boolean throwIt = (ti != 0);
+                testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs);
+                testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs);
+                testCatchException(String.class, new LinkageError("testing"), throwIt, nargs);
+            }
+        }
+    }
+
+    private static <T extends Throwable>
+    Object throwOrReturn(Object normal, T exception) throws T {
+        if (exception != null)  throw exception;
+        return normal;
+    }
+
+    void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
+        countTest();
+        if (verbosity >= 2)
+            System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs);
+        Class<? extends Throwable> exType = thrown.getClass();
+        MethodHandle throwOrReturn
+                = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn",
+                    MethodType.methodType(Object.class, Object.class, Throwable.class));
+        MethodHandle thrower = throwOrReturn;
+        while (thrower.type().parameterCount() < nargs)
+            thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class);
+        MethodHandle target = MethodHandles.catchException(thrower,
+                thrown.getClass(), ValueConversions.varargsList(1+nargs));
+        assertEquals(thrower.type(), target.type());
+        //System.out.println("catching with "+target+" : "+throwOrReturn);
+        Object[] args = randomArgs(nargs, Object.class);
+        args[1] = (throwIt ? thrown : null);
+        Object returned = target.invokeVarargs(args);
+        //System.out.println("return from "+target+" : "+returned);
+        if (!throwIt) {
+            assertSame(args[0], returned);
+        } else {
+            List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args));
+            catchArgs.add(0, thrown);
+            assertEquals(catchArgs, returned);
+        }
+    }
+
+    @Test
+    public void testThrowException() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("throwException");
+        testThrowException(int.class, new ClassCastException("testing"));
+        testThrowException(void.class, new java.io.IOException("testing"));
+        testThrowException(String.class, new LinkageError("testing"));
+    }
+
+    void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
+        countTest();
+        Class<? extends Throwable> exType = thrown.getClass();
+        MethodHandle target = MethodHandles.throwException(returnType, exType);
+        //System.out.println("throwing with "+target+" : "+thrown);
+        MethodType expectedType = MethodType.methodType(returnType, exType);
+        assertEquals(expectedType, target.type());
+        Throwable caught = null;
+        try {
+            Object res = target.invokeGeneric(thrown);
+            fail("got "+res+" instead of throwing "+thrown);
+        } catch (Throwable ex) {
+            if (ex != thrown) {
+                if (ex instanceof Error)  throw (Error)ex;
+                if (ex instanceof RuntimeException)  throw (RuntimeException)ex;
+            }
+            caught = ex;
+        }
+        assertSame(thrown, caught);
+    }
+
+    @Test
+    public void testCastFailure() throws Throwable {
+        if (CAN_SKIP_WORKING)  return;
+        startTest("testCastFailure");
+        testCastFailure("cast/argument", 11000);
+        testCastFailure("unbox/argument", 11000);
+        testCastFailure("cast/return", 11000);
+        testCastFailure("unbox/return", 11000);
+    }
+
+    static class Surprise extends JavaMethodHandle {
+        Surprise() { super("value"); }
+        Object value(Object x) {
+            trace("value", x);
+            if (boo != null)  return boo;
+            return x;
+        }
+        Object boo;
+        void boo(Object x) { boo = x; }
+
+        static void trace(String x, Object y) {
+            if (verbosity > 8) System.out.println(x+"="+y);
+        }
+        static Object  refIdentity(Object x)  { trace("ref.x", x); return x; }
+        static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
+        static int     intIdentity(int x)     { trace("int.x", x); return x; }
+        static MethodHandle REF_IDENTITY = PRIVATE.findStatic(
+                Surprise.class, "refIdentity",
+                    MethodType.methodType(Object.class, Object.class));
+        static MethodHandle BOX_IDENTITY = PRIVATE.findStatic(
+                Surprise.class, "boxIdentity",
+                    MethodType.methodType(Integer.class, Integer.class));
+        static MethodHandle INT_IDENTITY = PRIVATE.findStatic(
+                Surprise.class, "intIdentity",
+                    MethodType.methodType(int.class, int.class));
+    }
+
+    void testCastFailure(String mode, int okCount) throws Throwable {
+        countTest(false);
+        if (verbosity > 1)  System.out.println("mode="+mode);
+        Surprise boo = new Surprise();
+        MethodHandle identity = Surprise.REF_IDENTITY, surprise = boo;
+        if (mode.endsWith("/return")) {
+            if (mode.equals("unbox/return")) {
+                // fail on return to ((Integer)surprise).intValue
+                surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(int.class, Object.class));
+                identity = MethodHandles.convertArguments(identity, MethodType.methodType(int.class, Object.class));
+            } else if (mode.equals("cast/return")) {
+                // fail on return to (Integer)surprise
+                surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(Integer.class, Object.class));
+                identity = MethodHandles.convertArguments(identity, MethodType.methodType(Integer.class, Object.class));
+            }
+        } else if (mode.endsWith("/argument")) {
+            MethodHandle callee = null;
+            if (mode.equals("unbox/argument")) {
+                // fail on handing surprise to int argument
+                callee = Surprise.INT_IDENTITY;
+            } else if (mode.equals("cast/argument")) {
+                // fail on handing surprise to Integer argument
+                callee = Surprise.BOX_IDENTITY;
+            }
+            if (callee != null) {
+                callee = MethodHandles.convertArguments(callee, MethodType.genericMethodType(1));
+                surprise = MethodHandles.filterArguments(callee, surprise);
+                identity = MethodHandles.filterArguments(callee, identity);
+            }
+        }
+        assertNotSame(mode, surprise, boo);
+        identity = MethodHandles.convertArguments(identity, MethodType.genericMethodType(1));
+        surprise = MethodHandles.convertArguments(surprise, MethodType.genericMethodType(1));
+        Object x = 42;
+        for (int i = 0; i < okCount; i++) {
+            Object y = identity.invoke(x);
+            assertEquals(x, y);
+            Object z = surprise.invoke(x);
+            assertEquals(x, z);
+        }
+        boo.boo("Boo!");
+        Object y = identity.invoke(x);
+        assertEquals(x, y);
+        try {
+            Object z = surprise.invoke(x);
+            System.out.println("Failed to throw; got z="+z);
+            assertTrue(false);
+        } catch (Exception ex) {
+            if (verbosity > 1)
+                System.out.println("caught "+ex);
+            if (verbosity > 2)
+                ex.printStackTrace();
+            assertTrue(ex instanceof ClassCastException
+                    // FIXME: accept only one of the two for any given unit test
+                    || ex instanceof WrongMethodTypeException
+                    );
+        }
+    }
+
+}
+// Local abbreviated copy of sun.dyn.util.ValueConversions
+class ValueConversions {
+    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
+    private static final Object[] NO_ARGS_ARRAY = {};
+    private static Object[] makeArray(Object... args) { return args; }
+    private static Object[] array() { return NO_ARGS_ARRAY; }
+    private static Object[] array(Object a0)
+                { return makeArray(a0); }
+    private static Object[] array(Object a0, Object a1)
+                { return makeArray(a0, a1); }
+    private static Object[] array(Object a0, Object a1, Object a2)
+                { return makeArray(a0, a1, a2); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
+                { return makeArray(a0, a1, a2, a3); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
+                                  Object a4)
+                { return makeArray(a0, a1, a2, a3, a4); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
+                                  Object a4, Object a5)
+                { return makeArray(a0, a1, a2, a3, a4, a5); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
+                                  Object a4, Object a5, Object a6)
+                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
+                                  Object a4, Object a5, Object a6, Object a7)
+                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
+                                  Object a4, Object a5, Object a6, Object a7,
+                                  Object a8)
+                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
+    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
+                                  Object a4, Object a5, Object a6, Object a7,
+                                  Object a8, Object a9)
+                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
+    static MethodHandle[] makeArrays() {
+        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
+        MethodHandles.Lookup lookup = IMPL_LOOKUP;
+        for (;;) {
+            int nargs = arrays.size();
+            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
+            String name = "array";
+            MethodHandle array = null;
+            try {
+                array = lookup.findStatic(ValueConversions.class, name, type);
+            } catch (NoAccessException ex) {
+            }
+            if (array == null)  break;
+            arrays.add(array);
+        }
+        assert(arrays.size() == 11);  // current number of methods
+        return arrays.toArray(new MethodHandle[0]);
+    }
+    static final MethodHandle[] ARRAYS = makeArrays();
+
+    /** Return a method handle that takes the indicated number of Object
+     *  arguments and returns an Object array of them, as if for varargs.
+     */
+    public static MethodHandle varargsArray(int nargs) {
+        if (nargs < ARRAYS.length)
+            return ARRAYS[nargs];
+        // else need to spin bytecode or do something else fancy
+        throw new UnsupportedOperationException("NYI");
+    }
+
+    private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
+    private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
+    private static List<Object> list() { return NO_ARGS_LIST; }
+    private static List<Object> list(Object a0)
+                { return makeList(a0); }
+    private static List<Object> list(Object a0, Object a1)
+                { return makeList(a0, a1); }
+    private static List<Object> list(Object a0, Object a1, Object a2)
+                { return makeList(a0, a1, a2); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
+                { return makeList(a0, a1, a2, a3); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
+                                     Object a4)
+                { return makeList(a0, a1, a2, a3, a4); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
+                                     Object a4, Object a5)
+                { return makeList(a0, a1, a2, a3, a4, a5); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
+                                     Object a4, Object a5, Object a6)
+                { return makeList(a0, a1, a2, a3, a4, a5, a6); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
+                                     Object a4, Object a5, Object a6, Object a7)
+                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
+                                     Object a4, Object a5, Object a6, Object a7,
+                                     Object a8)
+                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
+    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
+                                     Object a4, Object a5, Object a6, Object a7,
+                                     Object a8, Object a9)
+                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
+    static MethodHandle[] makeLists() {
+        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
+        MethodHandles.Lookup lookup = IMPL_LOOKUP;
+        for (;;) {
+            int nargs = arrays.size();
+            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
+            String name = "list";
+            MethodHandle array = null;
+            try {
+                array = lookup.findStatic(ValueConversions.class, name, type);
+            } catch (NoAccessException ex) {
+            }
+            if (array == null)  break;
+            arrays.add(array);
+        }
+        assert(arrays.size() == 11);  // current number of methods
+        return arrays.toArray(new MethodHandle[0]);
+    }
+    static final MethodHandle[] LISTS = makeLists();
+
+    /** Return a method handle that takes the indicated number of Object
+     *  arguments and returns List.
+     */
+    public static MethodHandle varargsList(int nargs) {
+        if (nargs < LISTS.length)
+            return LISTS[nargs];
+        // else need to spin bytecode or do something else fancy
+        throw new UnsupportedOperationException("NYI");
+    }
+}
+// This guy tests access from outside the same package member, but inside
+// the package itself.
+class PackageSibling {
+    static Lookup lookup() {
+        return MethodHandles.lookup();
+    }
+}
--- a/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Wed Jan 13 23:26:51 2010 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1855 +0,0 @@
-/*
- * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Sun designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Sun in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
- */
-
-/* @test
- * @summary unit tests for java.dyn.MethodHandles
- * @compile -XDinvokedynamic MethodHandlesTest.java
- * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic jdk.java.dyn.MethodHandlesTest
- */
-
-package jdk.java.dyn;
-
-import java.dyn.*;
-import java.dyn.MethodHandles.Lookup;
-import java.lang.reflect.*;
-import java.util.*;
-import org.junit.*;
-import static org.junit.Assert.*;
-import static org.junit.Assume.*;
-
-
-/**
- *
- * @author jrose
- */
-public class MethodHandlesTest {
-    // How much output?
-    static int verbosity = 1;
-
-    // Set this true during development if you want to fast-forward to
-    // a particular new, non-working test.  Tests which are known to
-    // work (or have recently worked) test this flag and return on true.
-    static boolean CAN_SKIP_WORKING = false;
-    //static { CAN_SKIP_WORKING = true; }
-
-    // Set true to test more calls.  If false, some tests are just
-    // lookups, without exercising the actual method handle.
-    static boolean DO_MORE_CALLS = false;
-
-
-    @Test
-    public void testFirst() throws Throwable {
-        verbosity += 9; try {
-            // left blank for debugging
-        } finally { verbosity -= 9; }
-    }
-
-    // current failures
-    @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
-    public void testFail_1() throws Throwable {
-        testSpreadArguments(int.class, 0, 6);
-    }
-    @Test @Ignore("failure in JVM when expanding the stack")
-    public void testFail_2() throws Throwable {
-        // if CONV_OP_IMPLEMENTED_MASK includes OP_SPREAD_ARGS, this crashes:
-        testSpreadArguments(Object.class, 0, 2);
-    }
-    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
-    public void testFail_3() throws Throwable {
-        testSpreadArguments(int.class, 1, 2);
-    }
-    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
-    public void testFail_4() throws Throwable {
-        testCollectArguments(int.class, 1, 2);
-    }
-    @Test @Ignore("cannot collect leading primitive types")
-    public void testFail_5() throws Throwable {
-        testInvokers(MethodType.genericMethodType(2).changeParameterType(0, int.class));
-    }
-    @Test @Ignore("should not insert arguments beyond MethodHandlePushLimit")
-    public void testFail_6() throws Throwable {
-        testInsertArguments(0, 0, MAX_ARG_INCREASE+1);
-    }
-    static final int MAX_ARG_INCREASE = 3;
-
-    public MethodHandlesTest() {
-    }
-
-    @Before
-    public void checkImplementedPlatform() {
-        boolean platformOK = false;
-        Properties properties = System.getProperties();
-        String vers = properties.getProperty("java.vm.version");
-        String name = properties.getProperty("java.vm.name");
-        String arch = properties.getProperty("os.arch");
-        if (arch.equals("i386") &&
-            (name.contains("Client") || name.contains("Server"))
-            ) {
-            platformOK = true;
-        } else {
-            System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch));
-        }
-        assumeTrue(platformOK);
-    }
-
-    String testName;
-    int posTests, negTests;
-    @After
-    public void printCounts() {
-        if (verbosity >= 1 && (posTests | negTests) != 0) {
-            System.out.println();
-            if (posTests != 0)  System.out.println("=== "+testName+": "+posTests+" positive test cases run");
-            if (negTests != 0)  System.out.println("=== "+testName+": "+negTests+" negative test cases run");
-        }
-    }
-    void countTest(boolean positive) {
-        if (positive) ++posTests;
-        else          ++negTests;
-    }
-    void countTest() { countTest(true); }
-    void startTest(String name) {
-        if (testName != null)  printCounts();
-        if (verbosity >= 0)
-            System.out.println(name);
-        posTests = negTests = 0;
-        testName = name;
-    }
-
-    @BeforeClass
-    public static void setUpClass() throws Exception {
-        calledLog.clear();
-        calledLog.add(null);
-        nextArg = 1000000;
-    }
-
-    @AfterClass
-    public static void tearDownClass() throws Exception {
-    }
-
-    static List<Object> calledLog = new ArrayList<Object>();
-    static Object logEntry(String name, Object... args) {
-        return Arrays.asList(name, Arrays.asList(args));
-    }
-    static Object called(String name, Object... args) {
-        Object entry = logEntry(name, args);
-        calledLog.add(entry);
-        return entry;
-    }
-    static void assertCalled(String name, Object... args) {
-        Object expected = logEntry(name, args);
-        Object actual   = calledLog.get(calledLog.size() - 1);
-        if (expected.equals(actual))  return;
-        System.out.println("assertCalled "+name+":");
-        System.out.println("expected:   "+expected);
-        System.out.println("actual:     "+actual);
-        System.out.println("ex. types:  "+getClasses(expected));
-        System.out.println("act. types: "+getClasses(actual));
-        assertEquals("previous method call types", expected, actual);
-        assertEquals("previous method call", expected, actual);
-    }
-    static void printCalled(MethodHandle target, String name, Object... args) {
-        if (verbosity >= 2)
-            System.out.println("calling "+logEntry(name, args)+" on "+target);
-    }
-
-    static Object castToWrapper(Object value, Class<?> dst) {
-        Object wrap = null;
-        if (value instanceof Number)
-            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
-        if (value instanceof Character)
-            wrap = castToWrapperOrNull((char)(Character)value, dst);
-        if (wrap != null)  return wrap;
-        return dst.cast(value);
-    }
-
-    static Object castToWrapperOrNull(long value, Class<?> dst) {
-        if (dst == int.class || dst == Integer.class)
-            return (int)(value);
-        if (dst == long.class || dst == Long.class)
-            return (long)(value);
-        if (dst == char.class || dst == Character.class)
-            return (char)(value);
-        if (dst == short.class || dst == Short.class)
-            return (short)(value);
-        if (dst == float.class || dst == Float.class)
-            return (float)(value);
-        if (dst == double.class || dst == Double.class)
-            return (double)(value);
-        if (dst == byte.class || dst == Byte.class)
-            return (byte)(value);
-        if (dst == boolean.class || dst == boolean.class)
-            return ((value % 29) & 1) == 0;
-        return null;
-    }
-
-    static int nextArg;
-    static Object randomArg(Class<?> param) {
-        Object wrap = castToWrapperOrNull(nextArg, param);
-        if (wrap != null) {
-            nextArg++;
-            return wrap;
-        }
-//        import sun.dyn.util.Wrapper;
-//        Wrapper wrap = Wrapper.forBasicType(dst);
-//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
-//            wrap = Wrapper.forWrapperType(dst);
-//        if (wrap != Wrapper.OBJECT)
-//            return wrap.wrap(nextArg++);
-        if (param.isInterface() || param.isAssignableFrom(String.class))
-            return "#"+(nextArg++);
-        else
-            try {
-                return param.newInstance();
-            } catch (InstantiationException ex) {
-            } catch (IllegalAccessException ex) {
-            }
-        return null;  // random class not Object, String, Integer, etc.
-    }
-    static Object[] randomArgs(Class<?>... params) {
-        Object[] args = new Object[params.length];
-        for (int i = 0; i < args.length; i++)
-            args[i] = randomArg(params[i]);
-        return args;
-    }
-    static Object[] randomArgs(int nargs, Class<?> param) {
-        Object[] args = new Object[nargs];
-        for (int i = 0; i < args.length; i++)
-            args[i] = randomArg(param);
-        return args;
-    }
-
-    static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
-        return Arrays.copyOf(a, a.length, atype);
-    }
-    static <T> T[] cat(T[] a, T... b) {
-        int alen = a.length, blen = b.length;
-        if (blen == 0)  return a;
-        T[] c = Arrays.copyOf(a, alen + blen);
-        System.arraycopy(b, 0, c, alen, blen);
-        return c;
-    }
-    static Integer[] boxAll(int... vx) {
-        Integer[] res = new Integer[vx.length];
-        for (int i = 0; i < res.length; i++) {
-            res[i] = vx[i];
-        }
-        return res;
-    }
-    static Object getClasses(Object x) {
-        if (x == null)  return x;
-        if (x instanceof String)  return x;  // keep the name
-        if (x instanceof List) {
-            // recursively report classes of the list elements
-            Object[] xa = ((List)x).toArray();
-            for (int i = 0; i < xa.length; i++)
-                xa[i] = getClasses(xa[i]);
-            return Arrays.asList(xa);
-        }
-        return x.getClass().getSimpleName();
-    }
-
-    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
-        return changeArgTypes(target, 0, 999, argType);
-    }
-    static MethodHandle changeArgTypes(MethodHandle target,
-            int beg, int end, Class<?> argType) {
-        MethodType targetType = target.type();
-        end = Math.min(end, targetType.parameterCount());
-        ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
-        Collections.fill(argTypes.subList(beg, end), argType);
-        MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
-        return MethodHandles.convertArguments(target, ttype2);
-    }
-
-    // This lookup is good for all members in and under MethodHandlesTest.
-    static final Lookup PRIVATE = MethodHandles.lookup();
-    // This lookup is good for package-private members but not private ones.
-    static final Lookup PACKAGE = PackageSibling.lookup();
-    // This lookup is good only for public members.
-    static final Lookup PUBLIC  = MethodHandles.publicLookup();
-
-    // Subject methods...
-    static class Example implements IntExample {
-        final String name;
-        public Example() { name = "Example#"+(nextArg++); }
-        protected Example(String name) { this.name = name; }
-        protected Example(int x) { this(); called("protected <init>", this, x); }
-        @Override public String toString() { return name; }
-
-        public void            v0()     { called("v0", this); }
-        void                   pkg_v0() { called("pkg_v0", this); }
-        private void           pri_v0() { called("pri_v0", this); }
-        public static void     s0()     { called("s0"); }
-        static void            pkg_s0() { called("pkg_s0"); }
-        private static void    pri_s0() { called("pri_s0"); }
-
-        public Object          v1(Object x) { return called("v1", this, x); }
-        public Object          v2(Object x, Object y) { return called("v2", this, x, y); }
-        public Object          v2(Object x, int    y) { return called("v2", this, x, y); }
-        public Object          v2(int    x, Object y) { return called("v2", this, x, y); }
-        public Object          v2(int    x, int    y) { return called("v2", this, x, y); }
-        public static Object   s1(Object x) { return called("s1", x); }
-        public static Object   s2(int x)    { return called("s2", x); }
-        public static Object   s3(long x)   { return called("s3", x); }
-        public static Object   s4(int x, int y) { return called("s4", x, y); }
-        public static Object   s5(long x, int y) { return called("s5", x, y); }
-        public static Object   s6(int x, long y) { return called("s6", x, y); }
-        public static Object   s7(float x, double y) { return called("s7", x, y); }
-    }
-    public static class PubExample extends Example {
-    }
-    static class SubExample extends Example {
-        @Override public void  v0()     { called("Sub/v0", this); }
-        @Override void         pkg_v0() { called("Sub/pkg_v0", this); }
-        private      SubExample(int x)  { called("<init>", this, x); }
-        public SubExample() { super("SubExample#"+(nextArg++)); }
-    }
-    public static interface IntExample {
-        public void            v0();
-        static class Impl implements IntExample {
-            public void        v0()     { called("Int/v0", this); }
-            final String name;
-            public Impl() { name = "Example#"+(nextArg++); }
-        }
-    }
-
-    static final Object[][][] ACCESS_CASES = {
-        { { false, PUBLIC }, { false, PACKAGE }, { false, PRIVATE } },
-        { { false, PUBLIC }, { false, PACKAGE }, { true,  PRIVATE } },
-        { { false, PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE } },
-        { { true,  PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE } },
-    };
-
-    static Object[][] accessCases(Class<?> defc, String name) {
-        if (name.contains("pri_")) {
-            return ACCESS_CASES[1]; // PRIVATE only
-        } else if (name.contains("pkg_")) {
-            return ACCESS_CASES[2]; // not PUBLIC
-        } else {
-            assertTrue(name.indexOf('_') < 0);
-            boolean pubc = Modifier.isPublic(defc.getModifiers());
-            if (pubc)
-                return ACCESS_CASES[3]; // all access levels
-            return ACCESS_CASES[2]; // PACKAGE but not PUBLIC
-        }
-    }
-
-    @Test
-    public void testFindStatic() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("findStatic");
-        testFindStatic(PubExample.class, void.class, "s0");
-        testFindStatic(Example.class, void.class, "s0");
-        testFindStatic(Example.class, void.class, "pkg_s0");
-        testFindStatic(Example.class, void.class, "pri_s0");
-
-        testFindStatic(Example.class, Object.class, "s1", Object.class);
-        testFindStatic(Example.class, Object.class, "s2", int.class);
-        testFindStatic(Example.class, Object.class, "s3", long.class);
-        testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
-        testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
-        testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
-        testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
-
-        testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
-    }
-
-    void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        for (Object[] ac : accessCases(defc, name)) {
-            testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
-        }
-    }
-    void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        testFindStatic(true, lookup, defc, ret, name, params);
-    }
-    void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        countTest(positive);
-        MethodType type = MethodType.methodType(ret, params);
-        MethodHandle target = null;
-        RuntimeException noAccess = null;
-        try {
-            target = lookup.findStatic(defc, name, type);
-        } catch (NoAccessException ex) {
-            noAccess = ex;
-        }
-        if (verbosity >= 2)
-            System.out.println("findStatic "+lookup+": "+defc+"."+name+"/"+type+" => "+target
-                    +(noAccess == null ? "" : " !! "+noAccess));
-        if (positive && noAccess != null)  throw noAccess;
-        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
-        if (!positive)  return; // negative test failed as expected
-        assertEquals(type, target.type());
-        assertTrue(target.toString().contains(name));  // rough check
-        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
-        Object[] args = randomArgs(params);
-        printCalled(target, name, args);
-        target.invokeVarargs(args);
-        assertCalled(name, args);
-        System.out.print(':');
-    }
-
-    @Test
-    public void testFindVirtual() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("findVirtual");
-        testFindVirtual(Example.class, void.class, "v0");
-        testFindVirtual(Example.class, void.class, "pkg_v0");
-        testFindVirtual(Example.class, void.class, "pri_v0");
-        testFindVirtual(Example.class, Object.class, "v1", Object.class);
-        testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
-        testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
-        testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
-        testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
-        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
-        // test dispatch
-        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/v0");
-        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/v0");
-        testFindVirtual(SubExample.class,      IntExample.class, void.class, "Sub/v0");
-        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/pkg_v0");
-        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/pkg_v0");
-        testFindVirtual(Example.class,         IntExample.class, void.class, "v0");
-        testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
-    }
-
-    void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        Class<?> rcvc = defc;
-        testFindVirtual(rcvc, defc, ret, name, params);
-    }
-    void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        for (Object[] ac : accessCases(defc, name)) {
-            testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params);
-        }
-    }
-    void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
-    }
-    void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        countTest(positive);
-        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
-        MethodType type = MethodType.methodType(ret, params);
-        MethodHandle target = null;
-        RuntimeException noAccess = null;
-        try {
-            target = lookup.findVirtual(defc, methodName, type);
-        } catch (NoAccessException ex) {
-            noAccess = ex;
-        }
-        if (verbosity >= 2)
-            System.out.println("findVirtual "+lookup+": "+defc+"."+name+"/"+type+" => "+target
-                    +(noAccess == null ? "" : " !! "+noAccess));
-        if (positive && noAccess != null)  throw noAccess;
-        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
-        if (!positive)  return; // negative test failed as expected
-        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
-        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
-        MethodType ttype = target.type();
-        ttype = ttype.changeParameterType(0, defc); // FIXME: test this
-        assertEquals(typeWithSelf, ttype);
-        assertTrue(target.toString().contains(methodName));  // rough check
-        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
-        Object[] argsWithSelf = randomArgs(paramsWithSelf);
-        if (rcvc != defc)  argsWithSelf[0] = randomArg(rcvc);
-        printCalled(target, name, argsWithSelf);
-        target.invokeVarargs(argsWithSelf);
-        assertCalled(name, argsWithSelf);
-        System.out.print(':');
-    }
-
-    @Test
-    public void testFindSpecial() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("findSpecial");
-        testFindSpecial(Example.class, void.class, "v0");
-        testFindSpecial(Example.class, void.class, "pkg_v0");
-        testFindSpecial(false, PRIVATE, Example.class, void.class, "<init>", int.class);
-        testFindSpecial(false, PRIVATE, Example.class, void.class, "bogus");
-    }
-
-    void testFindSpecial(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        testFindSpecial(true,  PRIVATE, defc, ret, name, params);
-        testFindSpecial(false, PACKAGE, defc, ret, name, params);
-        testFindSpecial(false, PUBLIC,  defc, ret, name, params);
-    }
-    void testFindSpecial(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        countTest(positive);
-        MethodType type = MethodType.methodType(ret, params);
-        MethodHandle target = null;
-        RuntimeException noAccess = null;
-        try {
-            target = lookup.findSpecial(defc, name, type, defc);
-        } catch (NoAccessException ex) {
-            noAccess = ex;
-        }
-        if (verbosity >= 2)
-            System.out.println("findSpecial "+defc+"."+name+"/"+type+" => "+target
-                    +(noAccess == null ? "" : " !! "+noAccess));
-        if (positive && noAccess != null)  throw noAccess;
-        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
-        if (!positive)  return; // negative test failed as expected
-        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
-        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
-        MethodType ttype = target.type();
-        ttype = ttype.changeParameterType(0, defc); // FIXME: test this
-        assertEquals(typeWithSelf, ttype);
-        assertTrue(target.toString().contains(name));  // rough check
-        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
-        Object[] args = randomArgs(paramsWithSelf);
-        printCalled(target, name, args);
-        target.invokeVarargs(args);
-        assertCalled(name, args);
-        System.out.print(':');
-    }
-
-    @Test
-    public void testBind() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("bind");
-        testBind(Example.class, void.class, "v0");
-        testBind(Example.class, void.class, "pkg_v0");
-        testBind(Example.class, void.class, "pri_v0");
-        testBind(Example.class, Object.class, "v1", Object.class);
-        testBind(Example.class, Object.class, "v2", Object.class, Object.class);
-        testBind(Example.class, Object.class, "v2", Object.class, int.class);
-        testBind(Example.class, Object.class, "v2", int.class, Object.class);
-        testBind(Example.class, Object.class, "v2", int.class, int.class);
-        testBind(false, PRIVATE, Example.class, void.class, "bogus");
-        testBind(SubExample.class, void.class, "Sub/v0");
-        testBind(SubExample.class, void.class, "Sub/pkg_v0");
-        testBind(IntExample.Impl.class, void.class, "Int/v0");
-    }
-
-    void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        for (Object[] ac : accessCases(defc, name)) {
-            testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
-        }
-    }
-
-    void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        countTest(positive);
-        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
-        MethodType type = MethodType.methodType(ret, params);
-        Object receiver = randomArg(defc);
-        MethodHandle target = null;
-        RuntimeException noAccess = null;
-        try {
-            target = lookup.bind(receiver, methodName, type);
-        } catch (NoAccessException ex) {
-            noAccess = ex;
-        }
-        if (verbosity >= 2)
-            System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
-                    +(noAccess == null ? "" : " !! "+noAccess));
-        if (positive && noAccess != null)  throw noAccess;
-        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
-        if (!positive)  return; // negative test failed as expected
-        assertEquals(type, target.type());
-        Object[] args = randomArgs(params);
-        printCalled(target, name, args);
-        target.invokeVarargs(args);
-        Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
-        assertCalled(name, argsWithReceiver);
-        System.out.print(':');
-    }
-
-    @Test
-    public void testUnreflect() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("unreflect");
-        testUnreflect(Example.class, true, void.class, "s0");
-        testUnreflect(Example.class, true, void.class, "pkg_s0");
-        testUnreflect(Example.class, true, void.class, "pri_s0");
-
-        testUnreflect(Example.class, true, Object.class, "s1", Object.class);
-        testUnreflect(Example.class, true, Object.class, "s2", int.class);
-        //testUnreflect(Example.class, true, Object.class, "s3", long.class);
-        //testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class);
-        //testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class);
-        //testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class);
-
-        testUnreflect(Example.class, false, void.class, "v0");
-        testUnreflect(Example.class, false, void.class, "pkg_v0");
-        testUnreflect(Example.class, false, void.class, "pri_v0");
-        testUnreflect(Example.class, false, Object.class, "v1", Object.class);
-        testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
-        testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
-        testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
-        testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
-    }
-
-    void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        for (Object[] ac : accessCases(defc, name)) {
-            testUnreflect((Boolean)ac[0], (Lookup)ac[1], defc, isStatic, ret, name, params);
-        }
-    }
-    void testUnreflect(boolean positive, Lookup lookup, Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
-        countTest(positive);
-        MethodType type = MethodType.methodType(ret, params);
-        Method rmethod = null;
-        MethodHandle target = null;
-        RuntimeException noAccess = null;
-        try {
-            rmethod = defc.getDeclaredMethod(name, params);
-        } catch (NoSuchMethodException ex) {
-            throw new NoAccessException(ex);
-        }
-        assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
-        try {
-            target = lookup.unreflect(rmethod);
-        } catch (NoAccessException ex) {
-            noAccess = ex;
-        }
-        if (verbosity >= 2)
-            System.out.println("unreflect "+defc+"."+name+"/"+type+" => "+target
-                    +(noAccess == null ? "" : " !! "+noAccess));
-        if (positive && noAccess != null)  throw noAccess;
-        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
-        if (!positive)  return; // negative test failed as expected
-        Class<?>[] paramsMaybeWithSelf = params;
-        if (!isStatic) {
-            paramsMaybeWithSelf = cat(array(Class[].class, (Class)defc), params);
-        }
-        MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
-        MethodType ttype = target.type();
-        if (!isStatic)
-            ttype = ttype.changeParameterType(0, defc); // FIXME: test this
-        assertEquals(typeMaybeWithSelf, ttype);
-        Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
-        printCalled(target, name, argsMaybeWithSelf);
-        target.invokeVarargs(argsMaybeWithSelf);
-        assertCalled(name, argsMaybeWithSelf);
-        System.out.print(':');
-    }
-
-    @Test @Ignore("unimplemented")
-    public void testUnreflectSpecial() throws Throwable {
-        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
-        startTest("unreflectSpecial");
-        Method m = null;
-        MethodHandle expResult = null;
-        MethodHandle result = lookup.unreflectSpecial(m, Example.class);
-        assertEquals(expResult, result);
-        fail("The test case is a prototype.");
-    }
-
-    public static class HasFields {
-        boolean fZ = false;
-        byte fB = (byte)'B';
-        short fS = (short)'S';
-        char fC = 'C';
-        int fI = 'I';
-        long fJ = 'J';
-        float fF = 'F';
-        double fD = 'D';
-        static boolean sZ = true;
-        static byte sB = 1+(byte)'B';
-        static short sS = 1+(short)'S';
-        static char sC = 1+'C';
-        static int sI = 1+'I';
-        static long sJ = 1+'J';
-        static float sF = 1+'F';
-        static double sD = 1+'D';
-
-        Object fL = 'L';
-        String fR = "R";
-        static Object sL = 'M';
-        static String sR = "S";
-
-        static final Object[][] CASES;
-        static {
-            ArrayList<Object[]> cases = new ArrayList<Object[]>();
-            Object types[][] = {
-                {'L',Object.class}, {'R',String.class},
-                {'I',int.class}, {'J',long.class},
-                {'F',float.class}, {'D',double.class},
-                {'Z',boolean.class}, {'B',byte.class},
-                {'S',short.class}, {'C',char.class},
-            };
-            HasFields fields = new HasFields();
-            for (Object[] t : types) {
-                for (int kind = 0; kind <= 1; kind++) {
-                    boolean isStatic = (kind != 0);
-                    char btc = (Character)t[0];
-                    String name = (isStatic ? "s" : "f") + btc;
-                    Class<?> type = (Class<?>) t[1];
-                    Object value;
-                    Field field;
-                    try {
-                        field = HasFields.class.getDeclaredField(name);
-                    } catch (Exception ex) {
-                        throw new InternalError("no field HasFields."+name);
-                    }
-                    try {
-                        value = field.get(fields);
-                    } catch (Exception ex) {
-                        throw new InternalError("cannot fetch field HasFields."+name);
-                    }
-                    if (type == float.class) {
-                        float v = 'F';
-                        if (isStatic)  v++;
-                        assert(value.equals(v));
-                    }
-                    assert(name.equals(field.getName()));
-                    assert(type.equals(field.getType()));
-                    assert(isStatic == (Modifier.isStatic(field.getModifiers())));
-                    cases.add(new Object[]{ field, value });
-                }
-            }
-            CASES = cases.toArray(new Object[0][]);
-        }
-    }
-
-    @Test
-    public void testUnreflectGetter() throws Throwable {
-        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
-        startTest("unreflectGetter");
-        for (Object[] c : HasFields.CASES) {
-            Field f = (Field)c[0];
-            Object value = c[1];
-            Class<?> type = f.getType();
-            if (type.isPrimitive() && type != int.class)
-                continue;  //FIXME
-            testUnreflectGetter(lookup, f, type, value);
-        }
-    }
-    public void testUnreflectGetter(MethodHandles.Lookup lookup,
-            Field f, Class<?> type, Object value) throws Throwable {
-        countTest(true);
-        boolean isStatic = Modifier.isStatic(f.getModifiers());
-        MethodType expType = MethodType.methodType(type, HasFields.class);
-        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
-        MethodHandle mh = lookup.unreflectGetter(f);
-        assertSame(mh.type(), expType);
-        assertEquals(mh.toString(), f.getName());
-        HasFields fields = new HasFields();
-        Object sawValue;
-        Class<?> rtype = type;
-        if (type != int.class)  rtype = Object.class;
-        mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(rtype));
-        Object expValue = value;
-        for (int i = 0; i <= 1; i++) {
-            if (isStatic) {
-                if (type == int.class)
-                    sawValue = mh.<int>invoke();  // do these exactly
-                else
-                    sawValue = mh.invoke();
-            } else {
-                if (type == int.class)
-                    sawValue = mh.<int>invoke((Object) fields);
-                else
-                    sawValue = mh.invoke((Object) fields);
-            }
-            assertEquals(sawValue, expValue);
-            Object random = randomArg(type);
-            f.set(fields, random);
-            expValue = random;
-        }
-        f.set(fields, value);  // put it back
-    }
-
-
-    @Test
-    public void testUnreflectSetter() throws Throwable {
-        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
-        startTest("unreflectSetter");
-        for (Object[] c : HasFields.CASES) {
-            Field f = (Field)c[0];
-            Object value = c[1];
-            Class<?> type = f.getType();
-            if (type.isPrimitive() && type != int.class)
-                continue;  //FIXME
-            testUnreflectSetter(lookup, f, type, value);
-        }
-    }
-    public void testUnreflectSetter(MethodHandles.Lookup lookup,
-            Field f, Class<?> type, Object value) throws Throwable {
-        countTest(true);
-        boolean isStatic = Modifier.isStatic(f.getModifiers());
-        MethodType expType = MethodType.methodType(void.class, HasFields.class, type);
-        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
-        MethodHandle mh = lookup.unreflectSetter(f);
-        assertSame(mh.type(), expType);
-        assertEquals(mh.toString(), f.getName());
-        HasFields fields = new HasFields();
-        Object sawValue;
-        Class<?> vtype = type;
-        if (type != int.class)  vtype = Object.class;
-        int last = mh.type().parameterCount() - 1;
-        mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(void.class).changeParameterType(last, vtype));
-        assertEquals(f.get(fields), value);  // clean to start with
-        for (int i = 0; i <= 1; i++) {
-            Object putValue = randomArg(type);
-            if (isStatic) {
-                if (type == int.class)
-                    mh.<void>invoke((int)(Integer)putValue);  // do these exactly
-                else
-                    mh.<void>invoke(putValue);
-            } else {
-                if (type == int.class)
-                    mh.<void>invoke((Object) fields, (int)(Integer)putValue);
-                else
-                    mh.<void>invoke((Object) fields, putValue);
-            }
-            assertEquals(f.get(fields), putValue);
-        }
-        f.set(fields, value);  // put it back
-    }
-
-    @Test
-    public void testArrayElementGetter() throws Throwable {
-        startTest("arrayElementGetter");
-        testArrayElementGetterSetter(new Object[10], false);
-        testArrayElementGetterSetter(new String[10], false);
-        testArrayElementGetterSetter(new int[10], false);
-        // FIXME: Do the other primitive types.
-        //testArrayElementGetterSetter(new float[10], false);
-    }
-
-    @Test
-    public void testArrayElementSetter() throws Throwable {
-        startTest("arrayElementSetter");
-        testArrayElementGetterSetter(new Object[10], true);
-        testArrayElementGetterSetter(new String[10], true);
-        testArrayElementGetterSetter(new int[10], true);
-        // FIXME: Do the other primitive types.
-        //testArrayElementGetterSetter(new float[10], true);
-    }
-
-    public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable {
-        countTest(true);
-        Class<?> arrayType = array.getClass();
-        Class<?> elemType = arrayType.getComponentType();
-        MethodType expType = !testSetter
-                ? MethodType.methodType(elemType,   arrayType, int.class)
-                : MethodType.methodType(void.class, arrayType, int.class, elemType);
-        MethodHandle mh = !testSetter
-                ? MethodHandles.arrayElementGetter(arrayType)
-                : MethodHandles.arrayElementSetter(arrayType);
-        assertSame(mh.type(), expType);
-        //assertEquals(mh.toString(), f.getName());
-        Object sawValue, expValue;
-        List<Object> model = array2list(array);
-        int length = Array.getLength(array);
-        for (int i = 0; i < length; i++) {
-            // update array element
-            Object random = randomArg(elemType);
-            model.set(i, random);
-            if (testSetter) {
-                if (elemType == int.class)
-                    mh.<void>invoke((int[]) array, i, (int)(Integer)random);
-                else
-                    mh.invokeGeneric(array, i, random);
-                assertEquals(model, array2list(array));
-            } else {
-                Array.set(array, i, random);
-
-            }
-            // observe array element
-            sawValue = Array.get(array, i);
-            if (!testSetter) {
-                expValue = sawValue;
-                if (elemType == int.class)
-                    sawValue = mh.<int>invoke((int[]) array, i);
-                else
-                    sawValue = mh.invokeGeneric(array, i);
-                assertEquals(sawValue, expValue);
-                assertEquals(model, array2list(array));
-            }
-        }
-    }
-
-    List<Object> array2list(Object array) {
-        int length = Array.getLength(array);
-        ArrayList<Object> model = new ArrayList<Object>(length);
-        for (int i = 0; i < length; i++)
-            model.add(Array.get(array, i));
-        return model;
-    }
-
-    static class Callee {
-        static Object id() { return called("id"); }
-        static Object id(Object x) { return called("id", x); }
-        static Object id(Object x, Object y) { return called("id", x, y); }
-        static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
-        static Object id(Object... vx) { return called("id", vx); }
-        static MethodHandle ofType(int n) {
-            return ofType(Object.class, n);
-        }
-        static MethodHandle ofType(Class<?> rtype, int n) {
-            if (n == -1)
-                return ofType(MethodType.methodType(rtype, Object[].class));
-            return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
-        }
-        static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
-            return ofType(MethodType.methodType(rtype, ptypes));
-        }
-        static MethodHandle ofType(MethodType type) {
-            Class<?> rtype = type.returnType();
-            String pfx = "";
-            if (rtype != Object.class)
-                pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
-            String name = pfx+"id";
-            return PRIVATE.findStatic(Callee.class, name, type);
-        }
-    }
-
-    @Test
-    public void testConvertArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("convertArguments");
-        testConvert(Callee.ofType(1), null, "id", int.class);
-        testConvert(Callee.ofType(1), null, "id", String.class);
-        testConvert(Callee.ofType(1), null, "id", Integer.class);
-        testConvert(Callee.ofType(1), null, "id", short.class);
-    }
-
-    void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
-        testConvert(true, id, rtype, name, params);
-    }
-
-    void testConvert(boolean positive, MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
-        countTest(positive);
-        MethodType idType = id.type();
-        if (rtype == null)  rtype = idType.returnType();
-        for (int i = 0; i < params.length; i++) {
-            if (params[i] == null)  params[i] = idType.parameterType(i);
-        }
-        // simulate the pairwise conversion
-        MethodType newType = MethodType.methodType(rtype, params);
-        Object[] args = randomArgs(newType.parameterArray());
-        Object[] convArgs = args.clone();
-        for (int i = 0; i < args.length; i++) {
-            Class<?> src = newType.parameterType(i);
-            Class<?> dst = idType.parameterType(i);
-            if (src != dst)
-                convArgs[i] = castToWrapper(convArgs[i], dst);
-        }
-        Object convResult = id.invokeVarargs(convArgs);
-        {
-            Class<?> dst = newType.returnType();
-            Class<?> src = idType.returnType();
-            if (src != dst)
-                convResult = castToWrapper(convResult, dst);
-        }
-        MethodHandle target = null;
-        RuntimeException error = null;
-        try {
-            target = MethodHandles.convertArguments(id, newType);
-        } catch (RuntimeException ex) {
-            error = ex;
-        }
-        if (verbosity >= 2)
-            System.out.println("convert "+id+ " to "+newType+" => "+target
-                    +(error == null ? "" : " !! "+error));
-        if (positive && error != null)  throw error;
-        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
-        if (!positive)  return; // negative test failed as expected
-        assertEquals(newType, target.type());
-        printCalled(target, id.toString(), args);
-        Object result = target.invokeVarargs(args);
-        assertCalled(name, convArgs);
-        assertEquals(convResult, result);
-        System.out.print(':');
-    }
-
-    @Test
-    public void testPermuteArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("permuteArguments");
-        testPermuteArguments(4, Integer.class,  2, String.class,  0);
-        //testPermuteArguments(6, Integer.class,  0, null,         30);
-        //testPermuteArguments(4, Integer.class,  1, int.class,     6);
-    }
-    public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
-        if (verbosity >= 1)
-            System.out.println("permuteArguments "+max+"*"+type1.getName()
-                    +(t2c==0?"":"/"+t2c+"*"+type2.getName())
-                    +(dilution > 0 ? " with dilution "+dilution : ""));
-        int t2pos = t2c == 0 ? 0 : 1;
-        for (int inargs = t2pos+1; inargs <= max; inargs++) {
-            Class<?>[] types = new Class<?>[inargs];
-            Arrays.fill(types, type1);
-            if (t2c != 0) {
-                // Fill in a middle range with type2:
-                Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
-            }
-            Object[] args = randomArgs(types);
-            int numcases = 1;
-            for (int outargs = 0; outargs <= max; outargs++) {
-                if (outargs - inargs >= MAX_ARG_INCREASE)  continue;
-                int[] reorder = new int[outargs];
-                int casStep = dilution + 1;
-                // Avoid some common factors:
-                while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
-                       (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
-                    casStep++;
-                for (int cas = 0; cas < numcases; cas += casStep) {
-                    for (int i = 0, c = cas; i < outargs; i++) {
-                        reorder[i] = c % inargs;
-                        c /= inargs;
-                    }
-                    testPermuteArguments(args, types, reorder);
-                }
-                numcases *= inargs;
-                if (dilution > 10 && outargs >= 4) {
-                    // Do some special patterns, which we probably missed.
-                    // Replication of a single argument or argument pair.
-                    for (int i = 0; i < inargs; i++) {
-                        Arrays.fill(reorder, i);
-                        testPermuteArguments(args, types, reorder);
-                        for (int d = 1; d <= 2; d++) {
-                            if (i + d >= inargs)  continue;
-                            for (int j = 1; j < outargs; j += 2)
-                                reorder[j] += 1;
-                            testPermuteArguments(args, types, reorder);
-                            testPermuteArguments(args, types, reverse(reorder));
-                        }
-                    }
-                    // Repetition of a sequence of 3 or more arguments.
-                    for (int i = 1; i < inargs; i++) {
-                        for (int len = 3; len <= inargs; len++) {
-                            for (int j = 0; j < outargs; j++)
-                                reorder[j] = (i + (j % len)) % inargs;
-                            testPermuteArguments(args, types, reorder);
-                            testPermuteArguments(args, types, reverse(reorder));
-                        }
-                    }
-                }
-            }
-        }
-    }
-
-    static int[] reverse(int[] reorder) {
-        reorder = reorder.clone();
-        for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
-            int j = reorder.length - 1 - i;
-            int tem = reorder[i];
-            reorder[i] = reorder[j];
-            reorder[j] = tem;
-        }
-        return reorder;
-    }
-
-    void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
-        countTest();
-        if (args == null && types == null) {
-            int max = 0;
-            for (int j : reorder) {
-                if (max < j)  max = j;
-            }
-            args = randomArgs(max+1, Integer.class);
-        }
-        if (args == null) {
-            args = randomArgs(types);
-        }
-        if (types == null) {
-            types = new Class<?>[args.length];
-            for (int i = 0; i < args.length; i++)
-                types[i] = args[i].getClass();
-        }
-        int inargs = args.length, outargs = reorder.length;
-        assert(inargs == types.length);
-        if (verbosity >= 2)
-            System.out.println("permuteArguments "+Arrays.toString(reorder));
-        Object[] permArgs = new Object[outargs];
-        Class<?>[] permTypes = new Class<?>[outargs];
-        for (int i = 0; i < outargs; i++) {
-            permArgs[i] = args[reorder[i]];
-            permTypes[i] = types[reorder[i]];
-        }
-        if (verbosity >= 3) {
-            System.out.println("in args:   "+Arrays.asList(args));
-            System.out.println("out args:  "+Arrays.asList(permArgs));
-            System.out.println("in types:  "+Arrays.asList(types));
-            System.out.println("out types: "+Arrays.asList(permTypes));
-        }
-        MethodType inType  = MethodType.methodType(Object.class, types);
-        MethodType outType = MethodType.methodType(Object.class, permTypes);
-        MethodHandle target = MethodHandles.convertArguments(ValueConversions.varargsList(outargs), outType);
-        MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
-        Object result = newTarget.invokeVarargs(args);
-        Object expected = Arrays.asList(permArgs);
-        assertEquals(expected, result);
-    }
-
-
-    @Test
-    public void testSpreadArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("spreadArguments");
-        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
-            if (verbosity >= 2)
-                System.out.println("spreadArguments "+argType);
-            for (int nargs = 0; nargs < 10; nargs++) {
-                if (argType == int.class && nargs >= 6)  continue; // FIXME Fail_1
-                for (int pos = 0; pos < nargs; pos++) {
-                    if (argType == int.class && pos > 0)  continue; // FIXME Fail_3
-                    testSpreadArguments(argType, pos, nargs);
-                }
-            }
-        }
-    }
-    public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable {
-        countTest();
-        MethodHandle target = ValueConversions.varargsArray(nargs);
-        MethodHandle target2 = changeArgTypes(target, argType);
-        if (verbosity >= 2)
-            System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
-        Object[] args = randomArgs(target2.type().parameterArray());
-        // make sure the target does what we think it does:
-        if (pos == 0 && nargs < 5) {
-            Object[] check = (Object[]) target.invokeVarargs(args);
-            assertArrayEquals(args, check);
-            switch (nargs) {
-                case 0:
-                    check = target.<Object[]>invoke();
-                    assertArrayEquals(args, check);
-                    break;
-                case 1:
-                    check = target.<Object[]>invoke(args[0]);
-                    assertArrayEquals(args, check);
-                    break;
-                case 2:
-                    check = target.<Object[]>invoke(args[0], args[1]);
-                    assertArrayEquals(args, check);
-                    break;
-            }
-        }
-        List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList());
-        {   // modify newParams in place
-            List<Class<?>> spreadParams = newParams.subList(pos, nargs);
-            spreadParams.clear(); spreadParams.add(Object[].class);
-        }
-        MethodType newType = MethodType.methodType(Object.class, newParams);
-        MethodHandle result = MethodHandles.spreadArguments(target2, newType);
-        Object[] returnValue;
-        if (pos == 0) {
-            returnValue = (Object[]) result.invoke(args);
-        } else {
-            Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
-            args1[pos] = Arrays.copyOfRange(args, pos, args.length);
-            returnValue = (Object[]) result.invokeVarargs(args1);
-        }
-        assertArrayEquals(args, returnValue);
-    }
-
-    @Test
-    public void testCollectArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("collectArguments");
-        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
-            if (verbosity >= 2)
-                System.out.println("collectArguments "+argType);
-            for (int nargs = 0; nargs < 10; nargs++) {
-                for (int pos = 0; pos < nargs; pos++) {
-                    if (argType == int.class)  continue; // FIXME Fail_4
-                    testCollectArguments(argType, pos, nargs);
-                }
-            }
-        }
-    }
-    public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable {
-        countTest();
-        // fake up a MH with the same type as the desired adapter:
-        MethodHandle fake = ValueConversions.varargsArray(nargs);
-        fake = changeArgTypes(fake, argType);
-        MethodType newType = fake.type();
-        Object[] args = randomArgs(newType.parameterArray());
-        // here is what should happen:
-        Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
-        collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
-        // here is the MH which will witness the collected argument tail:
-        MethodHandle target = ValueConversions.varargsArray(pos+1);
-        target = changeArgTypes(target, 0, pos, argType);
-        target = changeArgTypes(target, pos, pos+1, Object[].class);
-        if (verbosity >= 2)
-            System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
-        MethodHandle result = MethodHandles.collectArguments(target, newType);
-        Object[] returnValue = (Object[]) result.invokeVarargs(args);
-//        assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
-//        returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
-//        collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
-        assertArrayEquals(collectedArgs, returnValue);
-    }
-
-    @Test
-    public void testInsertArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("insertArguments");
-        for (int nargs = 0; nargs <= 4; nargs++) {
-            for (int ins = 0; ins <= 4; ins++) {
-                if (ins > MAX_ARG_INCREASE)  continue;  // FIXME Fail_6
-                for (int pos = 0; pos <= nargs; pos++) {
-                    testInsertArguments(nargs, pos, ins);
-                }
-            }
-        }
-    }
-
-    void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
-        countTest();
-        MethodHandle target = ValueConversions.varargsArray(nargs + ins);
-        Object[] args = randomArgs(target.type().parameterArray());
-        List<Object> resList = Arrays.asList(args);
-        List<Object> argsToPass = new ArrayList<Object>(resList);
-        List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
-        if (verbosity >= 2)
-            System.out.println("insert: "+argsToInsert+" into "+target);
-        MethodHandle target2 = MethodHandles.insertArguments(target, pos,
-                (Object[]) argsToInsert.toArray());
-        argsToInsert.clear();  // remove from argsToInsert
-        Object res2 = target2.invokeVarargs(argsToPass);
-        Object res2List = Arrays.asList((Object[])res2);
-        if (verbosity >= 2)
-            System.out.println("result: "+res2List);
-        //if (!resList.equals(res2List))
-        //    System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
-        assertEquals(resList, res2List);
-    }
-
-    @Test
-    public void testFilterArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("filterArguments");
-        for (int nargs = 1; nargs <= 6; nargs++) {
-            for (int pos = 0; pos < nargs; pos++) {
-                testFilterArguments(nargs, pos);
-            }
-        }
-    }
-
-    void testFilterArguments(int nargs, int pos) throws Throwable {
-        countTest();
-        MethodHandle target = ValueConversions.varargsList(nargs);
-        MethodHandle filter = ValueConversions.varargsList(1);
-        filter = MethodHandles.convertArguments(filter, filter.type().generic());
-        Object[] argsToPass = randomArgs(nargs, Object.class);
-        if (verbosity >= 2)
-            System.out.println("filter "+target+" at "+pos+" with "+filter);
-        MethodHandle[] filters = new MethodHandle[pos*2+1];
-        filters[pos] = filter;
-        MethodHandle target2 = MethodHandles.filterArguments(target, filters);
-        // Simulate expected effect of filter on arglist:
-        Object[] filteredArgs = argsToPass.clone();
-        filteredArgs[pos] = filter.invoke(filteredArgs[pos]);
-        List<Object> expected = Arrays.asList(filteredArgs);
-        Object result = target2.invokeVarargs(argsToPass);
-        if (verbosity >= 2)
-            System.out.println("result: "+result);
-        if (!expected.equals(result))
-            System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+argsToPass+" => "+result);
-        assertEquals(expected, result);
-    }
-
-    @Test
-    public void testFoldArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("foldArguments");
-        for (int nargs = 0; nargs <= 4; nargs++) {
-            for (int fold = 0; fold <= nargs; fold++) {
-                for (int pos = 0; pos <= nargs; pos++) {
-                    testFoldArguments(nargs, pos, fold);
-                }
-            }
-        }
-    }
-
-    void testFoldArguments(int nargs, int pos, int fold) throws Throwable {
-        if (pos != 0)  return;  // can fold only at pos=0 for now
-        countTest();
-        MethodHandle target = ValueConversions.varargsList(1 + nargs);
-        MethodHandle combine = ValueConversions.varargsList(fold);
-        List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class));
-        if (verbosity >= 2)
-            System.out.println("fold "+target+" with "+combine);
-        MethodHandle target2 = MethodHandles.foldArguments(target, combine);
-        // Simulate expected effect of combiner on arglist:
-        List<Object> expected = new ArrayList<Object>(argsToPass);
-        List<Object> argsToFold = expected.subList(pos, pos + fold);
-        if (verbosity >= 2)
-            System.out.println("fold: "+argsToFold+" into "+target2);
-        Object foldedArgs = combine.invokeVarargs(argsToFold);
-        argsToFold.add(0, foldedArgs);
-        Object result = target2.invokeVarargs(argsToPass);
-        if (verbosity >= 2)
-            System.out.println("result: "+result);
-        if (!expected.equals(result))
-            System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result);
-        assertEquals(expected, result);
-    }
-
-    @Test
-    public void testDropArguments() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("dropArguments");
-        for (int nargs = 0; nargs <= 4; nargs++) {
-            for (int drop = 1; drop <= 4; drop++) {
-                for (int pos = 0; pos <= nargs; pos++) {
-                    testDropArguments(nargs, pos, drop);
-                }
-            }
-        }
-    }
-
-    void testDropArguments(int nargs, int pos, int drop) throws Throwable {
-        countTest();
-        MethodHandle target = ValueConversions.varargsArray(nargs);
-        Object[] args = randomArgs(target.type().parameterArray());
-        MethodHandle target2 = MethodHandles.dropArguments(target, pos,
-                Collections.nCopies(drop, Object.class).toArray(new Class[0]));
-        List<Object> resList = Arrays.asList(args);
-        List<Object> argsToDrop = new ArrayList<Object>(resList);
-        for (int i = drop; i > 0; i--) {
-            argsToDrop.add(pos, "blort#"+i);
-        }
-        Object res2 = target2.invokeVarargs(argsToDrop);
-        Object res2List = Arrays.asList((Object[])res2);
-        //if (!resList.equals(res2List))
-        //    System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
-        assertEquals(resList, res2List);
-    }
-
-    @Test
-    public void testInvokers() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
-        // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
-        Set<MethodType> done = new HashSet<MethodType>();
-        for (int i = 0; i <= 6; i++) {
-            MethodType gtype = MethodType.genericMethodType(i);
-            for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
-                for (int j = -1; j < i; j++) {
-                    MethodType type = gtype;
-                    if (j < 0)
-                        type = type.changeReturnType(argType);
-                    else if (argType == void.class)
-                        continue;
-                    else
-                        type = type.changeParameterType(j, argType);
-                    if (argType.isPrimitive() && j != i-1)  continue; // FIXME Fail_5
-                    if (done.add(type))
-                        testInvokers(type);
-                    MethodType vtype = type.changeReturnType(void.class);
-                    if (done.add(vtype))
-                        testInvokers(vtype);
-                }
-            }
-        }
-    }
-
-    public void testInvokers(MethodType type) throws Throwable {
-        if (verbosity >= 2)
-            System.out.println("test invokers for "+type);
-        int nargs = type.parameterCount();
-        boolean testRetCode = type.returnType() != void.class;
-        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
-                                MethodType.genericMethodType(0, true));
-        target = MethodHandles.collectArguments(target, type);
-        Object[] args = randomArgs(type.parameterArray());
-        List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args));
-        targetPlusArgs.add(0, target);
-        int code = (Integer) invokee(args);
-        Object log = logEntry("invokee", args);
-        assertEquals(log.hashCode(), code);
-        assertCalled("invokee", args);
-        MethodHandle inv;
-        Object result;
-        // exact invoker
-        countTest();
-        calledLog.clear();
-        inv = MethodHandles.exactInvoker(type);
-        result = inv.invokeVarargs(targetPlusArgs);
-        if (testRetCode)  assertEquals(code, result);
-        assertCalled("invokee", args);
-        // generic invoker
-        countTest();
-        inv = MethodHandles.genericInvoker(type);
-        if (nargs <= 3) {
-            calledLog.clear();
-            switch (nargs) {
-            case 0:
-                result = inv.invoke(target);
-                break;
-            case 1:
-                result = inv.invoke(target, args[0]);
-                break;
-            case 2:
-                result = inv.invoke(target, args[0], args[1]);
-                break;
-            case 3:
-                result = inv.invoke(target, args[0], args[1], args[2]);
-                break;
-            }
-            if (testRetCode)  assertEquals(code, result);
-            assertCalled("invokee", args);
-        }
-        calledLog.clear();
-        result = inv.invokeVarargs(targetPlusArgs);
-        if (testRetCode)  assertEquals(code, result);
-        assertCalled("invokee", args);
-        // varargs invoker #0
-        calledLog.clear();
-        inv = MethodHandles.varargsInvoker(type, 0);
-        result = inv.invoke(target, args);
-        if (testRetCode)  assertEquals(code, result);
-        assertCalled("invokee", args);
-        if (nargs >= 1) {
-            // varargs invoker #1
-            calledLog.clear();
-            inv = MethodHandles.varargsInvoker(type, 1);
-            result = inv.invoke(target, args[0], Arrays.copyOfRange(args, 1, nargs));
-            if (testRetCode)  assertEquals(code, result);
-            assertCalled("invokee", args);
-        }
-        if (nargs >= 2) {
-            // varargs invoker #2
-            calledLog.clear();
-            inv = MethodHandles.varargsInvoker(type, 2);
-            result = inv.invoke(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
-            if (testRetCode)  assertEquals(code, result);
-            assertCalled("invokee", args);
-        }
-        if (nargs >= 3) {
-            // varargs invoker #3
-            calledLog.clear();
-            inv = MethodHandles.varargsInvoker(type, 3);
-            result = inv.invoke(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
-            if (testRetCode)  assertEquals(code, result);
-            assertCalled("invokee", args);
-        }
-        for (int k = 0; k <= nargs; k++) {
-            // varargs invoker #0..N
-            countTest();
-            calledLog.clear();
-            inv = MethodHandles.varargsInvoker(type, k);
-            List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs);
-            List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
-            Object[] tail = tailList.toArray();
-            tailList.clear(); tailList.add(tail);
-            result = inv.invokeVarargs(targetPlusVarArgs);
-            if (testRetCode)  assertEquals(code, result);
-            assertCalled("invokee", args);
-        }
-        // dynamic invoker
-        countTest();
-        CallSite site = new CallSite(MethodHandlesTest.class, "foo", type);
-        inv = MethodHandles.dynamicInvoker(site);
-        site.setTarget(target);
-        calledLog.clear();
-        result = inv.invokeVarargs(args);
-        if (testRetCode)  assertEquals(code, result);
-        assertCalled("invokee", args);
-    }
-
-    static Object invokee(Object... args) {
-        return called("invokee", args).hashCode();
-    }
-
-    private static final String MISSING_ARG = "missingArg";
-    static Object targetIfEquals() {
-        return called("targetIfEquals");
-    }
-    static Object fallbackIfNotEquals() {
-        return called("fallbackIfNotEquals");
-    }
-    static Object targetIfEquals(Object x) {
-        assertEquals(x, MISSING_ARG);
-        return called("targetIfEquals", x);
-    }
-    static Object fallbackIfNotEquals(Object x) {
-        assertFalse(x.toString(), x.equals(MISSING_ARG));
-        return called("fallbackIfNotEquals", x);
-    }
-    static Object targetIfEquals(Object x, Object y) {
-        assertEquals(x, y);
-        return called("targetIfEquals", x, y);
-    }
-    static Object fallbackIfNotEquals(Object x, Object y) {
-        assertFalse(x.toString(), x.equals(y));
-        return called("fallbackIfNotEquals", x, y);
-    }
-    static Object targetIfEquals(Object x, Object y, Object z) {
-        assertEquals(x, y);
-        return called("targetIfEquals", x, y, z);
-    }
-    static Object fallbackIfNotEquals(Object x, Object y, Object z) {
-        assertFalse(x.toString(), x.equals(y));
-        return called("fallbackIfNotEquals", x, y, z);
-    }
-
-    @Test
-    public void testGuardWithTest() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("guardWithTest");
-        for (int nargs = 0; nargs <= 3; nargs++) {
-            if (nargs != 2)  continue;  // FIXME: test more later
-            testGuardWithTest(nargs, Object.class);
-            testGuardWithTest(nargs, String.class);
-        }
-    }
-    void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
-        countTest();
-        MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
-        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
-        MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
-        while (test.type().parameterCount() < nargs)
-            test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
-        while (test.type().parameterCount() > nargs)
-            test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
-        if (argClass != Object.class) {
-            test = changeArgTypes(test, argClass);
-            target = changeArgTypes(target, argClass);
-            fallback = changeArgTypes(fallback, argClass);
-        }
-        MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
-        assertEquals(target.type(), mh.type());
-        Object[][] argLists = {
-            { },
-            { "foo" }, { MISSING_ARG },
-            { "foo", "foo" }, { "foo", "bar" },
-            { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
-        };
-        for (Object[] argList : argLists) {
-            if (argList.length != nargs)  continue;
-            boolean equals;
-            switch (nargs) {
-            case 0:   equals = true; break;
-            case 1:   equals = MISSING_ARG.equals(argList[0]); break;
-            default:  equals = argList[0].equals(argList[1]); break;
-            }
-            String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
-            if (verbosity >= 2)
-                System.out.println(logEntry(willCall, argList));
-            Object result = mh.invokeVarargs(argList);
-            assertCalled(willCall, argList);
-        }
-    }
-
-    @Test
-    public void testCatchException() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("catchException");
-        for (int nargs = 2; nargs <= 6; nargs++) {
-            for (int ti = 0; ti <= 1; ti++) {
-                boolean throwIt = (ti != 0);
-                testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs);
-                testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs);
-                testCatchException(String.class, new LinkageError("testing"), throwIt, nargs);
-            }
-        }
-    }
-
-    private static <T extends Throwable>
-    Object throwOrReturn(Object normal, T exception) throws T {
-        if (exception != null)  throw exception;
-        return normal;
-    }
-
-    void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
-        countTest();
-        if (verbosity >= 2)
-            System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs);
-        Class<? extends Throwable> exType = thrown.getClass();
-        MethodHandle throwOrReturn
-                = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn",
-                    MethodType.methodType(Object.class, Object.class, Throwable.class));
-        MethodHandle thrower = throwOrReturn;
-        while (thrower.type().parameterCount() < nargs)
-            thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class);
-        MethodHandle target = MethodHandles.catchException(thrower,
-                thrown.getClass(), ValueConversions.varargsList(1+nargs));
-        assertEquals(thrower.type(), target.type());
-        //System.out.println("catching with "+target+" : "+throwOrReturn);
-        Object[] args = randomArgs(nargs, Object.class);
-        args[1] = (throwIt ? thrown : null);
-        Object returned = target.invokeVarargs(args);
-        //System.out.println("return from "+target+" : "+returned);
-        if (!throwIt) {
-            assertSame(args[0], returned);
-        } else {
-            List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args));
-            catchArgs.add(0, thrown);
-            assertEquals(catchArgs, returned);
-        }
-    }
-
-    @Test
-    public void testThrowException() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("throwException");
-        testThrowException(int.class, new ClassCastException("testing"));
-        testThrowException(void.class, new java.io.IOException("testing"));
-        testThrowException(String.class, new LinkageError("testing"));
-    }
-
-    void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
-        countTest();
-        Class<? extends Throwable> exType = thrown.getClass();
-        MethodHandle target = MethodHandles.throwException(returnType, exType);
-        //System.out.println("throwing with "+target+" : "+thrown);
-        MethodType expectedType = MethodType.methodType(returnType, exType);
-        assertEquals(expectedType, target.type());
-        Throwable caught = null;
-        try {
-            Object res = target.invokeGeneric(thrown);
-            fail("got "+res+" instead of throwing "+thrown);
-        } catch (Throwable ex) {
-            if (ex != thrown) {
-                if (ex instanceof Error)  throw (Error)ex;
-                if (ex instanceof RuntimeException)  throw (RuntimeException)ex;
-            }
-            caught = ex;
-        }
-        assertSame(thrown, caught);
-    }
-
-    @Test
-    public void testCastFailure() throws Throwable {
-        if (CAN_SKIP_WORKING)  return;
-        startTest("testCastFailure");
-        testCastFailure("cast/argument", 11000);
-        testCastFailure("unbox/argument", 11000);
-        testCastFailure("cast/return", 11000);
-        testCastFailure("unbox/return", 11000);
-    }
-
-    static class Surprise extends JavaMethodHandle {
-        Surprise() { super("value"); }
-        Object value(Object x) {
-            trace("value", x);
-            if (boo != null)  return boo;
-            return x;
-        }
-        Object boo;
-        void boo(Object x) { boo = x; }
-
-        static void trace(String x, Object y) {
-            if (verbosity > 8) System.out.println(x+"="+y);
-        }
-        static Object  refIdentity(Object x)  { trace("ref.x", x); return x; }
-        static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
-        static int     intIdentity(int x)     { trace("int.x", x); return x; }
-        static MethodHandle REF_IDENTITY = PRIVATE.findStatic(
-                Surprise.class, "refIdentity",
-                    MethodType.methodType(Object.class, Object.class));
-        static MethodHandle BOX_IDENTITY = PRIVATE.findStatic(
-                Surprise.class, "boxIdentity",
-                    MethodType.methodType(Integer.class, Integer.class));
-        static MethodHandle INT_IDENTITY = PRIVATE.findStatic(
-                Surprise.class, "intIdentity",
-                    MethodType.methodType(int.class, int.class));
-    }
-
-    void testCastFailure(String mode, int okCount) throws Throwable {
-        countTest(false);
-        if (verbosity > 1)  System.out.println("mode="+mode);
-        Surprise boo = new Surprise();
-        MethodHandle identity = Surprise.REF_IDENTITY, surprise = boo;
-        if (mode.endsWith("/return")) {
-            if (mode.equals("unbox/return")) {
-                // fail on return to ((Integer)surprise).intValue
-                surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(int.class, Object.class));
-                identity = MethodHandles.convertArguments(identity, MethodType.methodType(int.class, Object.class));
-            } else if (mode.equals("cast/return")) {
-                // fail on return to (Integer)surprise
-                surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(Integer.class, Object.class));
-                identity = MethodHandles.convertArguments(identity, MethodType.methodType(Integer.class, Object.class));
-            }
-        } else if (mode.endsWith("/argument")) {
-            MethodHandle callee = null;
-            if (mode.equals("unbox/argument")) {
-                // fail on handing surprise to int argument
-                callee = Surprise.INT_IDENTITY;
-            } else if (mode.equals("cast/argument")) {
-                // fail on handing surprise to Integer argument
-                callee = Surprise.BOX_IDENTITY;
-            }
-            if (callee != null) {
-                callee = MethodHandles.convertArguments(callee, MethodType.genericMethodType(1));
-                surprise = MethodHandles.filterArguments(callee, surprise);
-                identity = MethodHandles.filterArguments(callee, identity);
-            }
-        }
-        assertNotSame(mode, surprise, boo);
-        identity = MethodHandles.convertArguments(identity, MethodType.genericMethodType(1));
-        surprise = MethodHandles.convertArguments(surprise, MethodType.genericMethodType(1));
-        Object x = 42;
-        for (int i = 0; i < okCount; i++) {
-            Object y = identity.invoke(x);
-            assertEquals(x, y);
-            Object z = surprise.invoke(x);
-            assertEquals(x, z);
-        }
-        boo.boo("Boo!");
-        Object y = identity.invoke(x);
-        assertEquals(x, y);
-        try {
-            Object z = surprise.invoke(x);
-            System.out.println("Failed to throw; got z="+z);
-            assertTrue(false);
-        } catch (Exception ex) {
-            if (verbosity > 1)
-                System.out.println("caught "+ex);
-            if (verbosity > 2)
-                ex.printStackTrace();
-            assertTrue(ex instanceof ClassCastException
-                    // FIXME: accept only one of the two for any given unit test
-                    || ex instanceof WrongMethodTypeException
-                    );
-        }
-    }
-
-}
-// Local abbreviated copy of sun.dyn.util.ValueConversions
-class ValueConversions {
-    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
-    private static final Object[] NO_ARGS_ARRAY = {};
-    private static Object[] makeArray(Object... args) { return args; }
-    private static Object[] array() { return NO_ARGS_ARRAY; }
-    private static Object[] array(Object a0)
-                { return makeArray(a0); }
-    private static Object[] array(Object a0, Object a1)
-                { return makeArray(a0, a1); }
-    private static Object[] array(Object a0, Object a1, Object a2)
-                { return makeArray(a0, a1, a2); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
-                { return makeArray(a0, a1, a2, a3); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
-                                  Object a4)
-                { return makeArray(a0, a1, a2, a3, a4); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
-                                  Object a4, Object a5)
-                { return makeArray(a0, a1, a2, a3, a4, a5); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
-                                  Object a4, Object a5, Object a6)
-                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
-                                  Object a4, Object a5, Object a6, Object a7)
-                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
-                                  Object a4, Object a5, Object a6, Object a7,
-                                  Object a8)
-                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
-    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
-                                  Object a4, Object a5, Object a6, Object a7,
-                                  Object a8, Object a9)
-                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
-    static MethodHandle[] makeArrays() {
-        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
-        MethodHandles.Lookup lookup = IMPL_LOOKUP;
-        for (;;) {
-            int nargs = arrays.size();
-            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
-            String name = "array";
-            MethodHandle array = null;
-            try {
-                array = lookup.findStatic(ValueConversions.class, name, type);
-            } catch (NoAccessException ex) {
-            }
-            if (array == null)  break;
-            arrays.add(array);
-        }
-        assert(arrays.size() == 11);  // current number of methods
-        return arrays.toArray(new MethodHandle[0]);
-    }
-    static final MethodHandle[] ARRAYS = makeArrays();
-
-    /** Return a method handle that takes the indicated number of Object
-     *  arguments and returns an Object array of them, as if for varargs.
-     */
-    public static MethodHandle varargsArray(int nargs) {
-        if (nargs < ARRAYS.length)
-            return ARRAYS[nargs];
-        // else need to spin bytecode or do something else fancy
-        throw new UnsupportedOperationException("NYI");
-    }
-
-    private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
-    private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
-    private static List<Object> list() { return NO_ARGS_LIST; }
-    private static List<Object> list(Object a0)
-                { return makeList(a0); }
-    private static List<Object> list(Object a0, Object a1)
-                { return makeList(a0, a1); }
-    private static List<Object> list(Object a0, Object a1, Object a2)
-                { return makeList(a0, a1, a2); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
-                { return makeList(a0, a1, a2, a3); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
-                                     Object a4)
-                { return makeList(a0, a1, a2, a3, a4); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
-                                     Object a4, Object a5)
-                { return makeList(a0, a1, a2, a3, a4, a5); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
-                                     Object a4, Object a5, Object a6)
-                { return makeList(a0, a1, a2, a3, a4, a5, a6); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
-                                     Object a4, Object a5, Object a6, Object a7)
-                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
-                                     Object a4, Object a5, Object a6, Object a7,
-                                     Object a8)
-                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
-    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
-                                     Object a4, Object a5, Object a6, Object a7,
-                                     Object a8, Object a9)
-                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
-    static MethodHandle[] makeLists() {
-        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
-        MethodHandles.Lookup lookup = IMPL_LOOKUP;
-        for (;;) {
-            int nargs = arrays.size();
-            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
-            String name = "list";
-            MethodHandle array = null;
-            try {
-                array = lookup.findStatic(ValueConversions.class, name, type);
-            } catch (NoAccessException ex) {
-            }
-            if (array == null)  break;
-            arrays.add(array);
-        }
-        assert(arrays.size() == 11);  // current number of methods
-        return arrays.toArray(new MethodHandle[0]);
-    }
-    static final MethodHandle[] LISTS = makeLists();
-
-    /** Return a method handle that takes the indicated number of Object
-     *  arguments and returns List.
-     */
-    public static MethodHandle varargsList(int nargs) {
-        if (nargs < LISTS.length)
-            return LISTS[nargs];
-        // else need to spin bytecode or do something else fancy
-        throw new UnsupportedOperationException("NYI");
-    }
-}
-// This guy tests access from outside the same package member, but inside
-// the package itself.
-class PackageSibling {
-    static Lookup lookup() {
-        return MethodHandles.lookup();
-    }
-}