jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1 | /* |
jrose | 4151b9c | 2010-06-18 15:23:57 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. Oracle designates this |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 10 | * |
| 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 21 | * 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. |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | /* @test |
| 27 | * @summary unit tests for java.dyn.MethodHandles |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 28 | * @compile -source 7 -target 7 -XDallowTransitionalJSR292=no MethodHandlesTest.java |
| 29 | * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles test.java.dyn.MethodHandlesTest |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 32 | package test.java.dyn; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 33 | |
| 34 | import java.dyn.*; |
| 35 | import java.dyn.MethodHandles.Lookup; |
| 36 | import java.lang.reflect.*; |
| 37 | import java.util.*; |
| 38 | import org.junit.*; |
| 39 | import static org.junit.Assert.*; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 40 | import static org.junit.Assume.*; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * @author jrose |
| 46 | */ |
| 47 | public class MethodHandlesTest { |
| 48 | // How much output? |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 49 | static int verbosity = 0; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 50 | static { |
| 51 | String vstr = System.getProperty("test.java.dyn.MethodHandlesTest.verbosity"); |
| 52 | if (vstr != null) verbosity = Integer.parseInt(vstr); |
| 53 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 54 | |
| 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. |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 63 | static boolean DO_MORE_CALLS = true; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 64 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 65 | @Test |
| 66 | public void testFirst() throws Throwable { |
| 67 | verbosity += 9; try { |
| 68 | // left blank for debugging |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 69 | } finally { printCounts(); verbosity -= 9; } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 70 | } |
| 71 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 72 | // current failures |
| 73 | @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric") |
| 74 | public void testFail_1() throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 75 | // AMH.<init>: IllegalArgumentException: bad adapter (conversion=0xfffab300): adapter pushes too many parameters |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 76 | testSpreadArguments(int.class, 0, 6); |
| 77 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 78 | @Test @Ignore("failure in JVM when expanding the stack using asm stub for _adapter_spread_args") |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 79 | 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 { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 85 | // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 86 | testSpreadArguments(int.class, 1, 2); |
| 87 | } |
| 88 | @Test @Ignore("IllArgEx failure in call to ToGeneric.make") |
| 89 | public void testFail_4() throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 90 | // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 91 | testCollectArguments(int.class, 1, 2); |
| 92 | } |
| 93 | @Test @Ignore("cannot collect leading primitive types") |
| 94 | public void testFail_5() throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 95 | // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 96 | 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 { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 100 | // ValueConversions.varargsArray: UnsupportedOperationException: NYI: cannot form a varargs array of length 13 |
| 101 | testInsertArguments(0, 0, MAX_ARG_INCREASE+10); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 102 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 103 | static final int MAX_ARG_INCREASE = 3; |
| 104 | |
| 105 | public MethodHandlesTest() { |
| 106 | } |
| 107 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 108 | @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"); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 115 | if ((arch.equals("amd64") || arch.equals("i386") || arch.equals("x86") || |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 116 | 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 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 126 | String testName; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 127 | static int allPosTests, allNegTests; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 128 | int posTests, negTests; |
| 129 | @After |
| 130 | public void printCounts() { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 131 | if (verbosity >= 2 && (posTests | negTests) != 0) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 132 | 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"); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 135 | allPosTests += posTests; |
| 136 | allNegTests += negTests; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 137 | posTests = negTests = 0; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 138 | } |
| 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(); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 147 | if (verbosity >= 1) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 148 | 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); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 157 | nextArgVal = INITIAL_ARG_VAL; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @AfterClass |
| 161 | public static void tearDownClass() throws Exception { |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 162 | 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 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 168 | } |
| 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); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 182 | if (expected.equals(actual) && verbosity < 9) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 183 | 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)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 188 | assertEquals("previous method call", expected, actual); |
| 189 | } |
| 190 | static void printCalled(MethodHandle target, String name, Object... args) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 191 | if (verbosity >= 3) |
| 192 | System.out.println("calling MH="+target+" to "+name+Arrays.toString(args)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 193 | } |
| 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); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 218 | 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; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 222 | return null; |
| 223 | } |
| 224 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 225 | 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 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 256 | static Object randomArg(Class<?> param) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 257 | Object wrap = castToWrapperOrNull(nextArg(param), param); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 258 | if (wrap != null) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 259 | 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++); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 267 | if (param.isInterface()) { |
| 268 | for (Class<?> c : param.getClasses()) { |
| 269 | if (param.isAssignableFrom(c) && !c.isInterface()) |
| 270 | { param = c; break; } |
| 271 | } |
| 272 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 273 | if (param.isInterface() || param.isAssignableFrom(String.class)) |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 274 | return "#"+nextArg(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 275 | 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); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 335 | MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 336 | 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. |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 344 | static final Lookup PUBLIC = MethodHandles.publicLookup(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 345 | |
| 346 | // Subject methods... |
| 347 | static class Example implements IntExample { |
| 348 | final String name; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 349 | public Example() { name = "Example#"+nextArg(); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 350 | 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); } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 373 | |
| 374 | static final Lookup EXAMPLE = MethodHandles.lookup(); // for testing findSpecial |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 375 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 376 | static final Lookup EXAMPLE = Example.EXAMPLE; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 377 | public static class PubExample extends Example { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 378 | public PubExample() { super("PubExample#"+nextArg()); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 379 | } |
| 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); } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 384 | public SubExample() { super("SubExample#"+nextArg()); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 385 | } |
| 386 | public static interface IntExample { |
| 387 | public void v0(); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 388 | public static class Impl implements IntExample { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 389 | public void v0() { called("Int/v0", this); } |
| 390 | final String name; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 391 | public Impl() { name = "Impl#"+nextArg(); } |
| 392 | @Override public String toString() { return name; } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | static final Object[][][] ACCESS_CASES = { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 397 | { { 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 |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 403 | 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 |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 409 | } else { |
| 410 | assertTrue(name.indexOf('_') < 0); |
| 411 | boolean pubc = Modifier.isPublic(defc.getModifiers()); |
| 412 | if (pubc) |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 413 | cases = ACCESS_CASES[3]; // all access levels |
| 414 | else |
| 415 | cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 416 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 417 | 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); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 423 | } |
| 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); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 455 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 456 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 457 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 458 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 459 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 460 | target = lookup.in(defc).findStatic(defc, name, type); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 461 | } catch (NoAccessException ex) { |
| 462 | noAccess = ex; |
| 463 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 464 | if (verbosity >= 3) |
| 465 | System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 466 | +(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()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 471 | assertNameStringContains(target, name); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 472 | if (!DO_MORE_CALLS && lookup != PRIVATE) return; |
| 473 | Object[] args = randomArgs(params); |
| 474 | printCalled(target, name, args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 475 | target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 476 | assertCalled(name, args); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 477 | if (verbosity >= 1) |
| 478 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 479 | } |
| 480 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 481 | // 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 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 487 | @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 |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 525 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 526 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 527 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 528 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 529 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 530 | target = lookup.in(defc).findVirtual(defc, methodName, type); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 531 | } catch (NoAccessException ex) { |
| 532 | noAccess = ex; |
| 533 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 534 | if (verbosity >= 3) |
| 535 | System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 536 | +(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); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 541 | MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 542 | assertEquals(typeWithSelf, target.type()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 543 | assertNameStringContains(target, methodName); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 544 | 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); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 548 | target.invokeWithArguments(argsWithSelf); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 549 | assertCalled(name, argsWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 550 | if (verbosity >= 1) |
| 551 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | @Test |
| 555 | public void testFindSpecial() throws Throwable { |
| 556 | if (CAN_SKIP_WORKING) return; |
| 557 | startTest("findSpecial"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 558 | 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 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 567 | } |
| 568 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 569 | 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); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 575 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 576 | void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller, |
| 577 | Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 578 | countTest(positive); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 579 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 580 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 581 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 582 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 583 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 584 | if (verbosity >= 5) System.out.println(" lookup => "+lookup.in(specialCaller)); |
| 585 | target = lookup.in(specialCaller).findSpecial(defc, name, type, specialCaller); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 586 | } catch (NoAccessException ex) { |
| 587 | noAccess = ex; |
| 588 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 589 | 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)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 593 | 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 |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 596 | assertEquals(specialCaller, target.type().parameterType(0)); |
| 597 | assertEquals(type, target.type().dropParameterTypes(0,1)); |
| 598 | Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 599 | MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 600 | assertNameStringContains(target, name); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 601 | if (!DO_MORE_CALLS && lookup != PRIVATE && lookup != EXAMPLE) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 602 | Object[] args = randomArgs(paramsWithSelf); |
| 603 | printCalled(target, name, args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 604 | target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 605 | assertCalled(name, args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 606 | } |
| 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 |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 635 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 636 | Object receiver = randomArg(defc); |
| 637 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 638 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 639 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 640 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 641 | target = lookup.in(defc).bind(receiver, methodName, type); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 642 | } catch (NoAccessException ex) { |
| 643 | noAccess = ex; |
| 644 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 645 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 646 | 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); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 654 | target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 655 | Object[] argsWithReceiver = cat(array(Object[].class, receiver), args); |
| 656 | assertCalled(name, argsWithReceiver); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 657 | if (verbosity >= 1) |
| 658 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 659 | } |
| 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); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 671 | 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); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 675 | |
| 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)) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 688 | testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, (isStatic ? null : defc), ret, name, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 691 | 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 { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 699 | countTest(positive); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 700 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 701 | Method rmethod = null; |
| 702 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 703 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 704 | try { |
| 705 | rmethod = defc.getDeclaredMethod(name, params); |
| 706 | } catch (NoSuchMethodException ex) { |
| 707 | throw new NoAccessException(ex); |
| 708 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 709 | boolean isStatic = (rcvc == null); |
| 710 | boolean isSpecial = (specialCaller != null); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 711 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 712 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
| 713 | if (isSpecial) |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 714 | target = lookup.in(specialCaller).unreflectSpecial(rmethod, specialCaller); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 715 | else |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 716 | target = lookup.in(defc).unreflect(rmethod); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 717 | } catch (NoAccessException ex) { |
| 718 | noAccess = ex; |
| 719 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 720 | 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)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 726 | 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 |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 729 | assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers())); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 730 | Class<?>[] paramsMaybeWithSelf = params; |
| 731 | if (!isStatic) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 732 | paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 733 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 734 | MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 735 | if (isStatic) { |
| 736 | assertEquals(typeMaybeWithSelf, target.type()); |
| 737 | } else { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 738 | 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 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 744 | Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf); |
| 745 | printCalled(target, name, argsMaybeWithSelf); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 746 | target.invokeWithArguments(argsMaybeWithSelf); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 747 | assertCalled(name, argsMaybeWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 748 | if (verbosity >= 1) |
| 749 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 750 | } |
| 751 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 752 | 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 |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 760 | public void testUnreflectSpecial() throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 761 | if (CAN_SKIP_WORKING) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 762 | startTest("unreflectSpecial"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 763 | 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"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 770 | } |
| 771 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 772 | 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 | |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 839 | 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 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 848 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 849 | public void testUnreflectGetter() throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 850 | startTest("unreflectGetter"); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 851 | 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 |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 865 | for (Object[] c : HasFields.CASES) { |
| 866 | Field f = (Field)c[0]; |
| 867 | Object value = c[1]; |
| 868 | Class<?> type = f.getType(); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 869 | testGetter(lookup, f, type, value, testMode); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 870 | } |
| 871 | } |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 872 | public void testGetter(MethodHandles.Lookup lookup, |
| 873 | Field f, Class<?> type, Object value, int testMode) throws Throwable { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 874 | boolean isStatic = Modifier.isStatic(f.getModifiers()); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 875 | Class<?> fclass = f.getDeclaringClass(); |
| 876 | String fname = f.getName(); |
| 877 | Class<?> ftype = f.getType(); |
| 878 | if (!testModeMatches(testMode, isStatic)) return; |
| 879 | countTest(true); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 880 | 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); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 884 | assertNameStringContains(mh, fname); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 885 | 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) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 894 | sawValue = (int) mh.invokeExact(); // do these exactly |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 895 | else |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 896 | sawValue = mh.invokeExact(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 897 | } else { |
| 898 | if (type == int.class) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 899 | sawValue = (int) mh.invokeExact((Object) fields); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 900 | else |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 901 | sawValue = mh.invokeExact((Object) fields); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 902 | } |
| 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 |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 909 | } |
| 910 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 911 | |
| 912 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 913 | public void testUnreflectSetter() throws Throwable { |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 914 | 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 { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 928 | Lookup lookup = PRIVATE; // FIXME: test more lookups than this one |
| 929 | startTest("unreflectSetter"); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 930 | for (Object[] c : HasFields.CASES) { |
| 931 | Field f = (Field)c[0]; |
| 932 | Object value = c[1]; |
| 933 | Class<?> type = f.getType(); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 934 | testSetter(lookup, f, type, value, testMode); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 935 | } |
| 936 | } |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 937 | public void testSetter(MethodHandles.Lookup lookup, |
| 938 | Field f, Class<?> type, Object value, int testMode) throws Throwable { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 939 | boolean isStatic = Modifier.isStatic(f.getModifiers()); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 940 | Class<?> fclass = f.getDeclaringClass(); |
| 941 | String fname = f.getName(); |
| 942 | Class<?> ftype = f.getType(); |
| 943 | if (!testModeMatches(testMode, isStatic)) return; |
| 944 | countTest(true); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 945 | MethodType expType = MethodType.methodType(void.class, HasFields.class, type); |
| 946 | if (isStatic) expType = expType.dropParameterTypes(0, 1); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 947 | 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(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 955 | assertSame(mh.type(), expType); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 956 | assertNameStringContains(mh, fname); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 957 | 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) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 968 | mh.invokeExact((int)putValue); // do these exactly |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 969 | else |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 970 | mh.invokeExact(putValue); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 971 | } else { |
| 972 | if (type == int.class) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 973 | mh.invokeExact((Object) fields, (int)putValue); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 974 | else |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 975 | mh.invokeExact((Object) fields, putValue); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 976 | } |
| 977 | assertEquals(f.get(fields), putValue); |
| 978 | } |
| 979 | f.set(fields, value); // put it back |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 980 | } |
| 981 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 982 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 983 | public void testArrayElementGetter() throws Throwable { |
| 984 | startTest("arrayElementGetter"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 985 | testArrayElementGetterSetter(false); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 986 | } |
| 987 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 988 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 989 | public void testArrayElementSetter() throws Throwable { |
| 990 | startTest("arrayElementSetter"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 991 | 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); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable { |
| 1008 | countTest(true); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1009 | if (verbosity >= 2) System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+Array.getLength(array)+"]"); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1010 | 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); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1019 | 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 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1032 | 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) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1041 | mh.invokeExact((int[]) array, i, (int)random); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1042 | else if (elemType == boolean.class) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1043 | mh.invokeExact((boolean[]) array, i, (boolean)random); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1044 | else |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1045 | mh.invokeExact(array, i, random); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1046 | assertEquals(model, array2list(array)); |
| 1047 | } else { |
| 1048 | Array.set(array, i, random); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1049 | } |
| 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); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1055 | } |
| 1056 | // observe array element |
| 1057 | sawValue = Array.get(array, i); |
| 1058 | if (!testSetter) { |
| 1059 | expValue = sawValue; |
| 1060 | if (elemType == int.class) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1061 | sawValue = (int) mh.invokeExact((int[]) array, i); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1062 | else if (elemType == boolean.class) |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1063 | sawValue = (boolean) mh.invokeExact((boolean[]) array, i); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1064 | else |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1065 | sawValue = mh.invokeExact(array, i); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1066 | 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; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1078 | } |
| 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) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1091 | return ofType(MethodType.methodType(rtype, Object[].class)); |
| 1092 | return ofType(MethodType.genericMethodType(n).changeReturnType(rtype)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1093 | } |
| 1094 | static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1095 | return ofType(MethodType.methodType(rtype, ptypes)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1096 | } |
| 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"; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1103 | try { |
| 1104 | return PRIVATE.findStatic(Callee.class, name, type); |
| 1105 | } catch (Exception ex) { |
| 1106 | throw new RuntimeException(ex); |
| 1107 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1111 | static MethodHandle typeHandler2(MethodHandle target, MethodType newType) { |
| 1112 | MethodType oldType = target.type(); |
| 1113 | int oldArity = oldType.parameterCount(); |
| 1114 | int newArity = newType.parameterCount(); |
| 1115 | if (newArity < oldArity) |
| 1116 | return MethodHandles.insertArguments(target, oldArity, "OPTIONAL"); |
| 1117 | else if (newArity > oldArity) |
| 1118 | return MethodHandles.dropArguments(target, oldArity-1, newType.parameterType(oldArity-1)); |
| 1119 | else |
| 1120 | return target; // attempt no further conversions |
| 1121 | } |
| 1122 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1123 | @Test |
| 1124 | public void testConvertArguments() throws Throwable { |
| 1125 | if (CAN_SKIP_WORKING) return; |
| 1126 | startTest("convertArguments"); |
| 1127 | testConvert(Callee.ofType(1), null, "id", int.class); |
| 1128 | testConvert(Callee.ofType(1), null, "id", String.class); |
| 1129 | testConvert(Callee.ofType(1), null, "id", Integer.class); |
| 1130 | testConvert(Callee.ofType(1), null, "id", short.class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1131 | testConvert(Callee.ofType(1), null, "id", char.class); |
| 1132 | testConvert(Callee.ofType(1), null, "id", byte.class); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1136 | testConvert(true, false, id, rtype, name, params); |
| 1137 | testConvert(true, true, id, rtype, name, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1140 | @Test |
| 1141 | public void testTypeHandler() throws Throwable { |
| 1142 | MethodHandle id = Callee.ofType(1); |
| 1143 | MethodHandle th2 = PRIVATE.findStatic(MethodHandlesTest.class, "typeHandler2", |
| 1144 | MethodType.methodType(MethodHandle.class, MethodHandle.class, MethodType.class)); |
| 1145 | MethodHandle id2 = id.withTypeHandler(th2); |
| 1146 | testConvert(true, false, id2, null, "id", Object.class); |
| 1147 | testConvert(true, true, id2, null, "id", Object.class); |
| 1148 | if (true) return; //FIXME |
| 1149 | testConvert(true, false, id2, null, "id", String.class); // FIXME: throws WMT |
| 1150 | testConvert(false, true, id2, null, "id", String.class); // FIXME: should not succeed |
| 1151 | testConvert(false, false, id2, null, "id", Object.class, String.class); //FIXME: array[1] line 1164 |
| 1152 | testConvert(true, true, id2, null, "id", Object.class, String.class); |
| 1153 | testConvert(false, false, id2, null, "id"); |
| 1154 | testConvert(true, true, id2, null, "id"); |
| 1155 | } |
| 1156 | |
| 1157 | void testConvert(boolean positive, boolean useAsType, |
| 1158 | MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1159 | countTest(positive); |
| 1160 | MethodType idType = id.type(); |
| 1161 | if (rtype == null) rtype = idType.returnType(); |
| 1162 | for (int i = 0; i < params.length; i++) { |
| 1163 | if (params[i] == null) params[i] = idType.parameterType(i); |
| 1164 | } |
| 1165 | // simulate the pairwise conversion |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1166 | MethodType newType = MethodType.methodType(rtype, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1167 | Object[] args = randomArgs(newType.parameterArray()); |
| 1168 | Object[] convArgs = args.clone(); |
| 1169 | for (int i = 0; i < args.length; i++) { |
| 1170 | Class<?> src = newType.parameterType(i); |
| 1171 | Class<?> dst = idType.parameterType(i); |
| 1172 | if (src != dst) |
| 1173 | convArgs[i] = castToWrapper(convArgs[i], dst); |
| 1174 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1175 | Object convResult = id.invokeWithArguments(convArgs); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1176 | { |
| 1177 | Class<?> dst = newType.returnType(); |
| 1178 | Class<?> src = idType.returnType(); |
| 1179 | if (src != dst) |
| 1180 | convResult = castToWrapper(convResult, dst); |
| 1181 | } |
| 1182 | MethodHandle target = null; |
| 1183 | RuntimeException error = null; |
| 1184 | try { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1185 | if (useAsType) |
| 1186 | target = MethodHandles.convertArguments(id, newType); |
| 1187 | else |
| 1188 | target = id.asType(newType); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1189 | } catch (RuntimeException ex) { |
| 1190 | error = ex; |
| 1191 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1192 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1193 | System.out.println("convert "+id+ " to "+newType+" => "+target |
| 1194 | +(error == null ? "" : " !! "+error)); |
| 1195 | if (positive && error != null) throw error; |
| 1196 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 1197 | if (!positive) return; // negative test failed as expected |
| 1198 | assertEquals(newType, target.type()); |
| 1199 | printCalled(target, id.toString(), args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1200 | Object result = target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1201 | assertCalled(name, convArgs); |
| 1202 | assertEquals(convResult, result); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1203 | if (verbosity >= 1) |
| 1204 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | @Test |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1208 | public void testPermuteArguments() throws Throwable { |
| 1209 | if (CAN_SKIP_WORKING) return; |
| 1210 | startTest("permuteArguments"); |
| 1211 | testPermuteArguments(4, Integer.class, 2, String.class, 0); |
| 1212 | //testPermuteArguments(6, Integer.class, 0, null, 30); |
| 1213 | //testPermuteArguments(4, Integer.class, 1, int.class, 6); |
| 1214 | } |
| 1215 | public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1216 | if (verbosity >= 2) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1217 | System.out.println("permuteArguments "+max+"*"+type1.getName() |
| 1218 | +(t2c==0?"":"/"+t2c+"*"+type2.getName()) |
| 1219 | +(dilution > 0 ? " with dilution "+dilution : "")); |
| 1220 | int t2pos = t2c == 0 ? 0 : 1; |
| 1221 | for (int inargs = t2pos+1; inargs <= max; inargs++) { |
| 1222 | Class<?>[] types = new Class<?>[inargs]; |
| 1223 | Arrays.fill(types, type1); |
| 1224 | if (t2c != 0) { |
| 1225 | // Fill in a middle range with type2: |
| 1226 | Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2); |
| 1227 | } |
| 1228 | Object[] args = randomArgs(types); |
| 1229 | int numcases = 1; |
| 1230 | for (int outargs = 0; outargs <= max; outargs++) { |
| 1231 | if (outargs - inargs >= MAX_ARG_INCREASE) continue; |
| 1232 | int[] reorder = new int[outargs]; |
| 1233 | int casStep = dilution + 1; |
| 1234 | // Avoid some common factors: |
| 1235 | while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) || |
| 1236 | (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0)) |
| 1237 | casStep++; |
| 1238 | for (int cas = 0; cas < numcases; cas += casStep) { |
| 1239 | for (int i = 0, c = cas; i < outargs; i++) { |
| 1240 | reorder[i] = c % inargs; |
| 1241 | c /= inargs; |
| 1242 | } |
| 1243 | testPermuteArguments(args, types, reorder); |
| 1244 | } |
| 1245 | numcases *= inargs; |
| 1246 | if (dilution > 10 && outargs >= 4) { |
| 1247 | // Do some special patterns, which we probably missed. |
| 1248 | // Replication of a single argument or argument pair. |
| 1249 | for (int i = 0; i < inargs; i++) { |
| 1250 | Arrays.fill(reorder, i); |
| 1251 | testPermuteArguments(args, types, reorder); |
| 1252 | for (int d = 1; d <= 2; d++) { |
| 1253 | if (i + d >= inargs) continue; |
| 1254 | for (int j = 1; j < outargs; j += 2) |
| 1255 | reorder[j] += 1; |
| 1256 | testPermuteArguments(args, types, reorder); |
| 1257 | testPermuteArguments(args, types, reverse(reorder)); |
| 1258 | } |
| 1259 | } |
| 1260 | // Repetition of a sequence of 3 or more arguments. |
| 1261 | for (int i = 1; i < inargs; i++) { |
| 1262 | for (int len = 3; len <= inargs; len++) { |
| 1263 | for (int j = 0; j < outargs; j++) |
| 1264 | reorder[j] = (i + (j % len)) % inargs; |
| 1265 | testPermuteArguments(args, types, reorder); |
| 1266 | testPermuteArguments(args, types, reverse(reorder)); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | static int[] reverse(int[] reorder) { |
| 1275 | reorder = reorder.clone(); |
| 1276 | for (int i = 0, imax = reorder.length / 2; i < imax; i++) { |
| 1277 | int j = reorder.length - 1 - i; |
| 1278 | int tem = reorder[i]; |
| 1279 | reorder[i] = reorder[j]; |
| 1280 | reorder[j] = tem; |
| 1281 | } |
| 1282 | return reorder; |
| 1283 | } |
| 1284 | |
| 1285 | void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable { |
| 1286 | countTest(); |
| 1287 | if (args == null && types == null) { |
| 1288 | int max = 0; |
| 1289 | for (int j : reorder) { |
| 1290 | if (max < j) max = j; |
| 1291 | } |
| 1292 | args = randomArgs(max+1, Integer.class); |
| 1293 | } |
| 1294 | if (args == null) { |
| 1295 | args = randomArgs(types); |
| 1296 | } |
| 1297 | if (types == null) { |
| 1298 | types = new Class<?>[args.length]; |
| 1299 | for (int i = 0; i < args.length; i++) |
| 1300 | types[i] = args[i].getClass(); |
| 1301 | } |
| 1302 | int inargs = args.length, outargs = reorder.length; |
| 1303 | assert(inargs == types.length); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1304 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1305 | System.out.println("permuteArguments "+Arrays.toString(reorder)); |
| 1306 | Object[] permArgs = new Object[outargs]; |
| 1307 | Class<?>[] permTypes = new Class<?>[outargs]; |
| 1308 | for (int i = 0; i < outargs; i++) { |
| 1309 | permArgs[i] = args[reorder[i]]; |
| 1310 | permTypes[i] = types[reorder[i]]; |
| 1311 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1312 | if (verbosity >= 4) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1313 | System.out.println("in args: "+Arrays.asList(args)); |
| 1314 | System.out.println("out args: "+Arrays.asList(permArgs)); |
| 1315 | System.out.println("in types: "+Arrays.asList(types)); |
| 1316 | System.out.println("out types: "+Arrays.asList(permTypes)); |
| 1317 | } |
| 1318 | MethodType inType = MethodType.methodType(Object.class, types); |
| 1319 | MethodType outType = MethodType.methodType(Object.class, permTypes); |
| 1320 | MethodHandle target = MethodHandles.convertArguments(ValueConversions.varargsList(outargs), outType); |
| 1321 | MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1322 | Object result = newTarget.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1323 | Object expected = Arrays.asList(permArgs); |
| 1324 | assertEquals(expected, result); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | @Test |
| 1329 | public void testSpreadArguments() throws Throwable { |
| 1330 | if (CAN_SKIP_WORKING) return; |
| 1331 | startTest("spreadArguments"); |
| 1332 | for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1333 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1334 | System.out.println("spreadArguments "+argType); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1335 | // FIXME: enable _adapter_spread_args and fix Fail_2 |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1336 | for (int nargs = 0; nargs < 10; nargs++) { |
| 1337 | if (argType == int.class && nargs >= 6) continue; // FIXME Fail_1 |
| 1338 | for (int pos = 0; pos < nargs; pos++) { |
| 1339 | if (argType == int.class && pos > 0) continue; // FIXME Fail_3 |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1340 | testSpreadArguments(argType, pos, nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable { |
| 1346 | countTest(); |
| 1347 | MethodHandle target = ValueConversions.varargsArray(nargs); |
| 1348 | MethodHandle target2 = changeArgTypes(target, argType); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1349 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1350 | System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]"); |
| 1351 | Object[] args = randomArgs(target2.type().parameterArray()); |
| 1352 | // make sure the target does what we think it does: |
| 1353 | if (pos == 0 && nargs < 5) { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1354 | Object[] check = (Object[]) target.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1355 | assertArrayEquals(args, check); |
| 1356 | switch (nargs) { |
| 1357 | case 0: |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1358 | check = (Object[]) target.invokeExact(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1359 | assertArrayEquals(args, check); |
| 1360 | break; |
| 1361 | case 1: |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1362 | check = (Object[]) target.invokeExact(args[0]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1363 | assertArrayEquals(args, check); |
| 1364 | break; |
| 1365 | case 2: |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1366 | check = (Object[]) target.invokeExact(args[0], args[1]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1367 | assertArrayEquals(args, check); |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList()); |
| 1372 | { // modify newParams in place |
| 1373 | List<Class<?>> spreadParams = newParams.subList(pos, nargs); |
| 1374 | spreadParams.clear(); spreadParams.add(Object[].class); |
| 1375 | } |
| 1376 | MethodType newType = MethodType.methodType(Object.class, newParams); |
| 1377 | MethodHandle result = MethodHandles.spreadArguments(target2, newType); |
| 1378 | Object[] returnValue; |
| 1379 | if (pos == 0) { |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame^] | 1380 | // In the following line, the first cast implies |
| 1381 | // normal Object return value for the MH call (Object[])->Object, |
| 1382 | // while the second cast dynamically converts to an Object array. |
| 1383 | // Such a double cast is typical of MH.invokeExact. |
| 1384 | returnValue = (Object[]) (Object) result.invokeExact(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1385 | } else { |
| 1386 | Object[] args1 = Arrays.copyOfRange(args, 0, pos+1); |
| 1387 | args1[pos] = Arrays.copyOfRange(args, pos, args.length); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1388 | returnValue = (Object[]) result.invokeWithArguments(args1); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1389 | } |
| 1390 | assertArrayEquals(args, returnValue); |
| 1391 | } |
| 1392 | |
| 1393 | @Test |
| 1394 | public void testCollectArguments() throws Throwable { |
| 1395 | if (CAN_SKIP_WORKING) return; |
| 1396 | startTest("collectArguments"); |
| 1397 | for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1398 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1399 | System.out.println("collectArguments "+argType); |
| 1400 | for (int nargs = 0; nargs < 10; nargs++) { |
| 1401 | for (int pos = 0; pos < nargs; pos++) { |
| 1402 | if (argType == int.class) continue; // FIXME Fail_4 |
| 1403 | testCollectArguments(argType, pos, nargs); |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable { |
| 1409 | countTest(); |
| 1410 | // fake up a MH with the same type as the desired adapter: |
| 1411 | MethodHandle fake = ValueConversions.varargsArray(nargs); |
| 1412 | fake = changeArgTypes(fake, argType); |
| 1413 | MethodType newType = fake.type(); |
| 1414 | Object[] args = randomArgs(newType.parameterArray()); |
| 1415 | // here is what should happen: |
| 1416 | Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1); |
| 1417 | collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length); |
| 1418 | // here is the MH which will witness the collected argument tail: |
| 1419 | MethodHandle target = ValueConversions.varargsArray(pos+1); |
| 1420 | target = changeArgTypes(target, 0, pos, argType); |
| 1421 | target = changeArgTypes(target, pos, pos+1, Object[].class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1422 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1423 | System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]"); |
| 1424 | MethodHandle result = MethodHandles.collectArguments(target, newType); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1425 | Object[] returnValue = (Object[]) result.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1426 | // assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]); |
| 1427 | // returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]); |
| 1428 | // collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]); |
| 1429 | assertArrayEquals(collectedArgs, returnValue); |
| 1430 | } |
| 1431 | |
| 1432 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1433 | public void testInsertArguments() throws Throwable { |
| 1434 | if (CAN_SKIP_WORKING) return; |
| 1435 | startTest("insertArguments"); |
| 1436 | for (int nargs = 0; nargs <= 4; nargs++) { |
| 1437 | for (int ins = 0; ins <= 4; ins++) { |
| 1438 | if (ins > MAX_ARG_INCREASE) continue; // FIXME Fail_6 |
| 1439 | for (int pos = 0; pos <= nargs; pos++) { |
| 1440 | testInsertArguments(nargs, pos, ins); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | void testInsertArguments(int nargs, int pos, int ins) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1447 | countTest(); |
| 1448 | MethodHandle target = ValueConversions.varargsArray(nargs + ins); |
| 1449 | Object[] args = randomArgs(target.type().parameterArray()); |
| 1450 | List<Object> resList = Arrays.asList(args); |
| 1451 | List<Object> argsToPass = new ArrayList<Object>(resList); |
| 1452 | List<Object> argsToInsert = argsToPass.subList(pos, pos + ins); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1453 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1454 | System.out.println("insert: "+argsToInsert+" into "+target); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1455 | MethodHandle target2 = MethodHandles.insertArguments(target, pos, |
| 1456 | (Object[]) argsToInsert.toArray()); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1457 | argsToInsert.clear(); // remove from argsToInsert |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1458 | Object res2 = target2.invokeWithArguments(argsToPass); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1459 | Object res2List = Arrays.asList((Object[])res2); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1460 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1461 | System.out.println("result: "+res2List); |
| 1462 | //if (!resList.equals(res2List)) |
| 1463 | // System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List); |
| 1464 | assertEquals(resList, res2List); |
| 1465 | } |
| 1466 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1467 | @Test |
| 1468 | public void testFilterArguments() throws Throwable { |
| 1469 | if (CAN_SKIP_WORKING) return; |
| 1470 | startTest("filterArguments"); |
| 1471 | for (int nargs = 1; nargs <= 6; nargs++) { |
| 1472 | for (int pos = 0; pos < nargs; pos++) { |
| 1473 | testFilterArguments(nargs, pos); |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | void testFilterArguments(int nargs, int pos) throws Throwable { |
| 1479 | countTest(); |
| 1480 | MethodHandle target = ValueConversions.varargsList(nargs); |
| 1481 | MethodHandle filter = ValueConversions.varargsList(1); |
| 1482 | filter = MethodHandles.convertArguments(filter, filter.type().generic()); |
| 1483 | Object[] argsToPass = randomArgs(nargs, Object.class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1484 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1485 | System.out.println("filter "+target+" at "+pos+" with "+filter); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1486 | MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1487 | // Simulate expected effect of filter on arglist: |
| 1488 | Object[] filteredArgs = argsToPass.clone(); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1489 | filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1490 | List<Object> expected = Arrays.asList(filteredArgs); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1491 | Object result = target2.invokeWithArguments(argsToPass); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1492 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1493 | System.out.println("result: "+result); |
| 1494 | if (!expected.equals(result)) |
| 1495 | System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+argsToPass+" => "+result); |
| 1496 | assertEquals(expected, result); |
| 1497 | } |
| 1498 | |
| 1499 | @Test |
| 1500 | public void testFoldArguments() throws Throwable { |
| 1501 | if (CAN_SKIP_WORKING) return; |
| 1502 | startTest("foldArguments"); |
| 1503 | for (int nargs = 0; nargs <= 4; nargs++) { |
| 1504 | for (int fold = 0; fold <= nargs; fold++) { |
| 1505 | for (int pos = 0; pos <= nargs; pos++) { |
| 1506 | testFoldArguments(nargs, pos, fold); |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | void testFoldArguments(int nargs, int pos, int fold) throws Throwable { |
| 1513 | if (pos != 0) return; // can fold only at pos=0 for now |
| 1514 | countTest(); |
| 1515 | MethodHandle target = ValueConversions.varargsList(1 + nargs); |
jrose | 25aef89 | 2010-10-30 21:02:30 -0700 | [diff] [blame] | 1516 | MethodHandle combine = ValueConversions.varargsList(fold).asType(MethodType.genericMethodType(fold)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1517 | List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class)); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1518 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1519 | System.out.println("fold "+target+" with "+combine); |
| 1520 | MethodHandle target2 = MethodHandles.foldArguments(target, combine); |
| 1521 | // Simulate expected effect of combiner on arglist: |
| 1522 | List<Object> expected = new ArrayList<Object>(argsToPass); |
| 1523 | List<Object> argsToFold = expected.subList(pos, pos + fold); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1524 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1525 | System.out.println("fold: "+argsToFold+" into "+target2); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1526 | Object foldedArgs = combine.invokeWithArguments(argsToFold); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1527 | argsToFold.add(0, foldedArgs); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1528 | Object result = target2.invokeWithArguments(argsToPass); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1529 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1530 | System.out.println("result: "+result); |
| 1531 | if (!expected.equals(result)) |
| 1532 | System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result); |
| 1533 | assertEquals(expected, result); |
| 1534 | } |
| 1535 | |
| 1536 | @Test |
| 1537 | public void testDropArguments() throws Throwable { |
| 1538 | if (CAN_SKIP_WORKING) return; |
| 1539 | startTest("dropArguments"); |
| 1540 | for (int nargs = 0; nargs <= 4; nargs++) { |
| 1541 | for (int drop = 1; drop <= 4; drop++) { |
| 1542 | for (int pos = 0; pos <= nargs; pos++) { |
| 1543 | testDropArguments(nargs, pos, drop); |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | void testDropArguments(int nargs, int pos, int drop) throws Throwable { |
| 1550 | countTest(); |
| 1551 | MethodHandle target = ValueConversions.varargsArray(nargs); |
| 1552 | Object[] args = randomArgs(target.type().parameterArray()); |
| 1553 | MethodHandle target2 = MethodHandles.dropArguments(target, pos, |
| 1554 | Collections.nCopies(drop, Object.class).toArray(new Class[0])); |
| 1555 | List<Object> resList = Arrays.asList(args); |
| 1556 | List<Object> argsToDrop = new ArrayList<Object>(resList); |
| 1557 | for (int i = drop; i > 0; i--) { |
| 1558 | argsToDrop.add(pos, "blort#"+i); |
| 1559 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1560 | Object res2 = target2.invokeWithArguments(argsToDrop); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1561 | Object res2List = Arrays.asList((Object[])res2); |
| 1562 | //if (!resList.equals(res2List)) |
| 1563 | // System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List); |
| 1564 | assertEquals(resList, res2List); |
| 1565 | } |
| 1566 | |
| 1567 | @Test |
| 1568 | public void testInvokers() throws Throwable { |
| 1569 | if (CAN_SKIP_WORKING) return; |
| 1570 | startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker"); |
| 1571 | // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker |
| 1572 | Set<MethodType> done = new HashSet<MethodType>(); |
| 1573 | for (int i = 0; i <= 6; i++) { |
| 1574 | MethodType gtype = MethodType.genericMethodType(i); |
| 1575 | for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) { |
| 1576 | for (int j = -1; j < i; j++) { |
| 1577 | MethodType type = gtype; |
| 1578 | if (j < 0) |
| 1579 | type = type.changeReturnType(argType); |
| 1580 | else if (argType == void.class) |
| 1581 | continue; |
| 1582 | else |
| 1583 | type = type.changeParameterType(j, argType); |
| 1584 | if (argType.isPrimitive() && j != i-1) continue; // FIXME Fail_5 |
| 1585 | if (done.add(type)) |
| 1586 | testInvokers(type); |
| 1587 | MethodType vtype = type.changeReturnType(void.class); |
| 1588 | if (done.add(vtype)) |
| 1589 | testInvokers(vtype); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | public void testInvokers(MethodType type) throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1596 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1597 | System.out.println("test invokers for "+type); |
| 1598 | int nargs = type.parameterCount(); |
| 1599 | boolean testRetCode = type.returnType() != void.class; |
| 1600 | MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee", |
| 1601 | MethodType.genericMethodType(0, true)); |
| 1602 | target = MethodHandles.collectArguments(target, type); |
| 1603 | Object[] args = randomArgs(type.parameterArray()); |
| 1604 | List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args)); |
| 1605 | targetPlusArgs.add(0, target); |
| 1606 | int code = (Integer) invokee(args); |
| 1607 | Object log = logEntry("invokee", args); |
| 1608 | assertEquals(log.hashCode(), code); |
| 1609 | assertCalled("invokee", args); |
| 1610 | MethodHandle inv; |
| 1611 | Object result; |
| 1612 | // exact invoker |
| 1613 | countTest(); |
| 1614 | calledLog.clear(); |
| 1615 | inv = MethodHandles.exactInvoker(type); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1616 | result = inv.invokeWithArguments(targetPlusArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1617 | if (testRetCode) assertEquals(code, result); |
| 1618 | assertCalled("invokee", args); |
| 1619 | // generic invoker |
| 1620 | countTest(); |
| 1621 | inv = MethodHandles.genericInvoker(type); |
| 1622 | if (nargs <= 3) { |
| 1623 | calledLog.clear(); |
| 1624 | switch (nargs) { |
| 1625 | case 0: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1626 | result = inv.invokeExact(target); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1627 | break; |
| 1628 | case 1: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1629 | result = inv.invokeExact(target, args[0]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1630 | break; |
| 1631 | case 2: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1632 | result = inv.invokeExact(target, args[0], args[1]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1633 | break; |
| 1634 | case 3: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1635 | result = inv.invokeExact(target, args[0], args[1], args[2]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1636 | break; |
| 1637 | } |
| 1638 | if (testRetCode) assertEquals(code, result); |
| 1639 | assertCalled("invokee", args); |
| 1640 | } |
| 1641 | calledLog.clear(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1642 | result = inv.invokeWithArguments(targetPlusArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1643 | if (testRetCode) assertEquals(code, result); |
| 1644 | assertCalled("invokee", args); |
| 1645 | // varargs invoker #0 |
| 1646 | calledLog.clear(); |
| 1647 | inv = MethodHandles.varargsInvoker(type, 0); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1648 | result = inv.invokeExact(target, args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1649 | if (testRetCode) assertEquals(code, result); |
| 1650 | assertCalled("invokee", args); |
| 1651 | if (nargs >= 1) { |
| 1652 | // varargs invoker #1 |
| 1653 | calledLog.clear(); |
| 1654 | inv = MethodHandles.varargsInvoker(type, 1); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1655 | result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1656 | if (testRetCode) assertEquals(code, result); |
| 1657 | assertCalled("invokee", args); |
| 1658 | } |
| 1659 | if (nargs >= 2) { |
| 1660 | // varargs invoker #2 |
| 1661 | calledLog.clear(); |
| 1662 | inv = MethodHandles.varargsInvoker(type, 2); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1663 | result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1664 | if (testRetCode) assertEquals(code, result); |
| 1665 | assertCalled("invokee", args); |
| 1666 | } |
| 1667 | if (nargs >= 3) { |
| 1668 | // varargs invoker #3 |
| 1669 | calledLog.clear(); |
| 1670 | inv = MethodHandles.varargsInvoker(type, 3); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1671 | result = inv.invokeExact(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1672 | if (testRetCode) assertEquals(code, result); |
| 1673 | assertCalled("invokee", args); |
| 1674 | } |
| 1675 | for (int k = 0; k <= nargs; k++) { |
| 1676 | // varargs invoker #0..N |
| 1677 | countTest(); |
| 1678 | calledLog.clear(); |
| 1679 | inv = MethodHandles.varargsInvoker(type, k); |
| 1680 | List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs); |
| 1681 | List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs); |
| 1682 | Object[] tail = tailList.toArray(); |
| 1683 | tailList.clear(); tailList.add(tail); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1684 | result = inv.invokeWithArguments(targetPlusVarArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1685 | if (testRetCode) assertEquals(code, result); |
| 1686 | assertCalled("invokee", args); |
| 1687 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1688 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1689 | // dynamic invoker |
| 1690 | countTest(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1691 | CallSite site = new CallSite(type); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1692 | inv = MethodHandles.dynamicInvoker(site); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1693 | |
| 1694 | // see if we get the result of the original target: |
| 1695 | try { |
| 1696 | result = inv.invokeWithArguments(args); |
| 1697 | assertTrue("should not reach here", false); |
| 1698 | } catch (IllegalStateException ex) { |
| 1699 | String msg = ex.getMessage(); |
| 1700 | assertTrue(msg, msg.contains("site")); |
| 1701 | } |
| 1702 | |
| 1703 | // set new target after invoker is created, to make sure we track target |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1704 | site.setTarget(target); |
| 1705 | calledLog.clear(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1706 | result = inv.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1707 | if (testRetCode) assertEquals(code, result); |
| 1708 | assertCalled("invokee", args); |
| 1709 | } |
| 1710 | |
| 1711 | static Object invokee(Object... args) { |
| 1712 | return called("invokee", args).hashCode(); |
| 1713 | } |
| 1714 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1715 | private static final String MISSING_ARG = "missingArg"; |
| 1716 | static Object targetIfEquals() { |
| 1717 | return called("targetIfEquals"); |
| 1718 | } |
| 1719 | static Object fallbackIfNotEquals() { |
| 1720 | return called("fallbackIfNotEquals"); |
| 1721 | } |
| 1722 | static Object targetIfEquals(Object x) { |
| 1723 | assertEquals(x, MISSING_ARG); |
| 1724 | return called("targetIfEquals", x); |
| 1725 | } |
| 1726 | static Object fallbackIfNotEquals(Object x) { |
| 1727 | assertFalse(x.toString(), x.equals(MISSING_ARG)); |
| 1728 | return called("fallbackIfNotEquals", x); |
| 1729 | } |
| 1730 | static Object targetIfEquals(Object x, Object y) { |
| 1731 | assertEquals(x, y); |
| 1732 | return called("targetIfEquals", x, y); |
| 1733 | } |
| 1734 | static Object fallbackIfNotEquals(Object x, Object y) { |
| 1735 | assertFalse(x.toString(), x.equals(y)); |
| 1736 | return called("fallbackIfNotEquals", x, y); |
| 1737 | } |
| 1738 | static Object targetIfEquals(Object x, Object y, Object z) { |
| 1739 | assertEquals(x, y); |
| 1740 | return called("targetIfEquals", x, y, z); |
| 1741 | } |
| 1742 | static Object fallbackIfNotEquals(Object x, Object y, Object z) { |
| 1743 | assertFalse(x.toString(), x.equals(y)); |
| 1744 | return called("fallbackIfNotEquals", x, y, z); |
| 1745 | } |
| 1746 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1747 | @Test |
| 1748 | public void testGuardWithTest() throws Throwable { |
| 1749 | if (CAN_SKIP_WORKING) return; |
| 1750 | startTest("guardWithTest"); |
| 1751 | for (int nargs = 0; nargs <= 3; nargs++) { |
| 1752 | if (nargs != 2) continue; // FIXME: test more later |
| 1753 | testGuardWithTest(nargs, Object.class); |
| 1754 | testGuardWithTest(nargs, String.class); |
| 1755 | } |
| 1756 | } |
| 1757 | void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable { |
| 1758 | countTest(); |
| 1759 | MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class)); |
| 1760 | MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs)); |
| 1761 | MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs)); |
| 1762 | while (test.type().parameterCount() < nargs) |
| 1763 | test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class); |
| 1764 | while (test.type().parameterCount() > nargs) |
| 1765 | test = MethodHandles.insertArguments(test, 0, MISSING_ARG); |
| 1766 | if (argClass != Object.class) { |
| 1767 | test = changeArgTypes(test, argClass); |
| 1768 | target = changeArgTypes(target, argClass); |
| 1769 | fallback = changeArgTypes(fallback, argClass); |
| 1770 | } |
| 1771 | MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback); |
| 1772 | assertEquals(target.type(), mh.type()); |
| 1773 | Object[][] argLists = { |
| 1774 | { }, |
| 1775 | { "foo" }, { MISSING_ARG }, |
| 1776 | { "foo", "foo" }, { "foo", "bar" }, |
| 1777 | { "foo", "foo", "baz" }, { "foo", "bar", "baz" } |
| 1778 | }; |
| 1779 | for (Object[] argList : argLists) { |
| 1780 | if (argList.length != nargs) continue; |
| 1781 | boolean equals; |
| 1782 | switch (nargs) { |
| 1783 | case 0: equals = true; break; |
| 1784 | case 1: equals = MISSING_ARG.equals(argList[0]); break; |
| 1785 | default: equals = argList[0].equals(argList[1]); break; |
| 1786 | } |
| 1787 | String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1788 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1789 | System.out.println(logEntry(willCall, argList)); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1790 | Object result = mh.invokeWithArguments(argList); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1791 | assertCalled(willCall, argList); |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | @Test |
| 1796 | public void testCatchException() throws Throwable { |
| 1797 | if (CAN_SKIP_WORKING) return; |
| 1798 | startTest("catchException"); |
| 1799 | for (int nargs = 2; nargs <= 6; nargs++) { |
| 1800 | for (int ti = 0; ti <= 1; ti++) { |
| 1801 | boolean throwIt = (ti != 0); |
| 1802 | testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs); |
| 1803 | testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs); |
| 1804 | testCatchException(String.class, new LinkageError("testing"), throwIt, nargs); |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | private static <T extends Throwable> |
| 1810 | Object throwOrReturn(Object normal, T exception) throws T { |
| 1811 | if (exception != null) throw exception; |
| 1812 | return normal; |
| 1813 | } |
| 1814 | |
| 1815 | void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable { |
| 1816 | countTest(); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1817 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1818 | System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs); |
| 1819 | Class<? extends Throwable> exType = thrown.getClass(); |
| 1820 | MethodHandle throwOrReturn |
| 1821 | = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn", |
| 1822 | MethodType.methodType(Object.class, Object.class, Throwable.class)); |
| 1823 | MethodHandle thrower = throwOrReturn; |
| 1824 | while (thrower.type().parameterCount() < nargs) |
| 1825 | thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class); |
| 1826 | MethodHandle target = MethodHandles.catchException(thrower, |
| 1827 | thrown.getClass(), ValueConversions.varargsList(1+nargs)); |
| 1828 | assertEquals(thrower.type(), target.type()); |
| 1829 | //System.out.println("catching with "+target+" : "+throwOrReturn); |
| 1830 | Object[] args = randomArgs(nargs, Object.class); |
| 1831 | args[1] = (throwIt ? thrown : null); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1832 | Object returned = target.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1833 | //System.out.println("return from "+target+" : "+returned); |
| 1834 | if (!throwIt) { |
| 1835 | assertSame(args[0], returned); |
| 1836 | } else { |
| 1837 | List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args)); |
| 1838 | catchArgs.add(0, thrown); |
| 1839 | assertEquals(catchArgs, returned); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | @Test |
| 1844 | public void testThrowException() throws Throwable { |
| 1845 | if (CAN_SKIP_WORKING) return; |
| 1846 | startTest("throwException"); |
| 1847 | testThrowException(int.class, new ClassCastException("testing")); |
| 1848 | testThrowException(void.class, new java.io.IOException("testing")); |
| 1849 | testThrowException(String.class, new LinkageError("testing")); |
| 1850 | } |
| 1851 | |
| 1852 | void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable { |
| 1853 | countTest(); |
| 1854 | Class<? extends Throwable> exType = thrown.getClass(); |
| 1855 | MethodHandle target = MethodHandles.throwException(returnType, exType); |
| 1856 | //System.out.println("throwing with "+target+" : "+thrown); |
| 1857 | MethodType expectedType = MethodType.methodType(returnType, exType); |
| 1858 | assertEquals(expectedType, target.type()); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1859 | target = MethodHandles.convertArguments(target, target.type().generic()); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1860 | Throwable caught = null; |
| 1861 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1862 | Object res = target.invokeExact((Object) thrown); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1863 | fail("got "+res+" instead of throwing "+thrown); |
| 1864 | } catch (Throwable ex) { |
| 1865 | if (ex != thrown) { |
| 1866 | if (ex instanceof Error) throw (Error)ex; |
| 1867 | if (ex instanceof RuntimeException) throw (RuntimeException)ex; |
| 1868 | } |
| 1869 | caught = ex; |
| 1870 | } |
| 1871 | assertSame(thrown, caught); |
| 1872 | } |
| 1873 | |
| 1874 | @Test |
| 1875 | public void testCastFailure() throws Throwable { |
| 1876 | if (CAN_SKIP_WORKING) return; |
| 1877 | startTest("testCastFailure"); |
| 1878 | testCastFailure("cast/argument", 11000); |
| 1879 | testCastFailure("unbox/argument", 11000); |
| 1880 | testCastFailure("cast/return", 11000); |
| 1881 | testCastFailure("unbox/return", 11000); |
| 1882 | } |
| 1883 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1884 | static class Surprise { |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1885 | public MethodHandle asMethodHandle() { |
| 1886 | return VALUE.bindTo(this); |
| 1887 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1888 | Object value(Object x) { |
| 1889 | trace("value", x); |
| 1890 | if (boo != null) return boo; |
| 1891 | return x; |
| 1892 | } |
| 1893 | Object boo; |
| 1894 | void boo(Object x) { boo = x; } |
| 1895 | |
| 1896 | static void trace(String x, Object y) { |
| 1897 | if (verbosity > 8) System.out.println(x+"="+y); |
| 1898 | } |
| 1899 | static Object refIdentity(Object x) { trace("ref.x", x); return x; } |
| 1900 | static Integer boxIdentity(Integer x) { trace("box.x", x); return x; } |
| 1901 | static int intIdentity(int x) { trace("int.x", x); return x; } |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1902 | static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY; |
| 1903 | static { |
| 1904 | try { |
| 1905 | VALUE = PRIVATE.findVirtual( |
| 1906 | Surprise.class, "value", |
| 1907 | MethodType.methodType(Object.class, Object.class)); |
| 1908 | REF_IDENTITY = PRIVATE.findStatic( |
| 1909 | Surprise.class, "refIdentity", |
| 1910 | MethodType.methodType(Object.class, Object.class)); |
| 1911 | BOX_IDENTITY = PRIVATE.findStatic( |
| 1912 | Surprise.class, "boxIdentity", |
| 1913 | MethodType.methodType(Integer.class, Integer.class)); |
| 1914 | INT_IDENTITY = PRIVATE.findStatic( |
| 1915 | Surprise.class, "intIdentity", |
| 1916 | MethodType.methodType(int.class, int.class)); |
| 1917 | } catch (Exception ex) { |
| 1918 | throw new RuntimeException(ex); |
| 1919 | } |
| 1920 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | void testCastFailure(String mode, int okCount) throws Throwable { |
| 1924 | countTest(false); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1925 | if (verbosity > 2) System.out.println("mode="+mode); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1926 | Surprise boo = new Surprise(); |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1927 | MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1928 | if (mode.endsWith("/return")) { |
| 1929 | if (mode.equals("unbox/return")) { |
| 1930 | // fail on return to ((Integer)surprise).intValue |
| 1931 | surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(int.class, Object.class)); |
| 1932 | identity = MethodHandles.convertArguments(identity, MethodType.methodType(int.class, Object.class)); |
| 1933 | } else if (mode.equals("cast/return")) { |
| 1934 | // fail on return to (Integer)surprise |
| 1935 | surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(Integer.class, Object.class)); |
| 1936 | identity = MethodHandles.convertArguments(identity, MethodType.methodType(Integer.class, Object.class)); |
| 1937 | } |
| 1938 | } else if (mode.endsWith("/argument")) { |
| 1939 | MethodHandle callee = null; |
| 1940 | if (mode.equals("unbox/argument")) { |
| 1941 | // fail on handing surprise to int argument |
| 1942 | callee = Surprise.INT_IDENTITY; |
| 1943 | } else if (mode.equals("cast/argument")) { |
| 1944 | // fail on handing surprise to Integer argument |
| 1945 | callee = Surprise.BOX_IDENTITY; |
| 1946 | } |
| 1947 | if (callee != null) { |
| 1948 | callee = MethodHandles.convertArguments(callee, MethodType.genericMethodType(1)); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1949 | surprise = MethodHandles.filterArguments(callee, 0, surprise); |
| 1950 | identity = MethodHandles.filterArguments(callee, 0, identity); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1951 | } |
| 1952 | } |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1953 | assertNotSame(mode, surprise, surprise0); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1954 | identity = MethodHandles.convertArguments(identity, MethodType.genericMethodType(1)); |
| 1955 | surprise = MethodHandles.convertArguments(surprise, MethodType.genericMethodType(1)); |
| 1956 | Object x = 42; |
| 1957 | for (int i = 0; i < okCount; i++) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1958 | Object y = identity.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1959 | assertEquals(x, y); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1960 | Object z = surprise.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1961 | assertEquals(x, z); |
| 1962 | } |
| 1963 | boo.boo("Boo!"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1964 | Object y = identity.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1965 | assertEquals(x, y); |
| 1966 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1967 | Object z = surprise.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1968 | System.out.println("Failed to throw; got z="+z); |
| 1969 | assertTrue(false); |
| 1970 | } catch (Exception ex) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1971 | if (verbosity > 2) |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1972 | System.out.println("caught "+ex); |
| 1973 | if (verbosity > 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1974 | ex.printStackTrace(); |
| 1975 | assertTrue(ex instanceof ClassCastException |
| 1976 | // FIXME: accept only one of the two for any given unit test |
| 1977 | || ex instanceof WrongMethodTypeException |
| 1978 | ); |
| 1979 | } |
| 1980 | } |
| 1981 | |
jrose | 45b7a33 | 2010-05-03 23:32:47 -0700 | [diff] [blame] | 1982 | static Example userMethod(Object o, String s, int i) { |
| 1983 | called("userMethod", o, s, i); |
| 1984 | return null; |
| 1985 | } |
| 1986 | |
| 1987 | @Test |
| 1988 | public void testUserClassInSignature() throws Throwable { |
| 1989 | if (CAN_SKIP_WORKING) return; |
| 1990 | startTest("testUserClassInSignature"); |
| 1991 | Lookup lookup = MethodHandles.lookup(); |
| 1992 | String name; MethodType mt; MethodHandle mh; |
| 1993 | Object[] args; |
| 1994 | |
| 1995 | // Try a static method. |
| 1996 | name = "userMethod"; |
| 1997 | mt = MethodType.methodType(Example.class, Object.class, String.class, int.class); |
| 1998 | mh = lookup.findStatic(lookup.lookupClass(), name, mt); |
| 1999 | assertEquals(mt, mh.type()); |
| 2000 | assertEquals(Example.class, mh.type().returnType()); |
| 2001 | args = randomArgs(mh.type().parameterArray()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2002 | mh.invokeWithArguments(args); |
jrose | 45b7a33 | 2010-05-03 23:32:47 -0700 | [diff] [blame] | 2003 | assertCalled(name, args); |
| 2004 | |
| 2005 | // Try a virtual method. |
| 2006 | name = "v2"; |
| 2007 | mt = MethodType.methodType(Object.class, Object.class, int.class); |
| 2008 | mh = lookup.findVirtual(Example.class, name, mt); |
| 2009 | assertEquals(mt, mh.type().dropParameterTypes(0,1)); |
| 2010 | assertTrue(mh.type().parameterList().contains(Example.class)); |
| 2011 | args = randomArgs(mh.type().parameterArray()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2012 | mh.invokeWithArguments(args); |
jrose | 45b7a33 | 2010-05-03 23:32:47 -0700 | [diff] [blame] | 2013 | assertCalled(name, args); |
| 2014 | } |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2015 | |
| 2016 | static void runForRunnable() { |
| 2017 | called("runForRunnable"); |
| 2018 | } |
| 2019 | private interface Fooable { |
| 2020 | Object foo(Fooable x, Object y); |
| 2021 | // this is for randomArg: |
| 2022 | public class Impl implements Fooable { |
| 2023 | public Object foo(Fooable x, Object y) { |
| 2024 | throw new RuntimeException("do not call"); |
| 2025 | } |
| 2026 | final String name; |
| 2027 | public Impl() { name = "Fooable#"+nextArg(); } |
| 2028 | @Override public String toString() { return name; } |
| 2029 | } |
| 2030 | } |
| 2031 | static Object fooForFooable(Fooable x, Object y) { |
| 2032 | return called("fooForFooable", x, y); |
| 2033 | } |
| 2034 | private static class MyCheckedException extends Exception { |
| 2035 | } |
| 2036 | private interface WillThrow { |
| 2037 | void willThrow() throws MyCheckedException; |
| 2038 | } |
| 2039 | |
| 2040 | @Test |
| 2041 | public void testAsInstance() throws Throwable { |
| 2042 | if (CAN_SKIP_WORKING) return; |
| 2043 | Lookup lookup = MethodHandles.lookup(); |
| 2044 | { |
| 2045 | MethodType mt = MethodType.methodType(void.class); |
| 2046 | MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "runForRunnable", mt); |
| 2047 | Runnable proxy = MethodHandles.asInstance(mh, Runnable.class); |
| 2048 | proxy.run(); |
| 2049 | assertCalled("runForRunnable"); |
| 2050 | } |
| 2051 | { |
| 2052 | MethodType mt = MethodType.methodType(Object.class, Fooable.class, Object.class); |
| 2053 | MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "fooForFooable", mt); |
| 2054 | Fooable proxy = MethodHandles.asInstance(mh, Fooable.class); |
| 2055 | Object[] args = randomArgs(mt.parameterArray()); |
| 2056 | Object result = proxy.foo((Fooable) args[0], args[1]); |
| 2057 | assertCalled("fooForFooable", args); |
| 2058 | assertEquals(result, logEntry("fooForFooable", args)); |
| 2059 | } |
| 2060 | for (Throwable ex : new Throwable[] { new NullPointerException("ok"), |
| 2061 | new InternalError("ok"), |
| 2062 | new Throwable("fail"), |
| 2063 | new Exception("fail"), |
| 2064 | new MyCheckedException() |
| 2065 | }) { |
| 2066 | MethodHandle mh = MethodHandles.throwException(void.class, Throwable.class); |
| 2067 | mh = MethodHandles.insertArguments(mh, 0, ex); |
| 2068 | WillThrow proxy = MethodHandles.asInstance(mh, WillThrow.class); |
| 2069 | try { |
| 2070 | proxy.willThrow(); |
| 2071 | System.out.println("Failed to throw: "+ex); |
| 2072 | assertTrue(false); |
| 2073 | } catch (Throwable ex1) { |
| 2074 | if (verbosity > 2) { |
| 2075 | System.out.println("throw "+ex); |
| 2076 | System.out.println("catch "+(ex == ex1 ? "UNWRAPPED" : ex1)); |
| 2077 | } |
| 2078 | if (ex instanceof RuntimeException || |
| 2079 | ex instanceof Error) { |
| 2080 | assertSame("must pass unchecked exception out without wrapping", ex, ex1); |
| 2081 | } else if (ex instanceof MyCheckedException) { |
| 2082 | assertSame("must pass declared exception out without wrapping", ex, ex1); |
| 2083 | } else { |
| 2084 | assertNotSame("must pass undeclared checked exception with wrapping", ex, ex1); |
| 2085 | UndeclaredThrowableException utex = (UndeclaredThrowableException) ex1; |
| 2086 | assertSame(ex, utex.getCause()); |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | // Test error checking: |
| 2091 | MethodHandle genericMH = ValueConversions.varargsArray(0); |
| 2092 | genericMH = MethodHandles.convertArguments(genericMH, genericMH.type().generic()); |
| 2093 | for (Class<?> sam : new Class[] { Runnable.class, |
| 2094 | Fooable.class, |
| 2095 | Iterable.class }) { |
| 2096 | try { |
| 2097 | // Must throw, because none of these guys has generic type. |
| 2098 | MethodHandles.asInstance(genericMH, sam); |
| 2099 | System.out.println("Failed to throw"); |
| 2100 | assertTrue(false); |
| 2101 | } catch (IllegalArgumentException ex) { |
| 2102 | } |
| 2103 | } |
| 2104 | for (Class<?> nonSAM : new Class[] { Object.class, |
| 2105 | String.class, |
| 2106 | CharSequence.class, |
| 2107 | Example.class }) { |
| 2108 | try { |
| 2109 | MethodHandles.asInstance(ValueConversions.varargsArray(0), nonSAM); |
| 2110 | System.out.println("Failed to throw"); |
| 2111 | assertTrue(false); |
| 2112 | } catch (IllegalArgumentException ex) { |
| 2113 | } |
| 2114 | } |
| 2115 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2116 | } |
| 2117 | // Local abbreviated copy of sun.dyn.util.ValueConversions |
| 2118 | class ValueConversions { |
| 2119 | private static final Lookup IMPL_LOOKUP = MethodHandles.lookup(); |
| 2120 | private static final Object[] NO_ARGS_ARRAY = {}; |
| 2121 | private static Object[] makeArray(Object... args) { return args; } |
| 2122 | private static Object[] array() { return NO_ARGS_ARRAY; } |
| 2123 | private static Object[] array(Object a0) |
| 2124 | { return makeArray(a0); } |
| 2125 | private static Object[] array(Object a0, Object a1) |
| 2126 | { return makeArray(a0, a1); } |
| 2127 | private static Object[] array(Object a0, Object a1, Object a2) |
| 2128 | { return makeArray(a0, a1, a2); } |
| 2129 | private static Object[] array(Object a0, Object a1, Object a2, Object a3) |
| 2130 | { return makeArray(a0, a1, a2, a3); } |
| 2131 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2132 | Object a4) |
| 2133 | { return makeArray(a0, a1, a2, a3, a4); } |
| 2134 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2135 | Object a4, Object a5) |
| 2136 | { return makeArray(a0, a1, a2, a3, a4, a5); } |
| 2137 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2138 | Object a4, Object a5, Object a6) |
| 2139 | { return makeArray(a0, a1, a2, a3, a4, a5, a6); } |
| 2140 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2141 | Object a4, Object a5, Object a6, Object a7) |
| 2142 | { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); } |
| 2143 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2144 | Object a4, Object a5, Object a6, Object a7, |
| 2145 | Object a8) |
| 2146 | { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); } |
| 2147 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2148 | Object a4, Object a5, Object a6, Object a7, |
| 2149 | Object a8, Object a9) |
| 2150 | { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } |
| 2151 | static MethodHandle[] makeArrays() { |
| 2152 | ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>(); |
| 2153 | MethodHandles.Lookup lookup = IMPL_LOOKUP; |
| 2154 | for (;;) { |
| 2155 | int nargs = arrays.size(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2156 | MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2157 | String name = "array"; |
| 2158 | MethodHandle array = null; |
| 2159 | try { |
| 2160 | array = lookup.findStatic(ValueConversions.class, name, type); |
| 2161 | } catch (NoAccessException ex) { |
| 2162 | } |
| 2163 | if (array == null) break; |
| 2164 | arrays.add(array); |
| 2165 | } |
| 2166 | assert(arrays.size() == 11); // current number of methods |
| 2167 | return arrays.toArray(new MethodHandle[0]); |
| 2168 | } |
| 2169 | static final MethodHandle[] ARRAYS = makeArrays(); |
| 2170 | |
| 2171 | /** Return a method handle that takes the indicated number of Object |
| 2172 | * arguments and returns an Object array of them, as if for varargs. |
| 2173 | */ |
| 2174 | public static MethodHandle varargsArray(int nargs) { |
| 2175 | if (nargs < ARRAYS.length) |
| 2176 | return ARRAYS[nargs]; |
| 2177 | // else need to spin bytecode or do something else fancy |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2178 | throw new UnsupportedOperationException("NYI: cannot form a varargs array of length "+nargs); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2179 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2180 | |
| 2181 | private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY); |
| 2182 | private static List<Object> makeList(Object... args) { return Arrays.asList(args); } |
| 2183 | private static List<Object> list() { return NO_ARGS_LIST; } |
| 2184 | private static List<Object> list(Object a0) |
| 2185 | { return makeList(a0); } |
| 2186 | private static List<Object> list(Object a0, Object a1) |
| 2187 | { return makeList(a0, a1); } |
| 2188 | private static List<Object> list(Object a0, Object a1, Object a2) |
| 2189 | { return makeList(a0, a1, a2); } |
| 2190 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3) |
| 2191 | { return makeList(a0, a1, a2, a3); } |
| 2192 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2193 | Object a4) |
| 2194 | { return makeList(a0, a1, a2, a3, a4); } |
| 2195 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2196 | Object a4, Object a5) |
| 2197 | { return makeList(a0, a1, a2, a3, a4, a5); } |
| 2198 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2199 | Object a4, Object a5, Object a6) |
| 2200 | { return makeList(a0, a1, a2, a3, a4, a5, a6); } |
| 2201 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2202 | Object a4, Object a5, Object a6, Object a7) |
| 2203 | { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); } |
| 2204 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2205 | Object a4, Object a5, Object a6, Object a7, |
| 2206 | Object a8) |
| 2207 | { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); } |
| 2208 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2209 | Object a4, Object a5, Object a6, Object a7, |
| 2210 | Object a8, Object a9) |
| 2211 | { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } |
| 2212 | static MethodHandle[] makeLists() { |
| 2213 | ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>(); |
| 2214 | MethodHandles.Lookup lookup = IMPL_LOOKUP; |
| 2215 | for (;;) { |
| 2216 | int nargs = arrays.size(); |
| 2217 | MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class); |
| 2218 | String name = "list"; |
| 2219 | MethodHandle array = null; |
| 2220 | try { |
| 2221 | array = lookup.findStatic(ValueConversions.class, name, type); |
| 2222 | } catch (NoAccessException ex) { |
| 2223 | } |
| 2224 | if (array == null) break; |
| 2225 | arrays.add(array); |
| 2226 | } |
| 2227 | assert(arrays.size() == 11); // current number of methods |
| 2228 | return arrays.toArray(new MethodHandle[0]); |
| 2229 | } |
| 2230 | static final MethodHandle[] LISTS = makeLists(); |
| 2231 | |
| 2232 | /** Return a method handle that takes the indicated number of Object |
| 2233 | * arguments and returns List. |
| 2234 | */ |
| 2235 | public static MethodHandle varargsList(int nargs) { |
| 2236 | if (nargs < LISTS.length) |
| 2237 | return LISTS[nargs]; |
| 2238 | // else need to spin bytecode or do something else fancy |
| 2239 | throw new UnsupportedOperationException("NYI"); |
| 2240 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2241 | } |
| 2242 | // This guy tests access from outside the same package member, but inside |
| 2243 | // the package itself. |
| 2244 | class PackageSibling { |
| 2245 | static Lookup lookup() { |
| 2246 | return MethodHandles.lookup(); |
| 2247 | } |
| 2248 | } |