blob: b44f262fa024cf387138aa911b83b70e92d36b21 [file] [log] [blame]
jrose55220c32009-10-21 23:19:48 -07001/*
jrosef108fc02011-02-11 01:26:24 -08002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
jrose55220c32009-10-21 23:19:48 -07003 * 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
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
jrose55220c32009-10-21 23:19:48 -07008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
jrose55220c32009-10-21 23:19:48 -070010 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
ohair2283b9d2010-05-25 15:58:33 -070021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
jrose55220c32009-10-21 23:19:48 -070024 */
25
26/* @test
27 * @summary unit tests for java.dyn.MethodHandles
jrose1c1bfac2010-11-22 22:41:31 -080028 * @compile -source 7 -target 7 -XDallowTransitionalJSR292=no MethodHandlesTest.java
29 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles test.java.dyn.MethodHandlesTest
jrose55220c32009-10-21 23:19:48 -070030 */
31
jrose2cc9c832010-04-30 23:48:23 -070032package test.java.dyn;
jrose55220c32009-10-21 23:19:48 -070033
34import java.dyn.*;
35import java.dyn.MethodHandles.Lookup;
36import java.lang.reflect.*;
37import java.util.*;
38import org.junit.*;
39import static org.junit.Assert.*;
jrose2cc9c832010-04-30 23:48:23 -070040import static org.junit.Assume.*;
jrose55220c32009-10-21 23:19:48 -070041
42
43/**
44 *
45 * @author jrose
46 */
47public class MethodHandlesTest {
48 // How much output?
jrose2cc9c832010-04-30 23:48:23 -070049 static int verbosity = 0;
jrosecf98d422010-06-08 23:08:56 -070050 static {
51 String vstr = System.getProperty("test.java.dyn.MethodHandlesTest.verbosity");
52 if (vstr != null) verbosity = Integer.parseInt(vstr);
53 }
jrose55220c32009-10-21 23:19:48 -070054
55 // Set this true during development if you want to fast-forward to
56 // a particular new, non-working test. Tests which are known to
57 // work (or have recently worked) test this flag and return on true.
58 static boolean CAN_SKIP_WORKING = false;
59 //static { CAN_SKIP_WORKING = true; }
60
61 // Set true to test more calls. If false, some tests are just
62 // lookups, without exercising the actual method handle.
jrose2cc9c832010-04-30 23:48:23 -070063 static boolean DO_MORE_CALLS = true;
jrose55220c32009-10-21 23:19:48 -070064
jrose55220c32009-10-21 23:19:48 -070065 @Test
66 public void testFirst() throws Throwable {
67 verbosity += 9; try {
68 // left blank for debugging
jrose2cc9c832010-04-30 23:48:23 -070069 } finally { printCounts(); verbosity -= 9; }
jrose55220c32009-10-21 23:19:48 -070070 }
71
jrose10f3b682010-01-07 16:16:45 -080072 // current failures
73 @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
74 public void testFail_1() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -070075 // AMH.<init>: IllegalArgumentException: bad adapter (conversion=0xfffab300): adapter pushes too many parameters
jrose10f3b682010-01-07 16:16:45 -080076 testSpreadArguments(int.class, 0, 6);
77 }
jrose2cc9c832010-04-30 23:48:23 -070078 @Test @Ignore("failure in JVM when expanding the stack using asm stub for _adapter_spread_args")
jrose10f3b682010-01-07 16:16:45 -080079 public void testFail_2() throws Throwable {
80 // if CONV_OP_IMPLEMENTED_MASK includes OP_SPREAD_ARGS, this crashes:
81 testSpreadArguments(Object.class, 0, 2);
82 }
83 @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
84 public void testFail_3() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -070085 // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
jrose10f3b682010-01-07 16:16:45 -080086 testSpreadArguments(int.class, 1, 2);
87 }
88 @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
89 public void testFail_4() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -070090 // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
jrose10f3b682010-01-07 16:16:45 -080091 testCollectArguments(int.class, 1, 2);
92 }
93 @Test @Ignore("cannot collect leading primitive types")
94 public void testFail_5() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -070095 // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
jrose10f3b682010-01-07 16:16:45 -080096 testInvokers(MethodType.genericMethodType(2).changeParameterType(0, int.class));
97 }
98 @Test @Ignore("should not insert arguments beyond MethodHandlePushLimit")
99 public void testFail_6() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -0700100 // ValueConversions.varargsArray: UnsupportedOperationException: NYI: cannot form a varargs array of length 13
101 testInsertArguments(0, 0, MAX_ARG_INCREASE+10);
jrose10f3b682010-01-07 16:16:45 -0800102 }
jrose55220c32009-10-21 23:19:48 -0700103 static final int MAX_ARG_INCREASE = 3;
104
105 public MethodHandlesTest() {
106 }
107
jrose2cc9c832010-04-30 23:48:23 -0700108 @Before
109 public void checkImplementedPlatform() {
110 boolean platformOK = false;
111 Properties properties = System.getProperties();
112 String vers = properties.getProperty("java.vm.version");
113 String name = properties.getProperty("java.vm.name");
114 String arch = properties.getProperty("os.arch");
jrosecf98d422010-06-08 23:08:56 -0700115 if ((arch.equals("amd64") || arch.equals("i386") || arch.equals("x86") ||
jrose2cc9c832010-04-30 23:48:23 -0700116 arch.equals("sparc") || arch.equals("sparcv9")) &&
117 (name.contains("Client") || name.contains("Server"))
118 ) {
119 platformOK = true;
120 } else {
121 System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch));
122 }
123 assumeTrue(platformOK);
124 }
125
jrose55220c32009-10-21 23:19:48 -0700126 String testName;
jrosecf98d422010-06-08 23:08:56 -0700127 static int allPosTests, allNegTests;
jrose55220c32009-10-21 23:19:48 -0700128 int posTests, negTests;
129 @After
130 public void printCounts() {
jrose2cc9c832010-04-30 23:48:23 -0700131 if (verbosity >= 2 && (posTests | negTests) != 0) {
jrose55220c32009-10-21 23:19:48 -0700132 System.out.println();
133 if (posTests != 0) System.out.println("=== "+testName+": "+posTests+" positive test cases run");
134 if (negTests != 0) System.out.println("=== "+testName+": "+negTests+" negative test cases run");
jrosecf98d422010-06-08 23:08:56 -0700135 allPosTests += posTests;
136 allNegTests += negTests;
jrose2cc9c832010-04-30 23:48:23 -0700137 posTests = negTests = 0;
jrose55220c32009-10-21 23:19:48 -0700138 }
139 }
140 void countTest(boolean positive) {
141 if (positive) ++posTests;
142 else ++negTests;
143 }
144 void countTest() { countTest(true); }
145 void startTest(String name) {
146 if (testName != null) printCounts();
jrose2cc9c832010-04-30 23:48:23 -0700147 if (verbosity >= 1)
jrose55220c32009-10-21 23:19:48 -0700148 System.out.println(name);
149 posTests = negTests = 0;
150 testName = name;
151 }
152
153 @BeforeClass
154 public static void setUpClass() throws Exception {
155 calledLog.clear();
156 calledLog.add(null);
jrose2cc9c832010-04-30 23:48:23 -0700157 nextArgVal = INITIAL_ARG_VAL;
jrose55220c32009-10-21 23:19:48 -0700158 }
159
160 @AfterClass
161 public static void tearDownClass() throws Exception {
jrosecf98d422010-06-08 23:08:56 -0700162 int posTests = allPosTests, negTests = allNegTests;
163 if (verbosity >= 2 && (posTests | negTests) != 0) {
164 System.out.println();
165 if (posTests != 0) System.out.println("=== "+posTests+" total positive test cases");
166 if (negTests != 0) System.out.println("=== "+negTests+" total negative test cases");
167 }
jrose55220c32009-10-21 23:19:48 -0700168 }
169
170 static List<Object> calledLog = new ArrayList<Object>();
171 static Object logEntry(String name, Object... args) {
172 return Arrays.asList(name, Arrays.asList(args));
173 }
174 static Object called(String name, Object... args) {
175 Object entry = logEntry(name, args);
176 calledLog.add(entry);
177 return entry;
178 }
179 static void assertCalled(String name, Object... args) {
180 Object expected = logEntry(name, args);
181 Object actual = calledLog.get(calledLog.size() - 1);
jrose2cc9c832010-04-30 23:48:23 -0700182 if (expected.equals(actual) && verbosity < 9) return;
jrose55220c32009-10-21 23:19:48 -0700183 System.out.println("assertCalled "+name+":");
184 System.out.println("expected: "+expected);
185 System.out.println("actual: "+actual);
186 System.out.println("ex. types: "+getClasses(expected));
187 System.out.println("act. types: "+getClasses(actual));
jrose55220c32009-10-21 23:19:48 -0700188 assertEquals("previous method call", expected, actual);
189 }
190 static void printCalled(MethodHandle target, String name, Object... args) {
jrose2cc9c832010-04-30 23:48:23 -0700191 if (verbosity >= 3)
192 System.out.println("calling MH="+target+" to "+name+Arrays.toString(args));
jrose55220c32009-10-21 23:19:48 -0700193 }
194
195 static Object castToWrapper(Object value, Class<?> dst) {
196 Object wrap = null;
197 if (value instanceof Number)
198 wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
199 if (value instanceof Character)
200 wrap = castToWrapperOrNull((char)(Character)value, dst);
201 if (wrap != null) return wrap;
202 return dst.cast(value);
203 }
204
205 static Object castToWrapperOrNull(long value, Class<?> dst) {
206 if (dst == int.class || dst == Integer.class)
207 return (int)(value);
208 if (dst == long.class || dst == Long.class)
209 return (long)(value);
210 if (dst == char.class || dst == Character.class)
211 return (char)(value);
212 if (dst == short.class || dst == Short.class)
213 return (short)(value);
214 if (dst == float.class || dst == Float.class)
215 return (float)(value);
216 if (dst == double.class || dst == Double.class)
217 return (double)(value);
jrose10f3b682010-01-07 16:16:45 -0800218 if (dst == byte.class || dst == Byte.class)
219 return (byte)(value);
220 if (dst == boolean.class || dst == boolean.class)
221 return ((value % 29) & 1) == 0;
jrose55220c32009-10-21 23:19:48 -0700222 return null;
223 }
224
jrose2cc9c832010-04-30 23:48:23 -0700225 static final int ONE_MILLION = (1000*1000), // first int value
226 TEN_BILLION = (10*1000*1000*1000), // scale factor to reach upper 32 bits
227 INITIAL_ARG_VAL = ONE_MILLION << 1; // <<1 makes space for sign bit;
228 static long nextArgVal;
229 static long nextArg(boolean moreBits) {
230 long val = nextArgVal++;
231 long sign = -(val & 1); // alternate signs
232 val >>= 1;
233 if (moreBits)
234 // Guarantee some bits in the high word.
235 // In any case keep the decimal representation simple-looking,
236 // with lots of zeroes, so as not to make the printed decimal
237 // strings unnecessarily noisy.
238 val += (val % ONE_MILLION) * TEN_BILLION;
239 return val ^ sign;
240 }
241 static int nextArg() {
242 // Produce a 32-bit result something like ONE_MILLION+(smallint).
243 // Example: 1_000_042.
244 return (int) nextArg(false);
245 }
246 static long nextArg(Class<?> kind) {
247 if (kind == long.class || kind == Long.class ||
248 kind == double.class || kind == Double.class)
249 // produce a 64-bit result something like
250 // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
251 // Example: 10_000_420_001_000_042.
252 return nextArg(true);
253 return (long) nextArg();
254 }
255
jrose55220c32009-10-21 23:19:48 -0700256 static Object randomArg(Class<?> param) {
jrose2cc9c832010-04-30 23:48:23 -0700257 Object wrap = castToWrapperOrNull(nextArg(param), param);
jrose55220c32009-10-21 23:19:48 -0700258 if (wrap != null) {
jrose55220c32009-10-21 23:19:48 -0700259 return wrap;
260 }
261// import sun.dyn.util.Wrapper;
262// Wrapper wrap = Wrapper.forBasicType(dst);
263// if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
264// wrap = Wrapper.forWrapperType(dst);
265// if (wrap != Wrapper.OBJECT)
266// return wrap.wrap(nextArg++);
jrosebe2db602010-09-08 18:40:34 -0700267 if (param.isInterface()) {
268 for (Class<?> c : param.getClasses()) {
269 if (param.isAssignableFrom(c) && !c.isInterface())
270 { param = c; break; }
271 }
272 }
jrose55220c32009-10-21 23:19:48 -0700273 if (param.isInterface() || param.isAssignableFrom(String.class))
jrose2cc9c832010-04-30 23:48:23 -0700274 return "#"+nextArg();
jrose55220c32009-10-21 23:19:48 -0700275 else
276 try {
277 return param.newInstance();
278 } catch (InstantiationException ex) {
279 } catch (IllegalAccessException ex) {
280 }
281 return null; // random class not Object, String, Integer, etc.
282 }
283 static Object[] randomArgs(Class<?>... params) {
284 Object[] args = new Object[params.length];
285 for (int i = 0; i < args.length; i++)
286 args[i] = randomArg(params[i]);
287 return args;
288 }
289 static Object[] randomArgs(int nargs, Class<?> param) {
290 Object[] args = new Object[nargs];
291 for (int i = 0; i < args.length; i++)
292 args[i] = randomArg(param);
293 return args;
294 }
295
296 static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
297 return Arrays.copyOf(a, a.length, atype);
298 }
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 static Integer[] boxAll(int... vx) {
307 Integer[] res = new Integer[vx.length];
308 for (int i = 0; i < res.length; i++) {
309 res[i] = vx[i];
310 }
311 return res;
312 }
313 static Object getClasses(Object x) {
314 if (x == null) return x;
315 if (x instanceof String) return x; // keep the name
316 if (x instanceof List) {
317 // recursively report classes of the list elements
318 Object[] xa = ((List)x).toArray();
319 for (int i = 0; i < xa.length; i++)
320 xa[i] = getClasses(xa[i]);
321 return Arrays.asList(xa);
322 }
323 return x.getClass().getSimpleName();
324 }
325
jroseadc650a2011-02-11 01:26:28 -0800326 /** Return lambda(arg...[arity]) { new Object[]{ arg... } } */
327 static MethodHandle varargsList(int arity) {
328 return ValueConversions.varargsList(arity);
329 }
330 /** Return lambda(arg...[arity]) { Arrays.asList(arg...) } */
331 static MethodHandle varargsArray(int arity) {
332 return ValueConversions.varargsArray(arity);
333 }
334 /** Variation of varargsList, but with the given rtype. */
335 static MethodHandle varargsList(int arity, Class<?> rtype) {
336 MethodHandle list = varargsList(arity);
337 MethodType listType = list.type().changeReturnType(rtype);
338 if (List.class.isAssignableFrom(rtype) || rtype == void.class || rtype == Object.class) {
339 // OK
340 } else if (rtype.isAssignableFrom(String.class)) {
341 if (LIST_TO_STRING == null)
342 try {
343 LIST_TO_STRING = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToString",
344 MethodType.methodType(String.class, List.class));
345 } catch (Exception ex) { throw new RuntimeException(ex); }
346 list = MethodHandles.filterReturnValue(list, LIST_TO_STRING);
347 } else if (rtype.isPrimitive()) {
348 if (LIST_TO_INT == null)
349 try {
350 LIST_TO_INT = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToInt",
351 MethodType.methodType(int.class, List.class));
352 } catch (Exception ex) { throw new RuntimeException(ex); }
353 list = MethodHandles.filterReturnValue(list, LIST_TO_INT);
354 list = MethodHandles.explicitCastArguments(list, listType);
355 } else {
356 throw new RuntimeException("varargsList: "+rtype);
357 }
358 return list.asType(listType);
359 }
360 private static MethodHandle LIST_TO_STRING, LIST_TO_INT;
361 private static String listToString(List x) { return x.toString(); }
362 private static int listToInt(List x) { return x.toString().hashCode(); }
363
jrose55220c32009-10-21 23:19:48 -0700364 static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
365 return changeArgTypes(target, 0, 999, argType);
366 }
367 static MethodHandle changeArgTypes(MethodHandle target,
368 int beg, int end, Class<?> argType) {
369 MethodType targetType = target.type();
370 end = Math.min(end, targetType.parameterCount());
371 ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
372 Collections.fill(argTypes.subList(beg, end), argType);
jrose10f3b682010-01-07 16:16:45 -0800373 MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
jrose55220c32009-10-21 23:19:48 -0700374 return MethodHandles.convertArguments(target, ttype2);
375 }
376
377 // This lookup is good for all members in and under MethodHandlesTest.
378 static final Lookup PRIVATE = MethodHandles.lookup();
379 // This lookup is good for package-private members but not private ones.
380 static final Lookup PACKAGE = PackageSibling.lookup();
381 // This lookup is good only for public members.
jrose10f3b682010-01-07 16:16:45 -0800382 static final Lookup PUBLIC = MethodHandles.publicLookup();
jrose55220c32009-10-21 23:19:48 -0700383
384 // Subject methods...
385 static class Example implements IntExample {
386 final String name;
jrose2cc9c832010-04-30 23:48:23 -0700387 public Example() { name = "Example#"+nextArg(); }
jrose55220c32009-10-21 23:19:48 -0700388 protected Example(String name) { this.name = name; }
389 protected Example(int x) { this(); called("protected <init>", this, x); }
390 @Override public String toString() { return name; }
391
392 public void v0() { called("v0", this); }
393 void pkg_v0() { called("pkg_v0", this); }
394 private void pri_v0() { called("pri_v0", this); }
395 public static void s0() { called("s0"); }
396 static void pkg_s0() { called("pkg_s0"); }
397 private static void pri_s0() { called("pri_s0"); }
398
399 public Object v1(Object x) { return called("v1", this, x); }
400 public Object v2(Object x, Object y) { return called("v2", this, x, y); }
401 public Object v2(Object x, int y) { return called("v2", this, x, y); }
402 public Object v2(int x, Object y) { return called("v2", this, x, y); }
403 public Object v2(int x, int y) { return called("v2", this, x, y); }
404 public static Object s1(Object x) { return called("s1", x); }
405 public static Object s2(int x) { return called("s2", x); }
406 public static Object s3(long x) { return called("s3", x); }
407 public static Object s4(int x, int y) { return called("s4", x, y); }
408 public static Object s5(long x, int y) { return called("s5", x, y); }
409 public static Object s6(int x, long y) { return called("s6", x, y); }
410 public static Object s7(float x, double y) { return called("s7", x, y); }
jrose2cc9c832010-04-30 23:48:23 -0700411
412 static final Lookup EXAMPLE = MethodHandles.lookup(); // for testing findSpecial
jrose55220c32009-10-21 23:19:48 -0700413 }
jrose2cc9c832010-04-30 23:48:23 -0700414 static final Lookup EXAMPLE = Example.EXAMPLE;
jrose55220c32009-10-21 23:19:48 -0700415 public static class PubExample extends Example {
jrose2cc9c832010-04-30 23:48:23 -0700416 public PubExample() { super("PubExample#"+nextArg()); }
jrose55220c32009-10-21 23:19:48 -0700417 }
418 static class SubExample extends Example {
419 @Override public void v0() { called("Sub/v0", this); }
420 @Override void pkg_v0() { called("Sub/pkg_v0", this); }
421 private SubExample(int x) { called("<init>", this, x); }
jrose2cc9c832010-04-30 23:48:23 -0700422 public SubExample() { super("SubExample#"+nextArg()); }
jrose55220c32009-10-21 23:19:48 -0700423 }
424 public static interface IntExample {
425 public void v0();
jrosebe2db602010-09-08 18:40:34 -0700426 public static class Impl implements IntExample {
jrose55220c32009-10-21 23:19:48 -0700427 public void v0() { called("Int/v0", this); }
428 final String name;
jrose2cc9c832010-04-30 23:48:23 -0700429 public Impl() { name = "Impl#"+nextArg(); }
430 @Override public String toString() { return name; }
jrose55220c32009-10-21 23:19:48 -0700431 }
432 }
433
434 static final Object[][][] ACCESS_CASES = {
jrose2cc9c832010-04-30 23:48:23 -0700435 { { false, PUBLIC }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false
436 { { false, PUBLIC }, { false, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[1]: only PRIVATE
437 { { false, PUBLIC }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[2]: PUBLIC false
438 { { true, PUBLIC }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[3]: all true
jrose55220c32009-10-21 23:19:48 -0700439 };
440
jrose2cc9c832010-04-30 23:48:23 -0700441 static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) {
442 Object[][] cases;
443 if (name.contains("pri_") || isSpecial) {
444 cases = ACCESS_CASES[1]; // PRIVATE only
445 } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) {
446 cases = ACCESS_CASES[2]; // not PUBLIC
jrose10f3b682010-01-07 16:16:45 -0800447 } else {
448 assertTrue(name.indexOf('_') < 0);
449 boolean pubc = Modifier.isPublic(defc.getModifiers());
450 if (pubc)
jrose2cc9c832010-04-30 23:48:23 -0700451 cases = ACCESS_CASES[3]; // all access levels
452 else
453 cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC
jrose10f3b682010-01-07 16:16:45 -0800454 }
jrose2cc9c832010-04-30 23:48:23 -0700455 if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE)
456 cases = Arrays.copyOfRange(cases, 0, cases.length-1);
457 return cases;
458 }
459 static Object[][] accessCases(Class<?> defc, String name) {
460 return accessCases(defc, name, false);
jrose55220c32009-10-21 23:19:48 -0700461 }
462
463 @Test
464 public void testFindStatic() throws Throwable {
465 if (CAN_SKIP_WORKING) return;
466 startTest("findStatic");
467 testFindStatic(PubExample.class, void.class, "s0");
468 testFindStatic(Example.class, void.class, "s0");
469 testFindStatic(Example.class, void.class, "pkg_s0");
470 testFindStatic(Example.class, void.class, "pri_s0");
471
472 testFindStatic(Example.class, Object.class, "s1", Object.class);
473 testFindStatic(Example.class, Object.class, "s2", int.class);
474 testFindStatic(Example.class, Object.class, "s3", long.class);
475 testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
476 testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
477 testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
478 testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
479
480 testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
481 }
482
483 void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
484 for (Object[] ac : accessCases(defc, name)) {
485 testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
486 }
487 }
488 void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
489 testFindStatic(true, lookup, defc, ret, name, params);
490 }
491 void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
492 countTest(positive);
jrose10f3b682010-01-07 16:16:45 -0800493 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700494 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700495 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700496 try {
jrose2cc9c832010-04-30 23:48:23 -0700497 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700498 target = lookup.in(defc).findStatic(defc, name, type);
jrosef15905c2011-02-11 01:26:32 -0800499 } catch (ReflectiveOperationException ex) {
jrose55220c32009-10-21 23:19:48 -0700500 noAccess = ex;
jrosef15905c2011-02-11 01:26:32 -0800501 if (name.contains("bogus"))
502 assertTrue(noAccess instanceof NoSuchMethodException);
503 else
504 assertTrue(noAccess instanceof IllegalAccessException);
jrose55220c32009-10-21 23:19:48 -0700505 }
jrose2cc9c832010-04-30 23:48:23 -0700506 if (verbosity >= 3)
507 System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
jrose55220c32009-10-21 23:19:48 -0700508 +(noAccess == null ? "" : " !! "+noAccess));
509 if (positive && noAccess != null) throw noAccess;
510 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
511 if (!positive) return; // negative test failed as expected
512 assertEquals(type, target.type());
jrose900bafd2010-10-30 21:08:23 -0700513 assertNameStringContains(target, name);
jrose55220c32009-10-21 23:19:48 -0700514 if (!DO_MORE_CALLS && lookup != PRIVATE) return;
515 Object[] args = randomArgs(params);
516 printCalled(target, name, args);
jrose900bafd2010-10-30 21:08:23 -0700517 target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -0700518 assertCalled(name, args);
jrose2cc9c832010-04-30 23:48:23 -0700519 if (verbosity >= 1)
520 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700521 }
522
jrose900bafd2010-10-30 21:08:23 -0700523 // rough check of name string
524 static void assertNameStringContains(Object x, String s) {
525 if (x.toString().contains(s)) return;
526 assertEquals(s, x);
527 }
528
jrose55220c32009-10-21 23:19:48 -0700529 @Test
530 public void testFindVirtual() throws Throwable {
531 if (CAN_SKIP_WORKING) return;
532 startTest("findVirtual");
533 testFindVirtual(Example.class, void.class, "v0");
534 testFindVirtual(Example.class, void.class, "pkg_v0");
535 testFindVirtual(Example.class, void.class, "pri_v0");
536 testFindVirtual(Example.class, Object.class, "v1", Object.class);
537 testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
538 testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
539 testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
540 testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
541 testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
542 // test dispatch
543 testFindVirtual(SubExample.class, SubExample.class, void.class, "Sub/v0");
544 testFindVirtual(SubExample.class, Example.class, void.class, "Sub/v0");
545 testFindVirtual(SubExample.class, IntExample.class, void.class, "Sub/v0");
546 testFindVirtual(SubExample.class, SubExample.class, void.class, "Sub/pkg_v0");
547 testFindVirtual(SubExample.class, Example.class, void.class, "Sub/pkg_v0");
548 testFindVirtual(Example.class, IntExample.class, void.class, "v0");
549 testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
550 }
551
552 void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
553 Class<?> rcvc = defc;
554 testFindVirtual(rcvc, defc, ret, name, params);
555 }
556 void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
557 for (Object[] ac : accessCases(defc, name)) {
558 testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params);
559 }
560 }
561 void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
562 testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
563 }
564 void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
565 countTest(positive);
566 String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
jrose10f3b682010-01-07 16:16:45 -0800567 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700568 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700569 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700570 try {
jrose2cc9c832010-04-30 23:48:23 -0700571 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700572 target = lookup.in(defc).findVirtual(defc, methodName, type);
jrosef15905c2011-02-11 01:26:32 -0800573 } catch (ReflectiveOperationException ex) {
jrose55220c32009-10-21 23:19:48 -0700574 noAccess = ex;
jrosef15905c2011-02-11 01:26:32 -0800575 if (name.contains("bogus"))
576 assertTrue(noAccess instanceof NoSuchMethodException);
577 else
578 assertTrue(noAccess instanceof IllegalAccessException);
jrose55220c32009-10-21 23:19:48 -0700579 }
jrose2cc9c832010-04-30 23:48:23 -0700580 if (verbosity >= 3)
581 System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
jrose55220c32009-10-21 23:19:48 -0700582 +(noAccess == null ? "" : " !! "+noAccess));
583 if (positive && noAccess != null) throw noAccess;
584 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
585 if (!positive) return; // negative test failed as expected
586 Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
jrose10f3b682010-01-07 16:16:45 -0800587 MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700588 assertEquals(typeWithSelf, target.type());
jrose900bafd2010-10-30 21:08:23 -0700589 assertNameStringContains(target, methodName);
jrose55220c32009-10-21 23:19:48 -0700590 if (!DO_MORE_CALLS && lookup != PRIVATE) return;
591 Object[] argsWithSelf = randomArgs(paramsWithSelf);
592 if (rcvc != defc) argsWithSelf[0] = randomArg(rcvc);
593 printCalled(target, name, argsWithSelf);
jrose900bafd2010-10-30 21:08:23 -0700594 target.invokeWithArguments(argsWithSelf);
jrose55220c32009-10-21 23:19:48 -0700595 assertCalled(name, argsWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700596 if (verbosity >= 1)
597 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700598 }
599
600 @Test
601 public void testFindSpecial() throws Throwable {
602 if (CAN_SKIP_WORKING) return;
603 startTest("findSpecial");
jrose2cc9c832010-04-30 23:48:23 -0700604 testFindSpecial(SubExample.class, Example.class, void.class, "v0");
605 testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
606 // Do some negative testing:
jrosef15905c2011-02-11 01:26:32 -0800607 testFindSpecial(false, EXAMPLE, SubExample.class, Example.class, void.class, "bogus");
608 testFindSpecial(false, PRIVATE, SubExample.class, Example.class, void.class, "bogus");
jrose2cc9c832010-04-30 23:48:23 -0700609 for (Lookup lookup : new Lookup[]{ PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
610 testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
611 testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
612 testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
jrose2cc9c832010-04-30 23:48:23 -0700613 }
jrose55220c32009-10-21 23:19:48 -0700614 }
615
jrose2cc9c832010-04-30 23:48:23 -0700616 void testFindSpecial(Class<?> specialCaller,
617 Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
618 testFindSpecial(true, EXAMPLE, specialCaller, defc, ret, name, params);
619 testFindSpecial(true, PRIVATE, specialCaller, defc, ret, name, params);
620 testFindSpecial(false, PACKAGE, specialCaller, defc, ret, name, params);
621 testFindSpecial(false, PUBLIC, specialCaller, defc, ret, name, params);
jrose55220c32009-10-21 23:19:48 -0700622 }
jrose2cc9c832010-04-30 23:48:23 -0700623 void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
624 Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700625 countTest(positive);
jrose10f3b682010-01-07 16:16:45 -0800626 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700627 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700628 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700629 try {
jrose2cc9c832010-04-30 23:48:23 -0700630 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700631 if (verbosity >= 5) System.out.println(" lookup => "+lookup.in(specialCaller));
632 target = lookup.in(specialCaller).findSpecial(defc, name, type, specialCaller);
jrosef15905c2011-02-11 01:26:32 -0800633 } catch (ReflectiveOperationException ex) {
jrose55220c32009-10-21 23:19:48 -0700634 noAccess = ex;
jrosef15905c2011-02-11 01:26:32 -0800635 if (name.contains("bogus"))
636 assertTrue(noAccess instanceof NoSuchMethodException);
637 else
638 assertTrue(noAccess instanceof IllegalAccessException);
jrose55220c32009-10-21 23:19:48 -0700639 }
jrose2cc9c832010-04-30 23:48:23 -0700640 if (verbosity >= 3)
641 System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
642 +(target == null ? "" : target.type())
643 +(noAccess == null ? "" : " !! "+noAccess));
jrose55220c32009-10-21 23:19:48 -0700644 if (positive && noAccess != null) throw noAccess;
645 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
646 if (!positive) return; // negative test failed as expected
jrose2cc9c832010-04-30 23:48:23 -0700647 assertEquals(specialCaller, target.type().parameterType(0));
648 assertEquals(type, target.type().dropParameterTypes(0,1));
649 Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
jrose10f3b682010-01-07 16:16:45 -0800650 MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
jrose900bafd2010-10-30 21:08:23 -0700651 assertNameStringContains(target, name);
jrose2cc9c832010-04-30 23:48:23 -0700652 if (!DO_MORE_CALLS && lookup != PRIVATE && lookup != EXAMPLE) return;
jrose55220c32009-10-21 23:19:48 -0700653 Object[] args = randomArgs(paramsWithSelf);
654 printCalled(target, name, args);
jrose900bafd2010-10-30 21:08:23 -0700655 target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -0700656 assertCalled(name, args);
jrose55220c32009-10-21 23:19:48 -0700657 }
658
659 @Test
660 public void testBind() throws Throwable {
661 if (CAN_SKIP_WORKING) return;
662 startTest("bind");
663 testBind(Example.class, void.class, "v0");
664 testBind(Example.class, void.class, "pkg_v0");
665 testBind(Example.class, void.class, "pri_v0");
666 testBind(Example.class, Object.class, "v1", Object.class);
667 testBind(Example.class, Object.class, "v2", Object.class, Object.class);
668 testBind(Example.class, Object.class, "v2", Object.class, int.class);
669 testBind(Example.class, Object.class, "v2", int.class, Object.class);
670 testBind(Example.class, Object.class, "v2", int.class, int.class);
671 testBind(false, PRIVATE, Example.class, void.class, "bogus");
672 testBind(SubExample.class, void.class, "Sub/v0");
673 testBind(SubExample.class, void.class, "Sub/pkg_v0");
674 testBind(IntExample.Impl.class, void.class, "Int/v0");
675 }
676
677 void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
678 for (Object[] ac : accessCases(defc, name)) {
679 testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
680 }
681 }
682
683 void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
684 countTest(positive);
685 String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
jrose10f3b682010-01-07 16:16:45 -0800686 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700687 Object receiver = randomArg(defc);
688 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700689 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700690 try {
jrose2cc9c832010-04-30 23:48:23 -0700691 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700692 target = lookup.in(defc).bind(receiver, methodName, type);
jrosef15905c2011-02-11 01:26:32 -0800693 } catch (ReflectiveOperationException ex) {
jrose55220c32009-10-21 23:19:48 -0700694 noAccess = ex;
jrosef15905c2011-02-11 01:26:32 -0800695 if (name.contains("bogus"))
696 assertTrue(noAccess instanceof NoSuchMethodException);
697 else
698 assertTrue(noAccess instanceof IllegalAccessException);
jrose55220c32009-10-21 23:19:48 -0700699 }
jrose2cc9c832010-04-30 23:48:23 -0700700 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -0700701 System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
702 +(noAccess == null ? "" : " !! "+noAccess));
703 if (positive && noAccess != null) throw noAccess;
704 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
705 if (!positive) return; // negative test failed as expected
706 assertEquals(type, target.type());
707 Object[] args = randomArgs(params);
708 printCalled(target, name, args);
jrose900bafd2010-10-30 21:08:23 -0700709 target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -0700710 Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
711 assertCalled(name, argsWithReceiver);
jrose2cc9c832010-04-30 23:48:23 -0700712 if (verbosity >= 1)
713 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700714 }
715
716 @Test
717 public void testUnreflect() throws Throwable {
718 if (CAN_SKIP_WORKING) return;
719 startTest("unreflect");
720 testUnreflect(Example.class, true, void.class, "s0");
721 testUnreflect(Example.class, true, void.class, "pkg_s0");
722 testUnreflect(Example.class, true, void.class, "pri_s0");
723
724 testUnreflect(Example.class, true, Object.class, "s1", Object.class);
725 testUnreflect(Example.class, true, Object.class, "s2", int.class);
jrose2cc9c832010-04-30 23:48:23 -0700726 testUnreflect(Example.class, true, Object.class, "s3", long.class);
727 testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class);
728 testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class);
729 testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class);
jrose55220c32009-10-21 23:19:48 -0700730
731 testUnreflect(Example.class, false, void.class, "v0");
732 testUnreflect(Example.class, false, void.class, "pkg_v0");
733 testUnreflect(Example.class, false, void.class, "pri_v0");
734 testUnreflect(Example.class, false, Object.class, "v1", Object.class);
735 testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
736 testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
737 testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
738 testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
739 }
740
741 void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
742 for (Object[] ac : accessCases(defc, name)) {
jrose2cc9c832010-04-30 23:48:23 -0700743 testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, (isStatic ? null : defc), ret, name, params);
jrose55220c32009-10-21 23:19:48 -0700744 }
745 }
jrose2cc9c832010-04-30 23:48:23 -0700746 void testUnreflect(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
747 for (Object[] ac : accessCases(defc, name)) {
748 testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
749 }
750 }
751 void testUnreflectMaybeSpecial(Class<?> specialCaller,
752 boolean positive, Lookup lookup,
753 Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700754 countTest(positive);
jrose10f3b682010-01-07 16:16:45 -0800755 MethodType type = MethodType.methodType(ret, params);
jrosef15905c2011-02-11 01:26:32 -0800756 Method rmethod = defc.getDeclaredMethod(name, params);
jrose55220c32009-10-21 23:19:48 -0700757 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700758 Exception noAccess = null;
jrose2cc9c832010-04-30 23:48:23 -0700759 boolean isStatic = (rcvc == null);
760 boolean isSpecial = (specialCaller != null);
jrose55220c32009-10-21 23:19:48 -0700761 try {
jrose2cc9c832010-04-30 23:48:23 -0700762 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
763 if (isSpecial)
jrose900bafd2010-10-30 21:08:23 -0700764 target = lookup.in(specialCaller).unreflectSpecial(rmethod, specialCaller);
jrose2cc9c832010-04-30 23:48:23 -0700765 else
jrose900bafd2010-10-30 21:08:23 -0700766 target = lookup.in(defc).unreflect(rmethod);
jrosef15905c2011-02-11 01:26:32 -0800767 } catch (ReflectiveOperationException ex) {
jrose55220c32009-10-21 23:19:48 -0700768 noAccess = ex;
jrosef15905c2011-02-11 01:26:32 -0800769 if (name.contains("bogus"))
770 assertTrue(noAccess instanceof NoSuchMethodException);
771 else
772 assertTrue(noAccess instanceof IllegalAccessException);
jrose55220c32009-10-21 23:19:48 -0700773 }
jrose2cc9c832010-04-30 23:48:23 -0700774 if (verbosity >= 3)
775 System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
776 +(!isSpecial ? "" : " specialCaller="+specialCaller)
777 +( isStatic ? "" : " receiver="+rcvc)
778 +" => "+target
779 +(noAccess == null ? "" : " !! "+noAccess));
jrose55220c32009-10-21 23:19:48 -0700780 if (positive && noAccess != null) throw noAccess;
781 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
782 if (!positive) return; // negative test failed as expected
jrose2cc9c832010-04-30 23:48:23 -0700783 assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
jrose55220c32009-10-21 23:19:48 -0700784 Class<?>[] paramsMaybeWithSelf = params;
785 if (!isStatic) {
jrose2cc9c832010-04-30 23:48:23 -0700786 paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
jrose55220c32009-10-21 23:19:48 -0700787 }
jrose10f3b682010-01-07 16:16:45 -0800788 MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700789 if (isStatic) {
790 assertEquals(typeMaybeWithSelf, target.type());
791 } else {
jrose2cc9c832010-04-30 23:48:23 -0700792 if (isSpecial)
793 assertEquals(specialCaller, target.type().parameterType(0));
794 else
795 assertEquals(defc, target.type().parameterType(0));
796 assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
797 }
jrose55220c32009-10-21 23:19:48 -0700798 Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
799 printCalled(target, name, argsMaybeWithSelf);
jrose900bafd2010-10-30 21:08:23 -0700800 target.invokeWithArguments(argsMaybeWithSelf);
jrose55220c32009-10-21 23:19:48 -0700801 assertCalled(name, argsMaybeWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700802 if (verbosity >= 1)
803 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700804 }
805
jrose2cc9c832010-04-30 23:48:23 -0700806 void testUnreflectSpecial(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
807 for (Object[] ac : accessCases(defc, name, true)) {
808 Class<?> specialCaller = rcvc;
809 testUnreflectMaybeSpecial(specialCaller, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
810 }
811 }
812
813 @Test
jrose55220c32009-10-21 23:19:48 -0700814 public void testUnreflectSpecial() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -0700815 if (CAN_SKIP_WORKING) return;
jrose55220c32009-10-21 23:19:48 -0700816 startTest("unreflectSpecial");
jrose2cc9c832010-04-30 23:48:23 -0700817 testUnreflectSpecial(Example.class, Example.class, void.class, "v0");
818 testUnreflectSpecial(Example.class, SubExample.class, void.class, "v0");
819 testUnreflectSpecial(Example.class, Example.class, void.class, "pkg_v0");
820 testUnreflectSpecial(Example.class, SubExample.class, void.class, "pkg_v0");
821 testUnreflectSpecial(Example.class, Example.class, Object.class, "v2", int.class, int.class);
822 testUnreflectSpecial(Example.class, SubExample.class, Object.class, "v2", int.class, int.class);
823 testUnreflectMaybeSpecial(Example.class, false, PRIVATE, Example.class, Example.class, void.class, "s0");
jrose55220c32009-10-21 23:19:48 -0700824 }
825
jrose10f3b682010-01-07 16:16:45 -0800826 public static class HasFields {
827 boolean fZ = false;
828 byte fB = (byte)'B';
829 short fS = (short)'S';
830 char fC = 'C';
831 int fI = 'I';
832 long fJ = 'J';
833 float fF = 'F';
834 double fD = 'D';
835 static boolean sZ = true;
836 static byte sB = 1+(byte)'B';
837 static short sS = 1+(short)'S';
838 static char sC = 1+'C';
839 static int sI = 1+'I';
840 static long sJ = 1+'J';
841 static float sF = 1+'F';
842 static double sD = 1+'D';
843
844 Object fL = 'L';
845 String fR = "R";
846 static Object sL = 'M';
847 static String sR = "S";
848
849 static final Object[][] CASES;
850 static {
851 ArrayList<Object[]> cases = new ArrayList<Object[]>();
852 Object types[][] = {
853 {'L',Object.class}, {'R',String.class},
854 {'I',int.class}, {'J',long.class},
855 {'F',float.class}, {'D',double.class},
856 {'Z',boolean.class}, {'B',byte.class},
857 {'S',short.class}, {'C',char.class},
858 };
859 HasFields fields = new HasFields();
860 for (Object[] t : types) {
861 for (int kind = 0; kind <= 1; kind++) {
862 boolean isStatic = (kind != 0);
863 char btc = (Character)t[0];
864 String name = (isStatic ? "s" : "f") + btc;
865 Class<?> type = (Class<?>) t[1];
866 Object value;
867 Field field;
868 try {
869 field = HasFields.class.getDeclaredField(name);
870 } catch (Exception ex) {
871 throw new InternalError("no field HasFields."+name);
872 }
873 try {
874 value = field.get(fields);
875 } catch (Exception ex) {
876 throw new InternalError("cannot fetch field HasFields."+name);
877 }
878 if (type == float.class) {
879 float v = 'F';
880 if (isStatic) v++;
jrosef15905c2011-02-11 01:26:32 -0800881 assertTrue(value.equals(v));
jrose10f3b682010-01-07 16:16:45 -0800882 }
jrosef15905c2011-02-11 01:26:32 -0800883 assertTrue(name.equals(field.getName()));
884 assertTrue(type.equals(field.getType()));
885 assertTrue(isStatic == (Modifier.isStatic(field.getModifiers())));
jrose10f3b682010-01-07 16:16:45 -0800886 cases.add(new Object[]{ field, value });
887 }
888 }
jrosef15905c2011-02-11 01:26:32 -0800889 cases.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class });
890 cases.add(new Object[]{ new Object[]{ true, HasFields.class, "bogus_sL", Object.class }, Error.class });
jrose10f3b682010-01-07 16:16:45 -0800891 CASES = cases.toArray(new Object[0][]);
892 }
893 }
894
jrosef15905c2011-02-11 01:26:32 -0800895 static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10;
jrosecf98d422010-06-08 23:08:56 -0700896 static boolean testModeMatches(int testMode, boolean isStatic) {
897 switch (testMode) {
jrosef15905c2011-02-11 01:26:32 -0800898 case TEST_FIND_STATIC: return isStatic;
jrosecf98d422010-06-08 23:08:56 -0700899 case TEST_FIND_FIELD: return !isStatic;
jrosef15905c2011-02-11 01:26:32 -0800900 case TEST_UNREFLECT: return true; // unreflect matches both
jrosecf98d422010-06-08 23:08:56 -0700901 }
jrosef15905c2011-02-11 01:26:32 -0800902 throw new InternalError("testMode="+testMode);
jrosecf98d422010-06-08 23:08:56 -0700903 }
904
jrose10f3b682010-01-07 16:16:45 -0800905 @Test
jrose55220c32009-10-21 23:19:48 -0700906 public void testUnreflectGetter() throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700907 startTest("unreflectGetter");
jrosecf98d422010-06-08 23:08:56 -0700908 testGetter(TEST_UNREFLECT);
909 }
910 @Test
911 public void testFindGetter() throws Throwable {
912 startTest("findGetter");
913 testGetter(TEST_FIND_FIELD);
914 }
915 @Test
916 public void testFindStaticGetter() throws Throwable {
917 startTest("findStaticGetter");
jrosef15905c2011-02-11 01:26:32 -0800918 testGetter(TEST_FIND_STATIC);
jrosecf98d422010-06-08 23:08:56 -0700919 }
920 public void testGetter(int testMode) throws Throwable {
921 Lookup lookup = PRIVATE; // FIXME: test more lookups than this one
jrose10f3b682010-01-07 16:16:45 -0800922 for (Object[] c : HasFields.CASES) {
jrosef15905c2011-02-11 01:26:32 -0800923 boolean positive = (c[1] != Error.class);
924 testGetter(positive, lookup, c[0], c[1], testMode);
925 }
926 testGetter(true, lookup,
927 new Object[]{ true, System.class, "out", java.io.PrintStream.class },
928 System.out, testMode);
929 for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
930 testGetter(false, lookup,
931 new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
932 null, testMode);
jrose10f3b682010-01-07 16:16:45 -0800933 }
934 }
jrosef15905c2011-02-11 01:26:32 -0800935 public void testGetter(boolean positive, MethodHandles.Lookup lookup,
936 Object fieldRef, Object value, int testMode) throws Throwable {
937 testAccessor(positive, lookup, fieldRef, value, testMode);
938 }
939
940 public void testAccessor(boolean positive, MethodHandles.Lookup lookup,
941 Object fieldRef, Object value, int testMode0) throws Throwable {
942 boolean isGetter = ((testMode0 & TEST_SETTER) == 0);
943 int testMode = testMode0 & ~TEST_SETTER;
944 boolean isStatic;
945 Class<?> fclass;
946 String fname;
947 Class<?> ftype;
948 Field f = (fieldRef instanceof Field ? (Field)fieldRef : null);
949 if (f != null) {
950 isStatic = Modifier.isStatic(f.getModifiers());
951 fclass = f.getDeclaringClass();
952 fname = f.getName();
953 ftype = f.getType();
954 } else {
955 Object[] scnt = (Object[]) fieldRef;
956 isStatic = (Boolean) scnt[0];
957 fclass = (Class<?>) scnt[1];
958 fname = (String) scnt[2];
959 ftype = (Class<?>) scnt[3];
960 try {
961 f = fclass.getDeclaredField(fname);
962 } catch (ReflectiveOperationException ex) {
963 f = null;
964 }
965 }
jrosecf98d422010-06-08 23:08:56 -0700966 if (!testModeMatches(testMode, isStatic)) return;
jrosef15905c2011-02-11 01:26:32 -0800967 if (f == null && testMode == TEST_UNREFLECT) return;
968 countTest(positive);
969 MethodType expType;
970 if (isGetter)
971 expType = MethodType.methodType(ftype, HasFields.class);
972 else
973 expType = MethodType.methodType(void.class, HasFields.class, ftype);
jrose10f3b682010-01-07 16:16:45 -0800974 if (isStatic) expType = expType.dropParameterTypes(0, 1);
jrosef15905c2011-02-11 01:26:32 -0800975 Exception noAccess = null;
976 MethodHandle mh;
977 try {
978 switch (testMode0) {
979 case TEST_UNREFLECT: mh = lookup.unreflectGetter(f); break;
980 case TEST_FIND_FIELD: mh = lookup.findGetter(fclass, fname, ftype); break;
981 case TEST_FIND_STATIC: mh = lookup.findStaticGetter(fclass, fname, ftype); break;
982 case TEST_SETTER|
983 TEST_UNREFLECT: mh = lookup.unreflectSetter(f); break;
984 case TEST_SETTER|
985 TEST_FIND_FIELD: mh = lookup.findSetter(fclass, fname, ftype); break;
986 case TEST_SETTER|
987 TEST_FIND_STATIC: mh = lookup.findStaticSetter(fclass, fname, ftype); break;
988 default:
989 throw new InternalError("testMode="+testMode);
990 }
991 } catch (ReflectiveOperationException ex) {
992 mh = null;
993 noAccess = ex;
994 if (fname.contains("bogus"))
995 assertTrue(noAccess instanceof NoSuchFieldException);
996 else
997 assertTrue(noAccess instanceof IllegalAccessException);
998 }
999 if (verbosity >= 3)
1000 System.out.println("find"+(isStatic?"Static":"")+(isGetter?"Getter":"Setter")+" "+fclass.getName()+"."+fname+"/"+ftype
1001 +" => "+mh
1002 +(noAccess == null ? "" : " !! "+noAccess));
1003 if (positive && noAccess != null) throw new RuntimeException(noAccess);
1004 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, mh != null);
1005 if (!positive) return; // negative test failed as expected
1006 assertEquals((isStatic ? 0 : 1)+(isGetter ? 0 : 1), mh.type().parameterCount());
1007
1008
jrose10f3b682010-01-07 16:16:45 -08001009 assertSame(mh.type(), expType);
jrose900bafd2010-10-30 21:08:23 -07001010 assertNameStringContains(mh, fname);
jrose10f3b682010-01-07 16:16:45 -08001011 HasFields fields = new HasFields();
1012 Object sawValue;
jrosef15905c2011-02-11 01:26:32 -08001013 Class<?> vtype = ftype;
1014 if (ftype != int.class) vtype = Object.class;
1015 if (isGetter) {
1016 mh = MethodHandles.convertArguments(mh, mh.type().generic()
1017 .changeReturnType(vtype));
1018 } else {
1019 int last = mh.type().parameterCount() - 1;
1020 mh = MethodHandles.convertArguments(mh, mh.type().generic()
1021 .changeReturnType(void.class)
1022 .changeParameterType(last, vtype));
jrose10f3b682010-01-07 16:16:45 -08001023 }
jrosef15905c2011-02-11 01:26:32 -08001024 if (f != null && f.getDeclaringClass() == HasFields.class) {
1025 assertEquals(f.get(fields), value); // clean to start with
1026 }
1027 if (isGetter) {
1028 Object expValue = value;
1029 for (int i = 0; i <= 1; i++) {
1030 if (isStatic) {
1031 if (ftype == int.class)
1032 sawValue = (int) mh.invokeExact(); // do these exactly
1033 else
1034 sawValue = mh.invokeExact();
1035 } else {
1036 if (ftype == int.class)
1037 sawValue = (int) mh.invokeExact((Object) fields);
1038 else
1039 sawValue = mh.invokeExact((Object) fields);
1040 }
1041 assertEquals(sawValue, expValue);
1042 if (f != null && f.getDeclaringClass() == HasFields.class
1043 && !Modifier.isFinal(f.getModifiers())) {
1044 Object random = randomArg(ftype);
1045 f.set(fields, random);
1046 expValue = random;
1047 } else {
1048 break;
1049 }
1050 }
1051 } else {
1052 for (int i = 0; i <= 1; i++) {
1053 Object putValue = randomArg(ftype);
1054 if (isStatic) {
1055 if (ftype == int.class)
1056 mh.invokeExact((int)putValue); // do these exactly
1057 else
1058 mh.invokeExact(putValue);
1059 } else {
1060 if (ftype == int.class)
1061 mh.invokeExact((Object) fields, (int)putValue);
1062 else
1063 mh.invokeExact((Object) fields, putValue);
1064 }
1065 if (f != null && f.getDeclaringClass() == HasFields.class) {
1066 assertEquals(f.get(fields), putValue);
1067 }
1068 }
1069 }
1070 if (f != null && f.getDeclaringClass() == HasFields.class) {
1071 f.set(fields, value); // put it back
1072 }
jrose55220c32009-10-21 23:19:48 -07001073 }
1074
jrose10f3b682010-01-07 16:16:45 -08001075
1076 @Test
jrose55220c32009-10-21 23:19:48 -07001077 public void testUnreflectSetter() throws Throwable {
jrosecf98d422010-06-08 23:08:56 -07001078 startTest("unreflectSetter");
1079 testSetter(TEST_UNREFLECT);
1080 }
1081 @Test
1082 public void testFindSetter() throws Throwable {
1083 startTest("findSetter");
1084 testSetter(TEST_FIND_FIELD);
1085 }
1086 @Test
1087 public void testFindStaticSetter() throws Throwable {
1088 startTest("findStaticSetter");
jrosef15905c2011-02-11 01:26:32 -08001089 testSetter(TEST_FIND_STATIC);
jrosecf98d422010-06-08 23:08:56 -07001090 }
1091 public void testSetter(int testMode) throws Throwable {
jrose55220c32009-10-21 23:19:48 -07001092 Lookup lookup = PRIVATE; // FIXME: test more lookups than this one
1093 startTest("unreflectSetter");
jrose10f3b682010-01-07 16:16:45 -08001094 for (Object[] c : HasFields.CASES) {
jrosef15905c2011-02-11 01:26:32 -08001095 boolean positive = (c[1] != Error.class);
1096 testSetter(positive, lookup, c[0], c[1], testMode);
1097 }
1098 for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
1099 testSetter(false, lookup,
1100 new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
1101 null, testMode);
jrose10f3b682010-01-07 16:16:45 -08001102 }
1103 }
jrosef15905c2011-02-11 01:26:32 -08001104 public void testSetter(boolean positive, MethodHandles.Lookup lookup,
1105 Object fieldRef, Object value, int testMode) throws Throwable {
1106 testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER);
jrose55220c32009-10-21 23:19:48 -07001107 }
1108
jrose10f3b682010-01-07 16:16:45 -08001109 @Test
jrose55220c32009-10-21 23:19:48 -07001110 public void testArrayElementGetter() throws Throwable {
1111 startTest("arrayElementGetter");
jrose2cc9c832010-04-30 23:48:23 -07001112 testArrayElementGetterSetter(false);
jrose55220c32009-10-21 23:19:48 -07001113 }
1114
jrose10f3b682010-01-07 16:16:45 -08001115 @Test
jrose55220c32009-10-21 23:19:48 -07001116 public void testArrayElementSetter() throws Throwable {
1117 startTest("arrayElementSetter");
jrose2cc9c832010-04-30 23:48:23 -07001118 testArrayElementGetterSetter(true);
1119 }
1120
1121 public void testArrayElementGetterSetter(boolean testSetter) throws Throwable {
1122 testArrayElementGetterSetter(new Object[10], testSetter);
1123 testArrayElementGetterSetter(new String[10], testSetter);
1124 testArrayElementGetterSetter(new boolean[10], testSetter);
1125 testArrayElementGetterSetter(new byte[10], testSetter);
1126 testArrayElementGetterSetter(new char[10], testSetter);
1127 testArrayElementGetterSetter(new short[10], testSetter);
1128 testArrayElementGetterSetter(new int[10], testSetter);
1129 testArrayElementGetterSetter(new float[10], testSetter);
1130 testArrayElementGetterSetter(new long[10], testSetter);
1131 testArrayElementGetterSetter(new double[10], testSetter);
jrose10f3b682010-01-07 16:16:45 -08001132 }
1133
1134 public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable {
1135 countTest(true);
jrose2cc9c832010-04-30 23:48:23 -07001136 if (verbosity >= 2) System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+Array.getLength(array)+"]");
jrose10f3b682010-01-07 16:16:45 -08001137 Class<?> arrayType = array.getClass();
1138 Class<?> elemType = arrayType.getComponentType();
1139 MethodType expType = !testSetter
1140 ? MethodType.methodType(elemType, arrayType, int.class)
1141 : MethodType.methodType(void.class, arrayType, int.class, elemType);
1142 MethodHandle mh = !testSetter
1143 ? MethodHandles.arrayElementGetter(arrayType)
1144 : MethodHandles.arrayElementSetter(arrayType);
1145 assertSame(mh.type(), expType);
jrose2cc9c832010-04-30 23:48:23 -07001146 if (elemType != int.class && elemType != boolean.class) {
1147 MethodType gtype;
1148 if (true) { // FIXME: remove this path (and remove <void> below in the mh.invokes)
1149 gtype = mh.type().changeParameterType(0, Object.class);
1150 if (testSetter)
1151 gtype = gtype.changeParameterType(2, Object.class);
1152 else
1153 gtype = gtype.changeReturnType(Object.class);
1154 } else
1155 // FIXME: This simpler path hits a bug in convertArguments => ToGeneric
1156 gtype = mh.type().generic().changeParameterType(1, int.class);
1157 mh = MethodHandles.convertArguments(mh, gtype);
1158 }
jrose10f3b682010-01-07 16:16:45 -08001159 Object sawValue, expValue;
1160 List<Object> model = array2list(array);
1161 int length = Array.getLength(array);
1162 for (int i = 0; i < length; i++) {
1163 // update array element
1164 Object random = randomArg(elemType);
1165 model.set(i, random);
1166 if (testSetter) {
1167 if (elemType == int.class)
jrose1c1bfac2010-11-22 22:41:31 -08001168 mh.invokeExact((int[]) array, i, (int)random);
jrose2cc9c832010-04-30 23:48:23 -07001169 else if (elemType == boolean.class)
jrose1c1bfac2010-11-22 22:41:31 -08001170 mh.invokeExact((boolean[]) array, i, (boolean)random);
jrose10f3b682010-01-07 16:16:45 -08001171 else
jrose1c1bfac2010-11-22 22:41:31 -08001172 mh.invokeExact(array, i, random);
jrose10f3b682010-01-07 16:16:45 -08001173 assertEquals(model, array2list(array));
1174 } else {
1175 Array.set(array, i, random);
jrose2cc9c832010-04-30 23:48:23 -07001176 }
1177 if (verbosity >= 5) {
1178 List<Object> array2list = array2list(array);
1179 System.out.println("a["+i+"]="+random+" => "+array2list);
1180 if (!array2list.equals(model))
1181 System.out.println("*** != "+model);
jrose10f3b682010-01-07 16:16:45 -08001182 }
1183 // observe array element
1184 sawValue = Array.get(array, i);
1185 if (!testSetter) {
1186 expValue = sawValue;
1187 if (elemType == int.class)
jrose1c1bfac2010-11-22 22:41:31 -08001188 sawValue = (int) mh.invokeExact((int[]) array, i);
jrose2cc9c832010-04-30 23:48:23 -07001189 else if (elemType == boolean.class)
jrose1c1bfac2010-11-22 22:41:31 -08001190 sawValue = (boolean) mh.invokeExact((boolean[]) array, i);
jrose10f3b682010-01-07 16:16:45 -08001191 else
jrose2cc9c832010-04-30 23:48:23 -07001192 sawValue = mh.invokeExact(array, i);
jrose10f3b682010-01-07 16:16:45 -08001193 assertEquals(sawValue, expValue);
1194 assertEquals(model, array2list(array));
1195 }
1196 }
1197 }
1198
1199 List<Object> array2list(Object array) {
1200 int length = Array.getLength(array);
1201 ArrayList<Object> model = new ArrayList<Object>(length);
1202 for (int i = 0; i < length; i++)
1203 model.add(Array.get(array, i));
1204 return model;
jrose55220c32009-10-21 23:19:48 -07001205 }
1206
1207 static class Callee {
1208 static Object id() { return called("id"); }
1209 static Object id(Object x) { return called("id", x); }
1210 static Object id(Object x, Object y) { return called("id", x, y); }
1211 static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
1212 static Object id(Object... vx) { return called("id", vx); }
1213 static MethodHandle ofType(int n) {
1214 return ofType(Object.class, n);
1215 }
1216 static MethodHandle ofType(Class<?> rtype, int n) {
1217 if (n == -1)
jrose10f3b682010-01-07 16:16:45 -08001218 return ofType(MethodType.methodType(rtype, Object[].class));
1219 return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
jrose55220c32009-10-21 23:19:48 -07001220 }
1221 static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
jrose10f3b682010-01-07 16:16:45 -08001222 return ofType(MethodType.methodType(rtype, ptypes));
jrose55220c32009-10-21 23:19:48 -07001223 }
1224 static MethodHandle ofType(MethodType type) {
1225 Class<?> rtype = type.returnType();
1226 String pfx = "";
1227 if (rtype != Object.class)
1228 pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
1229 String name = pfx+"id";
jrosea1ebbe62010-09-08 18:40:23 -07001230 try {
1231 return PRIVATE.findStatic(Callee.class, name, type);
1232 } catch (Exception ex) {
1233 throw new RuntimeException(ex);
1234 }
jrose55220c32009-10-21 23:19:48 -07001235 }
1236 }
1237
1238 @Test
1239 public void testConvertArguments() throws Throwable {
1240 if (CAN_SKIP_WORKING) return;
1241 startTest("convertArguments");
1242 testConvert(Callee.ofType(1), null, "id", int.class);
1243 testConvert(Callee.ofType(1), null, "id", String.class);
1244 testConvert(Callee.ofType(1), null, "id", Integer.class);
1245 testConvert(Callee.ofType(1), null, "id", short.class);
jrose2cc9c832010-04-30 23:48:23 -07001246 testConvert(Callee.ofType(1), null, "id", char.class);
1247 testConvert(Callee.ofType(1), null, "id", byte.class);
jrose55220c32009-10-21 23:19:48 -07001248 }
1249
1250 void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
jrose900bafd2010-10-30 21:08:23 -07001251 testConvert(true, false, id, rtype, name, params);
1252 testConvert(true, true, id, rtype, name, params);
jrose55220c32009-10-21 23:19:48 -07001253 }
1254
jrose900bafd2010-10-30 21:08:23 -07001255 void testConvert(boolean positive, boolean useAsType,
1256 MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
jrose55220c32009-10-21 23:19:48 -07001257 countTest(positive);
1258 MethodType idType = id.type();
1259 if (rtype == null) rtype = idType.returnType();
1260 for (int i = 0; i < params.length; i++) {
1261 if (params[i] == null) params[i] = idType.parameterType(i);
1262 }
1263 // simulate the pairwise conversion
jrose10f3b682010-01-07 16:16:45 -08001264 MethodType newType = MethodType.methodType(rtype, params);
jrose55220c32009-10-21 23:19:48 -07001265 Object[] args = randomArgs(newType.parameterArray());
1266 Object[] convArgs = args.clone();
1267 for (int i = 0; i < args.length; i++) {
1268 Class<?> src = newType.parameterType(i);
1269 Class<?> dst = idType.parameterType(i);
1270 if (src != dst)
1271 convArgs[i] = castToWrapper(convArgs[i], dst);
1272 }
jrose900bafd2010-10-30 21:08:23 -07001273 Object convResult = id.invokeWithArguments(convArgs);
jrose55220c32009-10-21 23:19:48 -07001274 {
1275 Class<?> dst = newType.returnType();
1276 Class<?> src = idType.returnType();
1277 if (src != dst)
1278 convResult = castToWrapper(convResult, dst);
1279 }
1280 MethodHandle target = null;
1281 RuntimeException error = null;
1282 try {
jrose900bafd2010-10-30 21:08:23 -07001283 if (useAsType)
jrose900bafd2010-10-30 21:08:23 -07001284 target = id.asType(newType);
jrosef108fc02011-02-11 01:26:24 -08001285 else
1286 target = MethodHandles.convertArguments(id, newType);
jrose55220c32009-10-21 23:19:48 -07001287 } catch (RuntimeException ex) {
1288 error = ex;
1289 }
jrose2cc9c832010-04-30 23:48:23 -07001290 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -07001291 System.out.println("convert "+id+ " to "+newType+" => "+target
1292 +(error == null ? "" : " !! "+error));
1293 if (positive && error != null) throw error;
1294 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
1295 if (!positive) return; // negative test failed as expected
1296 assertEquals(newType, target.type());
1297 printCalled(target, id.toString(), args);
jrose900bafd2010-10-30 21:08:23 -07001298 Object result = target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -07001299 assertCalled(name, convArgs);
1300 assertEquals(convResult, result);
jrose2cc9c832010-04-30 23:48:23 -07001301 if (verbosity >= 1)
1302 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -07001303 }
1304
1305 @Test
jrosef108fc02011-02-11 01:26:24 -08001306 public void testVarargsCollector() throws Throwable {
1307 MethodHandle vac0 = PRIVATE.findStatic(MethodHandlesTest.class, "called",
1308 MethodType.methodType(Object.class, String.class, Object[].class));
1309 vac0 = vac0.bindTo("vac");
1310 MethodHandle vac = vac0.asVarargsCollector(Object[].class);
1311 testConvert(true, true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
1312 testConvert(true, true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
1313 for (Class<?> at : new Class[] { Object.class, String.class, Integer.class }) {
1314 testConvert(true, true, vac.asType(MethodType.genericMethodType(1)), null, "vac", at);
1315 testConvert(true, true, vac.asType(MethodType.genericMethodType(2)), null, "vac", at, at);
1316 }
1317 }
1318
1319 @Test
jrose10f3b682010-01-07 16:16:45 -08001320 public void testPermuteArguments() throws Throwable {
1321 if (CAN_SKIP_WORKING) return;
1322 startTest("permuteArguments");
1323 testPermuteArguments(4, Integer.class, 2, String.class, 0);
1324 //testPermuteArguments(6, Integer.class, 0, null, 30);
1325 //testPermuteArguments(4, Integer.class, 1, int.class, 6);
1326 }
1327 public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -07001328 if (verbosity >= 2)
jrose10f3b682010-01-07 16:16:45 -08001329 System.out.println("permuteArguments "+max+"*"+type1.getName()
1330 +(t2c==0?"":"/"+t2c+"*"+type2.getName())
1331 +(dilution > 0 ? " with dilution "+dilution : ""));
1332 int t2pos = t2c == 0 ? 0 : 1;
1333 for (int inargs = t2pos+1; inargs <= max; inargs++) {
1334 Class<?>[] types = new Class<?>[inargs];
1335 Arrays.fill(types, type1);
1336 if (t2c != 0) {
1337 // Fill in a middle range with type2:
1338 Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
1339 }
1340 Object[] args = randomArgs(types);
1341 int numcases = 1;
1342 for (int outargs = 0; outargs <= max; outargs++) {
1343 if (outargs - inargs >= MAX_ARG_INCREASE) continue;
1344 int[] reorder = new int[outargs];
1345 int casStep = dilution + 1;
1346 // Avoid some common factors:
1347 while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
1348 (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
1349 casStep++;
1350 for (int cas = 0; cas < numcases; cas += casStep) {
1351 for (int i = 0, c = cas; i < outargs; i++) {
1352 reorder[i] = c % inargs;
1353 c /= inargs;
1354 }
1355 testPermuteArguments(args, types, reorder);
1356 }
1357 numcases *= inargs;
1358 if (dilution > 10 && outargs >= 4) {
1359 // Do some special patterns, which we probably missed.
1360 // Replication of a single argument or argument pair.
1361 for (int i = 0; i < inargs; i++) {
1362 Arrays.fill(reorder, i);
1363 testPermuteArguments(args, types, reorder);
1364 for (int d = 1; d <= 2; d++) {
1365 if (i + d >= inargs) continue;
1366 for (int j = 1; j < outargs; j += 2)
1367 reorder[j] += 1;
1368 testPermuteArguments(args, types, reorder);
1369 testPermuteArguments(args, types, reverse(reorder));
1370 }
1371 }
1372 // Repetition of a sequence of 3 or more arguments.
1373 for (int i = 1; i < inargs; i++) {
1374 for (int len = 3; len <= inargs; len++) {
1375 for (int j = 0; j < outargs; j++)
1376 reorder[j] = (i + (j % len)) % inargs;
1377 testPermuteArguments(args, types, reorder);
1378 testPermuteArguments(args, types, reverse(reorder));
1379 }
1380 }
1381 }
1382 }
1383 }
1384 }
1385
1386 static int[] reverse(int[] reorder) {
1387 reorder = reorder.clone();
1388 for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
1389 int j = reorder.length - 1 - i;
1390 int tem = reorder[i];
1391 reorder[i] = reorder[j];
1392 reorder[j] = tem;
1393 }
1394 return reorder;
1395 }
1396
1397 void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
1398 countTest();
1399 if (args == null && types == null) {
1400 int max = 0;
1401 for (int j : reorder) {
1402 if (max < j) max = j;
1403 }
1404 args = randomArgs(max+1, Integer.class);
1405 }
1406 if (args == null) {
1407 args = randomArgs(types);
1408 }
1409 if (types == null) {
1410 types = new Class<?>[args.length];
1411 for (int i = 0; i < args.length; i++)
1412 types[i] = args[i].getClass();
1413 }
1414 int inargs = args.length, outargs = reorder.length;
jrosef15905c2011-02-11 01:26:32 -08001415 assertTrue(inargs == types.length);
jrose2cc9c832010-04-30 23:48:23 -07001416 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001417 System.out.println("permuteArguments "+Arrays.toString(reorder));
1418 Object[] permArgs = new Object[outargs];
1419 Class<?>[] permTypes = new Class<?>[outargs];
1420 for (int i = 0; i < outargs; i++) {
1421 permArgs[i] = args[reorder[i]];
1422 permTypes[i] = types[reorder[i]];
1423 }
jrose2cc9c832010-04-30 23:48:23 -07001424 if (verbosity >= 4) {
jrose10f3b682010-01-07 16:16:45 -08001425 System.out.println("in args: "+Arrays.asList(args));
1426 System.out.println("out args: "+Arrays.asList(permArgs));
1427 System.out.println("in types: "+Arrays.asList(types));
1428 System.out.println("out types: "+Arrays.asList(permTypes));
1429 }
1430 MethodType inType = MethodType.methodType(Object.class, types);
1431 MethodType outType = MethodType.methodType(Object.class, permTypes);
jroseadc650a2011-02-11 01:26:28 -08001432 MethodHandle target = MethodHandles.convertArguments(varargsList(outargs), outType);
jrose10f3b682010-01-07 16:16:45 -08001433 MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
jrose900bafd2010-10-30 21:08:23 -07001434 Object result = newTarget.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001435 Object expected = Arrays.asList(permArgs);
1436 assertEquals(expected, result);
1437 }
1438
1439
1440 @Test
1441 public void testSpreadArguments() throws Throwable {
1442 if (CAN_SKIP_WORKING) return;
1443 startTest("spreadArguments");
1444 for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
jrose2cc9c832010-04-30 23:48:23 -07001445 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001446 System.out.println("spreadArguments "+argType);
jrose2cc9c832010-04-30 23:48:23 -07001447 // FIXME: enable _adapter_spread_args and fix Fail_2
jrose10f3b682010-01-07 16:16:45 -08001448 for (int nargs = 0; nargs < 10; nargs++) {
1449 if (argType == int.class && nargs >= 6) continue; // FIXME Fail_1
1450 for (int pos = 0; pos < nargs; pos++) {
1451 if (argType == int.class && pos > 0) continue; // FIXME Fail_3
jrose2cc9c832010-04-30 23:48:23 -07001452 testSpreadArguments(argType, pos, nargs);
jrose10f3b682010-01-07 16:16:45 -08001453 }
1454 }
1455 }
1456 }
1457 public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable {
1458 countTest();
jroseadc650a2011-02-11 01:26:28 -08001459 MethodHandle target = varargsArray(nargs);
jrose10f3b682010-01-07 16:16:45 -08001460 MethodHandle target2 = changeArgTypes(target, argType);
jrose2cc9c832010-04-30 23:48:23 -07001461 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001462 System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
1463 Object[] args = randomArgs(target2.type().parameterArray());
1464 // make sure the target does what we think it does:
1465 if (pos == 0 && nargs < 5) {
jrose900bafd2010-10-30 21:08:23 -07001466 Object[] check = (Object[]) target.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001467 assertArrayEquals(args, check);
1468 switch (nargs) {
1469 case 0:
jrose1c1bfac2010-11-22 22:41:31 -08001470 check = (Object[]) target.invokeExact();
jrose10f3b682010-01-07 16:16:45 -08001471 assertArrayEquals(args, check);
1472 break;
1473 case 1:
jrose1c1bfac2010-11-22 22:41:31 -08001474 check = (Object[]) target.invokeExact(args[0]);
jrose10f3b682010-01-07 16:16:45 -08001475 assertArrayEquals(args, check);
1476 break;
1477 case 2:
jrose1c1bfac2010-11-22 22:41:31 -08001478 check = (Object[]) target.invokeExact(args[0], args[1]);
jrose10f3b682010-01-07 16:16:45 -08001479 assertArrayEquals(args, check);
1480 break;
1481 }
1482 }
1483 List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList());
1484 { // modify newParams in place
1485 List<Class<?>> spreadParams = newParams.subList(pos, nargs);
1486 spreadParams.clear(); spreadParams.add(Object[].class);
1487 }
1488 MethodType newType = MethodType.methodType(Object.class, newParams);
jroseadc650a2011-02-11 01:26:28 -08001489 MethodHandle result = target2.asSpreader(Object[].class, nargs-pos).asType(newType);
jrose10f3b682010-01-07 16:16:45 -08001490 Object[] returnValue;
1491 if (pos == 0) {
jrose1c1bfac2010-11-22 22:41:31 -08001492 // In the following line, the first cast implies
1493 // normal Object return value for the MH call (Object[])->Object,
1494 // while the second cast dynamically converts to an Object array.
1495 // Such a double cast is typical of MH.invokeExact.
1496 returnValue = (Object[]) (Object) result.invokeExact(args);
jrose10f3b682010-01-07 16:16:45 -08001497 } else {
1498 Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
1499 args1[pos] = Arrays.copyOfRange(args, pos, args.length);
jrose900bafd2010-10-30 21:08:23 -07001500 returnValue = (Object[]) result.invokeWithArguments(args1);
jrose10f3b682010-01-07 16:16:45 -08001501 }
1502 assertArrayEquals(args, returnValue);
1503 }
1504
1505 @Test
1506 public void testCollectArguments() throws Throwable {
1507 if (CAN_SKIP_WORKING) return;
1508 startTest("collectArguments");
1509 for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
jrose2cc9c832010-04-30 23:48:23 -07001510 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001511 System.out.println("collectArguments "+argType);
1512 for (int nargs = 0; nargs < 10; nargs++) {
1513 for (int pos = 0; pos < nargs; pos++) {
1514 if (argType == int.class) continue; // FIXME Fail_4
1515 testCollectArguments(argType, pos, nargs);
1516 }
1517 }
1518 }
1519 }
1520 public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable {
1521 countTest();
1522 // fake up a MH with the same type as the desired adapter:
jroseadc650a2011-02-11 01:26:28 -08001523 MethodHandle fake = varargsArray(nargs);
jrose10f3b682010-01-07 16:16:45 -08001524 fake = changeArgTypes(fake, argType);
1525 MethodType newType = fake.type();
1526 Object[] args = randomArgs(newType.parameterArray());
1527 // here is what should happen:
1528 Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
1529 collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
1530 // here is the MH which will witness the collected argument tail:
jroseadc650a2011-02-11 01:26:28 -08001531 MethodHandle target = varargsArray(pos+1);
jrose10f3b682010-01-07 16:16:45 -08001532 target = changeArgTypes(target, 0, pos, argType);
1533 target = changeArgTypes(target, pos, pos+1, Object[].class);
jrose2cc9c832010-04-30 23:48:23 -07001534 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001535 System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
jroseadc650a2011-02-11 01:26:28 -08001536 MethodHandle result = target.asCollector(Object[].class, nargs-pos).asType(newType);
jrose900bafd2010-10-30 21:08:23 -07001537 Object[] returnValue = (Object[]) result.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001538// assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
1539// returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
1540// collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
1541 assertArrayEquals(collectedArgs, returnValue);
1542 }
1543
1544 @Test
jrose55220c32009-10-21 23:19:48 -07001545 public void testInsertArguments() throws Throwable {
1546 if (CAN_SKIP_WORKING) return;
1547 startTest("insertArguments");
1548 for (int nargs = 0; nargs <= 4; nargs++) {
1549 for (int ins = 0; ins <= 4; ins++) {
1550 if (ins > MAX_ARG_INCREASE) continue; // FIXME Fail_6
1551 for (int pos = 0; pos <= nargs; pos++) {
1552 testInsertArguments(nargs, pos, ins);
1553 }
1554 }
1555 }
1556 }
1557
1558 void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
jrose55220c32009-10-21 23:19:48 -07001559 countTest();
jroseadc650a2011-02-11 01:26:28 -08001560 MethodHandle target = varargsArray(nargs + ins);
jrose55220c32009-10-21 23:19:48 -07001561 Object[] args = randomArgs(target.type().parameterArray());
1562 List<Object> resList = Arrays.asList(args);
1563 List<Object> argsToPass = new ArrayList<Object>(resList);
1564 List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
jrose2cc9c832010-04-30 23:48:23 -07001565 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -07001566 System.out.println("insert: "+argsToInsert+" into "+target);
jrose10f3b682010-01-07 16:16:45 -08001567 MethodHandle target2 = MethodHandles.insertArguments(target, pos,
1568 (Object[]) argsToInsert.toArray());
jrose55220c32009-10-21 23:19:48 -07001569 argsToInsert.clear(); // remove from argsToInsert
jrose900bafd2010-10-30 21:08:23 -07001570 Object res2 = target2.invokeWithArguments(argsToPass);
jrose55220c32009-10-21 23:19:48 -07001571 Object res2List = Arrays.asList((Object[])res2);
jrose2cc9c832010-04-30 23:48:23 -07001572 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -07001573 System.out.println("result: "+res2List);
1574 //if (!resList.equals(res2List))
1575 // System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
1576 assertEquals(resList, res2List);
1577 }
1578
jrose10f3b682010-01-07 16:16:45 -08001579 @Test
jroseadc650a2011-02-11 01:26:28 -08001580 public void testFilterReturnValue() throws Throwable {
1581 if (CAN_SKIP_WORKING) return;
1582 startTest("filterReturnValue");
1583 Class<?> classOfVCList = varargsList(1).invokeWithArguments(0).getClass();
1584 assertTrue(List.class.isAssignableFrom(classOfVCList));
1585 for (int nargs = 0; nargs <= 3; nargs++) {
1586 for (Class<?> rtype : new Class[] { Object.class,
1587 List.class,
1588 int.class,
1589 //byte.class, //FIXME: add this
1590 //long.class, //FIXME: add this
1591 CharSequence.class,
1592 String.class }) {
1593 testFilterReturnValue(nargs, rtype);
1594 }
1595 }
1596 }
1597
1598 void testFilterReturnValue(int nargs, Class<?> rtype) throws Throwable {
1599 countTest();
1600 MethodHandle target = varargsList(nargs, rtype);
1601 MethodHandle filter;
1602 if (List.class.isAssignableFrom(rtype) || rtype.isAssignableFrom(List.class))
1603 filter = varargsList(1); // add another layer of list-ness
1604 else
1605 filter = MethodHandles.identity(rtype);
1606 filter = filter.asType(MethodType.methodType(target.type().returnType(), rtype));
1607 Object[] argsToPass = randomArgs(nargs, Object.class);
1608 if (verbosity >= 3)
1609 System.out.println("filter "+target+" to "+rtype.getSimpleName()+" with "+filter);
1610 MethodHandle target2 = MethodHandles.filterReturnValue(target, filter);
1611 if (verbosity >= 4)
1612 System.out.println("filtered target: "+target2);
1613 // Simulate expected effect of filter on return value:
1614 Object unfiltered = target.invokeWithArguments(argsToPass);
1615 Object expected = filter.invokeWithArguments(unfiltered);
1616 if (verbosity >= 4)
1617 System.out.println("unfiltered: "+unfiltered+" : "+unfiltered.getClass().getSimpleName());
1618 if (verbosity >= 4)
1619 System.out.println("expected: "+expected+" : "+expected.getClass().getSimpleName());
1620 Object result = target2.invokeWithArguments(argsToPass);
1621 if (verbosity >= 3)
1622 System.out.println("result: "+result+" : "+result.getClass().getSimpleName());
1623 if (!expected.equals(result))
1624 System.out.println("*** fail at n/rt = "+nargs+"/"+rtype.getSimpleName()+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
1625 assertEquals(expected, result);
1626 }
1627
1628 @Test
jrose10f3b682010-01-07 16:16:45 -08001629 public void testFilterArguments() throws Throwable {
1630 if (CAN_SKIP_WORKING) return;
1631 startTest("filterArguments");
1632 for (int nargs = 1; nargs <= 6; nargs++) {
1633 for (int pos = 0; pos < nargs; pos++) {
1634 testFilterArguments(nargs, pos);
1635 }
1636 }
1637 }
1638
1639 void testFilterArguments(int nargs, int pos) throws Throwable {
1640 countTest();
jroseadc650a2011-02-11 01:26:28 -08001641 MethodHandle target = varargsList(nargs);
1642 MethodHandle filter = varargsList(1);
jrose10f3b682010-01-07 16:16:45 -08001643 filter = MethodHandles.convertArguments(filter, filter.type().generic());
1644 Object[] argsToPass = randomArgs(nargs, Object.class);
jrose2cc9c832010-04-30 23:48:23 -07001645 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001646 System.out.println("filter "+target+" at "+pos+" with "+filter);
jrose900bafd2010-10-30 21:08:23 -07001647 MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter);
jrose10f3b682010-01-07 16:16:45 -08001648 // Simulate expected effect of filter on arglist:
1649 Object[] filteredArgs = argsToPass.clone();
jrose2cc9c832010-04-30 23:48:23 -07001650 filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]);
jrose10f3b682010-01-07 16:16:45 -08001651 List<Object> expected = Arrays.asList(filteredArgs);
jrose900bafd2010-10-30 21:08:23 -07001652 Object result = target2.invokeWithArguments(argsToPass);
jrose2cc9c832010-04-30 23:48:23 -07001653 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001654 System.out.println("result: "+result);
1655 if (!expected.equals(result))
jroseadc650a2011-02-11 01:26:28 -08001656 System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
jrose10f3b682010-01-07 16:16:45 -08001657 assertEquals(expected, result);
1658 }
1659
1660 @Test
1661 public void testFoldArguments() throws Throwable {
1662 if (CAN_SKIP_WORKING) return;
1663 startTest("foldArguments");
1664 for (int nargs = 0; nargs <= 4; nargs++) {
1665 for (int fold = 0; fold <= nargs; fold++) {
1666 for (int pos = 0; pos <= nargs; pos++) {
1667 testFoldArguments(nargs, pos, fold);
1668 }
1669 }
1670 }
1671 }
1672
1673 void testFoldArguments(int nargs, int pos, int fold) throws Throwable {
1674 if (pos != 0) return; // can fold only at pos=0 for now
1675 countTest();
jroseadc650a2011-02-11 01:26:28 -08001676 MethodHandle target = varargsList(1 + nargs);
1677 MethodHandle combine = varargsList(fold).asType(MethodType.genericMethodType(fold));
jrose10f3b682010-01-07 16:16:45 -08001678 List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class));
jrose2cc9c832010-04-30 23:48:23 -07001679 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001680 System.out.println("fold "+target+" with "+combine);
1681 MethodHandle target2 = MethodHandles.foldArguments(target, combine);
1682 // Simulate expected effect of combiner on arglist:
1683 List<Object> expected = new ArrayList<Object>(argsToPass);
1684 List<Object> argsToFold = expected.subList(pos, pos + fold);
jrose2cc9c832010-04-30 23:48:23 -07001685 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001686 System.out.println("fold: "+argsToFold+" into "+target2);
jrose900bafd2010-10-30 21:08:23 -07001687 Object foldedArgs = combine.invokeWithArguments(argsToFold);
jrose10f3b682010-01-07 16:16:45 -08001688 argsToFold.add(0, foldedArgs);
jrose900bafd2010-10-30 21:08:23 -07001689 Object result = target2.invokeWithArguments(argsToPass);
jrose2cc9c832010-04-30 23:48:23 -07001690 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001691 System.out.println("result: "+result);
1692 if (!expected.equals(result))
jroseadc650a2011-02-11 01:26:28 -08001693 System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result+" != "+expected);
jrose10f3b682010-01-07 16:16:45 -08001694 assertEquals(expected, result);
1695 }
1696
1697 @Test
1698 public void testDropArguments() throws Throwable {
1699 if (CAN_SKIP_WORKING) return;
1700 startTest("dropArguments");
1701 for (int nargs = 0; nargs <= 4; nargs++) {
1702 for (int drop = 1; drop <= 4; drop++) {
1703 for (int pos = 0; pos <= nargs; pos++) {
1704 testDropArguments(nargs, pos, drop);
1705 }
1706 }
1707 }
1708 }
1709
1710 void testDropArguments(int nargs, int pos, int drop) throws Throwable {
1711 countTest();
jroseadc650a2011-02-11 01:26:28 -08001712 MethodHandle target = varargsArray(nargs);
jrose10f3b682010-01-07 16:16:45 -08001713 Object[] args = randomArgs(target.type().parameterArray());
1714 MethodHandle target2 = MethodHandles.dropArguments(target, pos,
1715 Collections.nCopies(drop, Object.class).toArray(new Class[0]));
1716 List<Object> resList = Arrays.asList(args);
1717 List<Object> argsToDrop = new ArrayList<Object>(resList);
1718 for (int i = drop; i > 0; i--) {
1719 argsToDrop.add(pos, "blort#"+i);
1720 }
jrose900bafd2010-10-30 21:08:23 -07001721 Object res2 = target2.invokeWithArguments(argsToDrop);
jrose10f3b682010-01-07 16:16:45 -08001722 Object res2List = Arrays.asList((Object[])res2);
1723 //if (!resList.equals(res2List))
1724 // System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
1725 assertEquals(resList, res2List);
1726 }
1727
1728 @Test
1729 public void testInvokers() throws Throwable {
1730 if (CAN_SKIP_WORKING) return;
1731 startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
1732 // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
1733 Set<MethodType> done = new HashSet<MethodType>();
1734 for (int i = 0; i <= 6; i++) {
1735 MethodType gtype = MethodType.genericMethodType(i);
1736 for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
1737 for (int j = -1; j < i; j++) {
1738 MethodType type = gtype;
1739 if (j < 0)
1740 type = type.changeReturnType(argType);
1741 else if (argType == void.class)
1742 continue;
1743 else
1744 type = type.changeParameterType(j, argType);
1745 if (argType.isPrimitive() && j != i-1) continue; // FIXME Fail_5
1746 if (done.add(type))
1747 testInvokers(type);
1748 MethodType vtype = type.changeReturnType(void.class);
1749 if (done.add(vtype))
1750 testInvokers(vtype);
1751 }
1752 }
1753 }
1754 }
1755
1756 public void testInvokers(MethodType type) throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -07001757 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001758 System.out.println("test invokers for "+type);
1759 int nargs = type.parameterCount();
1760 boolean testRetCode = type.returnType() != void.class;
1761 MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
1762 MethodType.genericMethodType(0, true));
jroseadc650a2011-02-11 01:26:28 -08001763 assertTrue(target.isVarargsCollector());
1764 target = target.asType(type);
jrose10f3b682010-01-07 16:16:45 -08001765 Object[] args = randomArgs(type.parameterArray());
1766 List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args));
1767 targetPlusArgs.add(0, target);
1768 int code = (Integer) invokee(args);
1769 Object log = logEntry("invokee", args);
1770 assertEquals(log.hashCode(), code);
1771 assertCalled("invokee", args);
1772 MethodHandle inv;
1773 Object result;
1774 // exact invoker
1775 countTest();
1776 calledLog.clear();
1777 inv = MethodHandles.exactInvoker(type);
jrose900bafd2010-10-30 21:08:23 -07001778 result = inv.invokeWithArguments(targetPlusArgs);
jrose10f3b682010-01-07 16:16:45 -08001779 if (testRetCode) assertEquals(code, result);
1780 assertCalled("invokee", args);
1781 // generic invoker
1782 countTest();
1783 inv = MethodHandles.genericInvoker(type);
1784 if (nargs <= 3) {
1785 calledLog.clear();
1786 switch (nargs) {
1787 case 0:
jrose2cc9c832010-04-30 23:48:23 -07001788 result = inv.invokeExact(target);
jrose10f3b682010-01-07 16:16:45 -08001789 break;
1790 case 1:
jrose2cc9c832010-04-30 23:48:23 -07001791 result = inv.invokeExact(target, args[0]);
jrose10f3b682010-01-07 16:16:45 -08001792 break;
1793 case 2:
jrose2cc9c832010-04-30 23:48:23 -07001794 result = inv.invokeExact(target, args[0], args[1]);
jrose10f3b682010-01-07 16:16:45 -08001795 break;
1796 case 3:
jrose2cc9c832010-04-30 23:48:23 -07001797 result = inv.invokeExact(target, args[0], args[1], args[2]);
jrose10f3b682010-01-07 16:16:45 -08001798 break;
1799 }
1800 if (testRetCode) assertEquals(code, result);
1801 assertCalled("invokee", args);
1802 }
1803 calledLog.clear();
jrose900bafd2010-10-30 21:08:23 -07001804 result = inv.invokeWithArguments(targetPlusArgs);
jrose10f3b682010-01-07 16:16:45 -08001805 if (testRetCode) assertEquals(code, result);
1806 assertCalled("invokee", args);
1807 // varargs invoker #0
1808 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001809 inv = MethodHandles.spreadInvoker(type, 0);
jrose2cc9c832010-04-30 23:48:23 -07001810 result = inv.invokeExact(target, args);
jrose10f3b682010-01-07 16:16:45 -08001811 if (testRetCode) assertEquals(code, result);
1812 assertCalled("invokee", args);
1813 if (nargs >= 1) {
1814 // varargs invoker #1
1815 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001816 inv = MethodHandles.spreadInvoker(type, 1);
jrose2cc9c832010-04-30 23:48:23 -07001817 result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs));
jrose10f3b682010-01-07 16:16:45 -08001818 if (testRetCode) assertEquals(code, result);
1819 assertCalled("invokee", args);
1820 }
1821 if (nargs >= 2) {
1822 // varargs invoker #2
1823 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001824 inv = MethodHandles.spreadInvoker(type, 2);
jrose2cc9c832010-04-30 23:48:23 -07001825 result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
jrose10f3b682010-01-07 16:16:45 -08001826 if (testRetCode) assertEquals(code, result);
1827 assertCalled("invokee", args);
1828 }
1829 if (nargs >= 3) {
1830 // varargs invoker #3
1831 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001832 inv = MethodHandles.spreadInvoker(type, 3);
jrose2cc9c832010-04-30 23:48:23 -07001833 result = inv.invokeExact(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
jrose10f3b682010-01-07 16:16:45 -08001834 if (testRetCode) assertEquals(code, result);
1835 assertCalled("invokee", args);
1836 }
1837 for (int k = 0; k <= nargs; k++) {
1838 // varargs invoker #0..N
1839 countTest();
1840 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001841 inv = MethodHandles.spreadInvoker(type, k);
jrose10f3b682010-01-07 16:16:45 -08001842 List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs);
1843 List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
1844 Object[] tail = tailList.toArray();
1845 tailList.clear(); tailList.add(tail);
jrose900bafd2010-10-30 21:08:23 -07001846 result = inv.invokeWithArguments(targetPlusVarArgs);
jrose10f3b682010-01-07 16:16:45 -08001847 if (testRetCode) assertEquals(code, result);
1848 assertCalled("invokee", args);
1849 }
jrose900bafd2010-10-30 21:08:23 -07001850
jrose10f3b682010-01-07 16:16:45 -08001851 // dynamic invoker
1852 countTest();
jroseb90d2e72010-12-16 15:59:27 -08001853 CallSite site = new MutableCallSite(type);
1854 inv = site.dynamicInvoker();
jrose900bafd2010-10-30 21:08:23 -07001855
1856 // see if we get the result of the original target:
1857 try {
1858 result = inv.invokeWithArguments(args);
1859 assertTrue("should not reach here", false);
1860 } catch (IllegalStateException ex) {
1861 String msg = ex.getMessage();
1862 assertTrue(msg, msg.contains("site"));
1863 }
1864
1865 // set new target after invoker is created, to make sure we track target
jrose10f3b682010-01-07 16:16:45 -08001866 site.setTarget(target);
1867 calledLog.clear();
jrose900bafd2010-10-30 21:08:23 -07001868 result = inv.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001869 if (testRetCode) assertEquals(code, result);
1870 assertCalled("invokee", args);
1871 }
1872
1873 static Object invokee(Object... args) {
1874 return called("invokee", args).hashCode();
1875 }
1876
jrose55220c32009-10-21 23:19:48 -07001877 private static final String MISSING_ARG = "missingArg";
1878 static Object targetIfEquals() {
1879 return called("targetIfEquals");
1880 }
1881 static Object fallbackIfNotEquals() {
1882 return called("fallbackIfNotEquals");
1883 }
1884 static Object targetIfEquals(Object x) {
1885 assertEquals(x, MISSING_ARG);
1886 return called("targetIfEquals", x);
1887 }
1888 static Object fallbackIfNotEquals(Object x) {
1889 assertFalse(x.toString(), x.equals(MISSING_ARG));
1890 return called("fallbackIfNotEquals", x);
1891 }
1892 static Object targetIfEquals(Object x, Object y) {
1893 assertEquals(x, y);
1894 return called("targetIfEquals", x, y);
1895 }
1896 static Object fallbackIfNotEquals(Object x, Object y) {
1897 assertFalse(x.toString(), x.equals(y));
1898 return called("fallbackIfNotEquals", x, y);
1899 }
1900 static Object targetIfEquals(Object x, Object y, Object z) {
1901 assertEquals(x, y);
1902 return called("targetIfEquals", x, y, z);
1903 }
1904 static Object fallbackIfNotEquals(Object x, Object y, Object z) {
1905 assertFalse(x.toString(), x.equals(y));
1906 return called("fallbackIfNotEquals", x, y, z);
1907 }
1908
jrose10f3b682010-01-07 16:16:45 -08001909 @Test
1910 public void testGuardWithTest() throws Throwable {
1911 if (CAN_SKIP_WORKING) return;
1912 startTest("guardWithTest");
1913 for (int nargs = 0; nargs <= 3; nargs++) {
1914 if (nargs != 2) continue; // FIXME: test more later
1915 testGuardWithTest(nargs, Object.class);
1916 testGuardWithTest(nargs, String.class);
1917 }
1918 }
1919 void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
1920 countTest();
1921 MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
1922 MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
1923 MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
1924 while (test.type().parameterCount() < nargs)
1925 test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
1926 while (test.type().parameterCount() > nargs)
1927 test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
1928 if (argClass != Object.class) {
1929 test = changeArgTypes(test, argClass);
1930 target = changeArgTypes(target, argClass);
1931 fallback = changeArgTypes(fallback, argClass);
1932 }
1933 MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
1934 assertEquals(target.type(), mh.type());
1935 Object[][] argLists = {
1936 { },
1937 { "foo" }, { MISSING_ARG },
1938 { "foo", "foo" }, { "foo", "bar" },
1939 { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
1940 };
1941 for (Object[] argList : argLists) {
1942 if (argList.length != nargs) continue;
1943 boolean equals;
1944 switch (nargs) {
1945 case 0: equals = true; break;
1946 case 1: equals = MISSING_ARG.equals(argList[0]); break;
1947 default: equals = argList[0].equals(argList[1]); break;
1948 }
1949 String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
jrose2cc9c832010-04-30 23:48:23 -07001950 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001951 System.out.println(logEntry(willCall, argList));
jrose900bafd2010-10-30 21:08:23 -07001952 Object result = mh.invokeWithArguments(argList);
jrose10f3b682010-01-07 16:16:45 -08001953 assertCalled(willCall, argList);
1954 }
1955 }
1956
1957 @Test
1958 public void testCatchException() throws Throwable {
1959 if (CAN_SKIP_WORKING) return;
1960 startTest("catchException");
1961 for (int nargs = 2; nargs <= 6; nargs++) {
1962 for (int ti = 0; ti <= 1; ti++) {
1963 boolean throwIt = (ti != 0);
1964 testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs);
1965 testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs);
1966 testCatchException(String.class, new LinkageError("testing"), throwIt, nargs);
1967 }
1968 }
1969 }
1970
1971 private static <T extends Throwable>
1972 Object throwOrReturn(Object normal, T exception) throws T {
1973 if (exception != null) throw exception;
1974 return normal;
1975 }
1976
1977 void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
1978 countTest();
jrose2cc9c832010-04-30 23:48:23 -07001979 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001980 System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs);
1981 Class<? extends Throwable> exType = thrown.getClass();
1982 MethodHandle throwOrReturn
1983 = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn",
1984 MethodType.methodType(Object.class, Object.class, Throwable.class));
jroseb90d2e72010-12-16 15:59:27 -08001985 MethodHandle thrower = throwOrReturn.asType(MethodType.genericMethodType(2));
jrose10f3b682010-01-07 16:16:45 -08001986 while (thrower.type().parameterCount() < nargs)
1987 thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class);
jroseadc650a2011-02-11 01:26:28 -08001988 MethodHandle catcher = varargsList(1+nargs).asType(MethodType.genericMethodType(1+nargs));
jrose10f3b682010-01-07 16:16:45 -08001989 MethodHandle target = MethodHandles.catchException(thrower,
jroseb90d2e72010-12-16 15:59:27 -08001990 thrown.getClass(), catcher);
jrose10f3b682010-01-07 16:16:45 -08001991 assertEquals(thrower.type(), target.type());
1992 //System.out.println("catching with "+target+" : "+throwOrReturn);
1993 Object[] args = randomArgs(nargs, Object.class);
1994 args[1] = (throwIt ? thrown : null);
jrose900bafd2010-10-30 21:08:23 -07001995 Object returned = target.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001996 //System.out.println("return from "+target+" : "+returned);
1997 if (!throwIt) {
1998 assertSame(args[0], returned);
1999 } else {
2000 List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args));
2001 catchArgs.add(0, thrown);
2002 assertEquals(catchArgs, returned);
2003 }
2004 }
2005
2006 @Test
2007 public void testThrowException() throws Throwable {
2008 if (CAN_SKIP_WORKING) return;
2009 startTest("throwException");
2010 testThrowException(int.class, new ClassCastException("testing"));
2011 testThrowException(void.class, new java.io.IOException("testing"));
2012 testThrowException(String.class, new LinkageError("testing"));
2013 }
2014
2015 void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
2016 countTest();
2017 Class<? extends Throwable> exType = thrown.getClass();
2018 MethodHandle target = MethodHandles.throwException(returnType, exType);
2019 //System.out.println("throwing with "+target+" : "+thrown);
2020 MethodType expectedType = MethodType.methodType(returnType, exType);
2021 assertEquals(expectedType, target.type());
jrose2cc9c832010-04-30 23:48:23 -07002022 target = MethodHandles.convertArguments(target, target.type().generic());
jrose10f3b682010-01-07 16:16:45 -08002023 Throwable caught = null;
2024 try {
jrose2cc9c832010-04-30 23:48:23 -07002025 Object res = target.invokeExact((Object) thrown);
jrose10f3b682010-01-07 16:16:45 -08002026 fail("got "+res+" instead of throwing "+thrown);
2027 } catch (Throwable ex) {
2028 if (ex != thrown) {
2029 if (ex instanceof Error) throw (Error)ex;
2030 if (ex instanceof RuntimeException) throw (RuntimeException)ex;
2031 }
2032 caught = ex;
2033 }
2034 assertSame(thrown, caught);
2035 }
2036
2037 @Test
2038 public void testCastFailure() throws Throwable {
2039 if (CAN_SKIP_WORKING) return;
2040 startTest("testCastFailure");
2041 testCastFailure("cast/argument", 11000);
2042 testCastFailure("unbox/argument", 11000);
2043 testCastFailure("cast/return", 11000);
2044 testCastFailure("unbox/return", 11000);
2045 }
2046
jrose900bafd2010-10-30 21:08:23 -07002047 static class Surprise {
jrosea1ebbe62010-09-08 18:40:23 -07002048 public MethodHandle asMethodHandle() {
2049 return VALUE.bindTo(this);
2050 }
jrose10f3b682010-01-07 16:16:45 -08002051 Object value(Object x) {
2052 trace("value", x);
2053 if (boo != null) return boo;
2054 return x;
2055 }
2056 Object boo;
2057 void boo(Object x) { boo = x; }
2058
2059 static void trace(String x, Object y) {
2060 if (verbosity > 8) System.out.println(x+"="+y);
2061 }
2062 static Object refIdentity(Object x) { trace("ref.x", x); return x; }
2063 static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
2064 static int intIdentity(int x) { trace("int.x", x); return x; }
jrosea1ebbe62010-09-08 18:40:23 -07002065 static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY;
2066 static {
2067 try {
2068 VALUE = PRIVATE.findVirtual(
2069 Surprise.class, "value",
2070 MethodType.methodType(Object.class, Object.class));
2071 REF_IDENTITY = PRIVATE.findStatic(
2072 Surprise.class, "refIdentity",
2073 MethodType.methodType(Object.class, Object.class));
2074 BOX_IDENTITY = PRIVATE.findStatic(
2075 Surprise.class, "boxIdentity",
2076 MethodType.methodType(Integer.class, Integer.class));
2077 INT_IDENTITY = PRIVATE.findStatic(
2078 Surprise.class, "intIdentity",
2079 MethodType.methodType(int.class, int.class));
2080 } catch (Exception ex) {
2081 throw new RuntimeException(ex);
2082 }
2083 }
jrose10f3b682010-01-07 16:16:45 -08002084 }
2085
2086 void testCastFailure(String mode, int okCount) throws Throwable {
2087 countTest(false);
jrose2cc9c832010-04-30 23:48:23 -07002088 if (verbosity > 2) System.out.println("mode="+mode);
jrose10f3b682010-01-07 16:16:45 -08002089 Surprise boo = new Surprise();
jrosea1ebbe62010-09-08 18:40:23 -07002090 MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0;
jrose10f3b682010-01-07 16:16:45 -08002091 if (mode.endsWith("/return")) {
2092 if (mode.equals("unbox/return")) {
2093 // fail on return to ((Integer)surprise).intValue
2094 surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(int.class, Object.class));
2095 identity = MethodHandles.convertArguments(identity, MethodType.methodType(int.class, Object.class));
2096 } else if (mode.equals("cast/return")) {
2097 // fail on return to (Integer)surprise
2098 surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(Integer.class, Object.class));
2099 identity = MethodHandles.convertArguments(identity, MethodType.methodType(Integer.class, Object.class));
2100 }
2101 } else if (mode.endsWith("/argument")) {
2102 MethodHandle callee = null;
2103 if (mode.equals("unbox/argument")) {
2104 // fail on handing surprise to int argument
2105 callee = Surprise.INT_IDENTITY;
2106 } else if (mode.equals("cast/argument")) {
2107 // fail on handing surprise to Integer argument
2108 callee = Surprise.BOX_IDENTITY;
2109 }
2110 if (callee != null) {
2111 callee = MethodHandles.convertArguments(callee, MethodType.genericMethodType(1));
jrose900bafd2010-10-30 21:08:23 -07002112 surprise = MethodHandles.filterArguments(callee, 0, surprise);
2113 identity = MethodHandles.filterArguments(callee, 0, identity);
jrose10f3b682010-01-07 16:16:45 -08002114 }
2115 }
jrosea1ebbe62010-09-08 18:40:23 -07002116 assertNotSame(mode, surprise, surprise0);
jrose10f3b682010-01-07 16:16:45 -08002117 identity = MethodHandles.convertArguments(identity, MethodType.genericMethodType(1));
2118 surprise = MethodHandles.convertArguments(surprise, MethodType.genericMethodType(1));
2119 Object x = 42;
2120 for (int i = 0; i < okCount; i++) {
jrose2cc9c832010-04-30 23:48:23 -07002121 Object y = identity.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08002122 assertEquals(x, y);
jrose2cc9c832010-04-30 23:48:23 -07002123 Object z = surprise.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08002124 assertEquals(x, z);
2125 }
2126 boo.boo("Boo!");
jrose2cc9c832010-04-30 23:48:23 -07002127 Object y = identity.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08002128 assertEquals(x, y);
2129 try {
jrose2cc9c832010-04-30 23:48:23 -07002130 Object z = surprise.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08002131 System.out.println("Failed to throw; got z="+z);
2132 assertTrue(false);
2133 } catch (Exception ex) {
jrose10f3b682010-01-07 16:16:45 -08002134 if (verbosity > 2)
jrose2cc9c832010-04-30 23:48:23 -07002135 System.out.println("caught "+ex);
2136 if (verbosity > 3)
jrose10f3b682010-01-07 16:16:45 -08002137 ex.printStackTrace();
2138 assertTrue(ex instanceof ClassCastException
2139 // FIXME: accept only one of the two for any given unit test
2140 || ex instanceof WrongMethodTypeException
2141 );
2142 }
2143 }
2144
jrose45b7a332010-05-03 23:32:47 -07002145 static Example userMethod(Object o, String s, int i) {
2146 called("userMethod", o, s, i);
2147 return null;
2148 }
2149
2150 @Test
2151 public void testUserClassInSignature() throws Throwable {
2152 if (CAN_SKIP_WORKING) return;
2153 startTest("testUserClassInSignature");
2154 Lookup lookup = MethodHandles.lookup();
2155 String name; MethodType mt; MethodHandle mh;
2156 Object[] args;
2157
2158 // Try a static method.
2159 name = "userMethod";
2160 mt = MethodType.methodType(Example.class, Object.class, String.class, int.class);
2161 mh = lookup.findStatic(lookup.lookupClass(), name, mt);
2162 assertEquals(mt, mh.type());
2163 assertEquals(Example.class, mh.type().returnType());
2164 args = randomArgs(mh.type().parameterArray());
jrose900bafd2010-10-30 21:08:23 -07002165 mh.invokeWithArguments(args);
jrose45b7a332010-05-03 23:32:47 -07002166 assertCalled(name, args);
2167
2168 // Try a virtual method.
2169 name = "v2";
2170 mt = MethodType.methodType(Object.class, Object.class, int.class);
2171 mh = lookup.findVirtual(Example.class, name, mt);
2172 assertEquals(mt, mh.type().dropParameterTypes(0,1));
2173 assertTrue(mh.type().parameterList().contains(Example.class));
2174 args = randomArgs(mh.type().parameterArray());
jrose900bafd2010-10-30 21:08:23 -07002175 mh.invokeWithArguments(args);
jrose45b7a332010-05-03 23:32:47 -07002176 assertCalled(name, args);
2177 }
jrosebe2db602010-09-08 18:40:34 -07002178
2179 static void runForRunnable() {
2180 called("runForRunnable");
2181 }
2182 private interface Fooable {
2183 Object foo(Fooable x, Object y);
2184 // this is for randomArg:
2185 public class Impl implements Fooable {
2186 public Object foo(Fooable x, Object y) {
2187 throw new RuntimeException("do not call");
2188 }
2189 final String name;
2190 public Impl() { name = "Fooable#"+nextArg(); }
2191 @Override public String toString() { return name; }
2192 }
2193 }
2194 static Object fooForFooable(Fooable x, Object y) {
2195 return called("fooForFooable", x, y);
2196 }
2197 private static class MyCheckedException extends Exception {
2198 }
2199 private interface WillThrow {
2200 void willThrow() throws MyCheckedException;
2201 }
2202
2203 @Test
2204 public void testAsInstance() throws Throwable {
2205 if (CAN_SKIP_WORKING) return;
2206 Lookup lookup = MethodHandles.lookup();
2207 {
2208 MethodType mt = MethodType.methodType(void.class);
2209 MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "runForRunnable", mt);
2210 Runnable proxy = MethodHandles.asInstance(mh, Runnable.class);
2211 proxy.run();
2212 assertCalled("runForRunnable");
2213 }
2214 {
2215 MethodType mt = MethodType.methodType(Object.class, Fooable.class, Object.class);
2216 MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "fooForFooable", mt);
2217 Fooable proxy = MethodHandles.asInstance(mh, Fooable.class);
2218 Object[] args = randomArgs(mt.parameterArray());
2219 Object result = proxy.foo((Fooable) args[0], args[1]);
2220 assertCalled("fooForFooable", args);
2221 assertEquals(result, logEntry("fooForFooable", args));
2222 }
2223 for (Throwable ex : new Throwable[] { new NullPointerException("ok"),
2224 new InternalError("ok"),
2225 new Throwable("fail"),
2226 new Exception("fail"),
2227 new MyCheckedException()
2228 }) {
2229 MethodHandle mh = MethodHandles.throwException(void.class, Throwable.class);
2230 mh = MethodHandles.insertArguments(mh, 0, ex);
2231 WillThrow proxy = MethodHandles.asInstance(mh, WillThrow.class);
2232 try {
2233 proxy.willThrow();
2234 System.out.println("Failed to throw: "+ex);
2235 assertTrue(false);
2236 } catch (Throwable ex1) {
2237 if (verbosity > 2) {
2238 System.out.println("throw "+ex);
2239 System.out.println("catch "+(ex == ex1 ? "UNWRAPPED" : ex1));
2240 }
2241 if (ex instanceof RuntimeException ||
2242 ex instanceof Error) {
2243 assertSame("must pass unchecked exception out without wrapping", ex, ex1);
2244 } else if (ex instanceof MyCheckedException) {
2245 assertSame("must pass declared exception out without wrapping", ex, ex1);
2246 } else {
2247 assertNotSame("must pass undeclared checked exception with wrapping", ex, ex1);
2248 UndeclaredThrowableException utex = (UndeclaredThrowableException) ex1;
2249 assertSame(ex, utex.getCause());
2250 }
2251 }
2252 }
2253 // Test error checking:
jrosebe2db602010-09-08 18:40:34 -07002254 for (Class<?> nonSAM : new Class[] { Object.class,
2255 String.class,
2256 CharSequence.class,
2257 Example.class }) {
2258 try {
jroseadc650a2011-02-11 01:26:28 -08002259 MethodHandles.asInstance(varargsArray(0), nonSAM);
jrosebe2db602010-09-08 18:40:34 -07002260 System.out.println("Failed to throw");
2261 assertTrue(false);
2262 } catch (IllegalArgumentException ex) {
2263 }
2264 }
2265 }
jrose55220c32009-10-21 23:19:48 -07002266}
2267// Local abbreviated copy of sun.dyn.util.ValueConversions
2268class ValueConversions {
2269 private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
2270 private static final Object[] NO_ARGS_ARRAY = {};
2271 private static Object[] makeArray(Object... args) { return args; }
2272 private static Object[] array() { return NO_ARGS_ARRAY; }
2273 private static Object[] array(Object a0)
2274 { return makeArray(a0); }
2275 private static Object[] array(Object a0, Object a1)
2276 { return makeArray(a0, a1); }
2277 private static Object[] array(Object a0, Object a1, Object a2)
2278 { return makeArray(a0, a1, a2); }
2279 private static Object[] array(Object a0, Object a1, Object a2, Object a3)
2280 { return makeArray(a0, a1, a2, a3); }
2281 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2282 Object a4)
2283 { return makeArray(a0, a1, a2, a3, a4); }
2284 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2285 Object a4, Object a5)
2286 { return makeArray(a0, a1, a2, a3, a4, a5); }
2287 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2288 Object a4, Object a5, Object a6)
2289 { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
2290 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2291 Object a4, Object a5, Object a6, Object a7)
2292 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
2293 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2294 Object a4, Object a5, Object a6, Object a7,
2295 Object a8)
2296 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
2297 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2298 Object a4, Object a5, Object a6, Object a7,
2299 Object a8, Object a9)
2300 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
2301 static MethodHandle[] makeArrays() {
2302 ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
2303 MethodHandles.Lookup lookup = IMPL_LOOKUP;
2304 for (;;) {
2305 int nargs = arrays.size();
jrose10f3b682010-01-07 16:16:45 -08002306 MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
jrose55220c32009-10-21 23:19:48 -07002307 String name = "array";
2308 MethodHandle array = null;
2309 try {
2310 array = lookup.findStatic(ValueConversions.class, name, type);
jrosef15905c2011-02-11 01:26:32 -08002311 } catch (ReflectiveOperationException ex) {
2312 // break from loop!
jrose55220c32009-10-21 23:19:48 -07002313 }
2314 if (array == null) break;
2315 arrays.add(array);
2316 }
jrosef15905c2011-02-11 01:26:32 -08002317 assertTrue(arrays.size() == 11); // current number of methods
jrose55220c32009-10-21 23:19:48 -07002318 return arrays.toArray(new MethodHandle[0]);
2319 }
2320 static final MethodHandle[] ARRAYS = makeArrays();
2321
2322 /** Return a method handle that takes the indicated number of Object
2323 * arguments and returns an Object array of them, as if for varargs.
2324 */
2325 public static MethodHandle varargsArray(int nargs) {
2326 if (nargs < ARRAYS.length)
2327 return ARRAYS[nargs];
2328 // else need to spin bytecode or do something else fancy
jrose2cc9c832010-04-30 23:48:23 -07002329 throw new UnsupportedOperationException("NYI: cannot form a varargs array of length "+nargs);
jrose55220c32009-10-21 23:19:48 -07002330 }
jrose10f3b682010-01-07 16:16:45 -08002331
2332 private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
2333 private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
2334 private static List<Object> list() { return NO_ARGS_LIST; }
2335 private static List<Object> list(Object a0)
2336 { return makeList(a0); }
2337 private static List<Object> list(Object a0, Object a1)
2338 { return makeList(a0, a1); }
2339 private static List<Object> list(Object a0, Object a1, Object a2)
2340 { return makeList(a0, a1, a2); }
2341 private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
2342 { return makeList(a0, a1, a2, a3); }
2343 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2344 Object a4)
2345 { return makeList(a0, a1, a2, a3, a4); }
2346 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2347 Object a4, Object a5)
2348 { return makeList(a0, a1, a2, a3, a4, a5); }
2349 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2350 Object a4, Object a5, Object a6)
2351 { return makeList(a0, a1, a2, a3, a4, a5, a6); }
2352 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2353 Object a4, Object a5, Object a6, Object a7)
2354 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
2355 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2356 Object a4, Object a5, Object a6, Object a7,
2357 Object a8)
2358 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
2359 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2360 Object a4, Object a5, Object a6, Object a7,
2361 Object a8, Object a9)
2362 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
2363 static MethodHandle[] makeLists() {
jroseadc650a2011-02-11 01:26:28 -08002364 ArrayList<MethodHandle> lists = new ArrayList<MethodHandle>();
jrose10f3b682010-01-07 16:16:45 -08002365 MethodHandles.Lookup lookup = IMPL_LOOKUP;
2366 for (;;) {
jroseadc650a2011-02-11 01:26:28 -08002367 int nargs = lists.size();
jrose10f3b682010-01-07 16:16:45 -08002368 MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
2369 String name = "list";
jroseadc650a2011-02-11 01:26:28 -08002370 MethodHandle list = null;
jrose10f3b682010-01-07 16:16:45 -08002371 try {
jroseadc650a2011-02-11 01:26:28 -08002372 list = lookup.findStatic(ValueConversions.class, name, type);
jrosef15905c2011-02-11 01:26:32 -08002373 } catch (ReflectiveOperationException ex) {
2374 // break from loop!
jrose10f3b682010-01-07 16:16:45 -08002375 }
jroseadc650a2011-02-11 01:26:28 -08002376 if (list == null) break;
2377 lists.add(list);
jrose10f3b682010-01-07 16:16:45 -08002378 }
jrosef15905c2011-02-11 01:26:32 -08002379 assertTrue(lists.size() == 11); // current number of methods
jroseadc650a2011-02-11 01:26:28 -08002380 return lists.toArray(new MethodHandle[0]);
jrose10f3b682010-01-07 16:16:45 -08002381 }
2382 static final MethodHandle[] LISTS = makeLists();
2383
2384 /** Return a method handle that takes the indicated number of Object
2385 * arguments and returns List.
2386 */
2387 public static MethodHandle varargsList(int nargs) {
2388 if (nargs < LISTS.length)
2389 return LISTS[nargs];
2390 // else need to spin bytecode or do something else fancy
2391 throw new UnsupportedOperationException("NYI");
2392 }
jrose55220c32009-10-21 23:19:48 -07002393}
2394// This guy tests access from outside the same package member, but inside
2395// the package itself.
2396class PackageSibling {
2397 static Lookup lookup() {
2398 return MethodHandles.lookup();
2399 }
2400}