jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1 | /* |
alanb | 0d05823 | 2012-11-02 15:50:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2009, 2012, 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 |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 27 | * @summary unit tests for java.lang.invoke.MethodHandles |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 28 | * @compile MethodHandlesTest.java remote/RemoteExample.java |
jiangli | 52f5708 | 2012-11-05 12:51:14 -0500 | [diff] [blame] | 29 | * @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -esa test.java.lang.invoke.MethodHandlesTest |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 32 | package test.java.lang.invoke; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 33 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 34 | import test.java.lang.invoke.remote.RemoteExample; |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 35 | import java.lang.invoke.*; |
| 36 | import java.lang.invoke.MethodHandles.Lookup; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 37 | import java.lang.reflect.*; |
| 38 | import java.util.*; |
| 39 | import org.junit.*; |
| 40 | import static org.junit.Assert.*; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * @author jrose |
| 46 | */ |
| 47 | public class MethodHandlesTest { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 48 | static final Class<?> THIS_CLASS = MethodHandlesTest.class; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 49 | // How much output? |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 50 | static int verbosity = 0; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 51 | static { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 52 | String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity"); |
| 53 | if (vstr == null) |
| 54 | vstr = System.getProperty(THIS_CLASS.getName()+".verbosity"); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 55 | if (vstr != null) verbosity = Integer.parseInt(vstr); |
| 56 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 57 | |
| 58 | // Set this true during development if you want to fast-forward to |
| 59 | // a particular new, non-working test. Tests which are known to |
| 60 | // work (or have recently worked) test this flag and return on true. |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 61 | static final boolean CAN_SKIP_WORKING; |
| 62 | static { |
| 63 | String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".CAN_SKIP_WORKING"); |
| 64 | if (vstr == null) |
| 65 | vstr = System.getProperty(THIS_CLASS.getName()+".CAN_SKIP_WORKING"); |
| 66 | CAN_SKIP_WORKING = Boolean.parseBoolean(vstr); |
| 67 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 68 | |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 69 | // Set 'true' to do about 15x fewer tests, especially those redundant with RicochetTest. |
| 70 | // This might be useful with -Xcomp stress tests that compile all method handles. |
| 71 | static boolean CAN_TEST_LIGHTLY = Boolean.getBoolean(THIS_CLASS.getName()+".CAN_TEST_LIGHTLY"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 72 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 73 | @Test |
| 74 | public void testFirst() throws Throwable { |
| 75 | verbosity += 9; try { |
| 76 | // left blank for debugging |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 77 | } finally { printCounts(); verbosity -= 9; } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static final int MAX_ARG_INCREASE = 3; |
| 81 | |
| 82 | public MethodHandlesTest() { |
| 83 | } |
| 84 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 85 | String testName; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 86 | static int allPosTests, allNegTests; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 87 | int posTests, negTests; |
| 88 | @After |
| 89 | public void printCounts() { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 90 | if (verbosity >= 2 && (posTests | negTests) != 0) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 91 | System.out.println(); |
| 92 | if (posTests != 0) System.out.println("=== "+testName+": "+posTests+" positive test cases run"); |
| 93 | if (negTests != 0) System.out.println("=== "+testName+": "+negTests+" negative test cases run"); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 94 | allPosTests += posTests; |
| 95 | allNegTests += negTests; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 96 | posTests = negTests = 0; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | void countTest(boolean positive) { |
| 100 | if (positive) ++posTests; |
| 101 | else ++negTests; |
| 102 | } |
| 103 | void countTest() { countTest(true); } |
| 104 | void startTest(String name) { |
| 105 | if (testName != null) printCounts(); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 106 | if (verbosity >= 1) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 107 | System.out.println(name); |
| 108 | posTests = negTests = 0; |
| 109 | testName = name; |
| 110 | } |
| 111 | |
| 112 | @BeforeClass |
| 113 | public static void setUpClass() throws Exception { |
| 114 | calledLog.clear(); |
| 115 | calledLog.add(null); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 116 | nextArgVal = INITIAL_ARG_VAL; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @AfterClass |
| 120 | public static void tearDownClass() throws Exception { |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 121 | int posTests = allPosTests, negTests = allNegTests; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 122 | if (verbosity >= 0 && (posTests | negTests) != 0) { |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 123 | System.out.println(); |
| 124 | if (posTests != 0) System.out.println("=== "+posTests+" total positive test cases"); |
| 125 | if (negTests != 0) System.out.println("=== "+negTests+" total negative test cases"); |
| 126 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 127 | } |
| 128 | |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 129 | static List<Object> calledLog = new ArrayList<>(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 130 | static Object logEntry(String name, Object... args) { |
| 131 | return Arrays.asList(name, Arrays.asList(args)); |
| 132 | } |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 133 | public static Object called(String name, Object... args) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 134 | Object entry = logEntry(name, args); |
| 135 | calledLog.add(entry); |
| 136 | return entry; |
| 137 | } |
| 138 | static void assertCalled(String name, Object... args) { |
| 139 | Object expected = logEntry(name, args); |
| 140 | Object actual = calledLog.get(calledLog.size() - 1); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 141 | if (expected.equals(actual) && verbosity < 9) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 142 | System.out.println("assertCalled "+name+":"); |
jrose | b5cd391 | 2013-10-05 05:30:38 -0700 | [diff] [blame^] | 143 | System.out.println("expected: "+deepToString(expected)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 144 | System.out.println("actual: "+actual); |
| 145 | System.out.println("ex. types: "+getClasses(expected)); |
| 146 | System.out.println("act. types: "+getClasses(actual)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 147 | assertEquals("previous method call", expected, actual); |
| 148 | } |
| 149 | static void printCalled(MethodHandle target, String name, Object... args) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 150 | if (verbosity >= 3) |
jrose | b5cd391 | 2013-10-05 05:30:38 -0700 | [diff] [blame^] | 151 | System.out.println("calling MH="+target+" to "+name+deepToString(args)); |
| 152 | } |
| 153 | static String deepToString(Object x) { |
| 154 | if (x == null) return "null"; |
| 155 | if (x instanceof Collection) |
| 156 | x = ((Collection)x).toArray(); |
| 157 | if (x instanceof Object[]) { |
| 158 | Object[] ax = (Object[]) x; |
| 159 | ax = Arrays.copyOf(ax, ax.length, Object[].class); |
| 160 | for (int i = 0; i < ax.length; i++) |
| 161 | ax[i] = deepToString(ax[i]); |
| 162 | x = Arrays.deepToString(ax); |
| 163 | } |
| 164 | if (x.getClass().isArray()) |
| 165 | try { |
| 166 | x = Arrays.class.getMethod("toString", x.getClass()).invoke(null, x); |
| 167 | } catch (ReflectiveOperationException ex) { throw new Error(ex); } |
| 168 | assert(!(x instanceof Object[])); |
| 169 | return x.toString(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static Object castToWrapper(Object value, Class<?> dst) { |
| 173 | Object wrap = null; |
| 174 | if (value instanceof Number) |
| 175 | wrap = castToWrapperOrNull(((Number)value).longValue(), dst); |
| 176 | if (value instanceof Character) |
| 177 | wrap = castToWrapperOrNull((char)(Character)value, dst); |
| 178 | if (wrap != null) return wrap; |
| 179 | return dst.cast(value); |
| 180 | } |
| 181 | |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 182 | @SuppressWarnings("cast") // primitive cast to (long) is part of the pattern |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 183 | static Object castToWrapperOrNull(long value, Class<?> dst) { |
| 184 | if (dst == int.class || dst == Integer.class) |
| 185 | return (int)(value); |
| 186 | if (dst == long.class || dst == Long.class) |
| 187 | return (long)(value); |
| 188 | if (dst == char.class || dst == Character.class) |
| 189 | return (char)(value); |
| 190 | if (dst == short.class || dst == Short.class) |
| 191 | return (short)(value); |
| 192 | if (dst == float.class || dst == Float.class) |
| 193 | return (float)(value); |
| 194 | if (dst == double.class || dst == Double.class) |
| 195 | return (double)(value); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 196 | if (dst == byte.class || dst == Byte.class) |
| 197 | return (byte)(value); |
| 198 | if (dst == boolean.class || dst == boolean.class) |
| 199 | return ((value % 29) & 1) == 0; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 200 | return null; |
| 201 | } |
| 202 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 203 | static final int ONE_MILLION = (1000*1000), // first int value |
| 204 | TEN_BILLION = (10*1000*1000*1000), // scale factor to reach upper 32 bits |
| 205 | INITIAL_ARG_VAL = ONE_MILLION << 1; // <<1 makes space for sign bit; |
| 206 | static long nextArgVal; |
| 207 | static long nextArg(boolean moreBits) { |
| 208 | long val = nextArgVal++; |
| 209 | long sign = -(val & 1); // alternate signs |
| 210 | val >>= 1; |
| 211 | if (moreBits) |
| 212 | // Guarantee some bits in the high word. |
| 213 | // In any case keep the decimal representation simple-looking, |
| 214 | // with lots of zeroes, so as not to make the printed decimal |
| 215 | // strings unnecessarily noisy. |
| 216 | val += (val % ONE_MILLION) * TEN_BILLION; |
| 217 | return val ^ sign; |
| 218 | } |
| 219 | static int nextArg() { |
| 220 | // Produce a 32-bit result something like ONE_MILLION+(smallint). |
| 221 | // Example: 1_000_042. |
| 222 | return (int) nextArg(false); |
| 223 | } |
| 224 | static long nextArg(Class<?> kind) { |
| 225 | if (kind == long.class || kind == Long.class || |
| 226 | kind == double.class || kind == Double.class) |
| 227 | // produce a 64-bit result something like |
| 228 | // ((TEN_BILLION+1) * (ONE_MILLION+(smallint))) |
| 229 | // Example: 10_000_420_001_000_042. |
| 230 | return nextArg(true); |
| 231 | return (long) nextArg(); |
| 232 | } |
| 233 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 234 | static Object randomArg(Class<?> param) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 235 | Object wrap = castToWrapperOrNull(nextArg(param), param); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 236 | if (wrap != null) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 237 | return wrap; |
| 238 | } |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 239 | // import sun.invoke.util.Wrapper; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 240 | // Wrapper wrap = Wrapper.forBasicType(dst); |
| 241 | // if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst)) |
| 242 | // wrap = Wrapper.forWrapperType(dst); |
| 243 | // if (wrap != Wrapper.OBJECT) |
| 244 | // return wrap.wrap(nextArg++); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 245 | if (param.isInterface()) { |
| 246 | for (Class<?> c : param.getClasses()) { |
| 247 | if (param.isAssignableFrom(c) && !c.isInterface()) |
| 248 | { param = c; break; } |
| 249 | } |
| 250 | } |
jrose | b5cd391 | 2013-10-05 05:30:38 -0700 | [diff] [blame^] | 251 | if (param.isArray()) { |
| 252 | Class<?> ctype = param.getComponentType(); |
| 253 | Object arg = Array.newInstance(ctype, 2); |
| 254 | Array.set(arg, 0, randomArg(ctype)); |
| 255 | return arg; |
| 256 | } |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 257 | if (param.isInterface() && param.isAssignableFrom(List.class)) |
| 258 | return Arrays.asList("#"+nextArg()); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 259 | if (param.isInterface() || param.isAssignableFrom(String.class)) |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 260 | return "#"+nextArg(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 261 | else |
| 262 | try { |
| 263 | return param.newInstance(); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 264 | } catch (InstantiationException | IllegalAccessException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 265 | } |
| 266 | return null; // random class not Object, String, Integer, etc. |
| 267 | } |
| 268 | static Object[] randomArgs(Class<?>... params) { |
| 269 | Object[] args = new Object[params.length]; |
| 270 | for (int i = 0; i < args.length; i++) |
| 271 | args[i] = randomArg(params[i]); |
| 272 | return args; |
| 273 | } |
| 274 | static Object[] randomArgs(int nargs, Class<?> param) { |
| 275 | Object[] args = new Object[nargs]; |
| 276 | for (int i = 0; i < args.length; i++) |
| 277 | args[i] = randomArg(param); |
| 278 | return args; |
| 279 | } |
| 280 | |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 281 | @SafeVarargs @SuppressWarnings("varargs") |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 282 | static <T, E extends T> T[] array(Class<T[]> atype, E... a) { |
| 283 | return Arrays.copyOf(a, a.length, atype); |
| 284 | } |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 285 | @SafeVarargs @SuppressWarnings("varargs") |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 286 | static <T> T[] cat(T[] a, T... b) { |
| 287 | int alen = a.length, blen = b.length; |
| 288 | if (blen == 0) return a; |
| 289 | T[] c = Arrays.copyOf(a, alen + blen); |
| 290 | System.arraycopy(b, 0, c, alen, blen); |
| 291 | return c; |
| 292 | } |
| 293 | static Integer[] boxAll(int... vx) { |
| 294 | Integer[] res = new Integer[vx.length]; |
| 295 | for (int i = 0; i < res.length; i++) { |
| 296 | res[i] = vx[i]; |
| 297 | } |
| 298 | return res; |
| 299 | } |
| 300 | static Object getClasses(Object x) { |
| 301 | if (x == null) return x; |
| 302 | if (x instanceof String) return x; // keep the name |
| 303 | if (x instanceof List) { |
| 304 | // recursively report classes of the list elements |
| 305 | Object[] xa = ((List)x).toArray(); |
| 306 | for (int i = 0; i < xa.length; i++) |
| 307 | xa[i] = getClasses(xa[i]); |
| 308 | return Arrays.asList(xa); |
| 309 | } |
| 310 | return x.getClass().getSimpleName(); |
| 311 | } |
| 312 | |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 313 | /** Return lambda(arg...[arity]) { new Object[]{ arg... } } */ |
| 314 | static MethodHandle varargsList(int arity) { |
| 315 | return ValueConversions.varargsList(arity); |
| 316 | } |
| 317 | /** Return lambda(arg...[arity]) { Arrays.asList(arg...) } */ |
| 318 | static MethodHandle varargsArray(int arity) { |
| 319 | return ValueConversions.varargsArray(arity); |
| 320 | } |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 321 | static MethodHandle varargsArray(Class<?> arrayType, int arity) { |
| 322 | return ValueConversions.varargsArray(arrayType, arity); |
| 323 | } |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 324 | /** Variation of varargsList, but with the given rtype. */ |
| 325 | static MethodHandle varargsList(int arity, Class<?> rtype) { |
| 326 | MethodHandle list = varargsList(arity); |
| 327 | MethodType listType = list.type().changeReturnType(rtype); |
| 328 | if (List.class.isAssignableFrom(rtype) || rtype == void.class || rtype == Object.class) { |
| 329 | // OK |
| 330 | } else if (rtype.isAssignableFrom(String.class)) { |
| 331 | if (LIST_TO_STRING == null) |
| 332 | try { |
| 333 | LIST_TO_STRING = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToString", |
| 334 | MethodType.methodType(String.class, List.class)); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 335 | } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); } |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 336 | list = MethodHandles.filterReturnValue(list, LIST_TO_STRING); |
| 337 | } else if (rtype.isPrimitive()) { |
| 338 | if (LIST_TO_INT == null) |
| 339 | try { |
| 340 | LIST_TO_INT = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToInt", |
| 341 | MethodType.methodType(int.class, List.class)); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 342 | } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); } |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 343 | list = MethodHandles.filterReturnValue(list, LIST_TO_INT); |
| 344 | list = MethodHandles.explicitCastArguments(list, listType); |
| 345 | } else { |
| 346 | throw new RuntimeException("varargsList: "+rtype); |
| 347 | } |
| 348 | return list.asType(listType); |
| 349 | } |
| 350 | private static MethodHandle LIST_TO_STRING, LIST_TO_INT; |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 351 | private static String listToString(List<?> x) { return x.toString(); } |
| 352 | private static int listToInt(List<?> x) { return x.toString().hashCode(); } |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 353 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 354 | static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) { |
| 355 | return changeArgTypes(target, 0, 999, argType); |
| 356 | } |
| 357 | static MethodHandle changeArgTypes(MethodHandle target, |
| 358 | int beg, int end, Class<?> argType) { |
| 359 | MethodType targetType = target.type(); |
| 360 | end = Math.min(end, targetType.parameterCount()); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 361 | ArrayList<Class<?>> argTypes = new ArrayList<>(targetType.parameterList()); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 362 | Collections.fill(argTypes.subList(beg, end), argType); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 363 | MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 364 | return target.asType(ttype2); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 365 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 366 | static MethodHandle addTrailingArgs(MethodHandle target, int nargs, Class<?> argClass) { |
| 367 | int targetLen = target.type().parameterCount(); |
| 368 | int extra = (nargs - targetLen); |
| 369 | if (extra <= 0) return target; |
| 370 | List<Class<?>> fakeArgs = Collections.<Class<?>>nCopies(extra, argClass); |
| 371 | return MethodHandles.dropArguments(target, targetLen, fakeArgs); |
| 372 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 373 | |
| 374 | // This lookup is good for all members in and under MethodHandlesTest. |
| 375 | static final Lookup PRIVATE = MethodHandles.lookup(); |
| 376 | // This lookup is good for package-private members but not private ones. |
| 377 | static final Lookup PACKAGE = PackageSibling.lookup(); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 378 | // This lookup is good for public members and protected members of PubExample |
| 379 | static final Lookup SUBCLASS = RemoteExample.lookup(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 380 | // This lookup is good only for public members. |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 381 | static final Lookup PUBLIC = MethodHandles.publicLookup(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 382 | |
| 383 | // Subject methods... |
| 384 | static class Example implements IntExample { |
| 385 | final String name; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 386 | public Example() { name = "Example#"+nextArg(); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 387 | protected Example(String name) { this.name = name; } |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 388 | @SuppressWarnings("LeakingThisInConstructor") |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 389 | protected Example(int x) { this(); called("protected <init>", this, x); } |
| 390 | @Override public String toString() { return name; } |
| 391 | |
| 392 | public void v0() { called("v0", this); } |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 393 | protected void pro_v0() { called("pro_v0", this); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 394 | void pkg_v0() { called("pkg_v0", this); } |
| 395 | private void pri_v0() { called("pri_v0", this); } |
| 396 | public static void s0() { called("s0"); } |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 397 | protected static void pro_s0() { called("pro_s0"); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 398 | static void pkg_s0() { called("pkg_s0"); } |
| 399 | private static void pri_s0() { called("pri_s0"); } |
| 400 | |
| 401 | public Object v1(Object x) { return called("v1", this, x); } |
| 402 | public Object v2(Object x, Object y) { return called("v2", this, x, y); } |
| 403 | public Object v2(Object x, int y) { return called("v2", this, x, y); } |
| 404 | public Object v2(int x, Object y) { return called("v2", this, x, y); } |
| 405 | public Object v2(int x, int y) { return called("v2", this, x, y); } |
| 406 | public static Object s1(Object x) { return called("s1", x); } |
| 407 | public static Object s2(int x) { return called("s2", x); } |
| 408 | public static Object s3(long x) { return called("s3", x); } |
| 409 | public static Object s4(int x, int y) { return called("s4", x, y); } |
| 410 | public static Object s5(long x, int y) { return called("s5", x, y); } |
| 411 | public static Object s6(int x, long y) { return called("s6", x, y); } |
| 412 | public static Object s7(float x, double y) { return called("s7", x, y); } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 413 | |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 414 | // for testing findConstructor: |
| 415 | public Example(String x, int y) { this.name = x+y; called("Example.<init>", x, y); } |
| 416 | public Example(int x, String y) { this.name = x+y; called("Example.<init>", x, y); } |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 417 | public Example(int x, int y) { this.name = x+""+y; called("Example.<init>", x, y); } |
| 418 | public Example(int x, long y) { this.name = x+""+y; called("Example.<init>", x, y); } |
| 419 | public Example(int x, float y) { this.name = x+""+y; called("Example.<init>", x, y); } |
| 420 | public Example(int x, double y) { this.name = x+""+y; called("Example.<init>", x, y); } |
| 421 | public Example(int x, int y, int z) { this.name = x+""+y+""+z; called("Example.<init>", x, y, z); } |
| 422 | public Example(int x, int y, int z, int a) { this.name = x+""+y+""+z+""+a; called("Example.<init>", x, y, z, a); } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 423 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 424 | static final Lookup EXAMPLE = MethodHandles.lookup(); // for testing findSpecial |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 425 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 426 | static final Lookup EXAMPLE = Example.EXAMPLE; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 427 | public static class PubExample extends Example { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 428 | public PubExample() { this("PubExample"); } |
| 429 | protected PubExample(String prefix) { super(prefix+"#"+nextArg()); } |
| 430 | protected void pro_v0() { called("Pub/pro_v0", this); } |
| 431 | protected static void pro_s0() { called("Pub/pro_s0"); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 432 | } |
| 433 | static class SubExample extends Example { |
| 434 | @Override public void v0() { called("Sub/v0", this); } |
| 435 | @Override void pkg_v0() { called("Sub/pkg_v0", this); } |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 436 | @SuppressWarnings("LeakingThisInConstructor") |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 437 | private SubExample(int x) { called("<init>", this, x); } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 438 | public SubExample() { super("SubExample#"+nextArg()); } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 439 | } |
| 440 | public static interface IntExample { |
| 441 | public void v0(); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 442 | public static class Impl implements IntExample { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 443 | public void v0() { called("Int/v0", this); } |
| 444 | final String name; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 445 | public Impl() { name = "Impl#"+nextArg(); } |
| 446 | @Override public String toString() { return name; } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 449 | static interface SubIntExample extends IntExample { } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 450 | |
| 451 | static final Object[][][] ACCESS_CASES = { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 452 | { { false, PUBLIC }, { false, SUBCLASS }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false |
| 453 | { { false, PUBLIC }, { false, SUBCLASS }, { false, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[1]: only PRIVATE |
| 454 | { { false, PUBLIC }, { false, SUBCLASS }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[2]: PUBLIC false |
| 455 | { { false, PUBLIC }, { true, SUBCLASS }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[3]: subclass OK |
| 456 | { { true, PUBLIC }, { true, SUBCLASS }, { true, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[4]: all true |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 457 | }; |
| 458 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 459 | static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) { |
| 460 | Object[][] cases; |
| 461 | if (name.contains("pri_") || isSpecial) { |
| 462 | cases = ACCESS_CASES[1]; // PRIVATE only |
| 463 | } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) { |
| 464 | cases = ACCESS_CASES[2]; // not PUBLIC |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 465 | } else if (name.contains("pro_")) { |
| 466 | cases = ACCESS_CASES[3]; // PUBLIC class, protected member |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 467 | } else { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 468 | assertTrue(name.indexOf('_') < 0 || name.contains("fin_")); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 469 | boolean pubc = Modifier.isPublic(defc.getModifiers()); |
| 470 | if (pubc) |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 471 | cases = ACCESS_CASES[4]; // all access levels |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 472 | else |
| 473 | cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 474 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 475 | if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE) |
| 476 | cases = Arrays.copyOfRange(cases, 0, cases.length-1); |
| 477 | return cases; |
| 478 | } |
| 479 | static Object[][] accessCases(Class<?> defc, String name) { |
| 480 | return accessCases(defc, name, false); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 481 | } |
| 482 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 483 | static Lookup maybeMoveIn(Lookup lookup, Class<?> defc) { |
| 484 | if (lookup == PUBLIC || lookup == SUBCLASS || lookup == PACKAGE) |
| 485 | // external views stay external |
| 486 | return lookup; |
| 487 | return lookup.in(defc); |
| 488 | } |
| 489 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 490 | @Test |
| 491 | public void testFindStatic() throws Throwable { |
| 492 | if (CAN_SKIP_WORKING) return; |
| 493 | startTest("findStatic"); |
| 494 | testFindStatic(PubExample.class, void.class, "s0"); |
| 495 | testFindStatic(Example.class, void.class, "s0"); |
| 496 | testFindStatic(Example.class, void.class, "pkg_s0"); |
| 497 | testFindStatic(Example.class, void.class, "pri_s0"); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 498 | testFindStatic(Example.class, void.class, "pro_s0"); |
| 499 | testFindStatic(PubExample.class, void.class, "Pub/pro_s0"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 500 | |
| 501 | testFindStatic(Example.class, Object.class, "s1", Object.class); |
| 502 | testFindStatic(Example.class, Object.class, "s2", int.class); |
| 503 | testFindStatic(Example.class, Object.class, "s3", long.class); |
| 504 | testFindStatic(Example.class, Object.class, "s4", int.class, int.class); |
| 505 | testFindStatic(Example.class, Object.class, "s5", long.class, int.class); |
| 506 | testFindStatic(Example.class, Object.class, "s6", int.class, long.class); |
| 507 | testFindStatic(Example.class, Object.class, "s7", float.class, double.class); |
| 508 | |
| 509 | testFindStatic(false, PRIVATE, Example.class, void.class, "bogus"); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 510 | testFindStatic(false, PRIVATE, Example.class, void.class, "v0"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 514 | for (Object[] ac : accessCases(defc, name)) { |
| 515 | testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params); |
| 516 | } |
| 517 | } |
| 518 | void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 519 | testFindStatic(true, lookup, defc, ret, name, params); |
| 520 | } |
| 521 | void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 522 | countTest(positive); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 523 | String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 524 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 525 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 526 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 527 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 528 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 529 | target = maybeMoveIn(lookup, defc).findStatic(defc, methodName, type); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 530 | } catch (ReflectiveOperationException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 531 | noAccess = ex; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 532 | if (verbosity >= 5) ex.printStackTrace(System.out); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 533 | if (name.contains("bogus")) |
| 534 | assertTrue(noAccess instanceof NoSuchMethodException); |
| 535 | else |
| 536 | assertTrue(noAccess instanceof IllegalAccessException); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 537 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 538 | if (verbosity >= 3) |
| 539 | System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 540 | +(noAccess == null ? "" : " !! "+noAccess)); |
| 541 | if (positive && noAccess != null) throw noAccess; |
| 542 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 543 | if (!positive) return; // negative test failed as expected |
| 544 | assertEquals(type, target.type()); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 545 | assertNameStringContains(target, methodName); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 546 | Object[] args = randomArgs(params); |
| 547 | printCalled(target, name, args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 548 | target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 549 | assertCalled(name, args); |
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 | |
jrose | 7301626 | 2011-05-17 19:48:19 -0700 | [diff] [blame] | 554 | static final boolean DEBUG_METHOD_HANDLE_NAMES = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES"); |
| 555 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 556 | // rough check of name string |
jrose | 7301626 | 2011-05-17 19:48:19 -0700 | [diff] [blame] | 557 | static void assertNameStringContains(MethodHandle x, String s) { |
| 558 | if (!DEBUG_METHOD_HANDLE_NAMES) { |
| 559 | // ignore s |
| 560 | assertEquals("MethodHandle"+x.type(), x.toString()); |
| 561 | return; |
| 562 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 563 | if (x.toString().contains(s)) return; |
| 564 | assertEquals(s, x); |
| 565 | } |
| 566 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 567 | @Test |
| 568 | public void testFindVirtual() throws Throwable { |
| 569 | if (CAN_SKIP_WORKING) return; |
| 570 | startTest("findVirtual"); |
| 571 | testFindVirtual(Example.class, void.class, "v0"); |
| 572 | testFindVirtual(Example.class, void.class, "pkg_v0"); |
| 573 | testFindVirtual(Example.class, void.class, "pri_v0"); |
| 574 | testFindVirtual(Example.class, Object.class, "v1", Object.class); |
| 575 | testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class); |
| 576 | testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class); |
| 577 | testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class); |
| 578 | testFindVirtual(Example.class, Object.class, "v2", int.class, int.class); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 579 | testFindVirtual(Example.class, void.class, "pro_v0"); |
| 580 | testFindVirtual(PubExample.class, void.class, "Pub/pro_v0"); |
| 581 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 582 | testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus"); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 583 | testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "s0"); |
| 584 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 585 | // test dispatch |
| 586 | testFindVirtual(SubExample.class, SubExample.class, void.class, "Sub/v0"); |
| 587 | testFindVirtual(SubExample.class, Example.class, void.class, "Sub/v0"); |
| 588 | testFindVirtual(SubExample.class, IntExample.class, void.class, "Sub/v0"); |
| 589 | testFindVirtual(SubExample.class, SubExample.class, void.class, "Sub/pkg_v0"); |
| 590 | testFindVirtual(SubExample.class, Example.class, void.class, "Sub/pkg_v0"); |
| 591 | testFindVirtual(Example.class, IntExample.class, void.class, "v0"); |
| 592 | testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0"); |
| 593 | } |
| 594 | |
jrose | b5cd391 | 2013-10-05 05:30:38 -0700 | [diff] [blame^] | 595 | @Test |
| 596 | public void testFindVirtualClone() throws Throwable { |
| 597 | // test some ad hoc system methods |
| 598 | testFindVirtual(false, PUBLIC, Object.class, Object.class, "clone"); |
| 599 | testFindVirtual(true, PUBLIC, Object[].class, Object.class, "clone"); |
| 600 | testFindVirtual(true, PUBLIC, int[].class, Object.class, "clone"); |
| 601 | for (Class<?> cls : new Class<?>[]{ boolean[].class, long[].class, float[].class, char[].class }) |
| 602 | testFindVirtual(true, PUBLIC, cls, Object.class, "clone"); |
| 603 | } |
| 604 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 605 | void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 606 | Class<?> rcvc = defc; |
| 607 | testFindVirtual(rcvc, defc, ret, name, params); |
| 608 | } |
| 609 | void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 610 | for (Object[] ac : accessCases(defc, name)) { |
| 611 | testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params); |
| 612 | } |
| 613 | } |
| 614 | void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 615 | testFindVirtual(true, lookup, rcvc, defc, ret, name, params); |
| 616 | } |
jrose | b5cd391 | 2013-10-05 05:30:38 -0700 | [diff] [blame^] | 617 | void testFindVirtual(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 618 | testFindVirtual(positive, lookup, defc, defc, ret, name, params); |
| 619 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 620 | void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 621 | countTest(positive); |
| 622 | String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 623 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 624 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 625 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 626 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 627 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 628 | target = maybeMoveIn(lookup, defc).findVirtual(defc, methodName, type); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 629 | } catch (ReflectiveOperationException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 630 | noAccess = ex; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 631 | if (verbosity >= 5) ex.printStackTrace(System.out); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 632 | if (name.contains("bogus")) |
| 633 | assertTrue(noAccess instanceof NoSuchMethodException); |
| 634 | else |
| 635 | assertTrue(noAccess instanceof IllegalAccessException); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 636 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 637 | if (verbosity >= 3) |
| 638 | System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 639 | +(noAccess == null ? "" : " !! "+noAccess)); |
| 640 | if (positive && noAccess != null) throw noAccess; |
| 641 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 642 | if (!positive) return; // negative test failed as expected |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 643 | Class<?> selfc = defc; |
| 644 | // predict receiver type narrowing: |
| 645 | if (lookup == SUBCLASS && |
| 646 | name.contains("pro_") && |
| 647 | selfc.isAssignableFrom(lookup.lookupClass())) { |
| 648 | selfc = lookup.lookupClass(); |
| 649 | if (name.startsWith("Pub/")) name = "Rem/"+name.substring(4); |
| 650 | } |
| 651 | Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)selfc), params); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 652 | MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 653 | assertEquals(typeWithSelf, target.type()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 654 | assertNameStringContains(target, methodName); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 655 | Object[] argsWithSelf = randomArgs(paramsWithSelf); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 656 | if (selfc.isAssignableFrom(rcvc) && rcvc != selfc) argsWithSelf[0] = randomArg(rcvc); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 657 | printCalled(target, name, argsWithSelf); |
jrose | b5cd391 | 2013-10-05 05:30:38 -0700 | [diff] [blame^] | 658 | Object res = target.invokeWithArguments(argsWithSelf); |
| 659 | if (Example.class.isAssignableFrom(defc) || IntExample.class.isAssignableFrom(defc)) { |
| 660 | assertCalled(name, argsWithSelf); |
| 661 | } else if (name.equals("clone")) { |
| 662 | // Ad hoc method call outside Example. For Object[].clone. |
| 663 | printCalled(target, name, argsWithSelf); |
| 664 | assertEquals(MethodType.methodType(Object.class, rcvc), target.type()); |
| 665 | Object orig = argsWithSelf[0]; |
| 666 | assertEquals(orig.getClass(), res.getClass()); |
| 667 | if (res instanceof Object[]) |
| 668 | assertArrayEquals((Object[])res, (Object[])argsWithSelf[0]); |
| 669 | assert(Arrays.deepEquals(new Object[]{res}, new Object[]{argsWithSelf[0]})); |
| 670 | } else { |
| 671 | assert(false) : Arrays.asList(positive, lookup, rcvc, defc, ret, name, deepToString(params)); |
| 672 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 673 | if (verbosity >= 1) |
| 674 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | @Test |
| 678 | public void testFindSpecial() throws Throwable { |
| 679 | if (CAN_SKIP_WORKING) return; |
| 680 | startTest("findSpecial"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 681 | testFindSpecial(SubExample.class, Example.class, void.class, "v0"); |
| 682 | testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0"); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 683 | testFindSpecial(RemoteExample.class, PubExample.class, void.class, "Pub/pro_v0"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 684 | // Do some negative testing: |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 685 | testFindSpecial(false, EXAMPLE, SubExample.class, Example.class, void.class, "bogus"); |
| 686 | testFindSpecial(false, PRIVATE, SubExample.class, Example.class, void.class, "bogus"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 687 | for (Lookup lookup : new Lookup[]{ PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) { |
| 688 | testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0"); |
| 689 | testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class); |
| 690 | testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 691 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 692 | } |
| 693 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 694 | void testFindSpecial(Class<?> specialCaller, |
| 695 | Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 696 | if (specialCaller == RemoteExample.class) { |
| 697 | testFindSpecial(false, EXAMPLE, specialCaller, defc, ret, name, params); |
| 698 | testFindSpecial(false, PRIVATE, specialCaller, defc, ret, name, params); |
| 699 | testFindSpecial(false, PACKAGE, specialCaller, defc, ret, name, params); |
| 700 | testFindSpecial(true, SUBCLASS, specialCaller, defc, ret, name, params); |
| 701 | testFindSpecial(false, PUBLIC, specialCaller, defc, ret, name, params); |
| 702 | return; |
| 703 | } |
| 704 | testFindSpecial(true, EXAMPLE, specialCaller, defc, ret, name, params); |
| 705 | testFindSpecial(true, PRIVATE, specialCaller, defc, ret, name, params); |
| 706 | testFindSpecial(false, PACKAGE, specialCaller, defc, ret, name, params); |
| 707 | testFindSpecial(false, SUBCLASS, specialCaller, defc, ret, name, params); |
| 708 | testFindSpecial(false, PUBLIC, specialCaller, defc, ret, name, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 709 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 710 | void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller, |
| 711 | Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 712 | countTest(positive); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 713 | String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 714 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 715 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 716 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 717 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 718 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 719 | if (verbosity >= 5) System.out.println(" lookup => "+maybeMoveIn(lookup, specialCaller)); |
| 720 | target = maybeMoveIn(lookup, specialCaller).findSpecial(defc, methodName, type, specialCaller); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 721 | } catch (ReflectiveOperationException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 722 | noAccess = ex; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 723 | if (verbosity >= 5) ex.printStackTrace(System.out); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 724 | if (name.contains("bogus")) |
| 725 | assertTrue(noAccess instanceof NoSuchMethodException); |
| 726 | else |
| 727 | assertTrue(noAccess instanceof IllegalAccessException); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 728 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 729 | if (verbosity >= 3) |
| 730 | System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target |
| 731 | +(target == null ? "" : target.type()) |
| 732 | +(noAccess == null ? "" : " !! "+noAccess)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 733 | if (positive && noAccess != null) throw noAccess; |
| 734 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 735 | if (!positive) return; // negative test failed as expected |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 736 | assertEquals(specialCaller, target.type().parameterType(0)); |
| 737 | assertEquals(type, target.type().dropParameterTypes(0,1)); |
| 738 | Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 739 | MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 740 | assertNameStringContains(target, methodName); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 741 | Object[] args = randomArgs(paramsWithSelf); |
| 742 | printCalled(target, name, args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 743 | target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 744 | assertCalled(name, args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | @Test |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 748 | public void testFindConstructor() throws Throwable { |
| 749 | if (CAN_SKIP_WORKING) return; |
| 750 | startTest("findConstructor"); |
| 751 | testFindConstructor(true, EXAMPLE, Example.class); |
| 752 | testFindConstructor(true, EXAMPLE, Example.class, int.class); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 753 | testFindConstructor(true, EXAMPLE, Example.class, int.class, int.class); |
| 754 | testFindConstructor(true, EXAMPLE, Example.class, int.class, long.class); |
| 755 | testFindConstructor(true, EXAMPLE, Example.class, int.class, float.class); |
| 756 | testFindConstructor(true, EXAMPLE, Example.class, int.class, double.class); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 757 | testFindConstructor(true, EXAMPLE, Example.class, String.class); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 758 | testFindConstructor(true, EXAMPLE, Example.class, int.class, int.class, int.class); |
| 759 | testFindConstructor(true, EXAMPLE, Example.class, int.class, int.class, int.class, int.class); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 760 | } |
| 761 | void testFindConstructor(boolean positive, Lookup lookup, |
| 762 | Class<?> defc, Class<?>... params) throws Throwable { |
| 763 | countTest(positive); |
| 764 | MethodType type = MethodType.methodType(void.class, params); |
| 765 | MethodHandle target = null; |
| 766 | Exception noAccess = null; |
| 767 | try { |
| 768 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type); |
| 769 | target = lookup.findConstructor(defc, type); |
| 770 | } catch (ReflectiveOperationException ex) { |
| 771 | noAccess = ex; |
| 772 | assertTrue(noAccess instanceof IllegalAccessException); |
| 773 | } |
| 774 | if (verbosity >= 3) |
| 775 | System.out.println("findConstructor "+defc.getName()+".<init>/"+type+" => "+target |
| 776 | +(target == null ? "" : target.type()) |
| 777 | +(noAccess == null ? "" : " !! "+noAccess)); |
| 778 | if (positive && noAccess != null) throw noAccess; |
| 779 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 780 | if (!positive) return; // negative test failed as expected |
| 781 | assertEquals(type.changeReturnType(defc), target.type()); |
| 782 | Object[] args = randomArgs(params); |
| 783 | printCalled(target, defc.getSimpleName(), args); |
| 784 | Object obj = target.invokeWithArguments(args); |
| 785 | if (!(defc == Example.class && params.length < 2)) |
| 786 | assertCalled(defc.getSimpleName()+".<init>", args); |
| 787 | assertTrue("instance of "+defc.getName(), defc.isInstance(obj)); |
| 788 | } |
| 789 | |
| 790 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 791 | public void testBind() throws Throwable { |
| 792 | if (CAN_SKIP_WORKING) return; |
| 793 | startTest("bind"); |
| 794 | testBind(Example.class, void.class, "v0"); |
| 795 | testBind(Example.class, void.class, "pkg_v0"); |
| 796 | testBind(Example.class, void.class, "pri_v0"); |
| 797 | testBind(Example.class, Object.class, "v1", Object.class); |
| 798 | testBind(Example.class, Object.class, "v2", Object.class, Object.class); |
| 799 | testBind(Example.class, Object.class, "v2", Object.class, int.class); |
| 800 | testBind(Example.class, Object.class, "v2", int.class, Object.class); |
| 801 | testBind(Example.class, Object.class, "v2", int.class, int.class); |
| 802 | testBind(false, PRIVATE, Example.class, void.class, "bogus"); |
| 803 | testBind(SubExample.class, void.class, "Sub/v0"); |
| 804 | testBind(SubExample.class, void.class, "Sub/pkg_v0"); |
| 805 | testBind(IntExample.Impl.class, void.class, "Int/v0"); |
| 806 | } |
| 807 | |
| 808 | void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 809 | for (Object[] ac : accessCases(defc, name)) { |
| 810 | testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 815 | countTest(positive); |
| 816 | String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 817 | MethodType type = MethodType.methodType(ret, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 818 | Object receiver = randomArg(defc); |
| 819 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 820 | Exception noAccess = null; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 821 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 822 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 823 | target = maybeMoveIn(lookup, defc).bind(receiver, methodName, type); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 824 | } catch (ReflectiveOperationException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 825 | noAccess = ex; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 826 | if (verbosity >= 5) ex.printStackTrace(System.out); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 827 | if (name.contains("bogus")) |
| 828 | assertTrue(noAccess instanceof NoSuchMethodException); |
| 829 | else |
| 830 | assertTrue(noAccess instanceof IllegalAccessException); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 831 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 832 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 833 | System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target |
| 834 | +(noAccess == null ? "" : " !! "+noAccess)); |
| 835 | if (positive && noAccess != null) throw noAccess; |
| 836 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 837 | if (!positive) return; // negative test failed as expected |
| 838 | assertEquals(type, target.type()); |
| 839 | Object[] args = randomArgs(params); |
| 840 | printCalled(target, name, args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 841 | target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 842 | Object[] argsWithReceiver = cat(array(Object[].class, receiver), args); |
| 843 | assertCalled(name, argsWithReceiver); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 844 | if (verbosity >= 1) |
| 845 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | @Test |
| 849 | public void testUnreflect() throws Throwable { |
| 850 | if (CAN_SKIP_WORKING) return; |
| 851 | startTest("unreflect"); |
| 852 | testUnreflect(Example.class, true, void.class, "s0"); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 853 | testUnreflect(Example.class, true, void.class, "pro_s0"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 854 | testUnreflect(Example.class, true, void.class, "pkg_s0"); |
| 855 | testUnreflect(Example.class, true, void.class, "pri_s0"); |
| 856 | |
| 857 | testUnreflect(Example.class, true, Object.class, "s1", Object.class); |
| 858 | testUnreflect(Example.class, true, Object.class, "s2", int.class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 859 | testUnreflect(Example.class, true, Object.class, "s3", long.class); |
| 860 | testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class); |
| 861 | testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class); |
| 862 | testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 863 | |
| 864 | testUnreflect(Example.class, false, void.class, "v0"); |
| 865 | testUnreflect(Example.class, false, void.class, "pkg_v0"); |
| 866 | testUnreflect(Example.class, false, void.class, "pri_v0"); |
| 867 | testUnreflect(Example.class, false, Object.class, "v1", Object.class); |
| 868 | testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class); |
| 869 | testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class); |
| 870 | testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class); |
| 871 | testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 872 | |
| 873 | // Test a public final member in another package: |
| 874 | testUnreflect(RemoteExample.class, false, void.class, "Rem/fin_v0"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 878 | for (Object[] ac : accessCases(defc, name)) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 879 | 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] | 880 | } |
| 881 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 882 | void testUnreflect(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 883 | for (Object[] ac : accessCases(defc, name)) { |
| 884 | testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params); |
| 885 | } |
| 886 | } |
| 887 | void testUnreflectMaybeSpecial(Class<?> specialCaller, |
| 888 | boolean positive, Lookup lookup, |
| 889 | Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 890 | countTest(positive); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 891 | String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 892 | MethodType type = MethodType.methodType(ret, params); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 893 | Method rmethod = defc.getDeclaredMethod(methodName, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 894 | MethodHandle target = null; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 895 | Exception noAccess = null; |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 896 | boolean isStatic = (rcvc == null); |
| 897 | boolean isSpecial = (specialCaller != null); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 898 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 899 | if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type); |
| 900 | if (isSpecial) |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 901 | target = maybeMoveIn(lookup, specialCaller).unreflectSpecial(rmethod, specialCaller); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 902 | else |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 903 | target = maybeMoveIn(lookup, defc).unreflect(rmethod); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 904 | } catch (ReflectiveOperationException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 905 | noAccess = ex; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 906 | if (verbosity >= 5) ex.printStackTrace(System.out); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 907 | if (name.contains("bogus")) |
| 908 | assertTrue(noAccess instanceof NoSuchMethodException); |
| 909 | else |
| 910 | assertTrue(noAccess instanceof IllegalAccessException); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 911 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 912 | if (verbosity >= 3) |
| 913 | System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type |
| 914 | +(!isSpecial ? "" : " specialCaller="+specialCaller) |
| 915 | +( isStatic ? "" : " receiver="+rcvc) |
| 916 | +" => "+target |
| 917 | +(noAccess == null ? "" : " !! "+noAccess)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 918 | if (positive && noAccess != null) throw noAccess; |
| 919 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 920 | if (!positive) return; // negative test failed as expected |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 921 | assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers())); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 922 | Class<?>[] paramsMaybeWithSelf = params; |
| 923 | if (!isStatic) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 924 | paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 925 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 926 | MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 927 | if (isStatic) { |
| 928 | assertEquals(typeMaybeWithSelf, target.type()); |
| 929 | } else { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 930 | if (isSpecial) |
| 931 | assertEquals(specialCaller, target.type().parameterType(0)); |
| 932 | else |
| 933 | assertEquals(defc, target.type().parameterType(0)); |
| 934 | assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc)); |
| 935 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 936 | Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf); |
| 937 | printCalled(target, name, argsMaybeWithSelf); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 938 | target.invokeWithArguments(argsMaybeWithSelf); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 939 | assertCalled(name, argsMaybeWithSelf); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 940 | if (verbosity >= 1) |
| 941 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 942 | } |
| 943 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 944 | void testUnreflectSpecial(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable { |
| 945 | for (Object[] ac : accessCases(defc, name, true)) { |
| 946 | Class<?> specialCaller = rcvc; |
| 947 | testUnreflectMaybeSpecial(specialCaller, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 952 | public void testUnreflectSpecial() throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 953 | if (CAN_SKIP_WORKING) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 954 | startTest("unreflectSpecial"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 955 | testUnreflectSpecial(Example.class, Example.class, void.class, "v0"); |
| 956 | testUnreflectSpecial(Example.class, SubExample.class, void.class, "v0"); |
| 957 | testUnreflectSpecial(Example.class, Example.class, void.class, "pkg_v0"); |
| 958 | testUnreflectSpecial(Example.class, SubExample.class, void.class, "pkg_v0"); |
| 959 | testUnreflectSpecial(Example.class, Example.class, Object.class, "v2", int.class, int.class); |
| 960 | testUnreflectSpecial(Example.class, SubExample.class, Object.class, "v2", int.class, int.class); |
| 961 | testUnreflectMaybeSpecial(Example.class, false, PRIVATE, Example.class, Example.class, void.class, "s0"); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 962 | } |
| 963 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 964 | public static class HasFields { |
| 965 | boolean fZ = false; |
| 966 | byte fB = (byte)'B'; |
| 967 | short fS = (short)'S'; |
| 968 | char fC = 'C'; |
| 969 | int fI = 'I'; |
| 970 | long fJ = 'J'; |
| 971 | float fF = 'F'; |
| 972 | double fD = 'D'; |
| 973 | static boolean sZ = true; |
| 974 | static byte sB = 1+(byte)'B'; |
| 975 | static short sS = 1+(short)'S'; |
| 976 | static char sC = 1+'C'; |
| 977 | static int sI = 1+'I'; |
| 978 | static long sJ = 1+'J'; |
| 979 | static float sF = 1+'F'; |
| 980 | static double sD = 1+'D'; |
| 981 | |
| 982 | Object fL = 'L'; |
| 983 | String fR = "R"; |
| 984 | static Object sL = 'M'; |
| 985 | static String sR = "S"; |
| 986 | |
| 987 | static final Object[][] CASES; |
| 988 | static { |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 989 | ArrayList<Object[]> cases = new ArrayList<>(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 990 | Object types[][] = { |
| 991 | {'L',Object.class}, {'R',String.class}, |
| 992 | {'I',int.class}, {'J',long.class}, |
| 993 | {'F',float.class}, {'D',double.class}, |
| 994 | {'Z',boolean.class}, {'B',byte.class}, |
| 995 | {'S',short.class}, {'C',char.class}, |
| 996 | }; |
| 997 | HasFields fields = new HasFields(); |
| 998 | for (Object[] t : types) { |
| 999 | for (int kind = 0; kind <= 1; kind++) { |
| 1000 | boolean isStatic = (kind != 0); |
| 1001 | char btc = (Character)t[0]; |
| 1002 | String name = (isStatic ? "s" : "f") + btc; |
| 1003 | Class<?> type = (Class<?>) t[1]; |
| 1004 | Object value; |
| 1005 | Field field; |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1006 | try { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1007 | field = HasFields.class.getDeclaredField(name); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1008 | } catch (NoSuchFieldException | SecurityException ex) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1009 | throw new InternalError("no field HasFields."+name); |
| 1010 | } |
| 1011 | try { |
| 1012 | value = field.get(fields); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1013 | } catch (IllegalArgumentException | IllegalAccessException ex) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1014 | throw new InternalError("cannot fetch field HasFields."+name); |
| 1015 | } |
| 1016 | if (type == float.class) { |
| 1017 | float v = 'F'; |
| 1018 | if (isStatic) v++; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1019 | assertTrue(value.equals(v)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1020 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1021 | assertTrue(name.equals(field.getName())); |
| 1022 | assertTrue(type.equals(field.getType())); |
| 1023 | assertTrue(isStatic == (Modifier.isStatic(field.getModifiers()))); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1024 | cases.add(new Object[]{ field, value }); |
| 1025 | } |
| 1026 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1027 | cases.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class }); |
| 1028 | cases.add(new Object[]{ new Object[]{ true, HasFields.class, "bogus_sL", Object.class }, Error.class }); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1029 | CASES = cases.toArray(new Object[0][]); |
| 1030 | } |
| 1031 | } |
| 1032 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1033 | static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10, TEST_BOUND = 0x20, TEST_NPE = 0x40; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1034 | static boolean testModeMatches(int testMode, boolean isStatic) { |
| 1035 | switch (testMode) { |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1036 | case TEST_FIND_STATIC: return isStatic; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1037 | case TEST_FIND_FIELD: return !isStatic; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1038 | case TEST_UNREFLECT: return true; // unreflect matches both |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1039 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1040 | throw new InternalError("testMode="+testMode); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1043 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1044 | public void testUnreflectGetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1045 | if (CAN_SKIP_WORKING) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1046 | startTest("unreflectGetter"); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1047 | testGetter(TEST_UNREFLECT); |
| 1048 | } |
| 1049 | @Test |
| 1050 | public void testFindGetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1051 | if (CAN_SKIP_WORKING) return; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1052 | startTest("findGetter"); |
| 1053 | testGetter(TEST_FIND_FIELD); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1054 | testGetter(TEST_FIND_FIELD | TEST_BOUND); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1055 | } |
| 1056 | @Test |
| 1057 | public void testFindStaticGetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1058 | if (CAN_SKIP_WORKING) return; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1059 | startTest("findStaticGetter"); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1060 | testGetter(TEST_FIND_STATIC); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1061 | } |
| 1062 | public void testGetter(int testMode) throws Throwable { |
| 1063 | Lookup lookup = PRIVATE; // FIXME: test more lookups than this one |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1064 | for (Object[] c : HasFields.CASES) { |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1065 | boolean positive = (c[1] != Error.class); |
| 1066 | testGetter(positive, lookup, c[0], c[1], testMode); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1067 | if (positive) |
| 1068 | testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1069 | } |
| 1070 | testGetter(true, lookup, |
| 1071 | new Object[]{ true, System.class, "out", java.io.PrintStream.class }, |
| 1072 | System.out, testMode); |
| 1073 | for (int isStaticN = 0; isStaticN <= 1; isStaticN++) { |
| 1074 | testGetter(false, lookup, |
| 1075 | new Object[]{ (isStaticN != 0), System.class, "bogus", char.class }, |
| 1076 | null, testMode); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1079 | public void testGetter(boolean positive, MethodHandles.Lookup lookup, |
| 1080 | Object fieldRef, Object value, int testMode) throws Throwable { |
| 1081 | testAccessor(positive, lookup, fieldRef, value, testMode); |
| 1082 | } |
| 1083 | |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1084 | public void testAccessor(boolean positive0, MethodHandles.Lookup lookup, |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1085 | Object fieldRef, Object value, int testMode0) throws Throwable { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1086 | if (verbosity >= 4) |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1087 | System.out.println("testAccessor"+Arrays.deepToString(new Object[]{positive0, lookup, fieldRef, value, testMode0})); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1088 | boolean isGetter = ((testMode0 & TEST_SETTER) == 0); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1089 | boolean doBound = ((testMode0 & TEST_BOUND) != 0); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1090 | boolean testNPE = ((testMode0 & TEST_NPE) != 0); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1091 | int testMode = testMode0 & ~(TEST_SETTER | TEST_BOUND | TEST_NPE); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1092 | boolean positive = positive0 && !testNPE; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1093 | boolean isStatic; |
| 1094 | Class<?> fclass; |
| 1095 | String fname; |
| 1096 | Class<?> ftype; |
| 1097 | Field f = (fieldRef instanceof Field ? (Field)fieldRef : null); |
| 1098 | if (f != null) { |
| 1099 | isStatic = Modifier.isStatic(f.getModifiers()); |
| 1100 | fclass = f.getDeclaringClass(); |
| 1101 | fname = f.getName(); |
| 1102 | ftype = f.getType(); |
| 1103 | } else { |
| 1104 | Object[] scnt = (Object[]) fieldRef; |
| 1105 | isStatic = (Boolean) scnt[0]; |
| 1106 | fclass = (Class<?>) scnt[1]; |
| 1107 | fname = (String) scnt[2]; |
| 1108 | ftype = (Class<?>) scnt[3]; |
| 1109 | try { |
| 1110 | f = fclass.getDeclaredField(fname); |
| 1111 | } catch (ReflectiveOperationException ex) { |
| 1112 | f = null; |
| 1113 | } |
| 1114 | } |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1115 | if (!testModeMatches(testMode, isStatic)) return; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1116 | if (f == null && testMode == TEST_UNREFLECT) return; |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1117 | if (testNPE && isStatic) return; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1118 | countTest(positive); |
| 1119 | MethodType expType; |
| 1120 | if (isGetter) |
| 1121 | expType = MethodType.methodType(ftype, HasFields.class); |
| 1122 | else |
| 1123 | expType = MethodType.methodType(void.class, HasFields.class, ftype); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1124 | if (isStatic) expType = expType.dropParameterTypes(0, 1); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1125 | Exception noAccess = null; |
| 1126 | MethodHandle mh; |
| 1127 | try { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1128 | switch (testMode0 & ~(TEST_BOUND | TEST_NPE)) { |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1129 | case TEST_UNREFLECT: mh = lookup.unreflectGetter(f); break; |
| 1130 | case TEST_FIND_FIELD: mh = lookup.findGetter(fclass, fname, ftype); break; |
| 1131 | case TEST_FIND_STATIC: mh = lookup.findStaticGetter(fclass, fname, ftype); break; |
| 1132 | case TEST_SETTER| |
| 1133 | TEST_UNREFLECT: mh = lookup.unreflectSetter(f); break; |
| 1134 | case TEST_SETTER| |
| 1135 | TEST_FIND_FIELD: mh = lookup.findSetter(fclass, fname, ftype); break; |
| 1136 | case TEST_SETTER| |
| 1137 | TEST_FIND_STATIC: mh = lookup.findStaticSetter(fclass, fname, ftype); break; |
| 1138 | default: |
| 1139 | throw new InternalError("testMode="+testMode); |
| 1140 | } |
| 1141 | } catch (ReflectiveOperationException ex) { |
| 1142 | mh = null; |
| 1143 | noAccess = ex; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1144 | if (verbosity >= 5) ex.printStackTrace(System.out); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1145 | if (fname.contains("bogus")) |
| 1146 | assertTrue(noAccess instanceof NoSuchFieldException); |
| 1147 | else |
| 1148 | assertTrue(noAccess instanceof IllegalAccessException); |
| 1149 | } |
| 1150 | if (verbosity >= 3) |
| 1151 | System.out.println("find"+(isStatic?"Static":"")+(isGetter?"Getter":"Setter")+" "+fclass.getName()+"."+fname+"/"+ftype |
| 1152 | +" => "+mh |
| 1153 | +(noAccess == null ? "" : " !! "+noAccess)); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1154 | if (positive && !testNPE && noAccess != null) throw new RuntimeException(noAccess); |
| 1155 | assertEquals(positive0 ? "positive test" : "negative test erroneously passed", positive0, mh != null); |
| 1156 | if (!positive && !testNPE) return; // negative access test failed as expected |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1157 | assertEquals((isStatic ? 0 : 1)+(isGetter ? 0 : 1), mh.type().parameterCount()); |
| 1158 | |
| 1159 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1160 | assertSame(mh.type(), expType); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1161 | //assertNameStringContains(mh, fname); // This does not hold anymore with LFs |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1162 | HasFields fields = new HasFields(); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1163 | HasFields fieldsForMH = fields; |
| 1164 | if (testNPE) fieldsForMH = null; // perturb MH argument to elicit expected error |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1165 | if (doBound) |
| 1166 | mh = mh.bindTo(fieldsForMH); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1167 | Object sawValue; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1168 | Class<?> vtype = ftype; |
| 1169 | if (ftype != int.class) vtype = Object.class; |
| 1170 | if (isGetter) { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1171 | mh = mh.asType(mh.type().generic() |
| 1172 | .changeReturnType(vtype)); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1173 | } else { |
| 1174 | int last = mh.type().parameterCount() - 1; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1175 | mh = mh.asType(mh.type().generic() |
| 1176 | .changeReturnType(void.class) |
| 1177 | .changeParameterType(last, vtype)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1178 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1179 | if (f != null && f.getDeclaringClass() == HasFields.class) { |
| 1180 | assertEquals(f.get(fields), value); // clean to start with |
| 1181 | } |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1182 | Throwable caughtEx = null; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1183 | if (isGetter) { |
| 1184 | Object expValue = value; |
| 1185 | for (int i = 0; i <= 1; i++) { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1186 | sawValue = null; // make DA rules happy under try/catch |
| 1187 | try { |
| 1188 | if (isStatic || doBound) { |
| 1189 | if (ftype == int.class) |
| 1190 | sawValue = (int) mh.invokeExact(); // do these exactly |
| 1191 | else |
| 1192 | sawValue = mh.invokeExact(); |
| 1193 | } else { |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1194 | if (ftype == int.class) |
| 1195 | sawValue = (int) mh.invokeExact((Object) fieldsForMH); |
| 1196 | else |
| 1197 | sawValue = mh.invokeExact((Object) fieldsForMH); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1198 | } |
| 1199 | } catch (RuntimeException ex) { |
| 1200 | if (ex instanceof NullPointerException && testNPE) { |
| 1201 | caughtEx = ex; |
| 1202 | break; |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1203 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1204 | } |
| 1205 | assertEquals(sawValue, expValue); |
| 1206 | if (f != null && f.getDeclaringClass() == HasFields.class |
| 1207 | && !Modifier.isFinal(f.getModifiers())) { |
| 1208 | Object random = randomArg(ftype); |
| 1209 | f.set(fields, random); |
| 1210 | expValue = random; |
| 1211 | } else { |
| 1212 | break; |
| 1213 | } |
| 1214 | } |
| 1215 | } else { |
| 1216 | for (int i = 0; i <= 1; i++) { |
| 1217 | Object putValue = randomArg(ftype); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1218 | try { |
| 1219 | if (isStatic || doBound) { |
| 1220 | if (ftype == int.class) |
| 1221 | mh.invokeExact((int)putValue); // do these exactly |
| 1222 | else |
| 1223 | mh.invokeExact(putValue); |
| 1224 | } else { |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1225 | if (ftype == int.class) |
| 1226 | mh.invokeExact((Object) fieldsForMH, (int)putValue); |
| 1227 | else |
| 1228 | mh.invokeExact((Object) fieldsForMH, putValue); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1229 | } |
| 1230 | } catch (RuntimeException ex) { |
| 1231 | if (ex instanceof NullPointerException && testNPE) { |
| 1232 | caughtEx = ex; |
| 1233 | break; |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1234 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1235 | } |
| 1236 | if (f != null && f.getDeclaringClass() == HasFields.class) { |
| 1237 | assertEquals(f.get(fields), putValue); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | if (f != null && f.getDeclaringClass() == HasFields.class) { |
| 1242 | f.set(fields, value); // put it back |
| 1243 | } |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1244 | if (testNPE) { |
| 1245 | if (caughtEx == null || !(caughtEx instanceof NullPointerException)) |
| 1246 | throw new RuntimeException("failed to catch NPE exception"+(caughtEx == null ? " (caughtEx=null)" : ""), caughtEx); |
| 1247 | caughtEx = null; // nullify expected exception |
| 1248 | } |
| 1249 | if (caughtEx != null) { |
| 1250 | throw new RuntimeException("unexpected exception", caughtEx); |
| 1251 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1254 | |
| 1255 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1256 | public void testUnreflectSetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1257 | if (CAN_SKIP_WORKING) return; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1258 | startTest("unreflectSetter"); |
| 1259 | testSetter(TEST_UNREFLECT); |
| 1260 | } |
| 1261 | @Test |
| 1262 | public void testFindSetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1263 | if (CAN_SKIP_WORKING) return; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1264 | startTest("findSetter"); |
| 1265 | testSetter(TEST_FIND_FIELD); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1266 | testSetter(TEST_FIND_FIELD | TEST_BOUND); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1267 | } |
| 1268 | @Test |
| 1269 | public void testFindStaticSetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1270 | if (CAN_SKIP_WORKING) return; |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1271 | startTest("findStaticSetter"); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1272 | testSetter(TEST_FIND_STATIC); |
jrose | cf98d42 | 2010-06-08 23:08:56 -0700 | [diff] [blame] | 1273 | } |
| 1274 | public void testSetter(int testMode) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1275 | Lookup lookup = PRIVATE; // FIXME: test more lookups than this one |
| 1276 | startTest("unreflectSetter"); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1277 | for (Object[] c : HasFields.CASES) { |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1278 | boolean positive = (c[1] != Error.class); |
| 1279 | testSetter(positive, lookup, c[0], c[1], testMode); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1280 | if (positive) |
| 1281 | testSetter(positive, lookup, c[0], c[1], testMode | TEST_NPE); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1282 | } |
| 1283 | for (int isStaticN = 0; isStaticN <= 1; isStaticN++) { |
| 1284 | testSetter(false, lookup, |
| 1285 | new Object[]{ (isStaticN != 0), System.class, "bogus", char.class }, |
| 1286 | null, testMode); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1287 | } |
| 1288 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1289 | public void testSetter(boolean positive, MethodHandles.Lookup lookup, |
| 1290 | Object fieldRef, Object value, int testMode) throws Throwable { |
| 1291 | testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1294 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1295 | public void testArrayElementGetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1296 | if (CAN_SKIP_WORKING) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1297 | startTest("arrayElementGetter"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1298 | testArrayElementGetterSetter(false); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1301 | @Test |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1302 | public void testArrayElementSetter() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1303 | if (CAN_SKIP_WORKING) return; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1304 | startTest("arrayElementSetter"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1305 | testArrayElementGetterSetter(true); |
| 1306 | } |
| 1307 | |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1308 | private static final int TEST_ARRAY_NONE = 0, TEST_ARRAY_NPE = 1, TEST_ARRAY_OOB = 2, TEST_ARRAY_ASE = 3; |
| 1309 | |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1310 | public void testArrayElementGetterSetter(boolean testSetter) throws Throwable { |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1311 | testArrayElementGetterSetter(testSetter, TEST_ARRAY_NONE); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1314 | @Test |
| 1315 | public void testArrayElementErrors() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1316 | if (CAN_SKIP_WORKING) return; |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1317 | startTest("arrayElementErrors"); |
| 1318 | testArrayElementGetterSetter(false, TEST_ARRAY_NPE); |
| 1319 | testArrayElementGetterSetter(true, TEST_ARRAY_NPE); |
| 1320 | testArrayElementGetterSetter(false, TEST_ARRAY_OOB); |
| 1321 | testArrayElementGetterSetter(true, TEST_ARRAY_OOB); |
| 1322 | testArrayElementGetterSetter(new Object[10], true, TEST_ARRAY_ASE); |
| 1323 | testArrayElementGetterSetter(new Example[10], true, TEST_ARRAY_ASE); |
| 1324 | testArrayElementGetterSetter(new IntExample[10], true, TEST_ARRAY_ASE); |
| 1325 | } |
| 1326 | |
| 1327 | public void testArrayElementGetterSetter(boolean testSetter, int negTest) throws Throwable { |
| 1328 | testArrayElementGetterSetter(new String[10], testSetter, negTest); |
| 1329 | testArrayElementGetterSetter(new Iterable<?>[10], testSetter, negTest); |
| 1330 | testArrayElementGetterSetter(new Example[10], testSetter, negTest); |
| 1331 | testArrayElementGetterSetter(new IntExample[10], testSetter, negTest); |
| 1332 | testArrayElementGetterSetter(new Object[10], testSetter, negTest); |
| 1333 | testArrayElementGetterSetter(new boolean[10], testSetter, negTest); |
| 1334 | testArrayElementGetterSetter(new byte[10], testSetter, negTest); |
| 1335 | testArrayElementGetterSetter(new char[10], testSetter, negTest); |
| 1336 | testArrayElementGetterSetter(new short[10], testSetter, negTest); |
| 1337 | testArrayElementGetterSetter(new int[10], testSetter, negTest); |
| 1338 | testArrayElementGetterSetter(new float[10], testSetter, negTest); |
| 1339 | testArrayElementGetterSetter(new long[10], testSetter, negTest); |
| 1340 | testArrayElementGetterSetter(new double[10], testSetter, negTest); |
| 1341 | } |
| 1342 | |
| 1343 | public void testArrayElementGetterSetter(Object array, boolean testSetter, int negTest) throws Throwable { |
| 1344 | boolean positive = (negTest == TEST_ARRAY_NONE); |
| 1345 | int length = java.lang.reflect.Array.getLength(array); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1346 | Class<?> arrayType = array.getClass(); |
| 1347 | Class<?> elemType = arrayType.getComponentType(); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1348 | Object arrayToMH = array; |
| 1349 | // this stanza allows negative tests to make argument perturbations: |
| 1350 | switch (negTest) { |
| 1351 | case TEST_ARRAY_NPE: |
| 1352 | arrayToMH = null; |
| 1353 | break; |
| 1354 | case TEST_ARRAY_OOB: |
| 1355 | assert(length > 0); |
| 1356 | arrayToMH = java.lang.reflect.Array.newInstance(elemType, 0); |
| 1357 | break; |
| 1358 | case TEST_ARRAY_ASE: |
| 1359 | assert(testSetter && !elemType.isPrimitive()); |
| 1360 | if (elemType == Object.class) |
| 1361 | arrayToMH = new StringBuffer[length]; // very random subclass of Object! |
| 1362 | else if (elemType == Example.class) |
| 1363 | arrayToMH = new SubExample[length]; |
| 1364 | else if (elemType == IntExample.class) |
| 1365 | arrayToMH = new SubIntExample[length]; |
| 1366 | else |
| 1367 | return; // can't make an ArrayStoreException test |
| 1368 | assert(arrayType.isInstance(arrayToMH)) |
| 1369 | : Arrays.asList(arrayType, arrayToMH.getClass(), testSetter, negTest); |
| 1370 | break; |
| 1371 | } |
| 1372 | countTest(positive); |
| 1373 | if (verbosity > 2) System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+length+"]"+(positive ? "" : " negative test #"+negTest+" using "+Arrays.deepToString(new Object[]{arrayToMH}))); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1374 | MethodType expType = !testSetter |
| 1375 | ? MethodType.methodType(elemType, arrayType, int.class) |
| 1376 | : MethodType.methodType(void.class, arrayType, int.class, elemType); |
| 1377 | MethodHandle mh = !testSetter |
| 1378 | ? MethodHandles.arrayElementGetter(arrayType) |
| 1379 | : MethodHandles.arrayElementSetter(arrayType); |
| 1380 | assertSame(mh.type(), expType); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1381 | if (elemType != int.class && elemType != boolean.class) { |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1382 | MethodType gtype = mh.type().generic().changeParameterType(1, int.class); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1383 | if (testSetter) gtype = gtype.changeReturnType(void.class); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1384 | mh = mh.asType(gtype); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1385 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1386 | Object sawValue, expValue; |
| 1387 | List<Object> model = array2list(array); |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1388 | Throwable caughtEx = null; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1389 | for (int i = 0; i < length; i++) { |
| 1390 | // update array element |
| 1391 | Object random = randomArg(elemType); |
| 1392 | model.set(i, random); |
| 1393 | if (testSetter) { |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1394 | try { |
| 1395 | if (elemType == int.class) |
| 1396 | mh.invokeExact((int[]) arrayToMH, i, (int)random); |
| 1397 | else if (elemType == boolean.class) |
| 1398 | mh.invokeExact((boolean[]) arrayToMH, i, (boolean)random); |
| 1399 | else |
| 1400 | mh.invokeExact(arrayToMH, i, random); |
| 1401 | } catch (RuntimeException ex) { |
| 1402 | caughtEx = ex; |
| 1403 | break; |
| 1404 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1405 | assertEquals(model, array2list(array)); |
| 1406 | } else { |
| 1407 | Array.set(array, i, random); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1408 | } |
| 1409 | if (verbosity >= 5) { |
| 1410 | List<Object> array2list = array2list(array); |
| 1411 | System.out.println("a["+i+"]="+random+" => "+array2list); |
| 1412 | if (!array2list.equals(model)) |
| 1413 | System.out.println("*** != "+model); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1414 | } |
| 1415 | // observe array element |
| 1416 | sawValue = Array.get(array, i); |
| 1417 | if (!testSetter) { |
| 1418 | expValue = sawValue; |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1419 | try { |
| 1420 | if (elemType == int.class) |
| 1421 | sawValue = (int) mh.invokeExact((int[]) arrayToMH, i); |
| 1422 | else if (elemType == boolean.class) |
| 1423 | sawValue = (boolean) mh.invokeExact((boolean[]) arrayToMH, i); |
| 1424 | else |
| 1425 | sawValue = mh.invokeExact(arrayToMH, i); |
| 1426 | } catch (RuntimeException ex) { |
| 1427 | caughtEx = ex; |
| 1428 | break; |
| 1429 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1430 | assertEquals(sawValue, expValue); |
| 1431 | assertEquals(model, array2list(array)); |
| 1432 | } |
| 1433 | } |
jrose | 485df12 | 2012-07-12 00:11:35 -0700 | [diff] [blame] | 1434 | if (!positive) { |
| 1435 | if (caughtEx == null) |
| 1436 | throw new RuntimeException("failed to catch exception for negTest="+negTest); |
| 1437 | // test the kind of exception |
| 1438 | Class<?> reqType = null; |
| 1439 | switch (negTest) { |
| 1440 | case TEST_ARRAY_ASE: reqType = ArrayStoreException.class; break; |
| 1441 | case TEST_ARRAY_OOB: reqType = ArrayIndexOutOfBoundsException.class; break; |
| 1442 | case TEST_ARRAY_NPE: reqType = NullPointerException.class; break; |
| 1443 | default: assert(false); |
| 1444 | } |
| 1445 | if (reqType.isInstance(caughtEx)) { |
| 1446 | caughtEx = null; // nullify expected exception |
| 1447 | } |
| 1448 | } |
| 1449 | if (caughtEx != null) { |
| 1450 | throw new RuntimeException("unexpected exception", caughtEx); |
| 1451 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | List<Object> array2list(Object array) { |
| 1455 | int length = Array.getLength(array); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1456 | ArrayList<Object> model = new ArrayList<>(length); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1457 | for (int i = 0; i < length; i++) |
| 1458 | model.add(Array.get(array, i)); |
| 1459 | return model; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | static class Callee { |
| 1463 | static Object id() { return called("id"); } |
| 1464 | static Object id(Object x) { return called("id", x); } |
| 1465 | static Object id(Object x, Object y) { return called("id", x, y); } |
| 1466 | static Object id(Object x, Object y, Object z) { return called("id", x, y, z); } |
| 1467 | static Object id(Object... vx) { return called("id", vx); } |
| 1468 | static MethodHandle ofType(int n) { |
| 1469 | return ofType(Object.class, n); |
| 1470 | } |
| 1471 | static MethodHandle ofType(Class<?> rtype, int n) { |
| 1472 | if (n == -1) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1473 | return ofType(MethodType.methodType(rtype, Object[].class)); |
| 1474 | return ofType(MethodType.genericMethodType(n).changeReturnType(rtype)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1475 | } |
| 1476 | static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1477 | return ofType(MethodType.methodType(rtype, ptypes)); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1478 | } |
| 1479 | static MethodHandle ofType(MethodType type) { |
| 1480 | Class<?> rtype = type.returnType(); |
| 1481 | String pfx = ""; |
| 1482 | if (rtype != Object.class) |
| 1483 | pfx = rtype.getSimpleName().substring(0, 1).toLowerCase(); |
| 1484 | String name = pfx+"id"; |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1485 | try { |
| 1486 | return PRIVATE.findStatic(Callee.class, name, type); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1487 | } catch (NoSuchMethodException | IllegalAccessException ex) { |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 1488 | throw new RuntimeException(ex); |
| 1489 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | @Test |
| 1494 | public void testConvertArguments() throws Throwable { |
| 1495 | if (CAN_SKIP_WORKING) return; |
| 1496 | startTest("convertArguments"); |
| 1497 | testConvert(Callee.ofType(1), null, "id", int.class); |
| 1498 | testConvert(Callee.ofType(1), null, "id", String.class); |
| 1499 | testConvert(Callee.ofType(1), null, "id", Integer.class); |
| 1500 | testConvert(Callee.ofType(1), null, "id", short.class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1501 | testConvert(Callee.ofType(1), null, "id", char.class); |
| 1502 | testConvert(Callee.ofType(1), null, "id", byte.class); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1506 | testConvert(true, id, rtype, name, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1509 | void testConvert(boolean positive, |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1510 | MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1511 | countTest(positive); |
| 1512 | MethodType idType = id.type(); |
| 1513 | if (rtype == null) rtype = idType.returnType(); |
| 1514 | for (int i = 0; i < params.length; i++) { |
| 1515 | if (params[i] == null) params[i] = idType.parameterType(i); |
| 1516 | } |
| 1517 | // simulate the pairwise conversion |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1518 | MethodType newType = MethodType.methodType(rtype, params); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1519 | Object[] args = randomArgs(newType.parameterArray()); |
| 1520 | Object[] convArgs = args.clone(); |
| 1521 | for (int i = 0; i < args.length; i++) { |
| 1522 | Class<?> src = newType.parameterType(i); |
| 1523 | Class<?> dst = idType.parameterType(i); |
| 1524 | if (src != dst) |
| 1525 | convArgs[i] = castToWrapper(convArgs[i], dst); |
| 1526 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1527 | Object convResult = id.invokeWithArguments(convArgs); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1528 | { |
| 1529 | Class<?> dst = newType.returnType(); |
| 1530 | Class<?> src = idType.returnType(); |
| 1531 | if (src != dst) |
| 1532 | convResult = castToWrapper(convResult, dst); |
| 1533 | } |
| 1534 | MethodHandle target = null; |
| 1535 | RuntimeException error = null; |
| 1536 | try { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1537 | target = id.asType(newType); |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 1538 | } catch (WrongMethodTypeException ex) { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1539 | error = ex; |
| 1540 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1541 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1542 | System.out.println("convert "+id+ " to "+newType+" => "+target |
| 1543 | +(error == null ? "" : " !! "+error)); |
| 1544 | if (positive && error != null) throw error; |
| 1545 | assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null); |
| 1546 | if (!positive) return; // negative test failed as expected |
| 1547 | assertEquals(newType, target.type()); |
| 1548 | printCalled(target, id.toString(), args); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1549 | Object result = target.invokeWithArguments(args); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1550 | assertCalled(name, convArgs); |
| 1551 | assertEquals(convResult, result); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1552 | if (verbosity >= 1) |
| 1553 | System.out.print(':'); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | @Test |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 1557 | public void testVarargsCollector() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1558 | if (CAN_SKIP_WORKING) return; |
| 1559 | startTest("varargsCollector"); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 1560 | MethodHandle vac0 = PRIVATE.findStatic(MethodHandlesTest.class, "called", |
| 1561 | MethodType.methodType(Object.class, String.class, Object[].class)); |
| 1562 | vac0 = vac0.bindTo("vac"); |
| 1563 | MethodHandle vac = vac0.asVarargsCollector(Object[].class); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1564 | testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac"); |
| 1565 | testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac"); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1566 | for (Class<?> at : new Class<?>[] { Object.class, String.class, Integer.class }) { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1567 | testConvert(true, vac.asType(MethodType.genericMethodType(1)), null, "vac", at); |
| 1568 | testConvert(true, vac.asType(MethodType.genericMethodType(2)), null, "vac", at, at); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1572 | @Test // SLOW |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1573 | public void testPermuteArguments() throws Throwable { |
| 1574 | if (CAN_SKIP_WORKING) return; |
| 1575 | startTest("permuteArguments"); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1576 | testPermuteArguments(4, Integer.class, 2, long.class, 6); |
| 1577 | if (CAN_TEST_LIGHTLY) return; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1578 | testPermuteArguments(4, Integer.class, 2, String.class, 0); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1579 | testPermuteArguments(6, Integer.class, 0, null, 30); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1580 | } |
| 1581 | 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] | 1582 | if (verbosity >= 2) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1583 | System.out.println("permuteArguments "+max+"*"+type1.getName() |
| 1584 | +(t2c==0?"":"/"+t2c+"*"+type2.getName()) |
| 1585 | +(dilution > 0 ? " with dilution "+dilution : "")); |
| 1586 | int t2pos = t2c == 0 ? 0 : 1; |
| 1587 | for (int inargs = t2pos+1; inargs <= max; inargs++) { |
| 1588 | Class<?>[] types = new Class<?>[inargs]; |
| 1589 | Arrays.fill(types, type1); |
| 1590 | if (t2c != 0) { |
| 1591 | // Fill in a middle range with type2: |
| 1592 | Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2); |
| 1593 | } |
| 1594 | Object[] args = randomArgs(types); |
| 1595 | int numcases = 1; |
| 1596 | for (int outargs = 0; outargs <= max; outargs++) { |
| 1597 | if (outargs - inargs >= MAX_ARG_INCREASE) continue; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1598 | int casStep = dilution + 1; |
| 1599 | // Avoid some common factors: |
| 1600 | while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) || |
| 1601 | (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0)) |
| 1602 | casStep++; |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1603 | testPermuteArguments(args, types, outargs, numcases, casStep); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1604 | numcases *= inargs; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1605 | if (CAN_TEST_LIGHTLY && outargs < max-2) continue; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1606 | if (dilution > 10 && outargs >= 4) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1607 | if (CAN_TEST_LIGHTLY) continue; |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1608 | int[] reorder = new int[outargs]; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1609 | // Do some special patterns, which we probably missed. |
| 1610 | // Replication of a single argument or argument pair. |
| 1611 | for (int i = 0; i < inargs; i++) { |
| 1612 | Arrays.fill(reorder, i); |
| 1613 | testPermuteArguments(args, types, reorder); |
| 1614 | for (int d = 1; d <= 2; d++) { |
| 1615 | if (i + d >= inargs) continue; |
| 1616 | for (int j = 1; j < outargs; j += 2) |
| 1617 | reorder[j] += 1; |
| 1618 | testPermuteArguments(args, types, reorder); |
| 1619 | testPermuteArguments(args, types, reverse(reorder)); |
| 1620 | } |
| 1621 | } |
| 1622 | // Repetition of a sequence of 3 or more arguments. |
| 1623 | for (int i = 1; i < inargs; i++) { |
| 1624 | for (int len = 3; len <= inargs; len++) { |
| 1625 | for (int j = 0; j < outargs; j++) |
| 1626 | reorder[j] = (i + (j % len)) % inargs; |
| 1627 | testPermuteArguments(args, types, reorder); |
| 1628 | testPermuteArguments(args, types, reverse(reorder)); |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1636 | public void testPermuteArguments(Object[] args, Class<?>[] types, |
| 1637 | int outargs, int numcases, int casStep) throws Throwable { |
| 1638 | int inargs = args.length; |
| 1639 | int[] reorder = new int[outargs]; |
| 1640 | for (int cas = 0; cas < numcases; cas += casStep) { |
| 1641 | for (int i = 0, c = cas; i < outargs; i++) { |
| 1642 | reorder[i] = c % inargs; |
| 1643 | c /= inargs; |
| 1644 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1645 | if (CAN_TEST_LIGHTLY && outargs >= 3 && (reorder[0] == reorder[1] || reorder[1] == reorder[2])) continue; |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1646 | testPermuteArguments(args, types, reorder); |
| 1647 | } |
| 1648 | } |
| 1649 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1650 | static int[] reverse(int[] reorder) { |
| 1651 | reorder = reorder.clone(); |
| 1652 | for (int i = 0, imax = reorder.length / 2; i < imax; i++) { |
| 1653 | int j = reorder.length - 1 - i; |
| 1654 | int tem = reorder[i]; |
| 1655 | reorder[i] = reorder[j]; |
| 1656 | reorder[j] = tem; |
| 1657 | } |
| 1658 | return reorder; |
| 1659 | } |
| 1660 | |
| 1661 | void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable { |
| 1662 | countTest(); |
| 1663 | if (args == null && types == null) { |
| 1664 | int max = 0; |
| 1665 | for (int j : reorder) { |
| 1666 | if (max < j) max = j; |
| 1667 | } |
| 1668 | args = randomArgs(max+1, Integer.class); |
| 1669 | } |
| 1670 | if (args == null) { |
| 1671 | args = randomArgs(types); |
| 1672 | } |
| 1673 | if (types == null) { |
| 1674 | types = new Class<?>[args.length]; |
| 1675 | for (int i = 0; i < args.length; i++) |
| 1676 | types[i] = args[i].getClass(); |
| 1677 | } |
| 1678 | int inargs = args.length, outargs = reorder.length; |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 1679 | assertTrue(inargs == types.length); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1680 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1681 | System.out.println("permuteArguments "+Arrays.toString(reorder)); |
| 1682 | Object[] permArgs = new Object[outargs]; |
| 1683 | Class<?>[] permTypes = new Class<?>[outargs]; |
| 1684 | for (int i = 0; i < outargs; i++) { |
| 1685 | permArgs[i] = args[reorder[i]]; |
| 1686 | permTypes[i] = types[reorder[i]]; |
| 1687 | } |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1688 | if (verbosity >= 4) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1689 | System.out.println("in args: "+Arrays.asList(args)); |
| 1690 | System.out.println("out args: "+Arrays.asList(permArgs)); |
| 1691 | System.out.println("in types: "+Arrays.asList(types)); |
| 1692 | System.out.println("out types: "+Arrays.asList(permTypes)); |
| 1693 | } |
| 1694 | MethodType inType = MethodType.methodType(Object.class, types); |
| 1695 | MethodType outType = MethodType.methodType(Object.class, permTypes); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1696 | MethodHandle target = varargsList(outargs).asType(outType); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1697 | MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1698 | if (verbosity >= 5) System.out.println("newTarget = "+newTarget); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1699 | Object result = newTarget.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1700 | Object expected = Arrays.asList(permArgs); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1701 | if (!expected.equals(result)) { |
| 1702 | System.out.println("*** failed permuteArguments "+Arrays.toString(reorder)+" types="+Arrays.asList(types)); |
| 1703 | System.out.println("in args: "+Arrays.asList(args)); |
| 1704 | System.out.println("out args: "+expected); |
| 1705 | System.out.println("bad args: "+result); |
| 1706 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1707 | assertEquals(expected, result); |
| 1708 | } |
| 1709 | |
| 1710 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1711 | @Test // SLOW |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1712 | public void testSpreadArguments() throws Throwable { |
| 1713 | if (CAN_SKIP_WORKING) return; |
| 1714 | startTest("spreadArguments"); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1715 | for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1716 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1717 | System.out.println("spreadArguments "+argType); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1718 | for (int nargs = 0; nargs < 50; nargs++) { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1719 | if (CAN_TEST_LIGHTLY && nargs > 11) break; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1720 | for (int pos = 0; pos <= nargs; pos++) { |
| 1721 | if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue; |
| 1722 | if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3) |
| 1723 | continue; |
| 1724 | testSpreadArguments(argType, pos, nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable { |
| 1730 | countTest(); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1731 | Class<?> arrayType = java.lang.reflect.Array.newInstance(argType, 0).getClass(); |
| 1732 | MethodHandle target2 = varargsArray(arrayType, nargs); |
| 1733 | MethodHandle target = target2.asType(target2.type().generic()); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1734 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1735 | System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]"); |
| 1736 | Object[] args = randomArgs(target2.type().parameterArray()); |
| 1737 | // make sure the target does what we think it does: |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1738 | if (pos == 0 && nargs < 5 && !argType.isPrimitive()) { |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1739 | Object[] check = (Object[]) target.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1740 | assertArrayEquals(args, check); |
| 1741 | switch (nargs) { |
| 1742 | case 0: |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1743 | check = (Object[]) (Object) target.invokeExact(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1744 | assertArrayEquals(args, check); |
| 1745 | break; |
| 1746 | case 1: |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1747 | check = (Object[]) (Object) target.invokeExact(args[0]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1748 | assertArrayEquals(args, check); |
| 1749 | break; |
| 1750 | case 2: |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1751 | check = (Object[]) (Object) target.invokeExact(args[0], args[1]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1752 | assertArrayEquals(args, check); |
| 1753 | break; |
| 1754 | } |
| 1755 | } |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1756 | List<Class<?>> newParams = new ArrayList<>(target2.type().parameterList()); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1757 | { // modify newParams in place |
| 1758 | List<Class<?>> spreadParams = newParams.subList(pos, nargs); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1759 | spreadParams.clear(); spreadParams.add(arrayType); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1760 | } |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1761 | MethodType newType = MethodType.methodType(arrayType, newParams); |
| 1762 | MethodHandle result = target2.asSpreader(arrayType, nargs-pos); |
| 1763 | assert(result.type() == newType) : Arrays.asList(result, newType); |
| 1764 | result = result.asType(newType.generic()); |
| 1765 | Object returnValue; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1766 | if (pos == 0) { |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1767 | Object args2 = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length)); |
| 1768 | returnValue = result.invokeExact(args2); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1769 | } else { |
| 1770 | Object[] args1 = Arrays.copyOfRange(args, 0, pos+1); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1771 | args1[pos] = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length)); |
| 1772 | returnValue = result.invokeWithArguments(args1); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1773 | } |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 1774 | String argstr = Arrays.toString(args); |
| 1775 | if (!argType.isPrimitive()) { |
| 1776 | Object[] rv = (Object[]) returnValue; |
| 1777 | String rvs = Arrays.toString(rv); |
| 1778 | if (!Arrays.equals(args, rv)) { |
| 1779 | System.out.println("method: "+result); |
| 1780 | System.out.println("expected: "+argstr); |
| 1781 | System.out.println("returned: "+rvs); |
| 1782 | assertArrayEquals(args, rv); |
| 1783 | } |
| 1784 | } else if (argType == int.class) { |
| 1785 | String rvs = Arrays.toString((int[]) returnValue); |
| 1786 | if (!argstr.equals(rvs)) { |
| 1787 | System.out.println("method: "+result); |
| 1788 | System.out.println("expected: "+argstr); |
| 1789 | System.out.println("returned: "+rvs); |
| 1790 | assertEquals(argstr, rvs); |
| 1791 | } |
| 1792 | } else if (argType == long.class) { |
| 1793 | String rvs = Arrays.toString((long[]) returnValue); |
| 1794 | if (!argstr.equals(rvs)) { |
| 1795 | System.out.println("method: "+result); |
| 1796 | System.out.println("expected: "+argstr); |
| 1797 | System.out.println("returned: "+rvs); |
| 1798 | assertEquals(argstr, rvs); |
| 1799 | } |
| 1800 | } else { |
| 1801 | // cannot test... |
| 1802 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1805 | @Test // SLOW |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1806 | public void testCollectArguments() throws Throwable { |
| 1807 | if (CAN_SKIP_WORKING) return; |
| 1808 | startTest("collectArguments"); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1809 | for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1810 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1811 | System.out.println("collectArguments "+argType); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1812 | for (int nargs = 0; nargs < 50; nargs++) { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1813 | if (CAN_TEST_LIGHTLY && nargs > 11) break; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1814 | for (int pos = 0; pos <= nargs; pos++) { |
| 1815 | if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue; |
| 1816 | if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3) |
| 1817 | continue; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1818 | testCollectArguments(argType, pos, nargs); |
| 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable { |
| 1824 | countTest(); |
| 1825 | // fake up a MH with the same type as the desired adapter: |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1826 | MethodHandle fake = varargsArray(nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1827 | fake = changeArgTypes(fake, argType); |
| 1828 | MethodType newType = fake.type(); |
| 1829 | Object[] args = randomArgs(newType.parameterArray()); |
| 1830 | // here is what should happen: |
| 1831 | Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1); |
| 1832 | collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length); |
| 1833 | // here is the MH which will witness the collected argument tail: |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1834 | MethodHandle target = varargsArray(pos+1); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1835 | target = changeArgTypes(target, 0, pos, argType); |
| 1836 | target = changeArgTypes(target, pos, pos+1, Object[].class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1837 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1838 | System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]"); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1839 | MethodHandle result = target.asCollector(Object[].class, nargs-pos).asType(newType); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1840 | Object[] returnValue = (Object[]) result.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1841 | // assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]); |
| 1842 | // returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]); |
| 1843 | // collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]); |
| 1844 | assertArrayEquals(collectedArgs, returnValue); |
| 1845 | } |
| 1846 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1847 | @Test // SLOW |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1848 | public void testInsertArguments() throws Throwable { |
| 1849 | if (CAN_SKIP_WORKING) return; |
| 1850 | startTest("insertArguments"); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1851 | for (int nargs = 0; nargs < 50; nargs++) { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1852 | if (CAN_TEST_LIGHTLY && nargs > 11) break; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1853 | for (int ins = 0; ins <= nargs; ins++) { |
| 1854 | if (nargs > 10 && ins > 4 && ins < nargs-4 && ins % 10 != 3) |
| 1855 | continue; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1856 | for (int pos = 0; pos <= nargs; pos++) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1857 | if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3) |
| 1858 | continue; |
| 1859 | if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1860 | testInsertArguments(nargs, pos, ins); |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | void testInsertArguments(int nargs, int pos, int ins) throws Throwable { |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1867 | countTest(); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1868 | MethodHandle target = varargsArray(nargs + ins); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1869 | Object[] args = randomArgs(target.type().parameterArray()); |
| 1870 | List<Object> resList = Arrays.asList(args); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1871 | List<Object> argsToPass = new ArrayList<>(resList); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1872 | List<Object> argsToInsert = argsToPass.subList(pos, pos + ins); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1873 | if (verbosity >= 3) |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 1874 | System.out.println("insert: "+argsToInsert+" @"+pos+" into "+target); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1875 | @SuppressWarnings("cast") // cast to spread Object... is helpful |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1876 | MethodHandle target2 = MethodHandles.insertArguments(target, pos, |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1877 | (Object[]/*...*/) argsToInsert.toArray()); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1878 | argsToInsert.clear(); // remove from argsToInsert |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1879 | Object res2 = target2.invokeWithArguments(argsToPass); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1880 | Object res2List = Arrays.asList((Object[])res2); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1881 | if (verbosity >= 3) |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 1882 | System.out.println("result: "+res2List); |
| 1883 | //if (!resList.equals(res2List)) |
| 1884 | // System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List); |
| 1885 | assertEquals(resList, res2List); |
| 1886 | } |
| 1887 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1888 | @Test |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1889 | public void testFilterReturnValue() throws Throwable { |
| 1890 | if (CAN_SKIP_WORKING) return; |
| 1891 | startTest("filterReturnValue"); |
| 1892 | Class<?> classOfVCList = varargsList(1).invokeWithArguments(0).getClass(); |
| 1893 | assertTrue(List.class.isAssignableFrom(classOfVCList)); |
| 1894 | for (int nargs = 0; nargs <= 3; nargs++) { |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1895 | for (Class<?> rtype : new Class<?>[] { Object.class, |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1896 | List.class, |
| 1897 | int.class, |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 1898 | byte.class, |
| 1899 | long.class, |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1900 | CharSequence.class, |
| 1901 | String.class }) { |
| 1902 | testFilterReturnValue(nargs, rtype); |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | void testFilterReturnValue(int nargs, Class<?> rtype) throws Throwable { |
| 1908 | countTest(); |
| 1909 | MethodHandle target = varargsList(nargs, rtype); |
| 1910 | MethodHandle filter; |
| 1911 | if (List.class.isAssignableFrom(rtype) || rtype.isAssignableFrom(List.class)) |
| 1912 | filter = varargsList(1); // add another layer of list-ness |
| 1913 | else |
| 1914 | filter = MethodHandles.identity(rtype); |
| 1915 | filter = filter.asType(MethodType.methodType(target.type().returnType(), rtype)); |
| 1916 | Object[] argsToPass = randomArgs(nargs, Object.class); |
| 1917 | if (verbosity >= 3) |
| 1918 | System.out.println("filter "+target+" to "+rtype.getSimpleName()+" with "+filter); |
| 1919 | MethodHandle target2 = MethodHandles.filterReturnValue(target, filter); |
| 1920 | if (verbosity >= 4) |
| 1921 | System.out.println("filtered target: "+target2); |
| 1922 | // Simulate expected effect of filter on return value: |
| 1923 | Object unfiltered = target.invokeWithArguments(argsToPass); |
| 1924 | Object expected = filter.invokeWithArguments(unfiltered); |
| 1925 | if (verbosity >= 4) |
| 1926 | System.out.println("unfiltered: "+unfiltered+" : "+unfiltered.getClass().getSimpleName()); |
| 1927 | if (verbosity >= 4) |
| 1928 | System.out.println("expected: "+expected+" : "+expected.getClass().getSimpleName()); |
| 1929 | Object result = target2.invokeWithArguments(argsToPass); |
| 1930 | if (verbosity >= 3) |
| 1931 | System.out.println("result: "+result+" : "+result.getClass().getSimpleName()); |
| 1932 | if (!expected.equals(result)) |
| 1933 | System.out.println("*** fail at n/rt = "+nargs+"/"+rtype.getSimpleName()+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected); |
| 1934 | assertEquals(expected, result); |
| 1935 | } |
| 1936 | |
| 1937 | @Test |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1938 | public void testFilterArguments() throws Throwable { |
| 1939 | if (CAN_SKIP_WORKING) return; |
| 1940 | startTest("filterArguments"); |
| 1941 | for (int nargs = 1; nargs <= 6; nargs++) { |
| 1942 | for (int pos = 0; pos < nargs; pos++) { |
| 1943 | testFilterArguments(nargs, pos); |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | void testFilterArguments(int nargs, int pos) throws Throwable { |
| 1949 | countTest(); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1950 | MethodHandle target = varargsList(nargs); |
| 1951 | MethodHandle filter = varargsList(1); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 1952 | filter = filter.asType(filter.type().generic()); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1953 | Object[] argsToPass = randomArgs(nargs, Object.class); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1954 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1955 | System.out.println("filter "+target+" at "+pos+" with "+filter); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1956 | MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1957 | // Simulate expected effect of filter on arglist: |
| 1958 | Object[] filteredArgs = argsToPass.clone(); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1959 | filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1960 | List<Object> expected = Arrays.asList(filteredArgs); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1961 | Object result = target2.invokeWithArguments(argsToPass); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1962 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1963 | System.out.println("result: "+result); |
| 1964 | if (!expected.equals(result)) |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1965 | System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1966 | assertEquals(expected, result); |
| 1967 | } |
| 1968 | |
| 1969 | @Test |
| 1970 | public void testFoldArguments() throws Throwable { |
| 1971 | if (CAN_SKIP_WORKING) return; |
| 1972 | startTest("foldArguments"); |
| 1973 | for (int nargs = 0; nargs <= 4; nargs++) { |
| 1974 | for (int fold = 0; fold <= nargs; fold++) { |
| 1975 | for (int pos = 0; pos <= nargs; pos++) { |
| 1976 | testFoldArguments(nargs, pos, fold); |
| 1977 | } |
| 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | void testFoldArguments(int nargs, int pos, int fold) throws Throwable { |
| 1983 | if (pos != 0) return; // can fold only at pos=0 for now |
| 1984 | countTest(); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 1985 | MethodHandle target = varargsList(1 + nargs); |
| 1986 | MethodHandle combine = varargsList(fold).asType(MethodType.genericMethodType(fold)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1987 | List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class)); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1988 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1989 | System.out.println("fold "+target+" with "+combine); |
| 1990 | MethodHandle target2 = MethodHandles.foldArguments(target, combine); |
| 1991 | // Simulate expected effect of combiner on arglist: |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 1992 | List<Object> expected = new ArrayList<>(argsToPass); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1993 | List<Object> argsToFold = expected.subList(pos, pos + fold); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1994 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1995 | System.out.println("fold: "+argsToFold+" into "+target2); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1996 | Object foldedArgs = combine.invokeWithArguments(argsToFold); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 1997 | argsToFold.add(0, foldedArgs); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1998 | Object result = target2.invokeWithArguments(argsToPass); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 1999 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2000 | System.out.println("result: "+result); |
| 2001 | if (!expected.equals(result)) |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 2002 | System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result+" != "+expected); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2003 | assertEquals(expected, result); |
| 2004 | } |
| 2005 | |
| 2006 | @Test |
| 2007 | public void testDropArguments() throws Throwable { |
| 2008 | if (CAN_SKIP_WORKING) return; |
| 2009 | startTest("dropArguments"); |
| 2010 | for (int nargs = 0; nargs <= 4; nargs++) { |
| 2011 | for (int drop = 1; drop <= 4; drop++) { |
| 2012 | for (int pos = 0; pos <= nargs; pos++) { |
| 2013 | testDropArguments(nargs, pos, drop); |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | void testDropArguments(int nargs, int pos, int drop) throws Throwable { |
| 2020 | countTest(); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 2021 | MethodHandle target = varargsArray(nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2022 | Object[] args = randomArgs(target.type().parameterArray()); |
| 2023 | MethodHandle target2 = MethodHandles.dropArguments(target, pos, |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2024 | Collections.nCopies(drop, Object.class).toArray(new Class<?>[0])); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2025 | List<Object> resList = Arrays.asList(args); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2026 | List<Object> argsToDrop = new ArrayList<>(resList); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2027 | for (int i = drop; i > 0; i--) { |
| 2028 | argsToDrop.add(pos, "blort#"+i); |
| 2029 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2030 | Object res2 = target2.invokeWithArguments(argsToDrop); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2031 | Object res2List = Arrays.asList((Object[])res2); |
| 2032 | //if (!resList.equals(res2List)) |
| 2033 | // System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List); |
| 2034 | assertEquals(resList, res2List); |
| 2035 | } |
| 2036 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2037 | @Test // SLOW |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2038 | public void testInvokers() throws Throwable { |
| 2039 | if (CAN_SKIP_WORKING) return; |
| 2040 | startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker"); |
| 2041 | // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2042 | Set<MethodType> done = new HashSet<>(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2043 | for (int i = 0; i <= 6; i++) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2044 | if (CAN_TEST_LIGHTLY && i > 3) break; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2045 | MethodType gtype = MethodType.genericMethodType(i); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2046 | for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2047 | for (int j = -1; j < i; j++) { |
| 2048 | MethodType type = gtype; |
| 2049 | if (j < 0) |
| 2050 | type = type.changeReturnType(argType); |
| 2051 | else if (argType == void.class) |
| 2052 | continue; |
| 2053 | else |
| 2054 | type = type.changeParameterType(j, argType); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2055 | if (done.add(type)) |
| 2056 | testInvokers(type); |
| 2057 | MethodType vtype = type.changeReturnType(void.class); |
| 2058 | if (done.add(vtype)) |
| 2059 | testInvokers(vtype); |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | public void testInvokers(MethodType type) throws Throwable { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2066 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2067 | System.out.println("test invokers for "+type); |
| 2068 | int nargs = type.parameterCount(); |
| 2069 | boolean testRetCode = type.returnType() != void.class; |
| 2070 | MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee", |
| 2071 | MethodType.genericMethodType(0, true)); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 2072 | assertTrue(target.isVarargsCollector()); |
| 2073 | target = target.asType(type); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2074 | Object[] args = randomArgs(type.parameterArray()); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2075 | List<Object> targetPlusArgs = new ArrayList<>(Arrays.asList(args)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2076 | targetPlusArgs.add(0, target); |
| 2077 | int code = (Integer) invokee(args); |
| 2078 | Object log = logEntry("invokee", args); |
| 2079 | assertEquals(log.hashCode(), code); |
| 2080 | assertCalled("invokee", args); |
| 2081 | MethodHandle inv; |
| 2082 | Object result; |
| 2083 | // exact invoker |
| 2084 | countTest(); |
| 2085 | calledLog.clear(); |
| 2086 | inv = MethodHandles.exactInvoker(type); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2087 | result = inv.invokeWithArguments(targetPlusArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2088 | if (testRetCode) assertEquals(code, result); |
| 2089 | assertCalled("invokee", args); |
| 2090 | // generic invoker |
| 2091 | countTest(); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 2092 | inv = MethodHandles.invoker(type); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2093 | if (nargs <= 3 && type == type.generic()) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2094 | calledLog.clear(); |
| 2095 | switch (nargs) { |
| 2096 | case 0: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2097 | result = inv.invokeExact(target); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2098 | break; |
| 2099 | case 1: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2100 | result = inv.invokeExact(target, args[0]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2101 | break; |
| 2102 | case 2: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2103 | result = inv.invokeExact(target, args[0], args[1]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2104 | break; |
| 2105 | case 3: |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2106 | result = inv.invokeExact(target, args[0], args[1], args[2]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2107 | break; |
| 2108 | } |
| 2109 | if (testRetCode) assertEquals(code, result); |
| 2110 | assertCalled("invokee", args); |
| 2111 | } |
| 2112 | calledLog.clear(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2113 | result = inv.invokeWithArguments(targetPlusArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2114 | if (testRetCode) assertEquals(code, result); |
| 2115 | assertCalled("invokee", args); |
| 2116 | // varargs invoker #0 |
| 2117 | calledLog.clear(); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 2118 | inv = MethodHandles.spreadInvoker(type, 0); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2119 | if (type.returnType() == Object.class) { |
| 2120 | result = inv.invokeExact(target, args); |
| 2121 | } else if (type.returnType() == void.class) { |
| 2122 | result = null; inv.invokeExact(target, args); |
| 2123 | } else { |
| 2124 | result = inv.invokeWithArguments(target, (Object) args); |
| 2125 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2126 | if (testRetCode) assertEquals(code, result); |
| 2127 | assertCalled("invokee", args); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2128 | if (nargs >= 1 && type == type.generic()) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2129 | // varargs invoker #1 |
| 2130 | calledLog.clear(); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 2131 | inv = MethodHandles.spreadInvoker(type, 1); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2132 | result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2133 | if (testRetCode) assertEquals(code, result); |
| 2134 | assertCalled("invokee", args); |
| 2135 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2136 | if (nargs >= 2 && type == type.generic()) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2137 | // varargs invoker #2 |
| 2138 | calledLog.clear(); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 2139 | inv = MethodHandles.spreadInvoker(type, 2); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2140 | result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2141 | if (testRetCode) assertEquals(code, result); |
| 2142 | assertCalled("invokee", args); |
| 2143 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2144 | if (nargs >= 3 && type == type.generic()) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2145 | // varargs invoker #3 |
| 2146 | calledLog.clear(); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 2147 | inv = MethodHandles.spreadInvoker(type, 3); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2148 | 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] | 2149 | if (testRetCode) assertEquals(code, result); |
| 2150 | assertCalled("invokee", args); |
| 2151 | } |
| 2152 | for (int k = 0; k <= nargs; k++) { |
| 2153 | // varargs invoker #0..N |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2154 | if (CAN_TEST_LIGHTLY && (k > 1 || k < nargs - 1)) continue; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2155 | countTest(); |
| 2156 | calledLog.clear(); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 2157 | inv = MethodHandles.spreadInvoker(type, k); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2158 | MethodType expType = (type.dropParameterTypes(k, nargs) |
| 2159 | .appendParameterTypes(Object[].class) |
| 2160 | .insertParameterTypes(0, MethodHandle.class)); |
| 2161 | assertEquals(expType, inv.type()); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2162 | List<Object> targetPlusVarArgs = new ArrayList<>(targetPlusArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2163 | List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs); |
| 2164 | Object[] tail = tailList.toArray(); |
| 2165 | tailList.clear(); tailList.add(tail); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2166 | result = inv.invokeWithArguments(targetPlusVarArgs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2167 | if (testRetCode) assertEquals(code, result); |
| 2168 | assertCalled("invokee", args); |
| 2169 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2170 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2171 | // dynamic invoker |
| 2172 | countTest(); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 2173 | CallSite site = new MutableCallSite(type); |
| 2174 | inv = site.dynamicInvoker(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2175 | |
| 2176 | // see if we get the result of the original target: |
| 2177 | try { |
| 2178 | result = inv.invokeWithArguments(args); |
| 2179 | assertTrue("should not reach here", false); |
| 2180 | } catch (IllegalStateException ex) { |
| 2181 | String msg = ex.getMessage(); |
| 2182 | assertTrue(msg, msg.contains("site")); |
| 2183 | } |
| 2184 | |
| 2185 | // set new target after invoker is created, to make sure we track target |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2186 | site.setTarget(target); |
| 2187 | calledLog.clear(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2188 | result = inv.invokeWithArguments(args); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2189 | if (testRetCode) assertEquals(code, result); |
| 2190 | assertCalled("invokee", args); |
| 2191 | } |
| 2192 | |
| 2193 | static Object invokee(Object... args) { |
| 2194 | return called("invokee", args).hashCode(); |
| 2195 | } |
| 2196 | |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2197 | private static final String MISSING_ARG = "missingArg"; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2198 | private static final String MISSING_ARG_2 = "missingArg#2"; |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2199 | static Object targetIfEquals() { |
| 2200 | return called("targetIfEquals"); |
| 2201 | } |
| 2202 | static Object fallbackIfNotEquals() { |
| 2203 | return called("fallbackIfNotEquals"); |
| 2204 | } |
| 2205 | static Object targetIfEquals(Object x) { |
| 2206 | assertEquals(x, MISSING_ARG); |
| 2207 | return called("targetIfEquals", x); |
| 2208 | } |
| 2209 | static Object fallbackIfNotEquals(Object x) { |
| 2210 | assertFalse(x.toString(), x.equals(MISSING_ARG)); |
| 2211 | return called("fallbackIfNotEquals", x); |
| 2212 | } |
| 2213 | static Object targetIfEquals(Object x, Object y) { |
| 2214 | assertEquals(x, y); |
| 2215 | return called("targetIfEquals", x, y); |
| 2216 | } |
| 2217 | static Object fallbackIfNotEquals(Object x, Object y) { |
| 2218 | assertFalse(x.toString(), x.equals(y)); |
| 2219 | return called("fallbackIfNotEquals", x, y); |
| 2220 | } |
| 2221 | static Object targetIfEquals(Object x, Object y, Object z) { |
| 2222 | assertEquals(x, y); |
| 2223 | return called("targetIfEquals", x, y, z); |
| 2224 | } |
| 2225 | static Object fallbackIfNotEquals(Object x, Object y, Object z) { |
| 2226 | assertFalse(x.toString(), x.equals(y)); |
| 2227 | return called("fallbackIfNotEquals", x, y, z); |
| 2228 | } |
| 2229 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2230 | @Test |
| 2231 | public void testGuardWithTest() throws Throwable { |
| 2232 | if (CAN_SKIP_WORKING) return; |
| 2233 | startTest("guardWithTest"); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2234 | for (int nargs = 0; nargs <= 50; nargs++) { |
| 2235 | if (CAN_TEST_LIGHTLY && nargs > 7) break; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2236 | testGuardWithTest(nargs, Object.class); |
| 2237 | testGuardWithTest(nargs, String.class); |
| 2238 | } |
| 2239 | } |
| 2240 | void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2241 | testGuardWithTest(nargs, 0, argClass); |
| 2242 | if (nargs <= 5 || nargs % 10 == 3) { |
| 2243 | for (int testDrops = 1; testDrops <= nargs; testDrops++) |
| 2244 | testGuardWithTest(nargs, testDrops, argClass); |
| 2245 | } |
| 2246 | } |
| 2247 | void testGuardWithTest(int nargs, int testDrops, Class<?> argClass) throws Throwable { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2248 | countTest(); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2249 | int nargs1 = Math.min(3, nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2250 | MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class)); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2251 | MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs1)); |
| 2252 | MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs1)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2253 | while (test.type().parameterCount() > nargs) |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2254 | // 0: test = constant(MISSING_ARG.equals(MISSING_ARG)) |
| 2255 | // 1: test = lambda (_) MISSING_ARG.equals(_) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2256 | test = MethodHandles.insertArguments(test, 0, MISSING_ARG); |
| 2257 | if (argClass != Object.class) { |
| 2258 | test = changeArgTypes(test, argClass); |
| 2259 | target = changeArgTypes(target, argClass); |
| 2260 | fallback = changeArgTypes(fallback, argClass); |
| 2261 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2262 | int testArgs = nargs - testDrops; |
| 2263 | assert(testArgs >= 0); |
| 2264 | test = addTrailingArgs(test, Math.min(testArgs, nargs), argClass); |
| 2265 | target = addTrailingArgs(target, nargs, argClass); |
| 2266 | fallback = addTrailingArgs(fallback, nargs, argClass); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2267 | Object[][] argLists = { |
| 2268 | { }, |
| 2269 | { "foo" }, { MISSING_ARG }, |
| 2270 | { "foo", "foo" }, { "foo", "bar" }, |
| 2271 | { "foo", "foo", "baz" }, { "foo", "bar", "baz" } |
| 2272 | }; |
| 2273 | for (Object[] argList : argLists) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2274 | Object[] argList1 = argList; |
| 2275 | if (argList.length != nargs) { |
| 2276 | if (argList.length != nargs1) continue; |
| 2277 | argList1 = Arrays.copyOf(argList, nargs); |
| 2278 | Arrays.fill(argList1, nargs1, nargs, MISSING_ARG_2); |
| 2279 | } |
| 2280 | MethodHandle test1 = test; |
| 2281 | if (test1.type().parameterCount() > testArgs) { |
| 2282 | int pc = test1.type().parameterCount(); |
| 2283 | test1 = MethodHandles.insertArguments(test, testArgs, Arrays.copyOfRange(argList1, testArgs, pc)); |
| 2284 | } |
| 2285 | MethodHandle mh = MethodHandles.guardWithTest(test1, target, fallback); |
| 2286 | assertEquals(target.type(), mh.type()); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2287 | boolean equals; |
| 2288 | switch (nargs) { |
| 2289 | case 0: equals = true; break; |
| 2290 | case 1: equals = MISSING_ARG.equals(argList[0]); break; |
| 2291 | default: equals = argList[0].equals(argList[1]); break; |
| 2292 | } |
| 2293 | String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2294 | if (verbosity >= 3) |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2295 | System.out.println(logEntry(willCall, argList)); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2296 | Object result = mh.invokeWithArguments(argList1); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2297 | assertCalled(willCall, argList); |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | @Test |
| 2302 | public void testCatchException() throws Throwable { |
| 2303 | if (CAN_SKIP_WORKING) return; |
| 2304 | startTest("catchException"); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2305 | for (int nargs = 0; nargs < 40; nargs++) { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2306 | if (CAN_TEST_LIGHTLY && nargs > 11) break; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2307 | for (int throwMode = 0; throwMode < THROW_MODE_LIMIT; throwMode++) { |
| 2308 | testCatchException(int.class, new ClassCastException("testing"), throwMode, nargs); |
| 2309 | if (CAN_TEST_LIGHTLY && nargs > 3) continue; |
| 2310 | testCatchException(void.class, new java.io.IOException("testing"), throwMode, nargs); |
| 2311 | testCatchException(String.class, new LinkageError("testing"), throwMode, nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | } |
| 2315 | |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2316 | static final int THROW_NOTHING = 0, THROW_CAUGHT = 1, THROW_UNCAUGHT = 2, THROW_THROUGH_ADAPTER = 3, THROW_MODE_LIMIT = 4; |
| 2317 | |
| 2318 | void testCatchException(Class<?> returnType, Throwable thrown, int throwMode, int nargs) throws Throwable { |
| 2319 | testCatchException(returnType, thrown, throwMode, nargs, 0); |
| 2320 | if (nargs <= 5 || nargs % 10 == 3) { |
| 2321 | for (int catchDrops = 1; catchDrops <= nargs; catchDrops++) |
| 2322 | testCatchException(returnType, thrown, throwMode, nargs, catchDrops); |
| 2323 | } |
| 2324 | } |
| 2325 | |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2326 | private static <T extends Throwable> |
| 2327 | Object throwOrReturn(Object normal, T exception) throws T { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2328 | if (exception != null) { |
| 2329 | called("throwOrReturn/throw", normal, exception); |
| 2330 | throw exception; |
| 2331 | } |
| 2332 | called("throwOrReturn/normal", normal, exception); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2333 | return normal; |
| 2334 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2335 | private int fakeIdentityCount; |
| 2336 | private Object fakeIdentity(Object x) { |
| 2337 | System.out.println("should throw through this!"); |
| 2338 | fakeIdentityCount++; |
| 2339 | return x; |
| 2340 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2341 | |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2342 | void testCatchException(Class<?> returnType, Throwable thrown, int throwMode, int nargs, int catchDrops) throws Throwable { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2343 | countTest(); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2344 | if (verbosity >= 3) |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2345 | System.out.println("catchException rt="+returnType+" throw="+throwMode+" nargs="+nargs+" drops="+catchDrops); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2346 | Class<? extends Throwable> exType = thrown.getClass(); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2347 | if (throwMode > THROW_CAUGHT) thrown = new UnsupportedOperationException("do not catch this"); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2348 | MethodHandle throwOrReturn |
| 2349 | = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn", |
| 2350 | MethodType.methodType(Object.class, Object.class, Throwable.class)); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2351 | if (throwMode == THROW_THROUGH_ADAPTER) { |
| 2352 | MethodHandle fakeIdentity |
| 2353 | = PRIVATE.findVirtual(MethodHandlesTest.class, "fakeIdentity", |
| 2354 | MethodType.methodType(Object.class, Object.class)).bindTo(this); |
| 2355 | for (int i = 0; i < 10; i++) |
| 2356 | throwOrReturn = MethodHandles.filterReturnValue(throwOrReturn, fakeIdentity); |
| 2357 | } |
| 2358 | int nargs1 = Math.max(2, nargs); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 2359 | MethodHandle thrower = throwOrReturn.asType(MethodType.genericMethodType(2)); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2360 | thrower = addTrailingArgs(thrower, nargs, Object.class); |
| 2361 | int catchArgc = 1 + nargs - catchDrops; |
| 2362 | MethodHandle catcher = varargsList(catchArgc).asType(MethodType.genericMethodType(catchArgc)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2363 | Object[] args = randomArgs(nargs, Object.class); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2364 | Object arg0 = MISSING_ARG; |
| 2365 | Object arg1 = (throwMode == THROW_NOTHING) ? (Throwable) null : thrown; |
| 2366 | if (nargs > 0) arg0 = args[0]; |
| 2367 | if (nargs > 1) args[1] = arg1; |
| 2368 | assertEquals(nargs1, thrower.type().parameterCount()); |
| 2369 | if (nargs < nargs1) { |
| 2370 | Object[] appendArgs = { arg0, arg1 }; |
| 2371 | appendArgs = Arrays.copyOfRange(appendArgs, nargs, nargs1); |
| 2372 | thrower = MethodHandles.insertArguments(thrower, nargs, appendArgs); |
| 2373 | } |
| 2374 | assertEquals(nargs, thrower.type().parameterCount()); |
| 2375 | MethodHandle target = MethodHandles.catchException(thrower, exType, catcher); |
| 2376 | assertEquals(thrower.type(), target.type()); |
| 2377 | assertEquals(nargs, target.type().parameterCount()); |
| 2378 | //System.out.println("catching with "+target+" : "+throwOrReturn); |
| 2379 | Object returned; |
| 2380 | try { |
| 2381 | returned = target.invokeWithArguments(args); |
| 2382 | } catch (Throwable ex) { |
| 2383 | assertSame("must get the out-of-band exception", thrown, ex); |
| 2384 | if (throwMode <= THROW_CAUGHT) |
| 2385 | assertEquals(THROW_UNCAUGHT, throwMode); |
| 2386 | returned = ex; |
| 2387 | } |
| 2388 | assertCalled("throwOrReturn/"+(throwMode == THROW_NOTHING ? "normal" : "throw"), arg0, arg1); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2389 | //System.out.println("return from "+target+" : "+returned); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2390 | if (throwMode == THROW_NOTHING) { |
| 2391 | assertSame(arg0, returned); |
| 2392 | } else if (throwMode == THROW_CAUGHT) { |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2393 | List<Object> catchArgs = new ArrayList<>(Arrays.asList(args)); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2394 | // catcher receives an initial subsequence of target arguments: |
| 2395 | catchArgs.subList(nargs - catchDrops, nargs).clear(); |
| 2396 | // catcher also receives the exception, prepended: |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2397 | catchArgs.add(0, thrown); |
| 2398 | assertEquals(catchArgs, returned); |
| 2399 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2400 | assertEquals(0, fakeIdentityCount); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | @Test |
| 2404 | public void testThrowException() throws Throwable { |
| 2405 | if (CAN_SKIP_WORKING) return; |
| 2406 | startTest("throwException"); |
| 2407 | testThrowException(int.class, new ClassCastException("testing")); |
| 2408 | testThrowException(void.class, new java.io.IOException("testing")); |
| 2409 | testThrowException(String.class, new LinkageError("testing")); |
| 2410 | } |
| 2411 | |
| 2412 | void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable { |
| 2413 | countTest(); |
| 2414 | Class<? extends Throwable> exType = thrown.getClass(); |
| 2415 | MethodHandle target = MethodHandles.throwException(returnType, exType); |
| 2416 | //System.out.println("throwing with "+target+" : "+thrown); |
| 2417 | MethodType expectedType = MethodType.methodType(returnType, exType); |
| 2418 | assertEquals(expectedType, target.type()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2419 | target = target.asType(target.type().generic()); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2420 | Throwable caught = null; |
| 2421 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2422 | Object res = target.invokeExact((Object) thrown); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2423 | fail("got "+res+" instead of throwing "+thrown); |
| 2424 | } catch (Throwable ex) { |
| 2425 | if (ex != thrown) { |
| 2426 | if (ex instanceof Error) throw (Error)ex; |
| 2427 | if (ex instanceof RuntimeException) throw (RuntimeException)ex; |
| 2428 | } |
| 2429 | caught = ex; |
| 2430 | } |
| 2431 | assertSame(thrown, caught); |
| 2432 | } |
| 2433 | |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2434 | @Test |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2435 | public void testInterfaceCast() throws Throwable { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2436 | //if (CAN_SKIP_WORKING) return; |
| 2437 | startTest("interfaceCast"); |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2438 | assert( (((Object)"foo") instanceof CharSequence)); |
| 2439 | assert(!(((Object)"foo") instanceof Iterable)); |
| 2440 | for (MethodHandle mh : new MethodHandle[]{ |
| 2441 | MethodHandles.identity(String.class), |
| 2442 | MethodHandles.identity(CharSequence.class), |
| 2443 | MethodHandles.identity(Iterable.class) |
| 2444 | }) { |
| 2445 | if (verbosity > 0) System.out.println("-- mh = "+mh); |
| 2446 | for (Class<?> ctype : new Class<?>[]{ |
| 2447 | Object.class, String.class, CharSequence.class, |
| 2448 | Number.class, Iterable.class |
| 2449 | }) { |
| 2450 | if (verbosity > 0) System.out.println("---- ctype = "+ctype.getName()); |
| 2451 | // doret docast |
| 2452 | testInterfaceCast(mh, ctype, false, false); |
| 2453 | testInterfaceCast(mh, ctype, true, false); |
| 2454 | testInterfaceCast(mh, ctype, false, true); |
| 2455 | testInterfaceCast(mh, ctype, true, true); |
| 2456 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2457 | } |
| 2458 | } |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2459 | private static Class<?> i2o(Class<?> c) { |
| 2460 | return (c.isInterface() ? Object.class : c); |
| 2461 | } |
| 2462 | public void testInterfaceCast(MethodHandle mh, Class<?> ctype, |
| 2463 | boolean doret, boolean docast) throws Throwable { |
| 2464 | MethodHandle mh0 = mh; |
| 2465 | if (verbosity > 1) |
| 2466 | System.out.println("mh="+mh+", ctype="+ctype.getName()+", doret="+doret+", docast="+docast); |
| 2467 | String normalRetVal = "normal return value"; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2468 | MethodType mt = mh.type(); |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2469 | MethodType mt0 = mt; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2470 | if (doret) mt = mt.changeReturnType(ctype); |
| 2471 | else mt = mt.changeParameterType(0, ctype); |
| 2472 | if (docast) mh = MethodHandles.explicitCastArguments(mh, mt); |
| 2473 | else mh = mh.asType(mt); |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2474 | assertEquals(mt, mh.type()); |
| 2475 | MethodType mt1 = mt; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2476 | // this bit is needed to make the interface types disappear for invokeWithArguments: |
| 2477 | mh = MethodHandles.explicitCastArguments(mh, mt.generic()); |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2478 | Class<?>[] step = { |
| 2479 | mt1.parameterType(0), // param as passed to mh at first |
| 2480 | mt0.parameterType(0), // param after incoming cast |
| 2481 | mt0.returnType(), // return value before cast |
| 2482 | mt1.returnType(), // return value after outgoing cast |
| 2483 | }; |
| 2484 | // where might a checkCast occur? |
| 2485 | boolean[] checkCast = new boolean[step.length]; |
| 2486 | // the string value must pass each step without causing an exception |
| 2487 | if (!docast) { |
| 2488 | if (!doret) { |
| 2489 | if (step[0] != step[1]) |
| 2490 | checkCast[1] = true; // incoming value is cast |
| 2491 | } else { |
| 2492 | if (step[2] != step[3]) |
| 2493 | checkCast[3] = true; // outgoing value is cast |
| 2494 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2495 | } |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2496 | boolean expectFail = false; |
| 2497 | for (int i = 0; i < step.length; i++) { |
| 2498 | Class<?> c = step[i]; |
| 2499 | if (!checkCast[i]) c = i2o(c); |
| 2500 | if (!c.isInstance(normalRetVal)) { |
| 2501 | if (verbosity > 3) |
| 2502 | System.out.println("expect failure at step "+i+" in "+Arrays.toString(step)+Arrays.toString(checkCast)); |
| 2503 | expectFail = true; |
| 2504 | break; |
| 2505 | } |
| 2506 | } |
| 2507 | countTest(!expectFail); |
| 2508 | if (verbosity > 2) |
| 2509 | System.out.println("expectFail="+expectFail+", mt="+mt); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2510 | Object res; |
| 2511 | try { |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2512 | res = mh.invokeWithArguments(normalRetVal); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2513 | } catch (Exception ex) { |
| 2514 | res = ex; |
| 2515 | } |
| 2516 | boolean sawFail = !(res instanceof String); |
| 2517 | if (sawFail != expectFail) { |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2518 | System.out.println("*** testInterfaceCast: mh0 = "+mh0); |
| 2519 | System.out.println(" retype using "+(docast ? "explicitCastArguments" : "asType")+" to "+mt+" => "+mh); |
| 2520 | System.out.println(" call returned "+res); |
| 2521 | System.out.println(" expected "+(expectFail ? "an exception" : normalRetVal)); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2522 | } |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2523 | if (!expectFail) { |
| 2524 | assertFalse(res.toString(), sawFail); |
| 2525 | assertEquals(normalRetVal, res); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2526 | } else { |
jrose | 4f019ca | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 2527 | assertTrue(res.toString(), sawFail); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2531 | @Test // SLOW |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2532 | public void testCastFailure() throws Throwable { |
| 2533 | if (CAN_SKIP_WORKING) return; |
| 2534 | startTest("testCastFailure"); |
| 2535 | testCastFailure("cast/argument", 11000); |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2536 | if (CAN_TEST_LIGHTLY) return; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2537 | testCastFailure("unbox/argument", 11000); |
| 2538 | testCastFailure("cast/return", 11000); |
| 2539 | testCastFailure("unbox/return", 11000); |
| 2540 | } |
| 2541 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2542 | static class Surprise { |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 2543 | public MethodHandle asMethodHandle() { |
| 2544 | return VALUE.bindTo(this); |
| 2545 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2546 | Object value(Object x) { |
| 2547 | trace("value", x); |
| 2548 | if (boo != null) return boo; |
| 2549 | return x; |
| 2550 | } |
| 2551 | Object boo; |
| 2552 | void boo(Object x) { boo = x; } |
| 2553 | |
| 2554 | static void trace(String x, Object y) { |
| 2555 | if (verbosity > 8) System.out.println(x+"="+y); |
| 2556 | } |
| 2557 | static Object refIdentity(Object x) { trace("ref.x", x); return x; } |
| 2558 | static Integer boxIdentity(Integer x) { trace("box.x", x); return x; } |
| 2559 | static int intIdentity(int x) { trace("int.x", x); return x; } |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 2560 | static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY; |
| 2561 | static { |
| 2562 | try { |
| 2563 | VALUE = PRIVATE.findVirtual( |
| 2564 | Surprise.class, "value", |
| 2565 | MethodType.methodType(Object.class, Object.class)); |
| 2566 | REF_IDENTITY = PRIVATE.findStatic( |
| 2567 | Surprise.class, "refIdentity", |
| 2568 | MethodType.methodType(Object.class, Object.class)); |
| 2569 | BOX_IDENTITY = PRIVATE.findStatic( |
| 2570 | Surprise.class, "boxIdentity", |
| 2571 | MethodType.methodType(Integer.class, Integer.class)); |
| 2572 | INT_IDENTITY = PRIVATE.findStatic( |
| 2573 | Surprise.class, "intIdentity", |
| 2574 | MethodType.methodType(int.class, int.class)); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2575 | } catch (NoSuchMethodException | IllegalAccessException ex) { |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 2576 | throw new RuntimeException(ex); |
| 2577 | } |
| 2578 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2581 | @SuppressWarnings("ConvertToStringSwitch") |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2582 | void testCastFailure(String mode, int okCount) throws Throwable { |
| 2583 | countTest(false); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2584 | if (verbosity > 2) System.out.println("mode="+mode); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2585 | Surprise boo = new Surprise(); |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 2586 | MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2587 | if (mode.endsWith("/return")) { |
| 2588 | if (mode.equals("unbox/return")) { |
| 2589 | // fail on return to ((Integer)surprise).intValue |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2590 | surprise = surprise.asType(MethodType.methodType(int.class, Object.class)); |
| 2591 | identity = identity.asType(MethodType.methodType(int.class, Object.class)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2592 | } else if (mode.equals("cast/return")) { |
| 2593 | // fail on return to (Integer)surprise |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2594 | surprise = surprise.asType(MethodType.methodType(Integer.class, Object.class)); |
| 2595 | identity = identity.asType(MethodType.methodType(Integer.class, Object.class)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2596 | } |
| 2597 | } else if (mode.endsWith("/argument")) { |
| 2598 | MethodHandle callee = null; |
| 2599 | if (mode.equals("unbox/argument")) { |
| 2600 | // fail on handing surprise to int argument |
| 2601 | callee = Surprise.INT_IDENTITY; |
| 2602 | } else if (mode.equals("cast/argument")) { |
| 2603 | // fail on handing surprise to Integer argument |
| 2604 | callee = Surprise.BOX_IDENTITY; |
| 2605 | } |
| 2606 | if (callee != null) { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2607 | callee = callee.asType(MethodType.genericMethodType(1)); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2608 | surprise = MethodHandles.filterArguments(callee, 0, surprise); |
| 2609 | identity = MethodHandles.filterArguments(callee, 0, identity); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2610 | } |
| 2611 | } |
jrose | a1ebbe6 | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 2612 | assertNotSame(mode, surprise, surprise0); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2613 | identity = identity.asType(MethodType.genericMethodType(1)); |
| 2614 | surprise = surprise.asType(MethodType.genericMethodType(1)); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2615 | Object x = 42; |
| 2616 | for (int i = 0; i < okCount; i++) { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2617 | Object y = identity.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2618 | assertEquals(x, y); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2619 | Object z = surprise.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2620 | assertEquals(x, z); |
| 2621 | } |
| 2622 | boo.boo("Boo!"); |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2623 | Object y = identity.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2624 | assertEquals(x, y); |
| 2625 | try { |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2626 | Object z = surprise.invokeExact(x); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2627 | System.out.println("Failed to throw; got z="+z); |
| 2628 | assertTrue(false); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 2629 | } catch (ClassCastException ex) { |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2630 | if (verbosity > 2) |
jrose | 2cc9c83 | 2010-04-30 23:48:23 -0700 | [diff] [blame] | 2631 | System.out.println("caught "+ex); |
| 2632 | if (verbosity > 3) |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2633 | ex.printStackTrace(System.out); |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 2634 | assertTrue(true); // all is well |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
jrose | 45b7a33 | 2010-05-03 23:32:47 -0700 | [diff] [blame] | 2638 | static Example userMethod(Object o, String s, int i) { |
| 2639 | called("userMethod", o, s, i); |
| 2640 | return null; |
| 2641 | } |
| 2642 | |
| 2643 | @Test |
| 2644 | public void testUserClassInSignature() throws Throwable { |
| 2645 | if (CAN_SKIP_WORKING) return; |
| 2646 | startTest("testUserClassInSignature"); |
| 2647 | Lookup lookup = MethodHandles.lookup(); |
| 2648 | String name; MethodType mt; MethodHandle mh; |
| 2649 | Object[] args; |
| 2650 | |
| 2651 | // Try a static method. |
| 2652 | name = "userMethod"; |
| 2653 | mt = MethodType.methodType(Example.class, Object.class, String.class, int.class); |
| 2654 | mh = lookup.findStatic(lookup.lookupClass(), name, mt); |
| 2655 | assertEquals(mt, mh.type()); |
| 2656 | assertEquals(Example.class, mh.type().returnType()); |
| 2657 | args = randomArgs(mh.type().parameterArray()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2658 | mh.invokeWithArguments(args); |
jrose | 45b7a33 | 2010-05-03 23:32:47 -0700 | [diff] [blame] | 2659 | assertCalled(name, args); |
| 2660 | |
| 2661 | // Try a virtual method. |
| 2662 | name = "v2"; |
| 2663 | mt = MethodType.methodType(Object.class, Object.class, int.class); |
| 2664 | mh = lookup.findVirtual(Example.class, name, mt); |
| 2665 | assertEquals(mt, mh.type().dropParameterTypes(0,1)); |
| 2666 | assertTrue(mh.type().parameterList().contains(Example.class)); |
| 2667 | args = randomArgs(mh.type().parameterArray()); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 2668 | mh.invokeWithArguments(args); |
jrose | 45b7a33 | 2010-05-03 23:32:47 -0700 | [diff] [blame] | 2669 | assertCalled(name, args); |
| 2670 | } |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2671 | |
| 2672 | static void runForRunnable() { |
| 2673 | called("runForRunnable"); |
| 2674 | } |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 2675 | public interface Fooable { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2676 | // overloads: |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2677 | Object foo(Object x, String y); |
| 2678 | List<?> foo(String x, int y); |
| 2679 | Object foo(String x); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2680 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2681 | static Object fooForFooable(String x, Object... y) { |
| 2682 | return called("fooForFooable/"+x, y); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2683 | } |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2684 | @SuppressWarnings("serial") // not really a public API, just a test case |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 2685 | public static class MyCheckedException extends Exception { |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2686 | } |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 2687 | public interface WillThrow { |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2688 | void willThrow() throws MyCheckedException; |
| 2689 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2690 | /*non-public*/ interface PrivateRunnable { |
| 2691 | public void run(); |
| 2692 | } |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2693 | |
| 2694 | @Test |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2695 | public void testAsInterfaceInstance() throws Throwable { |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2696 | if (CAN_SKIP_WORKING) return; |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2697 | startTest("asInterfaceInstance"); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2698 | Lookup lookup = MethodHandles.lookup(); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2699 | // test typical case: Runnable.run |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2700 | { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2701 | countTest(); |
| 2702 | if (verbosity >= 2) System.out.println("Runnable"); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2703 | MethodType mt = MethodType.methodType(void.class); |
| 2704 | MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "runForRunnable", mt); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2705 | Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2706 | proxy.run(); |
| 2707 | assertCalled("runForRunnable"); |
| 2708 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2709 | // well known single-name overloaded interface: Appendable.append |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2710 | { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2711 | countTest(); |
| 2712 | if (verbosity >= 2) System.out.println("Appendable"); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2713 | ArrayList<List<?>> appendResults = new ArrayList<>(); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2714 | MethodHandle append = lookup.bind(appendResults, "add", MethodType.methodType(boolean.class, Object.class)); |
| 2715 | append = append.asType(MethodType.methodType(void.class, List.class)); // specialize the type |
| 2716 | MethodHandle asList = lookup.findStatic(Arrays.class, "asList", MethodType.methodType(List.class, Object[].class)); |
| 2717 | MethodHandle mh = MethodHandles.filterReturnValue(asList, append).asVarargsCollector(Object[].class); |
| 2718 | Appendable proxy = MethodHandleProxies.asInterfaceInstance(Appendable.class, mh); |
| 2719 | proxy.append("one"); |
| 2720 | proxy.append("two", 3, 4); |
| 2721 | proxy.append('5'); |
| 2722 | assertEquals(Arrays.asList(Arrays.asList("one"), |
| 2723 | Arrays.asList("two", 3, 4), |
| 2724 | Arrays.asList('5')), |
| 2725 | appendResults); |
| 2726 | if (verbosity >= 3) System.out.println("appendResults="+appendResults); |
| 2727 | appendResults.clear(); |
| 2728 | Formatter formatter = new Formatter(proxy); |
| 2729 | String fmt = "foo str=%s char='%c' num=%d"; |
| 2730 | Object[] fmtArgs = { "str!", 'C', 42 }; |
| 2731 | String expect = String.format(fmt, fmtArgs); |
| 2732 | formatter.format(fmt, fmtArgs); |
| 2733 | String actual = ""; |
| 2734 | if (verbosity >= 3) System.out.println("appendResults="+appendResults); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2735 | for (List<?> l : appendResults) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2736 | Object x = l.get(0); |
| 2737 | switch (l.size()) { |
| 2738 | case 1: actual += x; continue; |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2739 | case 3: actual += ((String)x).substring((int)(Object)l.get(1), (int)(Object)l.get(2)); continue; |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2740 | } |
| 2741 | actual += l; |
| 2742 | } |
| 2743 | if (verbosity >= 3) System.out.println("expect="+expect); |
| 2744 | if (verbosity >= 3) System.out.println("actual="+actual); |
| 2745 | assertEquals(expect, actual); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2746 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2747 | // test case of an single name which is overloaded: Fooable.foo(...) |
| 2748 | { |
| 2749 | if (verbosity >= 2) System.out.println("Fooable"); |
| 2750 | MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "fooForFooable", |
| 2751 | MethodType.methodType(Object.class, String.class, Object[].class)); |
| 2752 | Fooable proxy = MethodHandleProxies.asInterfaceInstance(Fooable.class, mh); |
| 2753 | for (Method m : Fooable.class.getDeclaredMethods()) { |
| 2754 | countTest(); |
| 2755 | assertSame("foo", m.getName()); |
| 2756 | if (verbosity > 3) |
| 2757 | System.out.println("calling "+m); |
| 2758 | MethodHandle invoker = lookup.unreflect(m); |
| 2759 | MethodType mt = invoker.type(); |
| 2760 | Class<?>[] types = mt.parameterArray(); |
| 2761 | types[0] = int.class; // placeholder |
| 2762 | Object[] args = randomArgs(types); |
| 2763 | args[0] = proxy; |
| 2764 | if (verbosity > 3) |
| 2765 | System.out.println("calling "+m+" on "+Arrays.asList(args)); |
| 2766 | Object result = invoker.invokeWithArguments(args); |
| 2767 | if (verbosity > 4) |
| 2768 | System.out.println("result = "+result); |
| 2769 | String name = "fooForFooable/"+args[1]; |
| 2770 | Object[] argTail = Arrays.copyOfRange(args, 2, args.length); |
| 2771 | assertCalled(name, argTail); |
| 2772 | assertEquals(result, logEntry(name, argTail)); |
| 2773 | } |
| 2774 | } |
| 2775 | // test processing of thrown exceptions: |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2776 | for (Throwable ex : new Throwable[] { new NullPointerException("ok"), |
| 2777 | new InternalError("ok"), |
| 2778 | new Throwable("fail"), |
| 2779 | new Exception("fail"), |
| 2780 | new MyCheckedException() |
| 2781 | }) { |
| 2782 | MethodHandle mh = MethodHandles.throwException(void.class, Throwable.class); |
| 2783 | mh = MethodHandles.insertArguments(mh, 0, ex); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 2784 | WillThrow proxy = MethodHandleProxies.asInterfaceInstance(WillThrow.class, mh); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2785 | try { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2786 | countTest(); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2787 | proxy.willThrow(); |
| 2788 | System.out.println("Failed to throw: "+ex); |
| 2789 | assertTrue(false); |
| 2790 | } catch (Throwable ex1) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2791 | if (verbosity > 3) { |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2792 | System.out.println("throw "+ex); |
| 2793 | System.out.println("catch "+(ex == ex1 ? "UNWRAPPED" : ex1)); |
| 2794 | } |
| 2795 | if (ex instanceof RuntimeException || |
| 2796 | ex instanceof Error) { |
| 2797 | assertSame("must pass unchecked exception out without wrapping", ex, ex1); |
| 2798 | } else if (ex instanceof MyCheckedException) { |
| 2799 | assertSame("must pass declared exception out without wrapping", ex, ex1); |
| 2800 | } else { |
| 2801 | assertNotSame("must pass undeclared checked exception with wrapping", ex, ex1); |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 2802 | if (!(ex1 instanceof UndeclaredThrowableException) || ex1.getCause() != ex) { |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2803 | ex1.printStackTrace(System.out); |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 2804 | } |
| 2805 | assertSame(ex, ex1.getCause()); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2806 | UndeclaredThrowableException utex = (UndeclaredThrowableException) ex1; |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | } |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2810 | // Test error checking on bad interfaces: |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2811 | for (Class<?> nonSMI : new Class<?>[] { Object.class, |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2812 | String.class, |
| 2813 | CharSequence.class, |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2814 | java.io.Serializable.class, |
| 2815 | PrivateRunnable.class, |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2816 | Example.class }) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2817 | if (verbosity > 2) System.out.println(nonSMI.getName()); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2818 | try { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2819 | countTest(false); |
| 2820 | MethodHandleProxies.asInterfaceInstance(nonSMI, varargsArray(0)); |
| 2821 | assertTrue("Failed to throw on "+nonSMI.getName(), false); |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2822 | } catch (IllegalArgumentException ex) { |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2823 | if (verbosity > 2) System.out.println(nonSMI.getSimpleName()+": "+ex); |
| 2824 | // Object: java.lang.IllegalArgumentException: |
| 2825 | // not a public interface: java.lang.Object |
| 2826 | // String: java.lang.IllegalArgumentException: |
| 2827 | // not a public interface: java.lang.String |
| 2828 | // CharSequence: java.lang.IllegalArgumentException: |
| 2829 | // not a single-method interface: java.lang.CharSequence |
| 2830 | // Serializable: java.lang.IllegalArgumentException: |
| 2831 | // not a single-method interface: java.io.Serializable |
| 2832 | // PrivateRunnable: java.lang.IllegalArgumentException: |
| 2833 | // not a public interface: test.java.lang.invoke.MethodHandlesTest$PrivateRunnable |
| 2834 | // Example: java.lang.IllegalArgumentException: |
| 2835 | // not a public interface: test.java.lang.invoke.MethodHandlesTest$Example |
| 2836 | } |
| 2837 | } |
| 2838 | // Test error checking on interfaces with the wrong method type: |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2839 | for (Class<?> intfc : new Class<?>[] { Runnable.class /*arity 0*/, |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 2840 | Fooable.class /*arity 1 & 2*/ }) { |
| 2841 | int badArity = 1; // known to be incompatible |
| 2842 | if (verbosity > 2) System.out.println(intfc.getName()); |
| 2843 | try { |
| 2844 | countTest(false); |
| 2845 | MethodHandleProxies.asInterfaceInstance(intfc, varargsArray(badArity)); |
| 2846 | assertTrue("Failed to throw on "+intfc.getName(), false); |
| 2847 | } catch (WrongMethodTypeException ex) { |
| 2848 | if (verbosity > 2) System.out.println(intfc.getSimpleName()+": "+ex); |
| 2849 | // Runnable: java.lang.invoke.WrongMethodTypeException: |
| 2850 | // cannot convert MethodHandle(Object)Object[] to ()void |
| 2851 | // Fooable: java.lang.invoke.WrongMethodTypeException: |
| 2852 | // cannot convert MethodHandle(Object)Object[] to (Object,String)Object |
jrose | be2db60 | 2010-09-08 18:40:34 -0700 | [diff] [blame] | 2853 | } |
| 2854 | } |
| 2855 | } |
jrose | 43b8c12 | 2011-07-16 15:40:13 -0700 | [diff] [blame] | 2856 | |
| 2857 | @Test |
| 2858 | public void testRunnableProxy() throws Throwable { |
| 2859 | if (CAN_SKIP_WORKING) return; |
| 2860 | startTest("testRunnableProxy"); |
| 2861 | MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 2862 | MethodHandle run = lookup.findStatic(lookup.lookupClass(), "runForRunnable", MethodType.methodType(void.class)); |
| 2863 | Runnable r = MethodHandleProxies.asInterfaceInstance(Runnable.class, run); |
| 2864 | testRunnableProxy(r); |
| 2865 | assertCalled("runForRunnable"); |
| 2866 | } |
| 2867 | private static void testRunnableProxy(Runnable r) { |
| 2868 | //7058630: JSR 292 method handle proxy violates contract for Object methods |
| 2869 | r.run(); |
| 2870 | Object o = r; |
| 2871 | r = null; |
| 2872 | boolean eq = (o == o); |
| 2873 | int hc = System.identityHashCode(o); |
| 2874 | String st = o.getClass().getName() + "@" + Integer.toHexString(hc); |
| 2875 | Object expect = Arrays.asList(st, eq, hc); |
| 2876 | if (verbosity >= 2) System.out.println("expect st/eq/hc = "+expect); |
| 2877 | Object actual = Arrays.asList(o.toString(), o.equals(o), o.hashCode()); |
| 2878 | if (verbosity >= 2) System.out.println("actual st/eq/hc = "+actual); |
| 2879 | assertEquals(expect, actual); |
| 2880 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2881 | } |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 2882 | // Local abbreviated copy of sun.invoke.util.ValueConversions |
twisti | 10d37ec | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 2883 | // This guy tests access from outside the same package member, but inside |
| 2884 | // the package itself. |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2885 | class ValueConversions { |
| 2886 | private static final Lookup IMPL_LOOKUP = MethodHandles.lookup(); |
| 2887 | private static final Object[] NO_ARGS_ARRAY = {}; |
| 2888 | private static Object[] makeArray(Object... args) { return args; } |
| 2889 | private static Object[] array() { return NO_ARGS_ARRAY; } |
| 2890 | private static Object[] array(Object a0) |
| 2891 | { return makeArray(a0); } |
| 2892 | private static Object[] array(Object a0, Object a1) |
| 2893 | { return makeArray(a0, a1); } |
| 2894 | private static Object[] array(Object a0, Object a1, Object a2) |
| 2895 | { return makeArray(a0, a1, a2); } |
| 2896 | private static Object[] array(Object a0, Object a1, Object a2, Object a3) |
| 2897 | { return makeArray(a0, a1, a2, a3); } |
| 2898 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2899 | Object a4) |
| 2900 | { return makeArray(a0, a1, a2, a3, a4); } |
| 2901 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2902 | Object a4, Object a5) |
| 2903 | { return makeArray(a0, a1, a2, a3, a4, a5); } |
| 2904 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2905 | Object a4, Object a5, Object a6) |
| 2906 | { return makeArray(a0, a1, a2, a3, a4, a5, a6); } |
| 2907 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2908 | Object a4, Object a5, Object a6, Object a7) |
| 2909 | { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); } |
| 2910 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2911 | Object a4, Object a5, Object a6, Object a7, |
| 2912 | Object a8) |
| 2913 | { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); } |
| 2914 | private static Object[] array(Object a0, Object a1, Object a2, Object a3, |
| 2915 | Object a4, Object a5, Object a6, Object a7, |
| 2916 | Object a8, Object a9) |
| 2917 | { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } |
| 2918 | static MethodHandle[] makeArrays() { |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 2919 | ArrayList<MethodHandle> arrays = new ArrayList<>(); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2920 | MethodHandles.Lookup lookup = IMPL_LOOKUP; |
| 2921 | for (;;) { |
| 2922 | int nargs = arrays.size(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2923 | MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2924 | String name = "array"; |
| 2925 | MethodHandle array = null; |
| 2926 | try { |
| 2927 | array = lookup.findStatic(ValueConversions.class, name, type); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 2928 | } catch (ReflectiveOperationException ex) { |
| 2929 | // break from loop! |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2930 | } |
| 2931 | if (array == null) break; |
| 2932 | arrays.add(array); |
| 2933 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 2934 | assertTrue(arrays.size() == 11); // current number of methods |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2935 | return arrays.toArray(new MethodHandle[0]); |
| 2936 | } |
| 2937 | static final MethodHandle[] ARRAYS = makeArrays(); |
| 2938 | |
| 2939 | /** Return a method handle that takes the indicated number of Object |
| 2940 | * arguments and returns an Object array of them, as if for varargs. |
| 2941 | */ |
| 2942 | public static MethodHandle varargsArray(int nargs) { |
| 2943 | if (nargs < ARRAYS.length) |
| 2944 | return ARRAYS[nargs]; |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 2945 | return MethodHandles.identity(Object[].class).asCollector(Object[].class, nargs); |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 2946 | } |
jrose | 320b769 | 2011-05-12 19:27:49 -0700 | [diff] [blame] | 2947 | public static MethodHandle varargsArray(Class<?> arrayType, int nargs) { |
| 2948 | Class<?> elemType = arrayType.getComponentType(); |
| 2949 | MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)); |
| 2950 | MethodHandle mh = varargsArray(nargs); |
| 2951 | if (arrayType != Object[].class) |
| 2952 | mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType)); |
| 2953 | return mh.asType(vaType); |
| 2954 | } |
| 2955 | static Object changeArrayType(Class<?> arrayType, Object[] a) { |
| 2956 | Class<?> elemType = arrayType.getComponentType(); |
| 2957 | if (!elemType.isPrimitive()) |
| 2958 | return Arrays.copyOf(a, a.length, arrayType.asSubclass(Object[].class)); |
| 2959 | Object b = java.lang.reflect.Array.newInstance(elemType, a.length); |
| 2960 | for (int i = 0; i < a.length; i++) |
| 2961 | java.lang.reflect.Array.set(b, i, a[i]); |
| 2962 | return b; |
| 2963 | } |
| 2964 | private static final MethodHandle CHANGE_ARRAY_TYPE; |
| 2965 | static { |
| 2966 | try { |
| 2967 | CHANGE_ARRAY_TYPE = IMPL_LOOKUP.findStatic(ValueConversions.class, "changeArrayType", |
| 2968 | MethodType.methodType(Object.class, Class.class, Object[].class)); |
| 2969 | } catch (NoSuchMethodException | IllegalAccessException ex) { |
| 2970 | Error err = new InternalError("uncaught exception"); |
| 2971 | err.initCause(ex); |
| 2972 | throw err; |
| 2973 | } |
| 2974 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 2975 | |
| 2976 | private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY); |
| 2977 | private static List<Object> makeList(Object... args) { return Arrays.asList(args); } |
| 2978 | private static List<Object> list() { return NO_ARGS_LIST; } |
| 2979 | private static List<Object> list(Object a0) |
| 2980 | { return makeList(a0); } |
| 2981 | private static List<Object> list(Object a0, Object a1) |
| 2982 | { return makeList(a0, a1); } |
| 2983 | private static List<Object> list(Object a0, Object a1, Object a2) |
| 2984 | { return makeList(a0, a1, a2); } |
| 2985 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3) |
| 2986 | { return makeList(a0, a1, a2, a3); } |
| 2987 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2988 | Object a4) |
| 2989 | { return makeList(a0, a1, a2, a3, a4); } |
| 2990 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2991 | Object a4, Object a5) |
| 2992 | { return makeList(a0, a1, a2, a3, a4, a5); } |
| 2993 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2994 | Object a4, Object a5, Object a6) |
| 2995 | { return makeList(a0, a1, a2, a3, a4, a5, a6); } |
| 2996 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 2997 | Object a4, Object a5, Object a6, Object a7) |
| 2998 | { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); } |
| 2999 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 3000 | Object a4, Object a5, Object a6, Object a7, |
| 3001 | Object a8) |
| 3002 | { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); } |
| 3003 | private static List<Object> list(Object a0, Object a1, Object a2, Object a3, |
| 3004 | Object a4, Object a5, Object a6, Object a7, |
| 3005 | Object a8, Object a9) |
| 3006 | { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } |
| 3007 | static MethodHandle[] makeLists() { |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 3008 | ArrayList<MethodHandle> lists = new ArrayList<>(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3009 | MethodHandles.Lookup lookup = IMPL_LOOKUP; |
| 3010 | for (;;) { |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 3011 | int nargs = lists.size(); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3012 | MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class); |
| 3013 | String name = "list"; |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 3014 | MethodHandle list = null; |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3015 | try { |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 3016 | list = lookup.findStatic(ValueConversions.class, name, type); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 3017 | } catch (ReflectiveOperationException ex) { |
| 3018 | // break from loop! |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3019 | } |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 3020 | if (list == null) break; |
| 3021 | lists.add(list); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3022 | } |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 3023 | assertTrue(lists.size() == 11); // current number of methods |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 3024 | return lists.toArray(new MethodHandle[0]); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3025 | } |
| 3026 | static final MethodHandle[] LISTS = makeLists(); |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 3027 | static final MethodHandle AS_LIST; |
| 3028 | static { |
| 3029 | try { |
| 3030 | AS_LIST = IMPL_LOOKUP.findStatic(Arrays.class, "asList", MethodType.methodType(List.class, Object[].class)); |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 3031 | } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); } |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 3032 | } |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3033 | |
| 3034 | /** Return a method handle that takes the indicated number of Object |
| 3035 | * arguments and returns List. |
| 3036 | */ |
| 3037 | public static MethodHandle varargsList(int nargs) { |
| 3038 | if (nargs < LISTS.length) |
| 3039 | return LISTS[nargs]; |
jrose | af75194 | 2011-06-14 22:47:12 -0700 | [diff] [blame] | 3040 | return AS_LIST.asCollector(Object[].class, nargs); |
jrose | 10f3b68 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 3041 | } |
jrose | 55220c3 | 2009-10-21 23:19:48 -0700 | [diff] [blame] | 3042 | } |
| 3043 | // This guy tests access from outside the same package member, but inside |
| 3044 | // the package itself. |
| 3045 | class PackageSibling { |
| 3046 | static Lookup lookup() { |
| 3047 | return MethodHandles.lookup(); |
| 3048 | } |
| 3049 | } |