1 /*
   2  * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package test.java.lang.invoke;
  25 
  26 import org.junit.*;
  27 import test.java.lang.invoke.remote.RemoteExample;
  28 
  29 import java.lang.invoke.MethodHandle;
  30 import java.lang.invoke.MethodHandles;
  31 import java.lang.invoke.MethodHandles.Lookup;
  32 import java.lang.invoke.MethodType;
  33 import java.lang.reflect.Array;
  34 import java.lang.reflect.Field;
  35 import java.lang.reflect.Modifier;
  36 import java.util.ArrayList;
  37 import java.util.Arrays;
  38 import java.util.Collection;
  39 import java.util.Collections;
  40 import java.util.List;
  41 
  42 import static org.junit.Assert.*;
  43 
  44 /**
  45  *
  46  * @author jrose
  47  */
  48 public abstract class MethodHandlesTest {
  49 
  50     static final Class<?> THIS_CLASS = MethodHandlesTest.class;
  51     // How much output?
  52     static int verbosity = 0;
  53 
  54     static {
  55         String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
  56         if (vstr == null)
  57             vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
  58         if (vstr != null)  verbosity = Integer.parseInt(vstr);
  59     }
  60 
  61     // Set this true during development if you want to fast-forward to
  62     // a particular new, non-working test.  Tests which are known to
  63     // work (or have recently worked) test this flag and return on true.
  64     static final boolean CAN_SKIP_WORKING;
  65 
  66     static {
  67         String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".CAN_SKIP_WORKING");
  68         if (vstr == null)
  69             vstr = System.getProperty(THIS_CLASS.getName()+".CAN_SKIP_WORKING");
  70         CAN_SKIP_WORKING = Boolean.parseBoolean(vstr);
  71     }
  72 
  73     // Set 'true' to do about 15x fewer tests, especially those redundant with RicochetTest.
  74     // This might be useful with -Xcomp stress tests that compile all method handles.
  75     static boolean CAN_TEST_LIGHTLY = Boolean.getBoolean(THIS_CLASS.getName()+".CAN_TEST_LIGHTLY");
  76 
  77     static final int MAX_ARG_INCREASE = 3;
  78 
  79     String testName;
  80     static int allPosTests, allNegTests;
  81     int posTests, negTests;
  82 
  83     @After
  84     public void printCounts() {
  85         if (verbosity >= 2 && (posTests | negTests) != 0) {
  86             System.out.println();
  87             if (posTests != 0)  System.out.println("=== "+testName+": "+posTests+" positive test cases run");
  88             if (negTests != 0)  System.out.println("=== "+testName+": "+negTests+" negative test cases run");
  89             allPosTests += posTests;
  90             allNegTests += negTests;
  91             posTests = negTests = 0;
  92         }
  93     }
  94 
  95     void countTest(boolean positive) {
  96         if (positive) ++posTests;
  97         else          ++negTests;
  98     }
  99 
 100     void countTest() { countTest(true); }
 101 
 102     void startTest(String name) {
 103         if (testName != null)  printCounts();
 104         if (verbosity >= 1)
 105             System.out.println(name);
 106         posTests = negTests = 0;
 107         testName = name;
 108     }
 109 
 110     @BeforeClass
 111     public static void setUpClass() throws Exception {
 112         calledLog.clear();
 113         calledLog.add(null);
 114         nextArgVal = INITIAL_ARG_VAL;
 115     }
 116 
 117     @AfterClass
 118     public static void tearDownClass() throws Exception {
 119         int posTests = allPosTests, negTests = allNegTests;
 120         if (verbosity >= 0 && (posTests | negTests) != 0) {
 121             System.out.println();
 122             if (posTests != 0)  System.out.println("=== "+posTests+" total positive test cases");
 123             if (negTests != 0)  System.out.println("=== "+negTests+" total negative test cases");
 124         }
 125     }
 126 
 127     static List<Object> calledLog = new ArrayList<>();
 128 
 129     static Object logEntry(String name, Object... args) {
 130         return Arrays.asList(name, Arrays.asList(args));
 131     }
 132 
 133     public static Object called(String name, Object... args) {
 134         Object entry = logEntry(name, args);
 135         calledLog.add(entry);
 136         return entry;
 137     }
 138 
 139     static void assertCalled(String name, Object... args) {
 140         Object expected = logEntry(name, args);
 141         Object actual   = calledLog.get(calledLog.size() - 1);
 142         if (expected.equals(actual) && verbosity < 9)  return;
 143         System.out.println("assertCalled "+name+":");
 144         System.out.println("expected:   "+deepToString(expected));
 145         System.out.println("actual:     "+actual);
 146         System.out.println("ex. types:  "+getClasses(expected));
 147         System.out.println("act. types: "+getClasses(actual));
 148         assertEquals("previous method call", expected, actual);
 149     }
 150 
 151     static void printCalled(MethodHandle target, String name, Object... args) {
 152         if (verbosity >= 3)
 153             System.out.println("calling MH="+target+" to "+name+deepToString(args));
 154     }
 155 
 156     static String deepToString(Object x) {
 157         if (x == null)  return "null";
 158         if (x instanceof Collection)
 159             x = ((Collection)x).toArray();
 160         if (x instanceof Object[]) {
 161             Object[] ax = (Object[]) x;
 162             ax = Arrays.copyOf(ax, ax.length, Object[].class);
 163             for (int i = 0; i < ax.length; i++)
 164                 ax[i] = deepToString(ax[i]);
 165             x = Arrays.deepToString(ax);
 166         }
 167         if (x.getClass().isArray())
 168             try {
 169                 x = Arrays.class.getMethod("toString", x.getClass()).invoke(null, x);
 170             } catch (ReflectiveOperationException ex) { throw new Error(ex); }
 171         assert(!(x instanceof Object[]));
 172         return x.toString();
 173     }
 174 
 175     static Object castToWrapper(Object value, Class<?> dst) {
 176         Object wrap = null;
 177         if (value instanceof Number)
 178             wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
 179         if (value instanceof Character)
 180             wrap = castToWrapperOrNull((char)(Character)value, dst);
 181         if (wrap != null)  return wrap;
 182         return dst.cast(value);
 183     }
 184 
 185     @SuppressWarnings("cast")  // primitive cast to (long) is part of the pattern
 186     static Object castToWrapperOrNull(long value, Class<?> dst) {
 187         if (dst == int.class || dst == Integer.class)
 188             return (int)(value);
 189         if (dst == long.class || dst == Long.class)
 190             return (long)(value);
 191         if (dst == char.class || dst == Character.class)
 192             return (char)(value);
 193         if (dst == short.class || dst == Short.class)
 194             return (short)(value);
 195         if (dst == float.class || dst == Float.class)
 196             return (float)(value);
 197         if (dst == double.class || dst == Double.class)
 198             return (double)(value);
 199         if (dst == byte.class || dst == Byte.class)
 200             return (byte)(value);
 201         if (dst == boolean.class || dst == boolean.class)
 202             return ((value % 29) & 1) == 0;
 203         return null;
 204     }
 205 
 206     static final int ONE_MILLION = (1000*1000),  // first int value
 207                      TEN_BILLION = (10*1000*1000*1000),  // scale factor to reach upper 32 bits
 208                      INITIAL_ARG_VAL = ONE_MILLION << 1;  // <<1 makes space for sign bit;
 209     static long nextArgVal;
 210 
 211     static long nextArg(boolean moreBits) {
 212         long val = nextArgVal++;
 213         long sign = -(val & 1); // alternate signs
 214         val >>= 1;
 215         if (moreBits)
 216             // Guarantee some bits in the high word.
 217             // In any case keep the decimal representation simple-looking,
 218             // with lots of zeroes, so as not to make the printed decimal
 219             // strings unnecessarily noisy.
 220             val += (val % ONE_MILLION) * TEN_BILLION;
 221         return val ^ sign;
 222     }
 223 
 224     static int nextArg() {
 225         // Produce a 32-bit result something like ONE_MILLION+(smallint).
 226         // Example: 1_000_042.
 227         return (int) nextArg(false);
 228     }
 229 
 230     static long nextArg(Class<?> kind) {
 231         if (kind == long.class   || kind == Long.class ||
 232             kind == double.class || kind == Double.class)
 233             // produce a 64-bit result something like
 234             // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
 235             // Example: 10_000_420_001_000_042.
 236             return nextArg(true);
 237         return (long) nextArg();
 238     }
 239 
 240     static Object randomArg(Class<?> param) {
 241         Object wrap = castToWrapperOrNull(nextArg(param), param);
 242         if (wrap != null) {
 243             return wrap;
 244         }
 245         //import sun.invoke.util.Wrapper;
 246         //Wrapper wrap = Wrapper.forBasicType(dst);
 247         //if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
 248         //   wrap = Wrapper.forWrapperType(dst);
 249         //   if (wrap != Wrapper.OBJECT)
 250         //       return wrap.wrap(nextArg++);
 251         if (param.isInterface()) {
 252             for (Class<?> c : param.getClasses()) {
 253                 if (param.isAssignableFrom(c) && !c.isInterface())
 254                     { param = c; break; }
 255             }
 256         }
 257         if (param.isArray()) {
 258             Class<?> ctype = param.getComponentType();
 259             Object arg = Array.newInstance(ctype, 2);
 260             Array.set(arg, 0, randomArg(ctype));
 261             return arg;
 262         }
 263         if (param.isInterface() && param.isAssignableFrom(List.class))
 264             return Arrays.asList("#"+nextArg());
 265         if (param.isInterface() || param.isAssignableFrom(String.class))
 266             return "#"+nextArg();
 267         else
 268             try {
 269                 return param.newInstance();
 270             } catch (InstantiationException | IllegalAccessException ex) {
 271             }
 272         return null;  // random class not Object, String, Integer, etc.
 273     }
 274 
 275     static Object[] randomArgs(Class<?>... params) {
 276         Object[] args = new Object[params.length];
 277         for (int i = 0; i < args.length; i++)
 278             args[i] = randomArg(params[i]);
 279         return args;
 280     }
 281 
 282     static Object[] randomArgs(int nargs, Class<?> param) {
 283         Object[] args = new Object[nargs];
 284         for (int i = 0; i < args.length; i++)
 285             args[i] = randomArg(param);
 286         return args;
 287     }
 288 
 289     static Object[] randomArgs(List<Class<?>> params) {
 290         return randomArgs(params.toArray(new Class<?>[params.size()]));
 291     }
 292 
 293     @SafeVarargs @SuppressWarnings("varargs")
 294     static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
 295         return Arrays.copyOf(a, a.length, atype);
 296     }
 297 
 298     @SafeVarargs @SuppressWarnings("varargs")
 299     static <T> T[] cat(T[] a, T... b) {
 300         int alen = a.length, blen = b.length;
 301         if (blen == 0)  return a;
 302         T[] c = Arrays.copyOf(a, alen + blen);
 303         System.arraycopy(b, 0, c, alen, blen);
 304         return c;
 305     }
 306 
 307     static Integer[] boxAll(int... vx) {
 308         Integer[] res = new Integer[vx.length];
 309         for (int i = 0; i < res.length; i++) {
 310             res[i] = vx[i];
 311         }
 312         return res;
 313     }
 314 
 315     static Object getClasses(Object x) {
 316         if (x == null)  return x;
 317         if (x instanceof String)  return x;  // keep the name
 318         if (x instanceof List) {
 319             // recursively report classes of the list elements
 320             Object[] xa = ((List)x).toArray();
 321             for (int i = 0; i < xa.length; i++)
 322                 xa[i] = getClasses(xa[i]);
 323             return Arrays.asList(xa);
 324         }
 325         return x.getClass().getSimpleName();
 326     }
 327 
 328     /** Return lambda(arg...[arity]) { new Object[]{ arg... } } */
 329     static MethodHandle varargsList(int arity) {
 330         return ValueConversions.varargsList(arity);
 331     }
 332 
 333     /** Return lambda(arg...[arity]) { Arrays.asList(arg...) } */
 334     static MethodHandle varargsArray(int arity) {
 335         return ValueConversions.varargsArray(arity);
 336     }
 337 
 338     static MethodHandle varargsArray(Class<?> arrayType, int arity) {
 339         return ValueConversions.varargsArray(arrayType, arity);
 340     }
 341 
 342     /** Variation of varargsList, but with the given rtype. */
 343     static MethodHandle varargsList(int arity, Class<?> rtype) {
 344         MethodHandle list = varargsList(arity);
 345         MethodType listType = list.type().changeReturnType(rtype);
 346         if (List.class.isAssignableFrom(rtype) || rtype == void.class || rtype == Object.class) {
 347             // OK
 348         } else if (rtype.isAssignableFrom(String.class)) {
 349             if (LIST_TO_STRING == null)
 350                 try {
 351                     LIST_TO_STRING = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToString",
 352                                                         MethodType.methodType(String.class, List.class));
 353                 } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); }
 354             list = MethodHandles.filterReturnValue(list, LIST_TO_STRING);
 355         } else if (rtype.isPrimitive()) {
 356             if (LIST_TO_INT == null)
 357                 try {
 358                     LIST_TO_INT = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToInt",
 359                                                      MethodType.methodType(int.class, List.class));
 360                 } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); }
 361             list = MethodHandles.filterReturnValue(list, LIST_TO_INT);
 362             list = MethodHandles.explicitCastArguments(list, listType);
 363         } else {
 364             throw new RuntimeException("varargsList: "+rtype);
 365         }
 366         return list.asType(listType);
 367     }
 368 
 369     /** Variation of varargsList, but with the given ptypes and rtype. */
 370     static MethodHandle varargsList(List<Class<?>> ptypes, Class<?> rtype) {
 371         MethodHandle list = varargsList(ptypes.size(), rtype);
 372         return list.asType(MethodType.methodType(rtype, ptypes));
 373     }
 374 
 375     private static MethodHandle LIST_TO_STRING, LIST_TO_INT;
 376     private static String listToString(List<?> x) { return x.toString(); }
 377     private static int listToInt(List<?> x) { return x.toString().hashCode(); }
 378 
 379     static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
 380         return changeArgTypes(target, 0, 999, argType);
 381     }
 382 
 383     static MethodHandle changeArgTypes(MethodHandle target,
 384             int beg, int end, Class<?> argType) {
 385         MethodType targetType = target.type();
 386         end = Math.min(end, targetType.parameterCount());
 387         ArrayList<Class<?>> argTypes = new ArrayList<>(targetType.parameterList());
 388         Collections.fill(argTypes.subList(beg, end), argType);
 389         MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
 390         return target.asType(ttype2);
 391     }
 392 
 393     static MethodHandle addTrailingArgs(MethodHandle target, int nargs, Class<?> argClass) {
 394         int targetLen = target.type().parameterCount();
 395         int extra = (nargs - targetLen);
 396         if (extra <= 0)  return target;
 397         List<Class<?>> fakeArgs = Collections.<Class<?>>nCopies(extra, argClass);
 398         return MethodHandles.dropArguments(target, targetLen, fakeArgs);
 399     }
 400 
 401     // This lookup is good for all members in and under MethodHandlesTest.
 402     static final Lookup PRIVATE = MethodHandles.lookup();
 403     // This lookup is good for package-private members but not private ones.
 404     static final Lookup PACKAGE = PackageSibling.lookup();
 405     // This lookup is good for public members and protected members of PubExample
 406     static final Lookup SUBCLASS = RemoteExample.lookup();
 407     // This lookup is good only for public members in exported packages.
 408     static final Lookup PUBLIC  = MethodHandles.publicLookup();
 409 
 410     // Subject methods...
 411     static class Example implements IntExample {
 412         final String name;
 413         public Example() { name = "Example#"+nextArg(); }
 414         protected Example(String name) { this.name = name; }
 415         @SuppressWarnings("LeakingThisInConstructor")
 416         protected Example(int x) { this(); called("protected <init>", this, x); }
 417         //Example(Void x) { does not exist; lookup elicts NoSuchMethodException }
 418         @Override public String toString() { return name; }
 419 
 420         public void            v0()     { called("v0", this); }
 421         protected void         pro_v0() { called("pro_v0", this); }
 422         void                   pkg_v0() { called("pkg_v0", this); }
 423         private void           pri_v0() { called("pri_v0", this); }
 424         public static void     s0()     { called("s0"); }
 425         protected static void  pro_s0() { called("pro_s0"); }
 426         static void            pkg_s0() { called("pkg_s0"); }
 427         private static void    pri_s0() { called("pri_s0"); }
 428 
 429         public Object          v1(Object x) { return called("v1", this, x); }
 430         public Object          v2(Object x, Object y) { return called("v2", this, x, y); }
 431         public Object          v2(Object x, int    y) { return called("v2", this, x, y); }
 432         public Object          v2(int    x, Object y) { return called("v2", this, x, y); }
 433         public Object          v2(int    x, int    y) { return called("v2", this, x, y); }
 434         public static Object   s1(Object x) { return called("s1", x); }
 435         public static Object   s2(int x)    { return called("s2", x); }
 436         public static Object   s3(long x)   { return called("s3", x); }
 437         public static Object   s4(int x, int y) { return called("s4", x, y); }
 438         public static Object   s5(long x, int y) { return called("s5", x, y); }
 439         public static Object   s6(int x, long y) { return called("s6", x, y); }
 440         public static Object   s7(float x, double y) { return called("s7", x, y); }
 441 
 442         // for testing findConstructor:
 443         public Example(String x, int y) { this.name = x+y; called("Example.<init>", x, y); }
 444         public Example(int x, String y) { this.name = x+y; called("Example.<init>", x, y); }
 445         public Example(int x, int    y) { this.name = x+""+y; called("Example.<init>", x, y); }
 446         public Example(int x, long   y) { this.name = x+""+y; called("Example.<init>", x, y); }
 447         public Example(int x, float  y) { this.name = x+""+y; called("Example.<init>", x, y); }
 448         public Example(int x, double y) { this.name = x+""+y; called("Example.<init>", x, y); }
 449         public Example(int x, int    y, int z) { this.name = x+""+y+""+z; called("Example.<init>", x, y, z); }
 450         public Example(int x, int    y, int z, int a) { this.name = x+""+y+""+z+""+a; called("Example.<init>", x, y, z, a); }
 451 
 452         static final Lookup EXAMPLE = MethodHandles.lookup();  // for testing findSpecial
 453     }
 454 
 455     static final Lookup EXAMPLE = Example.EXAMPLE;
 456     public static class PubExample extends Example {
 457         public PubExample() { this("PubExample"); }
 458         protected PubExample(String prefix) { super(prefix+"#"+nextArg()); }
 459         protected void         pro_v0() { called("Pub/pro_v0", this); }
 460         protected static void  pro_s0() { called("Pub/pro_s0"); }
 461     }
 462 
 463     static class SubExample extends Example {
 464         @Override public void  v0()     { called("Sub/v0", this); }
 465         @Override void         pkg_v0() { called("Sub/pkg_v0", this); }
 466         @SuppressWarnings("LeakingThisInConstructor")
 467         private      SubExample(int x)  { called("<init>", this, x); }
 468         public SubExample() { super("SubExample#"+nextArg()); }
 469     }
 470 
 471     public static interface IntExample {
 472         public void            v0();
 473         public default void    vd() { called("vd", this); }
 474         public static class Impl implements IntExample {
 475             public void        v0()     { called("Int/v0", this); }
 476             final String name;
 477             public Impl() { name = "Impl#"+nextArg(); }
 478             @Override public String toString() { return name; }
 479         }
 480     }
 481 
 482     static interface SubIntExample extends IntExample { }
 483 
 484     static final Object[][][] ACCESS_CASES = {
 485         { { false, PUBLIC }, { false, SUBCLASS }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false
 486         { { false, PUBLIC }, { false, SUBCLASS }, { false, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[1]: only PRIVATE
 487         { { false, PUBLIC }, { false, SUBCLASS }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[2]: PUBLIC false
 488         { { false, PUBLIC }, { true, SUBCLASS }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[3]: subclass OK
 489         { { true, PUBLIC }, { true, SUBCLASS }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[4]: all true
 490     };
 491 
 492     static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) {
 493         Object[][] cases;
 494         if (name.contains("pri_") || isSpecial) {
 495             cases = ACCESS_CASES[1]; // PRIVATE only
 496         } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) {
 497             cases = ACCESS_CASES[2]; // not PUBLIC
 498         } else if (name.contains("pro_")) {
 499             cases = ACCESS_CASES[3]; // PUBLIC class, protected member
 500         } else {
 501             assertTrue(name.indexOf('_') < 0 || name.contains("fin_"));
 502             boolean pubc = Modifier.isPublic(defc.getModifiers());
 503             if (pubc)
 504                 cases = ACCESS_CASES[4]; // all access levels
 505             else
 506                 cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC
 507         }
 508         if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE)
 509             cases = Arrays.copyOfRange(cases, 0, cases.length-1);
 510         return cases;
 511     }
 512 
 513     static Object[][] accessCases(Class<?> defc, String name) {
 514         return accessCases(defc, name, false);
 515     }
 516 
 517     static Lookup maybeMoveIn(Lookup lookup, Class<?> defc) {
 518         if (lookup == PUBLIC || lookup == SUBCLASS || lookup == PACKAGE)
 519             // external views stay external
 520             return lookup;
 521         return lookup.in(defc);
 522     }
 523 
 524     /** Is findVirtual (etc.) of "&lt;init&lt;" supposed to elicit a NoSuchMethodException? */
 525     static final boolean INIT_REF_CAUSES_NSME = true;
 526 
 527     static void assertExceptionClass(Class<? extends Throwable> expected,
 528                                      Throwable actual) {
 529         if (expected.isInstance(actual))  return;
 530         actual.printStackTrace();
 531         assertEquals(expected, actual.getClass());
 532     }
 533 
 534     static final boolean DEBUG_METHOD_HANDLE_NAMES = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
 535 
 536     // rough check of name string
 537     static void assertNameStringContains(MethodHandle x, String s) {
 538         if (!DEBUG_METHOD_HANDLE_NAMES) {
 539             // ignore s
 540             assertEquals("MethodHandle"+x.type(), x.toString());
 541             return;
 542         }
 543         if (x.toString().contains(s))  return;
 544         assertEquals(s, x);
 545     }
 546 
 547     public static class HasFields {
 548         boolean iZ = false;
 549         byte iB = (byte)'B';
 550         short iS = (short)'S';
 551         char iC = 'C';
 552         int iI = 'I';
 553         long iJ = 'J';
 554         float iF = 'F';
 555         double iD = 'D';
 556         static boolean sZ = true;
 557         static byte sB = 1+(byte)'B';
 558         static short sS = 1+(short)'S';
 559         static char sC = 1+'C';
 560         static int sI = 1+'I';
 561         static long sJ = 1+'J';
 562         static float sF = 1+'F';
 563         static double sD = 1+'D';
 564         final static boolean fsZ = false;
 565         final static byte fsB = 2+(byte)'B';
 566         final static short fsS = 2+(short)'S';
 567         final static char fsC = 2+'C';
 568         final static int fsI = 2+'I';
 569         final static long fsJ = 2+'J';
 570         final static float fsF = 2+'F';
 571         final static double fsD = 2+'D';
 572 
 573         Object iL = 'L';
 574         String iR = "R";
 575         static Object sL = 'M';
 576         static String sR = "S";
 577         final static Object fsL = 'N';
 578         final static String fsR = "T";
 579 
 580         static final Object[][] CASES;
 581         static {
 582             ArrayList<Object[]> cases = new ArrayList<>();
 583             Object types[][] = {
 584                 {'L',Object.class}, {'R',String.class},
 585                 {'I',int.class}, {'J',long.class},
 586                 {'F',float.class}, {'D',double.class},
 587                 {'Z',boolean.class}, {'B',byte.class},
 588                 {'S',short.class}, {'C',char.class},
 589             };
 590             HasFields fields = new HasFields();
 591             for (Object[] t : types) {
 592                 for (int kind = 0; kind <= 2; kind++) {
 593                     boolean isStatic = (kind != 0);
 594                     boolean isFinal  = (kind == 2);
 595                     char btc = (Character)t[0];
 596                     String name = (isStatic ? "s" : "i") + btc;
 597                     if (isFinal) name = "f" + name;
 598                     Class<?> type = (Class<?>) t[1];
 599                     Object value;
 600                     Field field;
 601                     try {
 602                         field = HasFields.class.getDeclaredField(name);
 603                     } catch (NoSuchFieldException | SecurityException ex) {
 604                         throw new InternalError("no field HasFields."+name);
 605                     }
 606                     try {
 607                         value = field.get(fields);
 608                     } catch (IllegalArgumentException | IllegalAccessException ex) {
 609                         throw new InternalError("cannot fetch field HasFields."+name);
 610                     }
 611                     if (type == float.class) {
 612                         float v = 'F';
 613                         if (isStatic)  v++;
 614                         if (isFinal)   v++;
 615                         assertTrue(value.equals(v));
 616                     }
 617                     if (isFinal && isStatic) field.setAccessible(true);
 618                     assertTrue(name.equals(field.getName()));
 619                     assertTrue(type.equals(field.getType()));
 620                     assertTrue(isStatic == (Modifier.isStatic(field.getModifiers())));
 621                     assertTrue(isFinal  == (Modifier.isFinal(field.getModifiers())));
 622                     cases.add(new Object[]{ field, value });
 623                 }
 624             }
 625             cases.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class });
 626             cases.add(new Object[]{ new Object[]{ true,  HasFields.class, "bogus_sL", Object.class }, Error.class });
 627             CASES = cases.toArray(new Object[0][]);
 628         }
 629     }
 630 
 631     static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10, TEST_BOUND = 0x20, TEST_NPE = 0x40;
 632 
 633     static boolean testModeMatches(int testMode, boolean isStatic) {
 634         switch (testMode) {
 635         case TEST_FIND_STATIC:          return isStatic;
 636         case TEST_FIND_FIELD:           return !isStatic;
 637         case TEST_UNREFLECT:            return true;  // unreflect matches both
 638         }
 639         throw new InternalError("testMode="+testMode);
 640     }
 641 
 642     static class Callee {
 643         static Object id() { return called("id"); }
 644         static Object id(Object x) { return called("id", x); }
 645         static Object id(Object x, Object y) { return called("id", x, y); }
 646         static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
 647         static Object id(Object... vx) { return called("id", vx); }
 648         static MethodHandle ofType(int n) {
 649             return ofType(Object.class, n);
 650         }
 651         static MethodHandle ofType(Class<?> rtype, int n) {
 652             if (n == -1)
 653                 return ofType(MethodType.methodType(rtype, Object[].class));
 654             return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
 655         }
 656         static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
 657             return ofType(MethodType.methodType(rtype, ptypes));
 658         }
 659         static MethodHandle ofType(MethodType type) {
 660             Class<?> rtype = type.returnType();
 661             String pfx = "";
 662             if (rtype != Object.class)
 663                 pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
 664             String name = pfx+"id";
 665             try {
 666                 return PRIVATE.findStatic(Callee.class, name, type);
 667             } catch (NoSuchMethodException | IllegalAccessException ex) {
 668                 throw new RuntimeException(ex);
 669             }
 670         }
 671     }
 672 
 673     static Object invokee(Object... args) {
 674         return called("invokee", args).hashCode();
 675     }
 676 
 677     protected static final String MISSING_ARG = "missingArg";
 678     protected static final String MISSING_ARG_2 = "missingArg#2";
 679 
 680     static Object targetIfEquals() {
 681         return called("targetIfEquals");
 682     }
 683 
 684     static Object fallbackIfNotEquals() {
 685         return called("fallbackIfNotEquals");
 686     }
 687 
 688     static Object targetIfEquals(Object x) {
 689         assertEquals(x, MISSING_ARG);
 690         return called("targetIfEquals", x);
 691     }
 692 
 693     static Object fallbackIfNotEquals(Object x) {
 694         assertFalse(x.toString(), x.equals(MISSING_ARG));
 695         return called("fallbackIfNotEquals", x);
 696     }
 697 
 698     static Object targetIfEquals(Object x, Object y) {
 699         assertEquals(x, y);
 700         return called("targetIfEquals", x, y);
 701     }
 702 
 703     static Object fallbackIfNotEquals(Object x, Object y) {
 704         assertFalse(x.toString(), x.equals(y));
 705         return called("fallbackIfNotEquals", x, y);
 706     }
 707 
 708     static Object targetIfEquals(Object x, Object y, Object z) {
 709         assertEquals(x, y);
 710         return called("targetIfEquals", x, y, z);
 711     }
 712 
 713     static Object fallbackIfNotEquals(Object x, Object y, Object z) {
 714         assertFalse(x.toString(), x.equals(y));
 715         return called("fallbackIfNotEquals", x, y, z);
 716     }
 717 
 718     static boolean loopIntPred(int a) {
 719         if (verbosity >= 5) {
 720             System.out.println("int pred " + a + " -> " + (a < 7));
 721         }
 722         return a < 7;
 723     }
 724 
 725     static boolean loopDoublePred(int a, double b) {
 726         if (verbosity >= 5) {
 727             System.out.println("double pred (a=" + a + ") " + b + " -> " + (b > 0.5));
 728         }
 729         return b > 0.5;
 730     }
 731 
 732     static boolean loopStringPred(int a, double b, String c) {
 733         if (verbosity >= 5) {
 734             System.out.println("String pred (a=" + a + ",b=" + b + ") " + c + " -> " + (c.length() <= 9));
 735         }
 736         return c.length() <= 9;
 737     }
 738 
 739     static int loopIntStep(int a) {
 740         if (verbosity >= 5) {
 741             System.out.println("int step " + a + " -> " + (a + 1));
 742         }
 743         return a + 1;
 744     }
 745 
 746     static double loopDoubleStep(int a, double b) {
 747         if (verbosity >= 5) {
 748             System.out.println("double step (a=" + a + ") " + b + " -> " + (b / 2.0));
 749         }
 750         return b / 2.0;
 751     }
 752 
 753     static String loopStringStep(int a, double b, String c) {
 754         if (verbosity >= 5) {
 755             System.out.println("String step (a=" + a + ",b=" + b + ") " + c + " -> " + (c + a));
 756         }
 757         return c + a;
 758     }
 759 
 760     static void vtarget(String[] a) {
 761         // naught, akin to identity
 762     }
 763 
 764     static void vtargetThrow(String[] a) throws Exception {
 765         throw new Exception("thrown");
 766     }
 767 
 768     static void vcleanupPassThrough(Throwable t, String[] a) {
 769         assertNull(t);
 770         // naught, akin to identity
 771     }
 772 
 773     static void vcleanupAugment(Throwable t, String[] a) {
 774         assertNull(t);
 775         a[0] = "augmented";
 776     }
 777 
 778     static void vcleanupCatch(Throwable t, String[] a) {
 779         assertNotNull(t);
 780         a[0] = "caught";
 781     }
 782 
 783     static void vcleanupThrow(Throwable t, String[] a) throws Exception {
 784         assertNotNull(t);
 785         throw new Exception("rethrown");
 786     }
 787 }
 788 // Local abbreviated copy of sun.invoke.util.ValueConversions
 789 // This guy tests access from outside the same package member, but inside
 790 // the package itself.
 791 class ValueConversions {
 792     private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
 793     private static final Object[] NO_ARGS_ARRAY = {};
 794     private static Object[] makeArray(Object... args) { return args; }
 795     private static Object[] array() { return NO_ARGS_ARRAY; }
 796     private static Object[] array(Object a0)
 797                 { return makeArray(a0); }
 798     private static Object[] array(Object a0, Object a1)
 799                 { return makeArray(a0, a1); }
 800     private static Object[] array(Object a0, Object a1, Object a2)
 801                 { return makeArray(a0, a1, a2); }
 802     private static Object[] array(Object a0, Object a1, Object a2, Object a3)
 803                 { return makeArray(a0, a1, a2, a3); }
 804     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
 805                                   Object a4)
 806                 { return makeArray(a0, a1, a2, a3, a4); }
 807     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
 808                                   Object a4, Object a5)
 809                 { return makeArray(a0, a1, a2, a3, a4, a5); }
 810     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
 811                                   Object a4, Object a5, Object a6)
 812                 { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
 813     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
 814                                   Object a4, Object a5, Object a6, Object a7)
 815                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
 816     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
 817                                   Object a4, Object a5, Object a6, Object a7,
 818                                   Object a8)
 819                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
 820     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
 821                                   Object a4, Object a5, Object a6, Object a7,
 822                                   Object a8, Object a9)
 823                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
 824 
 825     static MethodHandle[] makeArrays() {
 826         ArrayList<MethodHandle> arrays = new ArrayList<>();
 827         MethodHandles.Lookup lookup = IMPL_LOOKUP;
 828         for (;;) {
 829             int nargs = arrays.size();
 830             MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
 831             String name = "array";
 832             MethodHandle array = null;
 833             try {
 834                 array = lookup.findStatic(ValueConversions.class, name, type);
 835             } catch (ReflectiveOperationException ex) {
 836                 // break from loop!
 837             }
 838             if (array == null)  break;
 839             arrays.add(array);
 840         }
 841         assertTrue(arrays.size() == 11);  // current number of methods
 842         return arrays.toArray(new MethodHandle[0]);
 843     }
 844 
 845     static final MethodHandle[] ARRAYS = makeArrays();
 846 
 847     /** Return a method handle that takes the indicated number of Object
 848      *  arguments and returns an Object array of them, as if for varargs.
 849      */
 850     public static MethodHandle varargsArray(int nargs) {
 851         if (nargs < ARRAYS.length)
 852             return ARRAYS[nargs];
 853         return MethodHandles.identity(Object[].class).asCollector(Object[].class, nargs);
 854     }
 855 
 856     public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
 857         Class<?> elemType = arrayType.getComponentType();
 858         MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
 859         MethodHandle mh = varargsArray(nargs);
 860         if (arrayType != Object[].class)
 861             mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType));
 862         return mh.asType(vaType);
 863     }
 864 
 865     static Object changeArrayType(Class<?> arrayType, Object[] a) {
 866         Class<?> elemType = arrayType.getComponentType();
 867         if (!elemType.isPrimitive())
 868             return Arrays.copyOf(a, a.length, arrayType.asSubclass(Object[].class));
 869         Object b = java.lang.reflect.Array.newInstance(elemType, a.length);
 870         for (int i = 0; i < a.length; i++)
 871             java.lang.reflect.Array.set(b, i, a[i]);
 872         return b;
 873     }
 874 
 875     private static final MethodHandle CHANGE_ARRAY_TYPE;
 876     static {
 877         try {
 878             CHANGE_ARRAY_TYPE = IMPL_LOOKUP.findStatic(ValueConversions.class, "changeArrayType",
 879                                                        MethodType.methodType(Object.class, Class.class, Object[].class));
 880         } catch (NoSuchMethodException | IllegalAccessException ex) {
 881             Error err = new InternalError("uncaught exception");
 882             err.initCause(ex);
 883             throw err;
 884         }
 885     }
 886 
 887     private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
 888     private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
 889     private static List<Object> list() { return NO_ARGS_LIST; }
 890     private static List<Object> list(Object a0)
 891                 { return makeList(a0); }
 892     private static List<Object> list(Object a0, Object a1)
 893                 { return makeList(a0, a1); }
 894     private static List<Object> list(Object a0, Object a1, Object a2)
 895                 { return makeList(a0, a1, a2); }
 896     private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
 897                 { return makeList(a0, a1, a2, a3); }
 898     private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
 899                                      Object a4)
 900                 { return makeList(a0, a1, a2, a3, a4); }
 901     private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
 902                                      Object a4, Object a5)
 903                 { return makeList(a0, a1, a2, a3, a4, a5); }
 904     private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
 905                                      Object a4, Object a5, Object a6)
 906                 { return makeList(a0, a1, a2, a3, a4, a5, a6); }
 907     private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
 908                                      Object a4, Object a5, Object a6, Object a7)
 909                 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
 910     private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
 911                                      Object a4, Object a5, Object a6, Object a7,
 912                                      Object a8)
 913                 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
 914     private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
 915                                      Object a4, Object a5, Object a6, Object a7,
 916                                      Object a8, Object a9)
 917                 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
 918 
 919     static MethodHandle[] makeLists() {
 920         ArrayList<MethodHandle> lists = new ArrayList<>();
 921         MethodHandles.Lookup lookup = IMPL_LOOKUP;
 922         for (;;) {
 923             int nargs = lists.size();
 924             MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
 925             String name = "list";
 926             MethodHandle list = null;
 927             try {
 928                 list = lookup.findStatic(ValueConversions.class, name, type);
 929             } catch (ReflectiveOperationException ex) {
 930                 // break from loop!
 931             }
 932             if (list == null)  break;
 933             lists.add(list);
 934         }
 935         assertTrue(lists.size() == 11);  // current number of methods
 936         return lists.toArray(new MethodHandle[0]);
 937     }
 938 
 939     static final MethodHandle[] LISTS = makeLists();
 940     static final MethodHandle AS_LIST;
 941 
 942     static {
 943         try {
 944             AS_LIST = IMPL_LOOKUP.findStatic(Arrays.class, "asList", MethodType.methodType(List.class, Object[].class));
 945         } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); }
 946     }
 947 
 948     /** Return a method handle that takes the indicated number of Object
 949      *  arguments and returns List.
 950      */
 951     public static MethodHandle varargsList(int nargs) {
 952         if (nargs < LISTS.length)
 953             return LISTS[nargs];
 954         return AS_LIST.asCollector(Object[].class, nargs);
 955     }
 956 }
 957 // This guy tests access from outside the same package member, but inside
 958 // the package itself.
 959 class PackageSibling {
 960     static Lookup lookup() {
 961         return MethodHandles.lookup();
 962     }
 963 }