blob: 6f27389893c82542db0afcbfcea4b24d4b5b7730 [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
326 static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
327 return changeArgTypes(target, 0, 999, argType);
328 }
329 static MethodHandle changeArgTypes(MethodHandle target,
330 int beg, int end, Class<?> argType) {
331 MethodType targetType = target.type();
332 end = Math.min(end, targetType.parameterCount());
333 ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
334 Collections.fill(argTypes.subList(beg, end), argType);
jrose10f3b682010-01-07 16:16:45 -0800335 MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
jrose55220c32009-10-21 23:19:48 -0700336 return MethodHandles.convertArguments(target, ttype2);
337 }
338
339 // This lookup is good for all members in and under MethodHandlesTest.
340 static final Lookup PRIVATE = MethodHandles.lookup();
341 // This lookup is good for package-private members but not private ones.
342 static final Lookup PACKAGE = PackageSibling.lookup();
343 // This lookup is good only for public members.
jrose10f3b682010-01-07 16:16:45 -0800344 static final Lookup PUBLIC = MethodHandles.publicLookup();
jrose55220c32009-10-21 23:19:48 -0700345
346 // Subject methods...
347 static class Example implements IntExample {
348 final String name;
jrose2cc9c832010-04-30 23:48:23 -0700349 public Example() { name = "Example#"+nextArg(); }
jrose55220c32009-10-21 23:19:48 -0700350 protected Example(String name) { this.name = name; }
351 protected Example(int x) { this(); called("protected <init>", this, x); }
352 @Override public String toString() { return name; }
353
354 public void v0() { called("v0", this); }
355 void pkg_v0() { called("pkg_v0", this); }
356 private void pri_v0() { called("pri_v0", this); }
357 public static void s0() { called("s0"); }
358 static void pkg_s0() { called("pkg_s0"); }
359 private static void pri_s0() { called("pri_s0"); }
360
361 public Object v1(Object x) { return called("v1", this, x); }
362 public Object v2(Object x, Object y) { return called("v2", this, x, y); }
363 public Object v2(Object x, int y) { return called("v2", this, x, y); }
364 public Object v2(int x, Object y) { return called("v2", this, x, y); }
365 public Object v2(int x, int y) { return called("v2", this, x, y); }
366 public static Object s1(Object x) { return called("s1", x); }
367 public static Object s2(int x) { return called("s2", x); }
368 public static Object s3(long x) { return called("s3", x); }
369 public static Object s4(int x, int y) { return called("s4", x, y); }
370 public static Object s5(long x, int y) { return called("s5", x, y); }
371 public static Object s6(int x, long y) { return called("s6", x, y); }
372 public static Object s7(float x, double y) { return called("s7", x, y); }
jrose2cc9c832010-04-30 23:48:23 -0700373
374 static final Lookup EXAMPLE = MethodHandles.lookup(); // for testing findSpecial
jrose55220c32009-10-21 23:19:48 -0700375 }
jrose2cc9c832010-04-30 23:48:23 -0700376 static final Lookup EXAMPLE = Example.EXAMPLE;
jrose55220c32009-10-21 23:19:48 -0700377 public static class PubExample extends Example {
jrose2cc9c832010-04-30 23:48:23 -0700378 public PubExample() { super("PubExample#"+nextArg()); }
jrose55220c32009-10-21 23:19:48 -0700379 }
380 static class SubExample extends Example {
381 @Override public void v0() { called("Sub/v0", this); }
382 @Override void pkg_v0() { called("Sub/pkg_v0", this); }
383 private SubExample(int x) { called("<init>", this, x); }
jrose2cc9c832010-04-30 23:48:23 -0700384 public SubExample() { super("SubExample#"+nextArg()); }
jrose55220c32009-10-21 23:19:48 -0700385 }
386 public static interface IntExample {
387 public void v0();
jrosebe2db602010-09-08 18:40:34 -0700388 public static class Impl implements IntExample {
jrose55220c32009-10-21 23:19:48 -0700389 public void v0() { called("Int/v0", this); }
390 final String name;
jrose2cc9c832010-04-30 23:48:23 -0700391 public Impl() { name = "Impl#"+nextArg(); }
392 @Override public String toString() { return name; }
jrose55220c32009-10-21 23:19:48 -0700393 }
394 }
395
396 static final Object[][][] ACCESS_CASES = {
jrose2cc9c832010-04-30 23:48:23 -0700397 { { false, PUBLIC }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false
398 { { false, PUBLIC }, { false, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[1]: only PRIVATE
399 { { false, PUBLIC }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[2]: PUBLIC false
400 { { true, PUBLIC }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[3]: all true
jrose55220c32009-10-21 23:19:48 -0700401 };
402
jrose2cc9c832010-04-30 23:48:23 -0700403 static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) {
404 Object[][] cases;
405 if (name.contains("pri_") || isSpecial) {
406 cases = ACCESS_CASES[1]; // PRIVATE only
407 } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) {
408 cases = ACCESS_CASES[2]; // not PUBLIC
jrose10f3b682010-01-07 16:16:45 -0800409 } else {
410 assertTrue(name.indexOf('_') < 0);
411 boolean pubc = Modifier.isPublic(defc.getModifiers());
412 if (pubc)
jrose2cc9c832010-04-30 23:48:23 -0700413 cases = ACCESS_CASES[3]; // all access levels
414 else
415 cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC
jrose10f3b682010-01-07 16:16:45 -0800416 }
jrose2cc9c832010-04-30 23:48:23 -0700417 if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE)
418 cases = Arrays.copyOfRange(cases, 0, cases.length-1);
419 return cases;
420 }
421 static Object[][] accessCases(Class<?> defc, String name) {
422 return accessCases(defc, name, false);
jrose55220c32009-10-21 23:19:48 -0700423 }
424
425 @Test
426 public void testFindStatic() throws Throwable {
427 if (CAN_SKIP_WORKING) return;
428 startTest("findStatic");
429 testFindStatic(PubExample.class, void.class, "s0");
430 testFindStatic(Example.class, void.class, "s0");
431 testFindStatic(Example.class, void.class, "pkg_s0");
432 testFindStatic(Example.class, void.class, "pri_s0");
433
434 testFindStatic(Example.class, Object.class, "s1", Object.class);
435 testFindStatic(Example.class, Object.class, "s2", int.class);
436 testFindStatic(Example.class, Object.class, "s3", long.class);
437 testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
438 testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
439 testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
440 testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
441
442 testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
443 }
444
445 void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
446 for (Object[] ac : accessCases(defc, name)) {
447 testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
448 }
449 }
450 void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
451 testFindStatic(true, lookup, defc, ret, name, params);
452 }
453 void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
454 countTest(positive);
jrose10f3b682010-01-07 16:16:45 -0800455 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700456 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700457 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700458 try {
jrose2cc9c832010-04-30 23:48:23 -0700459 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700460 target = lookup.in(defc).findStatic(defc, name, type);
jrose55220c32009-10-21 23:19:48 -0700461 } catch (NoAccessException ex) {
462 noAccess = ex;
463 }
jrose2cc9c832010-04-30 23:48:23 -0700464 if (verbosity >= 3)
465 System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
jrose55220c32009-10-21 23:19:48 -0700466 +(noAccess == null ? "" : " !! "+noAccess));
467 if (positive && noAccess != null) throw noAccess;
468 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
469 if (!positive) return; // negative test failed as expected
470 assertEquals(type, target.type());
jrose900bafd2010-10-30 21:08:23 -0700471 assertNameStringContains(target, name);
jrose55220c32009-10-21 23:19:48 -0700472 if (!DO_MORE_CALLS && lookup != PRIVATE) return;
473 Object[] args = randomArgs(params);
474 printCalled(target, name, args);
jrose900bafd2010-10-30 21:08:23 -0700475 target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -0700476 assertCalled(name, args);
jrose2cc9c832010-04-30 23:48:23 -0700477 if (verbosity >= 1)
478 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700479 }
480
jrose900bafd2010-10-30 21:08:23 -0700481 // rough check of name string
482 static void assertNameStringContains(Object x, String s) {
483 if (x.toString().contains(s)) return;
484 assertEquals(s, x);
485 }
486
jrose55220c32009-10-21 23:19:48 -0700487 @Test
488 public void testFindVirtual() throws Throwable {
489 if (CAN_SKIP_WORKING) return;
490 startTest("findVirtual");
491 testFindVirtual(Example.class, void.class, "v0");
492 testFindVirtual(Example.class, void.class, "pkg_v0");
493 testFindVirtual(Example.class, void.class, "pri_v0");
494 testFindVirtual(Example.class, Object.class, "v1", Object.class);
495 testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
496 testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
497 testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
498 testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
499 testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
500 // test dispatch
501 testFindVirtual(SubExample.class, SubExample.class, void.class, "Sub/v0");
502 testFindVirtual(SubExample.class, Example.class, void.class, "Sub/v0");
503 testFindVirtual(SubExample.class, IntExample.class, void.class, "Sub/v0");
504 testFindVirtual(SubExample.class, SubExample.class, void.class, "Sub/pkg_v0");
505 testFindVirtual(SubExample.class, Example.class, void.class, "Sub/pkg_v0");
506 testFindVirtual(Example.class, IntExample.class, void.class, "v0");
507 testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
508 }
509
510 void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
511 Class<?> rcvc = defc;
512 testFindVirtual(rcvc, defc, ret, name, params);
513 }
514 void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
515 for (Object[] ac : accessCases(defc, name)) {
516 testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params);
517 }
518 }
519 void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
520 testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
521 }
522 void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
523 countTest(positive);
524 String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
jrose10f3b682010-01-07 16:16:45 -0800525 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700526 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700527 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700528 try {
jrose2cc9c832010-04-30 23:48:23 -0700529 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700530 target = lookup.in(defc).findVirtual(defc, methodName, type);
jrose55220c32009-10-21 23:19:48 -0700531 } catch (NoAccessException ex) {
532 noAccess = ex;
533 }
jrose2cc9c832010-04-30 23:48:23 -0700534 if (verbosity >= 3)
535 System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
jrose55220c32009-10-21 23:19:48 -0700536 +(noAccess == null ? "" : " !! "+noAccess));
537 if (positive && noAccess != null) throw noAccess;
538 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
539 if (!positive) return; // negative test failed as expected
540 Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
jrose10f3b682010-01-07 16:16:45 -0800541 MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700542 assertEquals(typeWithSelf, target.type());
jrose900bafd2010-10-30 21:08:23 -0700543 assertNameStringContains(target, methodName);
jrose55220c32009-10-21 23:19:48 -0700544 if (!DO_MORE_CALLS && lookup != PRIVATE) return;
545 Object[] argsWithSelf = randomArgs(paramsWithSelf);
546 if (rcvc != defc) argsWithSelf[0] = randomArg(rcvc);
547 printCalled(target, name, argsWithSelf);
jrose900bafd2010-10-30 21:08:23 -0700548 target.invokeWithArguments(argsWithSelf);
jrose55220c32009-10-21 23:19:48 -0700549 assertCalled(name, argsWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700550 if (verbosity >= 1)
551 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700552 }
553
554 @Test
555 public void testFindSpecial() throws Throwable {
556 if (CAN_SKIP_WORKING) return;
557 startTest("findSpecial");
jrose2cc9c832010-04-30 23:48:23 -0700558 testFindSpecial(SubExample.class, Example.class, void.class, "v0");
559 testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
560 // Do some negative testing:
561 for (Lookup lookup : new Lookup[]{ PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
562 testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
563 testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
564 testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
565 testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "bogus");
566 }
jrose55220c32009-10-21 23:19:48 -0700567 }
568
jrose2cc9c832010-04-30 23:48:23 -0700569 void testFindSpecial(Class<?> specialCaller,
570 Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
571 testFindSpecial(true, EXAMPLE, specialCaller, defc, ret, name, params);
572 testFindSpecial(true, PRIVATE, specialCaller, defc, ret, name, params);
573 testFindSpecial(false, PACKAGE, specialCaller, defc, ret, name, params);
574 testFindSpecial(false, PUBLIC, specialCaller, defc, ret, name, params);
jrose55220c32009-10-21 23:19:48 -0700575 }
jrose2cc9c832010-04-30 23:48:23 -0700576 void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
577 Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700578 countTest(positive);
jrose10f3b682010-01-07 16:16:45 -0800579 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700580 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700581 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700582 try {
jrose2cc9c832010-04-30 23:48:23 -0700583 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700584 if (verbosity >= 5) System.out.println(" lookup => "+lookup.in(specialCaller));
585 target = lookup.in(specialCaller).findSpecial(defc, name, type, specialCaller);
jrose55220c32009-10-21 23:19:48 -0700586 } catch (NoAccessException ex) {
587 noAccess = ex;
588 }
jrose2cc9c832010-04-30 23:48:23 -0700589 if (verbosity >= 3)
590 System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
591 +(target == null ? "" : target.type())
592 +(noAccess == null ? "" : " !! "+noAccess));
jrose55220c32009-10-21 23:19:48 -0700593 if (positive && noAccess != null) throw noAccess;
594 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
595 if (!positive) return; // negative test failed as expected
jrose2cc9c832010-04-30 23:48:23 -0700596 assertEquals(specialCaller, target.type().parameterType(0));
597 assertEquals(type, target.type().dropParameterTypes(0,1));
598 Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
jrose10f3b682010-01-07 16:16:45 -0800599 MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
jrose900bafd2010-10-30 21:08:23 -0700600 assertNameStringContains(target, name);
jrose2cc9c832010-04-30 23:48:23 -0700601 if (!DO_MORE_CALLS && lookup != PRIVATE && lookup != EXAMPLE) return;
jrose55220c32009-10-21 23:19:48 -0700602 Object[] args = randomArgs(paramsWithSelf);
603 printCalled(target, name, args);
jrose900bafd2010-10-30 21:08:23 -0700604 target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -0700605 assertCalled(name, args);
jrose55220c32009-10-21 23:19:48 -0700606 }
607
608 @Test
609 public void testBind() throws Throwable {
610 if (CAN_SKIP_WORKING) return;
611 startTest("bind");
612 testBind(Example.class, void.class, "v0");
613 testBind(Example.class, void.class, "pkg_v0");
614 testBind(Example.class, void.class, "pri_v0");
615 testBind(Example.class, Object.class, "v1", Object.class);
616 testBind(Example.class, Object.class, "v2", Object.class, Object.class);
617 testBind(Example.class, Object.class, "v2", Object.class, int.class);
618 testBind(Example.class, Object.class, "v2", int.class, Object.class);
619 testBind(Example.class, Object.class, "v2", int.class, int.class);
620 testBind(false, PRIVATE, Example.class, void.class, "bogus");
621 testBind(SubExample.class, void.class, "Sub/v0");
622 testBind(SubExample.class, void.class, "Sub/pkg_v0");
623 testBind(IntExample.Impl.class, void.class, "Int/v0");
624 }
625
626 void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
627 for (Object[] ac : accessCases(defc, name)) {
628 testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
629 }
630 }
631
632 void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
633 countTest(positive);
634 String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
jrose10f3b682010-01-07 16:16:45 -0800635 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700636 Object receiver = randomArg(defc);
637 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700638 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700639 try {
jrose2cc9c832010-04-30 23:48:23 -0700640 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
jrose900bafd2010-10-30 21:08:23 -0700641 target = lookup.in(defc).bind(receiver, methodName, type);
jrose55220c32009-10-21 23:19:48 -0700642 } catch (NoAccessException ex) {
643 noAccess = ex;
644 }
jrose2cc9c832010-04-30 23:48:23 -0700645 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -0700646 System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
647 +(noAccess == null ? "" : " !! "+noAccess));
648 if (positive && noAccess != null) throw noAccess;
649 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
650 if (!positive) return; // negative test failed as expected
651 assertEquals(type, target.type());
652 Object[] args = randomArgs(params);
653 printCalled(target, name, args);
jrose900bafd2010-10-30 21:08:23 -0700654 target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -0700655 Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
656 assertCalled(name, argsWithReceiver);
jrose2cc9c832010-04-30 23:48:23 -0700657 if (verbosity >= 1)
658 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700659 }
660
661 @Test
662 public void testUnreflect() throws Throwable {
663 if (CAN_SKIP_WORKING) return;
664 startTest("unreflect");
665 testUnreflect(Example.class, true, void.class, "s0");
666 testUnreflect(Example.class, true, void.class, "pkg_s0");
667 testUnreflect(Example.class, true, void.class, "pri_s0");
668
669 testUnreflect(Example.class, true, Object.class, "s1", Object.class);
670 testUnreflect(Example.class, true, Object.class, "s2", int.class);
jrose2cc9c832010-04-30 23:48:23 -0700671 testUnreflect(Example.class, true, Object.class, "s3", long.class);
672 testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class);
673 testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class);
674 testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class);
jrose55220c32009-10-21 23:19:48 -0700675
676 testUnreflect(Example.class, false, void.class, "v0");
677 testUnreflect(Example.class, false, void.class, "pkg_v0");
678 testUnreflect(Example.class, false, void.class, "pri_v0");
679 testUnreflect(Example.class, false, Object.class, "v1", Object.class);
680 testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
681 testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
682 testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
683 testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
684 }
685
686 void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
687 for (Object[] ac : accessCases(defc, name)) {
jrose2cc9c832010-04-30 23:48:23 -0700688 testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, (isStatic ? null : defc), ret, name, params);
jrose55220c32009-10-21 23:19:48 -0700689 }
690 }
jrose2cc9c832010-04-30 23:48:23 -0700691 void testUnreflect(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
692 for (Object[] ac : accessCases(defc, name)) {
693 testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
694 }
695 }
696 void testUnreflectMaybeSpecial(Class<?> specialCaller,
697 boolean positive, Lookup lookup,
698 Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700699 countTest(positive);
jrose10f3b682010-01-07 16:16:45 -0800700 MethodType type = MethodType.methodType(ret, params);
jrose55220c32009-10-21 23:19:48 -0700701 Method rmethod = null;
702 MethodHandle target = null;
jrosea1ebbe62010-09-08 18:40:23 -0700703 Exception noAccess = null;
jrose55220c32009-10-21 23:19:48 -0700704 try {
705 rmethod = defc.getDeclaredMethod(name, params);
706 } catch (NoSuchMethodException ex) {
707 throw new NoAccessException(ex);
708 }
jrose2cc9c832010-04-30 23:48:23 -0700709 boolean isStatic = (rcvc == null);
710 boolean isSpecial = (specialCaller != null);
jrose55220c32009-10-21 23:19:48 -0700711 try {
jrose2cc9c832010-04-30 23:48:23 -0700712 if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
713 if (isSpecial)
jrose900bafd2010-10-30 21:08:23 -0700714 target = lookup.in(specialCaller).unreflectSpecial(rmethod, specialCaller);
jrose2cc9c832010-04-30 23:48:23 -0700715 else
jrose900bafd2010-10-30 21:08:23 -0700716 target = lookup.in(defc).unreflect(rmethod);
jrose55220c32009-10-21 23:19:48 -0700717 } catch (NoAccessException ex) {
718 noAccess = ex;
719 }
jrose2cc9c832010-04-30 23:48:23 -0700720 if (verbosity >= 3)
721 System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
722 +(!isSpecial ? "" : " specialCaller="+specialCaller)
723 +( isStatic ? "" : " receiver="+rcvc)
724 +" => "+target
725 +(noAccess == null ? "" : " !! "+noAccess));
jrose55220c32009-10-21 23:19:48 -0700726 if (positive && noAccess != null) throw noAccess;
727 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
728 if (!positive) return; // negative test failed as expected
jrose2cc9c832010-04-30 23:48:23 -0700729 assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
jrose55220c32009-10-21 23:19:48 -0700730 Class<?>[] paramsMaybeWithSelf = params;
731 if (!isStatic) {
jrose2cc9c832010-04-30 23:48:23 -0700732 paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
jrose55220c32009-10-21 23:19:48 -0700733 }
jrose10f3b682010-01-07 16:16:45 -0800734 MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700735 if (isStatic) {
736 assertEquals(typeMaybeWithSelf, target.type());
737 } else {
jrose2cc9c832010-04-30 23:48:23 -0700738 if (isSpecial)
739 assertEquals(specialCaller, target.type().parameterType(0));
740 else
741 assertEquals(defc, target.type().parameterType(0));
742 assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
743 }
jrose55220c32009-10-21 23:19:48 -0700744 Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
745 printCalled(target, name, argsMaybeWithSelf);
jrose900bafd2010-10-30 21:08:23 -0700746 target.invokeWithArguments(argsMaybeWithSelf);
jrose55220c32009-10-21 23:19:48 -0700747 assertCalled(name, argsMaybeWithSelf);
jrose2cc9c832010-04-30 23:48:23 -0700748 if (verbosity >= 1)
749 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -0700750 }
751
jrose2cc9c832010-04-30 23:48:23 -0700752 void testUnreflectSpecial(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
753 for (Object[] ac : accessCases(defc, name, true)) {
754 Class<?> specialCaller = rcvc;
755 testUnreflectMaybeSpecial(specialCaller, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
756 }
757 }
758
759 @Test
jrose55220c32009-10-21 23:19:48 -0700760 public void testUnreflectSpecial() throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -0700761 if (CAN_SKIP_WORKING) return;
jrose55220c32009-10-21 23:19:48 -0700762 startTest("unreflectSpecial");
jrose2cc9c832010-04-30 23:48:23 -0700763 testUnreflectSpecial(Example.class, Example.class, void.class, "v0");
764 testUnreflectSpecial(Example.class, SubExample.class, void.class, "v0");
765 testUnreflectSpecial(Example.class, Example.class, void.class, "pkg_v0");
766 testUnreflectSpecial(Example.class, SubExample.class, void.class, "pkg_v0");
767 testUnreflectSpecial(Example.class, Example.class, Object.class, "v2", int.class, int.class);
768 testUnreflectSpecial(Example.class, SubExample.class, Object.class, "v2", int.class, int.class);
769 testUnreflectMaybeSpecial(Example.class, false, PRIVATE, Example.class, Example.class, void.class, "s0");
jrose55220c32009-10-21 23:19:48 -0700770 }
771
jrose10f3b682010-01-07 16:16:45 -0800772 public static class HasFields {
773 boolean fZ = false;
774 byte fB = (byte)'B';
775 short fS = (short)'S';
776 char fC = 'C';
777 int fI = 'I';
778 long fJ = 'J';
779 float fF = 'F';
780 double fD = 'D';
781 static boolean sZ = true;
782 static byte sB = 1+(byte)'B';
783 static short sS = 1+(short)'S';
784 static char sC = 1+'C';
785 static int sI = 1+'I';
786 static long sJ = 1+'J';
787 static float sF = 1+'F';
788 static double sD = 1+'D';
789
790 Object fL = 'L';
791 String fR = "R";
792 static Object sL = 'M';
793 static String sR = "S";
794
795 static final Object[][] CASES;
796 static {
797 ArrayList<Object[]> cases = new ArrayList<Object[]>();
798 Object types[][] = {
799 {'L',Object.class}, {'R',String.class},
800 {'I',int.class}, {'J',long.class},
801 {'F',float.class}, {'D',double.class},
802 {'Z',boolean.class}, {'B',byte.class},
803 {'S',short.class}, {'C',char.class},
804 };
805 HasFields fields = new HasFields();
806 for (Object[] t : types) {
807 for (int kind = 0; kind <= 1; kind++) {
808 boolean isStatic = (kind != 0);
809 char btc = (Character)t[0];
810 String name = (isStatic ? "s" : "f") + btc;
811 Class<?> type = (Class<?>) t[1];
812 Object value;
813 Field field;
814 try {
815 field = HasFields.class.getDeclaredField(name);
816 } catch (Exception ex) {
817 throw new InternalError("no field HasFields."+name);
818 }
819 try {
820 value = field.get(fields);
821 } catch (Exception ex) {
822 throw new InternalError("cannot fetch field HasFields."+name);
823 }
824 if (type == float.class) {
825 float v = 'F';
826 if (isStatic) v++;
827 assert(value.equals(v));
828 }
829 assert(name.equals(field.getName()));
830 assert(type.equals(field.getType()));
831 assert(isStatic == (Modifier.isStatic(field.getModifiers())));
832 cases.add(new Object[]{ field, value });
833 }
834 }
835 CASES = cases.toArray(new Object[0][]);
836 }
837 }
838
jrosecf98d422010-06-08 23:08:56 -0700839 static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC_FIELD = 3;
840 static boolean testModeMatches(int testMode, boolean isStatic) {
841 switch (testMode) {
842 case TEST_FIND_STATIC_FIELD: return isStatic;
843 case TEST_FIND_FIELD: return !isStatic;
844 default: return true; // unreflect matches both
845 }
846 }
847
jrose10f3b682010-01-07 16:16:45 -0800848 @Test
jrose55220c32009-10-21 23:19:48 -0700849 public void testUnreflectGetter() throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700850 startTest("unreflectGetter");
jrosecf98d422010-06-08 23:08:56 -0700851 testGetter(TEST_UNREFLECT);
852 }
853 @Test
854 public void testFindGetter() throws Throwable {
855 startTest("findGetter");
856 testGetter(TEST_FIND_FIELD);
857 }
858 @Test
859 public void testFindStaticGetter() throws Throwable {
860 startTest("findStaticGetter");
861 testGetter(TEST_FIND_STATIC_FIELD);
862 }
863 public void testGetter(int testMode) throws Throwable {
864 Lookup lookup = PRIVATE; // FIXME: test more lookups than this one
jrose10f3b682010-01-07 16:16:45 -0800865 for (Object[] c : HasFields.CASES) {
866 Field f = (Field)c[0];
867 Object value = c[1];
868 Class<?> type = f.getType();
jrosecf98d422010-06-08 23:08:56 -0700869 testGetter(lookup, f, type, value, testMode);
jrose10f3b682010-01-07 16:16:45 -0800870 }
871 }
jrosecf98d422010-06-08 23:08:56 -0700872 public void testGetter(MethodHandles.Lookup lookup,
873 Field f, Class<?> type, Object value, int testMode) throws Throwable {
jrose10f3b682010-01-07 16:16:45 -0800874 boolean isStatic = Modifier.isStatic(f.getModifiers());
jrosecf98d422010-06-08 23:08:56 -0700875 Class<?> fclass = f.getDeclaringClass();
876 String fname = f.getName();
877 Class<?> ftype = f.getType();
878 if (!testModeMatches(testMode, isStatic)) return;
879 countTest(true);
jrose10f3b682010-01-07 16:16:45 -0800880 MethodType expType = MethodType.methodType(type, HasFields.class);
881 if (isStatic) expType = expType.dropParameterTypes(0, 1);
882 MethodHandle mh = lookup.unreflectGetter(f);
883 assertSame(mh.type(), expType);
jrose900bafd2010-10-30 21:08:23 -0700884 assertNameStringContains(mh, fname);
jrose10f3b682010-01-07 16:16:45 -0800885 HasFields fields = new HasFields();
886 Object sawValue;
887 Class<?> rtype = type;
888 if (type != int.class) rtype = Object.class;
889 mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(rtype));
890 Object expValue = value;
891 for (int i = 0; i <= 1; i++) {
892 if (isStatic) {
893 if (type == int.class)
jrose1c1bfac2010-11-22 22:41:31 -0800894 sawValue = (int) mh.invokeExact(); // do these exactly
jrose10f3b682010-01-07 16:16:45 -0800895 else
jrose2cc9c832010-04-30 23:48:23 -0700896 sawValue = mh.invokeExact();
jrose10f3b682010-01-07 16:16:45 -0800897 } else {
898 if (type == int.class)
jrose1c1bfac2010-11-22 22:41:31 -0800899 sawValue = (int) mh.invokeExact((Object) fields);
jrose10f3b682010-01-07 16:16:45 -0800900 else
jrose2cc9c832010-04-30 23:48:23 -0700901 sawValue = mh.invokeExact((Object) fields);
jrose10f3b682010-01-07 16:16:45 -0800902 }
903 assertEquals(sawValue, expValue);
904 Object random = randomArg(type);
905 f.set(fields, random);
906 expValue = random;
907 }
908 f.set(fields, value); // put it back
jrose55220c32009-10-21 23:19:48 -0700909 }
910
jrose10f3b682010-01-07 16:16:45 -0800911
912 @Test
jrose55220c32009-10-21 23:19:48 -0700913 public void testUnreflectSetter() throws Throwable {
jrosecf98d422010-06-08 23:08:56 -0700914 startTest("unreflectSetter");
915 testSetter(TEST_UNREFLECT);
916 }
917 @Test
918 public void testFindSetter() throws Throwable {
919 startTest("findSetter");
920 testSetter(TEST_FIND_FIELD);
921 }
922 @Test
923 public void testFindStaticSetter() throws Throwable {
924 startTest("findStaticSetter");
925 testSetter(TEST_FIND_STATIC_FIELD);
926 }
927 public void testSetter(int testMode) throws Throwable {
jrose55220c32009-10-21 23:19:48 -0700928 Lookup lookup = PRIVATE; // FIXME: test more lookups than this one
929 startTest("unreflectSetter");
jrose10f3b682010-01-07 16:16:45 -0800930 for (Object[] c : HasFields.CASES) {
931 Field f = (Field)c[0];
932 Object value = c[1];
933 Class<?> type = f.getType();
jrosecf98d422010-06-08 23:08:56 -0700934 testSetter(lookup, f, type, value, testMode);
jrose10f3b682010-01-07 16:16:45 -0800935 }
936 }
jrosecf98d422010-06-08 23:08:56 -0700937 public void testSetter(MethodHandles.Lookup lookup,
938 Field f, Class<?> type, Object value, int testMode) throws Throwable {
jrose10f3b682010-01-07 16:16:45 -0800939 boolean isStatic = Modifier.isStatic(f.getModifiers());
jrosecf98d422010-06-08 23:08:56 -0700940 Class<?> fclass = f.getDeclaringClass();
941 String fname = f.getName();
942 Class<?> ftype = f.getType();
943 if (!testModeMatches(testMode, isStatic)) return;
944 countTest(true);
jrose10f3b682010-01-07 16:16:45 -0800945 MethodType expType = MethodType.methodType(void.class, HasFields.class, type);
946 if (isStatic) expType = expType.dropParameterTypes(0, 1);
jrosecf98d422010-06-08 23:08:56 -0700947 MethodHandle mh;
948 if (testMode == TEST_UNREFLECT)
949 mh = lookup.unreflectSetter(f);
950 else if (testMode == TEST_FIND_FIELD)
951 mh = lookup.findSetter(fclass, fname, ftype);
952 else if (testMode == TEST_FIND_STATIC_FIELD)
953 mh = lookup.findStaticSetter(fclass, fname, ftype);
954 else throw new InternalError();
jrose10f3b682010-01-07 16:16:45 -0800955 assertSame(mh.type(), expType);
jrose900bafd2010-10-30 21:08:23 -0700956 assertNameStringContains(mh, fname);
jrose10f3b682010-01-07 16:16:45 -0800957 HasFields fields = new HasFields();
958 Object sawValue;
959 Class<?> vtype = type;
960 if (type != int.class) vtype = Object.class;
961 int last = mh.type().parameterCount() - 1;
962 mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(void.class).changeParameterType(last, vtype));
963 assertEquals(f.get(fields), value); // clean to start with
964 for (int i = 0; i <= 1; i++) {
965 Object putValue = randomArg(type);
966 if (isStatic) {
967 if (type == int.class)
jrose1c1bfac2010-11-22 22:41:31 -0800968 mh.invokeExact((int)putValue); // do these exactly
jrose10f3b682010-01-07 16:16:45 -0800969 else
jrose1c1bfac2010-11-22 22:41:31 -0800970 mh.invokeExact(putValue);
jrose10f3b682010-01-07 16:16:45 -0800971 } else {
972 if (type == int.class)
jrose1c1bfac2010-11-22 22:41:31 -0800973 mh.invokeExact((Object) fields, (int)putValue);
jrose10f3b682010-01-07 16:16:45 -0800974 else
jrose1c1bfac2010-11-22 22:41:31 -0800975 mh.invokeExact((Object) fields, putValue);
jrose10f3b682010-01-07 16:16:45 -0800976 }
977 assertEquals(f.get(fields), putValue);
978 }
979 f.set(fields, value); // put it back
jrose55220c32009-10-21 23:19:48 -0700980 }
981
jrose10f3b682010-01-07 16:16:45 -0800982 @Test
jrose55220c32009-10-21 23:19:48 -0700983 public void testArrayElementGetter() throws Throwable {
984 startTest("arrayElementGetter");
jrose2cc9c832010-04-30 23:48:23 -0700985 testArrayElementGetterSetter(false);
jrose55220c32009-10-21 23:19:48 -0700986 }
987
jrose10f3b682010-01-07 16:16:45 -0800988 @Test
jrose55220c32009-10-21 23:19:48 -0700989 public void testArrayElementSetter() throws Throwable {
990 startTest("arrayElementSetter");
jrose2cc9c832010-04-30 23:48:23 -0700991 testArrayElementGetterSetter(true);
992 }
993
994 public void testArrayElementGetterSetter(boolean testSetter) throws Throwable {
995 testArrayElementGetterSetter(new Object[10], testSetter);
996 testArrayElementGetterSetter(new String[10], testSetter);
997 testArrayElementGetterSetter(new boolean[10], testSetter);
998 testArrayElementGetterSetter(new byte[10], testSetter);
999 testArrayElementGetterSetter(new char[10], testSetter);
1000 testArrayElementGetterSetter(new short[10], testSetter);
1001 testArrayElementGetterSetter(new int[10], testSetter);
1002 testArrayElementGetterSetter(new float[10], testSetter);
1003 testArrayElementGetterSetter(new long[10], testSetter);
1004 testArrayElementGetterSetter(new double[10], testSetter);
jrose10f3b682010-01-07 16:16:45 -08001005 }
1006
1007 public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable {
1008 countTest(true);
jrose2cc9c832010-04-30 23:48:23 -07001009 if (verbosity >= 2) System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+Array.getLength(array)+"]");
jrose10f3b682010-01-07 16:16:45 -08001010 Class<?> arrayType = array.getClass();
1011 Class<?> elemType = arrayType.getComponentType();
1012 MethodType expType = !testSetter
1013 ? MethodType.methodType(elemType, arrayType, int.class)
1014 : MethodType.methodType(void.class, arrayType, int.class, elemType);
1015 MethodHandle mh = !testSetter
1016 ? MethodHandles.arrayElementGetter(arrayType)
1017 : MethodHandles.arrayElementSetter(arrayType);
1018 assertSame(mh.type(), expType);
jrose2cc9c832010-04-30 23:48:23 -07001019 if (elemType != int.class && elemType != boolean.class) {
1020 MethodType gtype;
1021 if (true) { // FIXME: remove this path (and remove <void> below in the mh.invokes)
1022 gtype = mh.type().changeParameterType(0, Object.class);
1023 if (testSetter)
1024 gtype = gtype.changeParameterType(2, Object.class);
1025 else
1026 gtype = gtype.changeReturnType(Object.class);
1027 } else
1028 // FIXME: This simpler path hits a bug in convertArguments => ToGeneric
1029 gtype = mh.type().generic().changeParameterType(1, int.class);
1030 mh = MethodHandles.convertArguments(mh, gtype);
1031 }
jrose10f3b682010-01-07 16:16:45 -08001032 Object sawValue, expValue;
1033 List<Object> model = array2list(array);
1034 int length = Array.getLength(array);
1035 for (int i = 0; i < length; i++) {
1036 // update array element
1037 Object random = randomArg(elemType);
1038 model.set(i, random);
1039 if (testSetter) {
1040 if (elemType == int.class)
jrose1c1bfac2010-11-22 22:41:31 -08001041 mh.invokeExact((int[]) array, i, (int)random);
jrose2cc9c832010-04-30 23:48:23 -07001042 else if (elemType == boolean.class)
jrose1c1bfac2010-11-22 22:41:31 -08001043 mh.invokeExact((boolean[]) array, i, (boolean)random);
jrose10f3b682010-01-07 16:16:45 -08001044 else
jrose1c1bfac2010-11-22 22:41:31 -08001045 mh.invokeExact(array, i, random);
jrose10f3b682010-01-07 16:16:45 -08001046 assertEquals(model, array2list(array));
1047 } else {
1048 Array.set(array, i, random);
jrose2cc9c832010-04-30 23:48:23 -07001049 }
1050 if (verbosity >= 5) {
1051 List<Object> array2list = array2list(array);
1052 System.out.println("a["+i+"]="+random+" => "+array2list);
1053 if (!array2list.equals(model))
1054 System.out.println("*** != "+model);
jrose10f3b682010-01-07 16:16:45 -08001055 }
1056 // observe array element
1057 sawValue = Array.get(array, i);
1058 if (!testSetter) {
1059 expValue = sawValue;
1060 if (elemType == int.class)
jrose1c1bfac2010-11-22 22:41:31 -08001061 sawValue = (int) mh.invokeExact((int[]) array, i);
jrose2cc9c832010-04-30 23:48:23 -07001062 else if (elemType == boolean.class)
jrose1c1bfac2010-11-22 22:41:31 -08001063 sawValue = (boolean) mh.invokeExact((boolean[]) array, i);
jrose10f3b682010-01-07 16:16:45 -08001064 else
jrose2cc9c832010-04-30 23:48:23 -07001065 sawValue = mh.invokeExact(array, i);
jrose10f3b682010-01-07 16:16:45 -08001066 assertEquals(sawValue, expValue);
1067 assertEquals(model, array2list(array));
1068 }
1069 }
1070 }
1071
1072 List<Object> array2list(Object array) {
1073 int length = Array.getLength(array);
1074 ArrayList<Object> model = new ArrayList<Object>(length);
1075 for (int i = 0; i < length; i++)
1076 model.add(Array.get(array, i));
1077 return model;
jrose55220c32009-10-21 23:19:48 -07001078 }
1079
1080 static class Callee {
1081 static Object id() { return called("id"); }
1082 static Object id(Object x) { return called("id", x); }
1083 static Object id(Object x, Object y) { return called("id", x, y); }
1084 static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
1085 static Object id(Object... vx) { return called("id", vx); }
1086 static MethodHandle ofType(int n) {
1087 return ofType(Object.class, n);
1088 }
1089 static MethodHandle ofType(Class<?> rtype, int n) {
1090 if (n == -1)
jrose10f3b682010-01-07 16:16:45 -08001091 return ofType(MethodType.methodType(rtype, Object[].class));
1092 return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
jrose55220c32009-10-21 23:19:48 -07001093 }
1094 static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
jrose10f3b682010-01-07 16:16:45 -08001095 return ofType(MethodType.methodType(rtype, ptypes));
jrose55220c32009-10-21 23:19:48 -07001096 }
1097 static MethodHandle ofType(MethodType type) {
1098 Class<?> rtype = type.returnType();
1099 String pfx = "";
1100 if (rtype != Object.class)
1101 pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
1102 String name = pfx+"id";
jrosea1ebbe62010-09-08 18:40:23 -07001103 try {
1104 return PRIVATE.findStatic(Callee.class, name, type);
1105 } catch (Exception ex) {
1106 throw new RuntimeException(ex);
1107 }
jrose55220c32009-10-21 23:19:48 -07001108 }
1109 }
1110
1111 @Test
1112 public void testConvertArguments() throws Throwable {
1113 if (CAN_SKIP_WORKING) return;
1114 startTest("convertArguments");
1115 testConvert(Callee.ofType(1), null, "id", int.class);
1116 testConvert(Callee.ofType(1), null, "id", String.class);
1117 testConvert(Callee.ofType(1), null, "id", Integer.class);
1118 testConvert(Callee.ofType(1), null, "id", short.class);
jrose2cc9c832010-04-30 23:48:23 -07001119 testConvert(Callee.ofType(1), null, "id", char.class);
1120 testConvert(Callee.ofType(1), null, "id", byte.class);
jrose55220c32009-10-21 23:19:48 -07001121 }
1122
1123 void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
jrose900bafd2010-10-30 21:08:23 -07001124 testConvert(true, false, id, rtype, name, params);
1125 testConvert(true, true, id, rtype, name, params);
jrose55220c32009-10-21 23:19:48 -07001126 }
1127
jrose900bafd2010-10-30 21:08:23 -07001128 void testConvert(boolean positive, boolean useAsType,
1129 MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
jrose55220c32009-10-21 23:19:48 -07001130 countTest(positive);
1131 MethodType idType = id.type();
1132 if (rtype == null) rtype = idType.returnType();
1133 for (int i = 0; i < params.length; i++) {
1134 if (params[i] == null) params[i] = idType.parameterType(i);
1135 }
1136 // simulate the pairwise conversion
jrose10f3b682010-01-07 16:16:45 -08001137 MethodType newType = MethodType.methodType(rtype, params);
jrose55220c32009-10-21 23:19:48 -07001138 Object[] args = randomArgs(newType.parameterArray());
1139 Object[] convArgs = args.clone();
1140 for (int i = 0; i < args.length; i++) {
1141 Class<?> src = newType.parameterType(i);
1142 Class<?> dst = idType.parameterType(i);
1143 if (src != dst)
1144 convArgs[i] = castToWrapper(convArgs[i], dst);
1145 }
jrose900bafd2010-10-30 21:08:23 -07001146 Object convResult = id.invokeWithArguments(convArgs);
jrose55220c32009-10-21 23:19:48 -07001147 {
1148 Class<?> dst = newType.returnType();
1149 Class<?> src = idType.returnType();
1150 if (src != dst)
1151 convResult = castToWrapper(convResult, dst);
1152 }
1153 MethodHandle target = null;
1154 RuntimeException error = null;
1155 try {
jrose900bafd2010-10-30 21:08:23 -07001156 if (useAsType)
jrose900bafd2010-10-30 21:08:23 -07001157 target = id.asType(newType);
jrosef108fc02011-02-11 01:26:24 -08001158 else
1159 target = MethodHandles.convertArguments(id, newType);
jrose55220c32009-10-21 23:19:48 -07001160 } catch (RuntimeException ex) {
1161 error = ex;
1162 }
jrose2cc9c832010-04-30 23:48:23 -07001163 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -07001164 System.out.println("convert "+id+ " to "+newType+" => "+target
1165 +(error == null ? "" : " !! "+error));
1166 if (positive && error != null) throw error;
1167 assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
1168 if (!positive) return; // negative test failed as expected
1169 assertEquals(newType, target.type());
1170 printCalled(target, id.toString(), args);
jrose900bafd2010-10-30 21:08:23 -07001171 Object result = target.invokeWithArguments(args);
jrose55220c32009-10-21 23:19:48 -07001172 assertCalled(name, convArgs);
1173 assertEquals(convResult, result);
jrose2cc9c832010-04-30 23:48:23 -07001174 if (verbosity >= 1)
1175 System.out.print(':');
jrose55220c32009-10-21 23:19:48 -07001176 }
1177
1178 @Test
jrosef108fc02011-02-11 01:26:24 -08001179 public void testVarargsCollector() throws Throwable {
1180 MethodHandle vac0 = PRIVATE.findStatic(MethodHandlesTest.class, "called",
1181 MethodType.methodType(Object.class, String.class, Object[].class));
1182 vac0 = vac0.bindTo("vac");
1183 MethodHandle vac = vac0.asVarargsCollector(Object[].class);
1184 testConvert(true, true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
1185 testConvert(true, true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
1186 for (Class<?> at : new Class[] { Object.class, String.class, Integer.class }) {
1187 testConvert(true, true, vac.asType(MethodType.genericMethodType(1)), null, "vac", at);
1188 testConvert(true, true, vac.asType(MethodType.genericMethodType(2)), null, "vac", at, at);
1189 }
1190 }
1191
1192 @Test
jrose10f3b682010-01-07 16:16:45 -08001193 public void testPermuteArguments() throws Throwable {
1194 if (CAN_SKIP_WORKING) return;
1195 startTest("permuteArguments");
1196 testPermuteArguments(4, Integer.class, 2, String.class, 0);
1197 //testPermuteArguments(6, Integer.class, 0, null, 30);
1198 //testPermuteArguments(4, Integer.class, 1, int.class, 6);
1199 }
1200 public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -07001201 if (verbosity >= 2)
jrose10f3b682010-01-07 16:16:45 -08001202 System.out.println("permuteArguments "+max+"*"+type1.getName()
1203 +(t2c==0?"":"/"+t2c+"*"+type2.getName())
1204 +(dilution > 0 ? " with dilution "+dilution : ""));
1205 int t2pos = t2c == 0 ? 0 : 1;
1206 for (int inargs = t2pos+1; inargs <= max; inargs++) {
1207 Class<?>[] types = new Class<?>[inargs];
1208 Arrays.fill(types, type1);
1209 if (t2c != 0) {
1210 // Fill in a middle range with type2:
1211 Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
1212 }
1213 Object[] args = randomArgs(types);
1214 int numcases = 1;
1215 for (int outargs = 0; outargs <= max; outargs++) {
1216 if (outargs - inargs >= MAX_ARG_INCREASE) continue;
1217 int[] reorder = new int[outargs];
1218 int casStep = dilution + 1;
1219 // Avoid some common factors:
1220 while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
1221 (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
1222 casStep++;
1223 for (int cas = 0; cas < numcases; cas += casStep) {
1224 for (int i = 0, c = cas; i < outargs; i++) {
1225 reorder[i] = c % inargs;
1226 c /= inargs;
1227 }
1228 testPermuteArguments(args, types, reorder);
1229 }
1230 numcases *= inargs;
1231 if (dilution > 10 && outargs >= 4) {
1232 // Do some special patterns, which we probably missed.
1233 // Replication of a single argument or argument pair.
1234 for (int i = 0; i < inargs; i++) {
1235 Arrays.fill(reorder, i);
1236 testPermuteArguments(args, types, reorder);
1237 for (int d = 1; d <= 2; d++) {
1238 if (i + d >= inargs) continue;
1239 for (int j = 1; j < outargs; j += 2)
1240 reorder[j] += 1;
1241 testPermuteArguments(args, types, reorder);
1242 testPermuteArguments(args, types, reverse(reorder));
1243 }
1244 }
1245 // Repetition of a sequence of 3 or more arguments.
1246 for (int i = 1; i < inargs; i++) {
1247 for (int len = 3; len <= inargs; len++) {
1248 for (int j = 0; j < outargs; j++)
1249 reorder[j] = (i + (j % len)) % inargs;
1250 testPermuteArguments(args, types, reorder);
1251 testPermuteArguments(args, types, reverse(reorder));
1252 }
1253 }
1254 }
1255 }
1256 }
1257 }
1258
1259 static int[] reverse(int[] reorder) {
1260 reorder = reorder.clone();
1261 for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
1262 int j = reorder.length - 1 - i;
1263 int tem = reorder[i];
1264 reorder[i] = reorder[j];
1265 reorder[j] = tem;
1266 }
1267 return reorder;
1268 }
1269
1270 void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
1271 countTest();
1272 if (args == null && types == null) {
1273 int max = 0;
1274 for (int j : reorder) {
1275 if (max < j) max = j;
1276 }
1277 args = randomArgs(max+1, Integer.class);
1278 }
1279 if (args == null) {
1280 args = randomArgs(types);
1281 }
1282 if (types == null) {
1283 types = new Class<?>[args.length];
1284 for (int i = 0; i < args.length; i++)
1285 types[i] = args[i].getClass();
1286 }
1287 int inargs = args.length, outargs = reorder.length;
1288 assert(inargs == types.length);
jrose2cc9c832010-04-30 23:48:23 -07001289 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001290 System.out.println("permuteArguments "+Arrays.toString(reorder));
1291 Object[] permArgs = new Object[outargs];
1292 Class<?>[] permTypes = new Class<?>[outargs];
1293 for (int i = 0; i < outargs; i++) {
1294 permArgs[i] = args[reorder[i]];
1295 permTypes[i] = types[reorder[i]];
1296 }
jrose2cc9c832010-04-30 23:48:23 -07001297 if (verbosity >= 4) {
jrose10f3b682010-01-07 16:16:45 -08001298 System.out.println("in args: "+Arrays.asList(args));
1299 System.out.println("out args: "+Arrays.asList(permArgs));
1300 System.out.println("in types: "+Arrays.asList(types));
1301 System.out.println("out types: "+Arrays.asList(permTypes));
1302 }
1303 MethodType inType = MethodType.methodType(Object.class, types);
1304 MethodType outType = MethodType.methodType(Object.class, permTypes);
1305 MethodHandle target = MethodHandles.convertArguments(ValueConversions.varargsList(outargs), outType);
1306 MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
jrose900bafd2010-10-30 21:08:23 -07001307 Object result = newTarget.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001308 Object expected = Arrays.asList(permArgs);
1309 assertEquals(expected, result);
1310 }
1311
1312
1313 @Test
1314 public void testSpreadArguments() throws Throwable {
1315 if (CAN_SKIP_WORKING) return;
1316 startTest("spreadArguments");
1317 for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
jrose2cc9c832010-04-30 23:48:23 -07001318 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001319 System.out.println("spreadArguments "+argType);
jrose2cc9c832010-04-30 23:48:23 -07001320 // FIXME: enable _adapter_spread_args and fix Fail_2
jrose10f3b682010-01-07 16:16:45 -08001321 for (int nargs = 0; nargs < 10; nargs++) {
1322 if (argType == int.class && nargs >= 6) continue; // FIXME Fail_1
1323 for (int pos = 0; pos < nargs; pos++) {
1324 if (argType == int.class && pos > 0) continue; // FIXME Fail_3
jrose2cc9c832010-04-30 23:48:23 -07001325 testSpreadArguments(argType, pos, nargs);
jrose10f3b682010-01-07 16:16:45 -08001326 }
1327 }
1328 }
1329 }
1330 public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable {
1331 countTest();
1332 MethodHandle target = ValueConversions.varargsArray(nargs);
1333 MethodHandle target2 = changeArgTypes(target, argType);
jrose2cc9c832010-04-30 23:48:23 -07001334 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001335 System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
1336 Object[] args = randomArgs(target2.type().parameterArray());
1337 // make sure the target does what we think it does:
1338 if (pos == 0 && nargs < 5) {
jrose900bafd2010-10-30 21:08:23 -07001339 Object[] check = (Object[]) target.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001340 assertArrayEquals(args, check);
1341 switch (nargs) {
1342 case 0:
jrose1c1bfac2010-11-22 22:41:31 -08001343 check = (Object[]) target.invokeExact();
jrose10f3b682010-01-07 16:16:45 -08001344 assertArrayEquals(args, check);
1345 break;
1346 case 1:
jrose1c1bfac2010-11-22 22:41:31 -08001347 check = (Object[]) target.invokeExact(args[0]);
jrose10f3b682010-01-07 16:16:45 -08001348 assertArrayEquals(args, check);
1349 break;
1350 case 2:
jrose1c1bfac2010-11-22 22:41:31 -08001351 check = (Object[]) target.invokeExact(args[0], args[1]);
jrose10f3b682010-01-07 16:16:45 -08001352 assertArrayEquals(args, check);
1353 break;
1354 }
1355 }
1356 List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList());
1357 { // modify newParams in place
1358 List<Class<?>> spreadParams = newParams.subList(pos, nargs);
1359 spreadParams.clear(); spreadParams.add(Object[].class);
1360 }
1361 MethodType newType = MethodType.methodType(Object.class, newParams);
1362 MethodHandle result = MethodHandles.spreadArguments(target2, newType);
1363 Object[] returnValue;
1364 if (pos == 0) {
jrose1c1bfac2010-11-22 22:41:31 -08001365 // In the following line, the first cast implies
1366 // normal Object return value for the MH call (Object[])->Object,
1367 // while the second cast dynamically converts to an Object array.
1368 // Such a double cast is typical of MH.invokeExact.
1369 returnValue = (Object[]) (Object) result.invokeExact(args);
jrose10f3b682010-01-07 16:16:45 -08001370 } else {
1371 Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
1372 args1[pos] = Arrays.copyOfRange(args, pos, args.length);
jrose900bafd2010-10-30 21:08:23 -07001373 returnValue = (Object[]) result.invokeWithArguments(args1);
jrose10f3b682010-01-07 16:16:45 -08001374 }
1375 assertArrayEquals(args, returnValue);
1376 }
1377
1378 @Test
1379 public void testCollectArguments() throws Throwable {
1380 if (CAN_SKIP_WORKING) return;
1381 startTest("collectArguments");
1382 for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
jrose2cc9c832010-04-30 23:48:23 -07001383 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001384 System.out.println("collectArguments "+argType);
1385 for (int nargs = 0; nargs < 10; nargs++) {
1386 for (int pos = 0; pos < nargs; pos++) {
1387 if (argType == int.class) continue; // FIXME Fail_4
1388 testCollectArguments(argType, pos, nargs);
1389 }
1390 }
1391 }
1392 }
1393 public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable {
1394 countTest();
1395 // fake up a MH with the same type as the desired adapter:
1396 MethodHandle fake = ValueConversions.varargsArray(nargs);
1397 fake = changeArgTypes(fake, argType);
1398 MethodType newType = fake.type();
1399 Object[] args = randomArgs(newType.parameterArray());
1400 // here is what should happen:
1401 Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
1402 collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
1403 // here is the MH which will witness the collected argument tail:
1404 MethodHandle target = ValueConversions.varargsArray(pos+1);
1405 target = changeArgTypes(target, 0, pos, argType);
1406 target = changeArgTypes(target, pos, pos+1, Object[].class);
jrose2cc9c832010-04-30 23:48:23 -07001407 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001408 System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
1409 MethodHandle result = MethodHandles.collectArguments(target, newType);
jrose900bafd2010-10-30 21:08:23 -07001410 Object[] returnValue = (Object[]) result.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001411// assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
1412// returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
1413// collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
1414 assertArrayEquals(collectedArgs, returnValue);
1415 }
1416
1417 @Test
jrose55220c32009-10-21 23:19:48 -07001418 public void testInsertArguments() throws Throwable {
1419 if (CAN_SKIP_WORKING) return;
1420 startTest("insertArguments");
1421 for (int nargs = 0; nargs <= 4; nargs++) {
1422 for (int ins = 0; ins <= 4; ins++) {
1423 if (ins > MAX_ARG_INCREASE) continue; // FIXME Fail_6
1424 for (int pos = 0; pos <= nargs; pos++) {
1425 testInsertArguments(nargs, pos, ins);
1426 }
1427 }
1428 }
1429 }
1430
1431 void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
jrose55220c32009-10-21 23:19:48 -07001432 countTest();
1433 MethodHandle target = ValueConversions.varargsArray(nargs + ins);
1434 Object[] args = randomArgs(target.type().parameterArray());
1435 List<Object> resList = Arrays.asList(args);
1436 List<Object> argsToPass = new ArrayList<Object>(resList);
1437 List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
jrose2cc9c832010-04-30 23:48:23 -07001438 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -07001439 System.out.println("insert: "+argsToInsert+" into "+target);
jrose10f3b682010-01-07 16:16:45 -08001440 MethodHandle target2 = MethodHandles.insertArguments(target, pos,
1441 (Object[]) argsToInsert.toArray());
jrose55220c32009-10-21 23:19:48 -07001442 argsToInsert.clear(); // remove from argsToInsert
jrose900bafd2010-10-30 21:08:23 -07001443 Object res2 = target2.invokeWithArguments(argsToPass);
jrose55220c32009-10-21 23:19:48 -07001444 Object res2List = Arrays.asList((Object[])res2);
jrose2cc9c832010-04-30 23:48:23 -07001445 if (verbosity >= 3)
jrose55220c32009-10-21 23:19:48 -07001446 System.out.println("result: "+res2List);
1447 //if (!resList.equals(res2List))
1448 // System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
1449 assertEquals(resList, res2List);
1450 }
1451
jrose10f3b682010-01-07 16:16:45 -08001452 @Test
1453 public void testFilterArguments() throws Throwable {
1454 if (CAN_SKIP_WORKING) return;
1455 startTest("filterArguments");
1456 for (int nargs = 1; nargs <= 6; nargs++) {
1457 for (int pos = 0; pos < nargs; pos++) {
1458 testFilterArguments(nargs, pos);
1459 }
1460 }
1461 }
1462
1463 void testFilterArguments(int nargs, int pos) throws Throwable {
1464 countTest();
1465 MethodHandle target = ValueConversions.varargsList(nargs);
1466 MethodHandle filter = ValueConversions.varargsList(1);
1467 filter = MethodHandles.convertArguments(filter, filter.type().generic());
1468 Object[] argsToPass = randomArgs(nargs, Object.class);
jrose2cc9c832010-04-30 23:48:23 -07001469 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001470 System.out.println("filter "+target+" at "+pos+" with "+filter);
jrose900bafd2010-10-30 21:08:23 -07001471 MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter);
jrose10f3b682010-01-07 16:16:45 -08001472 // Simulate expected effect of filter on arglist:
1473 Object[] filteredArgs = argsToPass.clone();
jrose2cc9c832010-04-30 23:48:23 -07001474 filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]);
jrose10f3b682010-01-07 16:16:45 -08001475 List<Object> expected = Arrays.asList(filteredArgs);
jrose900bafd2010-10-30 21:08:23 -07001476 Object result = target2.invokeWithArguments(argsToPass);
jrose2cc9c832010-04-30 23:48:23 -07001477 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001478 System.out.println("result: "+result);
1479 if (!expected.equals(result))
1480 System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+argsToPass+" => "+result);
1481 assertEquals(expected, result);
1482 }
1483
1484 @Test
1485 public void testFoldArguments() throws Throwable {
1486 if (CAN_SKIP_WORKING) return;
1487 startTest("foldArguments");
1488 for (int nargs = 0; nargs <= 4; nargs++) {
1489 for (int fold = 0; fold <= nargs; fold++) {
1490 for (int pos = 0; pos <= nargs; pos++) {
1491 testFoldArguments(nargs, pos, fold);
1492 }
1493 }
1494 }
1495 }
1496
1497 void testFoldArguments(int nargs, int pos, int fold) throws Throwable {
1498 if (pos != 0) return; // can fold only at pos=0 for now
1499 countTest();
1500 MethodHandle target = ValueConversions.varargsList(1 + nargs);
jrose25aef892010-10-30 21:02:30 -07001501 MethodHandle combine = ValueConversions.varargsList(fold).asType(MethodType.genericMethodType(fold));
jrose10f3b682010-01-07 16:16:45 -08001502 List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class));
jrose2cc9c832010-04-30 23:48:23 -07001503 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001504 System.out.println("fold "+target+" with "+combine);
1505 MethodHandle target2 = MethodHandles.foldArguments(target, combine);
1506 // Simulate expected effect of combiner on arglist:
1507 List<Object> expected = new ArrayList<Object>(argsToPass);
1508 List<Object> argsToFold = expected.subList(pos, pos + fold);
jrose2cc9c832010-04-30 23:48:23 -07001509 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001510 System.out.println("fold: "+argsToFold+" into "+target2);
jrose900bafd2010-10-30 21:08:23 -07001511 Object foldedArgs = combine.invokeWithArguments(argsToFold);
jrose10f3b682010-01-07 16:16:45 -08001512 argsToFold.add(0, foldedArgs);
jrose900bafd2010-10-30 21:08:23 -07001513 Object result = target2.invokeWithArguments(argsToPass);
jrose2cc9c832010-04-30 23:48:23 -07001514 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001515 System.out.println("result: "+result);
1516 if (!expected.equals(result))
1517 System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result);
1518 assertEquals(expected, result);
1519 }
1520
1521 @Test
1522 public void testDropArguments() throws Throwable {
1523 if (CAN_SKIP_WORKING) return;
1524 startTest("dropArguments");
1525 for (int nargs = 0; nargs <= 4; nargs++) {
1526 for (int drop = 1; drop <= 4; drop++) {
1527 for (int pos = 0; pos <= nargs; pos++) {
1528 testDropArguments(nargs, pos, drop);
1529 }
1530 }
1531 }
1532 }
1533
1534 void testDropArguments(int nargs, int pos, int drop) throws Throwable {
1535 countTest();
1536 MethodHandle target = ValueConversions.varargsArray(nargs);
1537 Object[] args = randomArgs(target.type().parameterArray());
1538 MethodHandle target2 = MethodHandles.dropArguments(target, pos,
1539 Collections.nCopies(drop, Object.class).toArray(new Class[0]));
1540 List<Object> resList = Arrays.asList(args);
1541 List<Object> argsToDrop = new ArrayList<Object>(resList);
1542 for (int i = drop; i > 0; i--) {
1543 argsToDrop.add(pos, "blort#"+i);
1544 }
jrose900bafd2010-10-30 21:08:23 -07001545 Object res2 = target2.invokeWithArguments(argsToDrop);
jrose10f3b682010-01-07 16:16:45 -08001546 Object res2List = Arrays.asList((Object[])res2);
1547 //if (!resList.equals(res2List))
1548 // System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
1549 assertEquals(resList, res2List);
1550 }
1551
1552 @Test
1553 public void testInvokers() throws Throwable {
1554 if (CAN_SKIP_WORKING) return;
1555 startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
1556 // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
1557 Set<MethodType> done = new HashSet<MethodType>();
1558 for (int i = 0; i <= 6; i++) {
1559 MethodType gtype = MethodType.genericMethodType(i);
1560 for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
1561 for (int j = -1; j < i; j++) {
1562 MethodType type = gtype;
1563 if (j < 0)
1564 type = type.changeReturnType(argType);
1565 else if (argType == void.class)
1566 continue;
1567 else
1568 type = type.changeParameterType(j, argType);
1569 if (argType.isPrimitive() && j != i-1) continue; // FIXME Fail_5
1570 if (done.add(type))
1571 testInvokers(type);
1572 MethodType vtype = type.changeReturnType(void.class);
1573 if (done.add(vtype))
1574 testInvokers(vtype);
1575 }
1576 }
1577 }
1578 }
1579
1580 public void testInvokers(MethodType type) throws Throwable {
jrose2cc9c832010-04-30 23:48:23 -07001581 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001582 System.out.println("test invokers for "+type);
1583 int nargs = type.parameterCount();
1584 boolean testRetCode = type.returnType() != void.class;
1585 MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
1586 MethodType.genericMethodType(0, true));
1587 target = MethodHandles.collectArguments(target, type);
1588 Object[] args = randomArgs(type.parameterArray());
1589 List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args));
1590 targetPlusArgs.add(0, target);
1591 int code = (Integer) invokee(args);
1592 Object log = logEntry("invokee", args);
1593 assertEquals(log.hashCode(), code);
1594 assertCalled("invokee", args);
1595 MethodHandle inv;
1596 Object result;
1597 // exact invoker
1598 countTest();
1599 calledLog.clear();
1600 inv = MethodHandles.exactInvoker(type);
jrose900bafd2010-10-30 21:08:23 -07001601 result = inv.invokeWithArguments(targetPlusArgs);
jrose10f3b682010-01-07 16:16:45 -08001602 if (testRetCode) assertEquals(code, result);
1603 assertCalled("invokee", args);
1604 // generic invoker
1605 countTest();
1606 inv = MethodHandles.genericInvoker(type);
1607 if (nargs <= 3) {
1608 calledLog.clear();
1609 switch (nargs) {
1610 case 0:
jrose2cc9c832010-04-30 23:48:23 -07001611 result = inv.invokeExact(target);
jrose10f3b682010-01-07 16:16:45 -08001612 break;
1613 case 1:
jrose2cc9c832010-04-30 23:48:23 -07001614 result = inv.invokeExact(target, args[0]);
jrose10f3b682010-01-07 16:16:45 -08001615 break;
1616 case 2:
jrose2cc9c832010-04-30 23:48:23 -07001617 result = inv.invokeExact(target, args[0], args[1]);
jrose10f3b682010-01-07 16:16:45 -08001618 break;
1619 case 3:
jrose2cc9c832010-04-30 23:48:23 -07001620 result = inv.invokeExact(target, args[0], args[1], args[2]);
jrose10f3b682010-01-07 16:16:45 -08001621 break;
1622 }
1623 if (testRetCode) assertEquals(code, result);
1624 assertCalled("invokee", args);
1625 }
1626 calledLog.clear();
jrose900bafd2010-10-30 21:08:23 -07001627 result = inv.invokeWithArguments(targetPlusArgs);
jrose10f3b682010-01-07 16:16:45 -08001628 if (testRetCode) assertEquals(code, result);
1629 assertCalled("invokee", args);
1630 // varargs invoker #0
1631 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001632 inv = MethodHandles.spreadInvoker(type, 0);
jrose2cc9c832010-04-30 23:48:23 -07001633 result = inv.invokeExact(target, args);
jrose10f3b682010-01-07 16:16:45 -08001634 if (testRetCode) assertEquals(code, result);
1635 assertCalled("invokee", args);
1636 if (nargs >= 1) {
1637 // varargs invoker #1
1638 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001639 inv = MethodHandles.spreadInvoker(type, 1);
jrose2cc9c832010-04-30 23:48:23 -07001640 result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs));
jrose10f3b682010-01-07 16:16:45 -08001641 if (testRetCode) assertEquals(code, result);
1642 assertCalled("invokee", args);
1643 }
1644 if (nargs >= 2) {
1645 // varargs invoker #2
1646 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001647 inv = MethodHandles.spreadInvoker(type, 2);
jrose2cc9c832010-04-30 23:48:23 -07001648 result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
jrose10f3b682010-01-07 16:16:45 -08001649 if (testRetCode) assertEquals(code, result);
1650 assertCalled("invokee", args);
1651 }
1652 if (nargs >= 3) {
1653 // varargs invoker #3
1654 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001655 inv = MethodHandles.spreadInvoker(type, 3);
jrose2cc9c832010-04-30 23:48:23 -07001656 result = inv.invokeExact(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
jrose10f3b682010-01-07 16:16:45 -08001657 if (testRetCode) assertEquals(code, result);
1658 assertCalled("invokee", args);
1659 }
1660 for (int k = 0; k <= nargs; k++) {
1661 // varargs invoker #0..N
1662 countTest();
1663 calledLog.clear();
jrosef108fc02011-02-11 01:26:24 -08001664 inv = MethodHandles.spreadInvoker(type, k);
jrose10f3b682010-01-07 16:16:45 -08001665 List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs);
1666 List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
1667 Object[] tail = tailList.toArray();
1668 tailList.clear(); tailList.add(tail);
jrose900bafd2010-10-30 21:08:23 -07001669 result = inv.invokeWithArguments(targetPlusVarArgs);
jrose10f3b682010-01-07 16:16:45 -08001670 if (testRetCode) assertEquals(code, result);
1671 assertCalled("invokee", args);
1672 }
jrose900bafd2010-10-30 21:08:23 -07001673
jrose10f3b682010-01-07 16:16:45 -08001674 // dynamic invoker
1675 countTest();
jroseb90d2e72010-12-16 15:59:27 -08001676 CallSite site = new MutableCallSite(type);
1677 inv = site.dynamicInvoker();
jrose900bafd2010-10-30 21:08:23 -07001678
1679 // see if we get the result of the original target:
1680 try {
1681 result = inv.invokeWithArguments(args);
1682 assertTrue("should not reach here", false);
1683 } catch (IllegalStateException ex) {
1684 String msg = ex.getMessage();
1685 assertTrue(msg, msg.contains("site"));
1686 }
1687
1688 // set new target after invoker is created, to make sure we track target
jrose10f3b682010-01-07 16:16:45 -08001689 site.setTarget(target);
1690 calledLog.clear();
jrose900bafd2010-10-30 21:08:23 -07001691 result = inv.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001692 if (testRetCode) assertEquals(code, result);
1693 assertCalled("invokee", args);
1694 }
1695
1696 static Object invokee(Object... args) {
1697 return called("invokee", args).hashCode();
1698 }
1699
jrose55220c32009-10-21 23:19:48 -07001700 private static final String MISSING_ARG = "missingArg";
1701 static Object targetIfEquals() {
1702 return called("targetIfEquals");
1703 }
1704 static Object fallbackIfNotEquals() {
1705 return called("fallbackIfNotEquals");
1706 }
1707 static Object targetIfEquals(Object x) {
1708 assertEquals(x, MISSING_ARG);
1709 return called("targetIfEquals", x);
1710 }
1711 static Object fallbackIfNotEquals(Object x) {
1712 assertFalse(x.toString(), x.equals(MISSING_ARG));
1713 return called("fallbackIfNotEquals", x);
1714 }
1715 static Object targetIfEquals(Object x, Object y) {
1716 assertEquals(x, y);
1717 return called("targetIfEquals", x, y);
1718 }
1719 static Object fallbackIfNotEquals(Object x, Object y) {
1720 assertFalse(x.toString(), x.equals(y));
1721 return called("fallbackIfNotEquals", x, y);
1722 }
1723 static Object targetIfEquals(Object x, Object y, Object z) {
1724 assertEquals(x, y);
1725 return called("targetIfEquals", x, y, z);
1726 }
1727 static Object fallbackIfNotEquals(Object x, Object y, Object z) {
1728 assertFalse(x.toString(), x.equals(y));
1729 return called("fallbackIfNotEquals", x, y, z);
1730 }
1731
jrose10f3b682010-01-07 16:16:45 -08001732 @Test
1733 public void testGuardWithTest() throws Throwable {
1734 if (CAN_SKIP_WORKING) return;
1735 startTest("guardWithTest");
1736 for (int nargs = 0; nargs <= 3; nargs++) {
1737 if (nargs != 2) continue; // FIXME: test more later
1738 testGuardWithTest(nargs, Object.class);
1739 testGuardWithTest(nargs, String.class);
1740 }
1741 }
1742 void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
1743 countTest();
1744 MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
1745 MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
1746 MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
1747 while (test.type().parameterCount() < nargs)
1748 test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
1749 while (test.type().parameterCount() > nargs)
1750 test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
1751 if (argClass != Object.class) {
1752 test = changeArgTypes(test, argClass);
1753 target = changeArgTypes(target, argClass);
1754 fallback = changeArgTypes(fallback, argClass);
1755 }
1756 MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
1757 assertEquals(target.type(), mh.type());
1758 Object[][] argLists = {
1759 { },
1760 { "foo" }, { MISSING_ARG },
1761 { "foo", "foo" }, { "foo", "bar" },
1762 { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
1763 };
1764 for (Object[] argList : argLists) {
1765 if (argList.length != nargs) continue;
1766 boolean equals;
1767 switch (nargs) {
1768 case 0: equals = true; break;
1769 case 1: equals = MISSING_ARG.equals(argList[0]); break;
1770 default: equals = argList[0].equals(argList[1]); break;
1771 }
1772 String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
jrose2cc9c832010-04-30 23:48:23 -07001773 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001774 System.out.println(logEntry(willCall, argList));
jrose900bafd2010-10-30 21:08:23 -07001775 Object result = mh.invokeWithArguments(argList);
jrose10f3b682010-01-07 16:16:45 -08001776 assertCalled(willCall, argList);
1777 }
1778 }
1779
1780 @Test
1781 public void testCatchException() throws Throwable {
1782 if (CAN_SKIP_WORKING) return;
1783 startTest("catchException");
1784 for (int nargs = 2; nargs <= 6; nargs++) {
1785 for (int ti = 0; ti <= 1; ti++) {
1786 boolean throwIt = (ti != 0);
1787 testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs);
1788 testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs);
1789 testCatchException(String.class, new LinkageError("testing"), throwIt, nargs);
1790 }
1791 }
1792 }
1793
1794 private static <T extends Throwable>
1795 Object throwOrReturn(Object normal, T exception) throws T {
1796 if (exception != null) throw exception;
1797 return normal;
1798 }
1799
1800 void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
1801 countTest();
jrose2cc9c832010-04-30 23:48:23 -07001802 if (verbosity >= 3)
jrose10f3b682010-01-07 16:16:45 -08001803 System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs);
1804 Class<? extends Throwable> exType = thrown.getClass();
1805 MethodHandle throwOrReturn
1806 = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn",
1807 MethodType.methodType(Object.class, Object.class, Throwable.class));
jroseb90d2e72010-12-16 15:59:27 -08001808 MethodHandle thrower = throwOrReturn.asType(MethodType.genericMethodType(2));
jrose10f3b682010-01-07 16:16:45 -08001809 while (thrower.type().parameterCount() < nargs)
1810 thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class);
jroseb90d2e72010-12-16 15:59:27 -08001811 MethodHandle catcher = ValueConversions.varargsList(1+nargs).asType(MethodType.genericMethodType(1+nargs));
jrose10f3b682010-01-07 16:16:45 -08001812 MethodHandle target = MethodHandles.catchException(thrower,
jroseb90d2e72010-12-16 15:59:27 -08001813 thrown.getClass(), catcher);
jrose10f3b682010-01-07 16:16:45 -08001814 assertEquals(thrower.type(), target.type());
1815 //System.out.println("catching with "+target+" : "+throwOrReturn);
1816 Object[] args = randomArgs(nargs, Object.class);
1817 args[1] = (throwIt ? thrown : null);
jrose900bafd2010-10-30 21:08:23 -07001818 Object returned = target.invokeWithArguments(args);
jrose10f3b682010-01-07 16:16:45 -08001819 //System.out.println("return from "+target+" : "+returned);
1820 if (!throwIt) {
1821 assertSame(args[0], returned);
1822 } else {
1823 List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args));
1824 catchArgs.add(0, thrown);
1825 assertEquals(catchArgs, returned);
1826 }
1827 }
1828
1829 @Test
1830 public void testThrowException() throws Throwable {
1831 if (CAN_SKIP_WORKING) return;
1832 startTest("throwException");
1833 testThrowException(int.class, new ClassCastException("testing"));
1834 testThrowException(void.class, new java.io.IOException("testing"));
1835 testThrowException(String.class, new LinkageError("testing"));
1836 }
1837
1838 void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
1839 countTest();
1840 Class<? extends Throwable> exType = thrown.getClass();
1841 MethodHandle target = MethodHandles.throwException(returnType, exType);
1842 //System.out.println("throwing with "+target+" : "+thrown);
1843 MethodType expectedType = MethodType.methodType(returnType, exType);
1844 assertEquals(expectedType, target.type());
jrose2cc9c832010-04-30 23:48:23 -07001845 target = MethodHandles.convertArguments(target, target.type().generic());
jrose10f3b682010-01-07 16:16:45 -08001846 Throwable caught = null;
1847 try {
jrose2cc9c832010-04-30 23:48:23 -07001848 Object res = target.invokeExact((Object) thrown);
jrose10f3b682010-01-07 16:16:45 -08001849 fail("got "+res+" instead of throwing "+thrown);
1850 } catch (Throwable ex) {
1851 if (ex != thrown) {
1852 if (ex instanceof Error) throw (Error)ex;
1853 if (ex instanceof RuntimeException) throw (RuntimeException)ex;
1854 }
1855 caught = ex;
1856 }
1857 assertSame(thrown, caught);
1858 }
1859
1860 @Test
1861 public void testCastFailure() throws Throwable {
1862 if (CAN_SKIP_WORKING) return;
1863 startTest("testCastFailure");
1864 testCastFailure("cast/argument", 11000);
1865 testCastFailure("unbox/argument", 11000);
1866 testCastFailure("cast/return", 11000);
1867 testCastFailure("unbox/return", 11000);
1868 }
1869
jrose900bafd2010-10-30 21:08:23 -07001870 static class Surprise {
jrosea1ebbe62010-09-08 18:40:23 -07001871 public MethodHandle asMethodHandle() {
1872 return VALUE.bindTo(this);
1873 }
jrose10f3b682010-01-07 16:16:45 -08001874 Object value(Object x) {
1875 trace("value", x);
1876 if (boo != null) return boo;
1877 return x;
1878 }
1879 Object boo;
1880 void boo(Object x) { boo = x; }
1881
1882 static void trace(String x, Object y) {
1883 if (verbosity > 8) System.out.println(x+"="+y);
1884 }
1885 static Object refIdentity(Object x) { trace("ref.x", x); return x; }
1886 static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
1887 static int intIdentity(int x) { trace("int.x", x); return x; }
jrosea1ebbe62010-09-08 18:40:23 -07001888 static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY;
1889 static {
1890 try {
1891 VALUE = PRIVATE.findVirtual(
1892 Surprise.class, "value",
1893 MethodType.methodType(Object.class, Object.class));
1894 REF_IDENTITY = PRIVATE.findStatic(
1895 Surprise.class, "refIdentity",
1896 MethodType.methodType(Object.class, Object.class));
1897 BOX_IDENTITY = PRIVATE.findStatic(
1898 Surprise.class, "boxIdentity",
1899 MethodType.methodType(Integer.class, Integer.class));
1900 INT_IDENTITY = PRIVATE.findStatic(
1901 Surprise.class, "intIdentity",
1902 MethodType.methodType(int.class, int.class));
1903 } catch (Exception ex) {
1904 throw new RuntimeException(ex);
1905 }
1906 }
jrose10f3b682010-01-07 16:16:45 -08001907 }
1908
1909 void testCastFailure(String mode, int okCount) throws Throwable {
1910 countTest(false);
jrose2cc9c832010-04-30 23:48:23 -07001911 if (verbosity > 2) System.out.println("mode="+mode);
jrose10f3b682010-01-07 16:16:45 -08001912 Surprise boo = new Surprise();
jrosea1ebbe62010-09-08 18:40:23 -07001913 MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0;
jrose10f3b682010-01-07 16:16:45 -08001914 if (mode.endsWith("/return")) {
1915 if (mode.equals("unbox/return")) {
1916 // fail on return to ((Integer)surprise).intValue
1917 surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(int.class, Object.class));
1918 identity = MethodHandles.convertArguments(identity, MethodType.methodType(int.class, Object.class));
1919 } else if (mode.equals("cast/return")) {
1920 // fail on return to (Integer)surprise
1921 surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(Integer.class, Object.class));
1922 identity = MethodHandles.convertArguments(identity, MethodType.methodType(Integer.class, Object.class));
1923 }
1924 } else if (mode.endsWith("/argument")) {
1925 MethodHandle callee = null;
1926 if (mode.equals("unbox/argument")) {
1927 // fail on handing surprise to int argument
1928 callee = Surprise.INT_IDENTITY;
1929 } else if (mode.equals("cast/argument")) {
1930 // fail on handing surprise to Integer argument
1931 callee = Surprise.BOX_IDENTITY;
1932 }
1933 if (callee != null) {
1934 callee = MethodHandles.convertArguments(callee, MethodType.genericMethodType(1));
jrose900bafd2010-10-30 21:08:23 -07001935 surprise = MethodHandles.filterArguments(callee, 0, surprise);
1936 identity = MethodHandles.filterArguments(callee, 0, identity);
jrose10f3b682010-01-07 16:16:45 -08001937 }
1938 }
jrosea1ebbe62010-09-08 18:40:23 -07001939 assertNotSame(mode, surprise, surprise0);
jrose10f3b682010-01-07 16:16:45 -08001940 identity = MethodHandles.convertArguments(identity, MethodType.genericMethodType(1));
1941 surprise = MethodHandles.convertArguments(surprise, MethodType.genericMethodType(1));
1942 Object x = 42;
1943 for (int i = 0; i < okCount; i++) {
jrose2cc9c832010-04-30 23:48:23 -07001944 Object y = identity.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08001945 assertEquals(x, y);
jrose2cc9c832010-04-30 23:48:23 -07001946 Object z = surprise.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08001947 assertEquals(x, z);
1948 }
1949 boo.boo("Boo!");
jrose2cc9c832010-04-30 23:48:23 -07001950 Object y = identity.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08001951 assertEquals(x, y);
1952 try {
jrose2cc9c832010-04-30 23:48:23 -07001953 Object z = surprise.invokeExact(x);
jrose10f3b682010-01-07 16:16:45 -08001954 System.out.println("Failed to throw; got z="+z);
1955 assertTrue(false);
1956 } catch (Exception ex) {
jrose10f3b682010-01-07 16:16:45 -08001957 if (verbosity > 2)
jrose2cc9c832010-04-30 23:48:23 -07001958 System.out.println("caught "+ex);
1959 if (verbosity > 3)
jrose10f3b682010-01-07 16:16:45 -08001960 ex.printStackTrace();
1961 assertTrue(ex instanceof ClassCastException
1962 // FIXME: accept only one of the two for any given unit test
1963 || ex instanceof WrongMethodTypeException
1964 );
1965 }
1966 }
1967
jrose45b7a332010-05-03 23:32:47 -07001968 static Example userMethod(Object o, String s, int i) {
1969 called("userMethod", o, s, i);
1970 return null;
1971 }
1972
1973 @Test
1974 public void testUserClassInSignature() throws Throwable {
1975 if (CAN_SKIP_WORKING) return;
1976 startTest("testUserClassInSignature");
1977 Lookup lookup = MethodHandles.lookup();
1978 String name; MethodType mt; MethodHandle mh;
1979 Object[] args;
1980
1981 // Try a static method.
1982 name = "userMethod";
1983 mt = MethodType.methodType(Example.class, Object.class, String.class, int.class);
1984 mh = lookup.findStatic(lookup.lookupClass(), name, mt);
1985 assertEquals(mt, mh.type());
1986 assertEquals(Example.class, mh.type().returnType());
1987 args = randomArgs(mh.type().parameterArray());
jrose900bafd2010-10-30 21:08:23 -07001988 mh.invokeWithArguments(args);
jrose45b7a332010-05-03 23:32:47 -07001989 assertCalled(name, args);
1990
1991 // Try a virtual method.
1992 name = "v2";
1993 mt = MethodType.methodType(Object.class, Object.class, int.class);
1994 mh = lookup.findVirtual(Example.class, name, mt);
1995 assertEquals(mt, mh.type().dropParameterTypes(0,1));
1996 assertTrue(mh.type().parameterList().contains(Example.class));
1997 args = randomArgs(mh.type().parameterArray());
jrose900bafd2010-10-30 21:08:23 -07001998 mh.invokeWithArguments(args);
jrose45b7a332010-05-03 23:32:47 -07001999 assertCalled(name, args);
2000 }
jrosebe2db602010-09-08 18:40:34 -07002001
2002 static void runForRunnable() {
2003 called("runForRunnable");
2004 }
2005 private interface Fooable {
2006 Object foo(Fooable x, Object y);
2007 // this is for randomArg:
2008 public class Impl implements Fooable {
2009 public Object foo(Fooable x, Object y) {
2010 throw new RuntimeException("do not call");
2011 }
2012 final String name;
2013 public Impl() { name = "Fooable#"+nextArg(); }
2014 @Override public String toString() { return name; }
2015 }
2016 }
2017 static Object fooForFooable(Fooable x, Object y) {
2018 return called("fooForFooable", x, y);
2019 }
2020 private static class MyCheckedException extends Exception {
2021 }
2022 private interface WillThrow {
2023 void willThrow() throws MyCheckedException;
2024 }
2025
2026 @Test
2027 public void testAsInstance() throws Throwable {
2028 if (CAN_SKIP_WORKING) return;
2029 Lookup lookup = MethodHandles.lookup();
2030 {
2031 MethodType mt = MethodType.methodType(void.class);
2032 MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "runForRunnable", mt);
2033 Runnable proxy = MethodHandles.asInstance(mh, Runnable.class);
2034 proxy.run();
2035 assertCalled("runForRunnable");
2036 }
2037 {
2038 MethodType mt = MethodType.methodType(Object.class, Fooable.class, Object.class);
2039 MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "fooForFooable", mt);
2040 Fooable proxy = MethodHandles.asInstance(mh, Fooable.class);
2041 Object[] args = randomArgs(mt.parameterArray());
2042 Object result = proxy.foo((Fooable) args[0], args[1]);
2043 assertCalled("fooForFooable", args);
2044 assertEquals(result, logEntry("fooForFooable", args));
2045 }
2046 for (Throwable ex : new Throwable[] { new NullPointerException("ok"),
2047 new InternalError("ok"),
2048 new Throwable("fail"),
2049 new Exception("fail"),
2050 new MyCheckedException()
2051 }) {
2052 MethodHandle mh = MethodHandles.throwException(void.class, Throwable.class);
2053 mh = MethodHandles.insertArguments(mh, 0, ex);
2054 WillThrow proxy = MethodHandles.asInstance(mh, WillThrow.class);
2055 try {
2056 proxy.willThrow();
2057 System.out.println("Failed to throw: "+ex);
2058 assertTrue(false);
2059 } catch (Throwable ex1) {
2060 if (verbosity > 2) {
2061 System.out.println("throw "+ex);
2062 System.out.println("catch "+(ex == ex1 ? "UNWRAPPED" : ex1));
2063 }
2064 if (ex instanceof RuntimeException ||
2065 ex instanceof Error) {
2066 assertSame("must pass unchecked exception out without wrapping", ex, ex1);
2067 } else if (ex instanceof MyCheckedException) {
2068 assertSame("must pass declared exception out without wrapping", ex, ex1);
2069 } else {
2070 assertNotSame("must pass undeclared checked exception with wrapping", ex, ex1);
2071 UndeclaredThrowableException utex = (UndeclaredThrowableException) ex1;
2072 assertSame(ex, utex.getCause());
2073 }
2074 }
2075 }
2076 // Test error checking:
jrosebe2db602010-09-08 18:40:34 -07002077 for (Class<?> nonSAM : new Class[] { Object.class,
2078 String.class,
2079 CharSequence.class,
2080 Example.class }) {
2081 try {
2082 MethodHandles.asInstance(ValueConversions.varargsArray(0), nonSAM);
2083 System.out.println("Failed to throw");
2084 assertTrue(false);
2085 } catch (IllegalArgumentException ex) {
2086 }
2087 }
2088 }
jrose55220c32009-10-21 23:19:48 -07002089}
2090// Local abbreviated copy of sun.dyn.util.ValueConversions
2091class ValueConversions {
2092 private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
2093 private static final Object[] NO_ARGS_ARRAY = {};
2094 private static Object[] makeArray(Object... args) { return args; }
2095 private static Object[] array() { return NO_ARGS_ARRAY; }
2096 private static Object[] array(Object a0)
2097 { return makeArray(a0); }
2098 private static Object[] array(Object a0, Object a1)
2099 { return makeArray(a0, a1); }
2100 private static Object[] array(Object a0, Object a1, Object a2)
2101 { return makeArray(a0, a1, a2); }
2102 private static Object[] array(Object a0, Object a1, Object a2, Object a3)
2103 { return makeArray(a0, a1, a2, a3); }
2104 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2105 Object a4)
2106 { return makeArray(a0, a1, a2, a3, a4); }
2107 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2108 Object a4, Object a5)
2109 { return makeArray(a0, a1, a2, a3, a4, a5); }
2110 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2111 Object a4, Object a5, Object a6)
2112 { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
2113 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2114 Object a4, Object a5, Object a6, Object a7)
2115 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
2116 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2117 Object a4, Object a5, Object a6, Object a7,
2118 Object a8)
2119 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
2120 private static Object[] array(Object a0, Object a1, Object a2, Object a3,
2121 Object a4, Object a5, Object a6, Object a7,
2122 Object a8, Object a9)
2123 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
2124 static MethodHandle[] makeArrays() {
2125 ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
2126 MethodHandles.Lookup lookup = IMPL_LOOKUP;
2127 for (;;) {
2128 int nargs = arrays.size();
jrose10f3b682010-01-07 16:16:45 -08002129 MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
jrose55220c32009-10-21 23:19:48 -07002130 String name = "array";
2131 MethodHandle array = null;
2132 try {
2133 array = lookup.findStatic(ValueConversions.class, name, type);
2134 } catch (NoAccessException ex) {
2135 }
2136 if (array == null) break;
2137 arrays.add(array);
2138 }
2139 assert(arrays.size() == 11); // current number of methods
2140 return arrays.toArray(new MethodHandle[0]);
2141 }
2142 static final MethodHandle[] ARRAYS = makeArrays();
2143
2144 /** Return a method handle that takes the indicated number of Object
2145 * arguments and returns an Object array of them, as if for varargs.
2146 */
2147 public static MethodHandle varargsArray(int nargs) {
2148 if (nargs < ARRAYS.length)
2149 return ARRAYS[nargs];
2150 // else need to spin bytecode or do something else fancy
jrose2cc9c832010-04-30 23:48:23 -07002151 throw new UnsupportedOperationException("NYI: cannot form a varargs array of length "+nargs);
jrose55220c32009-10-21 23:19:48 -07002152 }
jrose10f3b682010-01-07 16:16:45 -08002153
2154 private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
2155 private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
2156 private static List<Object> list() { return NO_ARGS_LIST; }
2157 private static List<Object> list(Object a0)
2158 { return makeList(a0); }
2159 private static List<Object> list(Object a0, Object a1)
2160 { return makeList(a0, a1); }
2161 private static List<Object> list(Object a0, Object a1, Object a2)
2162 { return makeList(a0, a1, a2); }
2163 private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
2164 { return makeList(a0, a1, a2, a3); }
2165 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2166 Object a4)
2167 { return makeList(a0, a1, a2, a3, a4); }
2168 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2169 Object a4, Object a5)
2170 { return makeList(a0, a1, a2, a3, a4, a5); }
2171 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2172 Object a4, Object a5, Object a6)
2173 { return makeList(a0, a1, a2, a3, a4, a5, a6); }
2174 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2175 Object a4, Object a5, Object a6, Object a7)
2176 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
2177 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2178 Object a4, Object a5, Object a6, Object a7,
2179 Object a8)
2180 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
2181 private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
2182 Object a4, Object a5, Object a6, Object a7,
2183 Object a8, Object a9)
2184 { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
2185 static MethodHandle[] makeLists() {
2186 ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
2187 MethodHandles.Lookup lookup = IMPL_LOOKUP;
2188 for (;;) {
2189 int nargs = arrays.size();
2190 MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
2191 String name = "list";
2192 MethodHandle array = null;
2193 try {
2194 array = lookup.findStatic(ValueConversions.class, name, type);
2195 } catch (NoAccessException ex) {
2196 }
2197 if (array == null) break;
2198 arrays.add(array);
2199 }
2200 assert(arrays.size() == 11); // current number of methods
2201 return arrays.toArray(new MethodHandle[0]);
2202 }
2203 static final MethodHandle[] LISTS = makeLists();
2204
2205 /** Return a method handle that takes the indicated number of Object
2206 * arguments and returns List.
2207 */
2208 public static MethodHandle varargsList(int nargs) {
2209 if (nargs < LISTS.length)
2210 return LISTS[nargs];
2211 // else need to spin bytecode or do something else fancy
2212 throw new UnsupportedOperationException("NYI");
2213 }
jrose55220c32009-10-21 23:19:48 -07002214}
2215// This guy tests access from outside the same package member, but inside
2216// the package itself.
2217class PackageSibling {
2218 static Lookup lookup() {
2219 return MethodHandles.lookup();
2220 }
2221}