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