jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 1 | /* |
alanb | 0d05823 | 2012-11-02 15:50:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
jrose | 900bafd | 2010-10-30 21:08:23 -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 |
| 7 | * published by the Free Software Foundation. Oracle designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Oracle in the LICENSE file that accompanied this code. |
| 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 | * |
| 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. |
| 24 | */ |
| 25 | |
| 26 | /* @test |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 27 | * @summary example code used in javadoc for java.lang.invoke API |
jrose | 9633f5f | 2011-04-07 22:07:06 -0700 | [diff] [blame] | 28 | * @compile JavaDocExamplesTest.java |
henryjen | f6613f4 | 2013-08-12 12:11:04 -0700 | [diff] [blame] | 29 | * @run testng/othervm test.java.lang.invoke.JavaDocExamplesTest |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 32 | package test.java.lang.invoke; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 33 | |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 34 | import java.lang.invoke.*; |
| 35 | import static java.lang.invoke.MethodHandles.*; |
| 36 | import static java.lang.invoke.MethodType.*; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 37 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 38 | import java.util.*; |
| 39 | |
henryjen | f6613f4 | 2013-08-12 12:11:04 -0700 | [diff] [blame] | 40 | import org.testng.*; |
| 41 | import static org.testng.AssertJUnit.*; |
| 42 | import org.testng.annotations.*; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * @author jrose |
| 46 | */ |
| 47 | public class JavaDocExamplesTest { |
henryjen | f6613f4 | 2013-08-12 12:11:04 -0700 | [diff] [blame] | 48 | /** Wrapper for running the TestNG tests in this module. |
| 49 | * Put TestNG on the classpath! |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 50 | */ |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 51 | public static void main(String... ignore) throws Throwable { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 52 | new JavaDocExamplesTest().run(); |
| 53 | } |
henryjen | f6613f4 | 2013-08-12 12:11:04 -0700 | [diff] [blame] | 54 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 55 | public void run() throws Throwable { |
jrose | 942f3d3 | 2013-10-05 05:30:39 -0700 | [diff] [blame] | 56 | testMisc(); |
| 57 | testFindStatic(); |
| 58 | testFindConstructor(); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 59 | testFindVirtual(); |
jrose | 942f3d3 | 2013-10-05 05:30:39 -0700 | [diff] [blame] | 60 | testFindSpecial(); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 61 | testPermuteArguments(); |
| 62 | testDropArguments(); |
| 63 | testFilterArguments(); |
| 64 | testFoldArguments(); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 65 | testFoldArguments2(); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 66 | testMethodHandlesSummary(); |
| 67 | testAsSpreader(); |
| 68 | testAsCollector(); |
| 69 | testAsVarargsCollector(); |
| 70 | testAsFixedArity(); |
| 71 | testAsTypeCornerCases(); |
| 72 | testMutableCallSite(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 73 | } |
| 74 | // How much output? |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 75 | static final Class<?> THIS_CLASS = JavaDocExamplesTest.class; |
| 76 | static int verbosity = Integer.getInteger(THIS_CLASS.getSimpleName()+".verbosity", 0); |
| 77 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 78 | |
| 79 | {} |
| 80 | static final private Lookup LOOKUP = lookup(); |
| 81 | // static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class, |
| 82 | // "concat", methodType(String.class, String.class)); |
| 83 | // static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class, |
| 84 | // "hashCode", methodType(int.class)); |
| 85 | |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 86 | // form required if ReflectiveOperationException is intercepted: |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 87 | static final private MethodHandle CONCAT_2, HASHCODE_2, ADD_2, SUB_2; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 88 | static { |
| 89 | try { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 90 | Class<?> THIS_CLASS = LOOKUP.lookupClass(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 91 | CONCAT_2 = LOOKUP.findVirtual(String.class, |
| 92 | "concat", methodType(String.class, String.class)); |
| 93 | HASHCODE_2 = LOOKUP.findVirtual(Object.class, |
| 94 | "hashCode", methodType(int.class)); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 95 | ADD_2 = LOOKUP.findStatic(THIS_CLASS, "add", methodType(int.class, int.class, int.class)); |
| 96 | SUB_2 = LOOKUP.findStatic(THIS_CLASS, "sub", methodType(int.class, int.class, int.class)); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 97 | } catch (ReflectiveOperationException ex) { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 98 | throw new RuntimeException(ex); |
| 99 | } |
| 100 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 101 | static int add(int x, int y) { return x + y; } |
| 102 | static int sub(int x, int y) { return x - y; } |
| 103 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 104 | {} |
| 105 | |
jrose | 942f3d3 | 2013-10-05 05:30:39 -0700 | [diff] [blame] | 106 | @Test public void testMisc() throws Throwable { |
| 107 | // Extra tests, not from javadoc: |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 108 | {} |
| 109 | MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class, |
| 110 | "concat", methodType(String.class, String.class)); |
| 111 | MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class, |
| 112 | "hashCode", methodType(int.class)); |
| 113 | //assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y")); |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame] | 114 | assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y")); |
| 115 | assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y")); |
| 116 | //assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy")); |
| 117 | assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy")); |
| 118 | assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 119 | {} |
| 120 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 121 | |
jrose | 942f3d3 | 2013-10-05 05:30:39 -0700 | [diff] [blame] | 122 | @Test public void testFindStatic() throws Throwable { |
| 123 | {} |
| 124 | MethodHandle MH_asList = publicLookup().findStatic(Arrays.class, |
| 125 | "asList", methodType(List.class, Object[].class)); |
| 126 | assertEquals("[x, y]", MH_asList.invoke("x", "y").toString()); |
| 127 | {} |
| 128 | } |
| 129 | |
| 130 | @Test public void testFindVirtual() throws Throwable { |
| 131 | {} |
| 132 | MethodHandle MH_concat = publicLookup().findVirtual(String.class, |
| 133 | "concat", methodType(String.class, String.class)); |
| 134 | MethodHandle MH_hashCode = publicLookup().findVirtual(Object.class, |
| 135 | "hashCode", methodType(int.class)); |
| 136 | MethodHandle MH_hashCode_String = publicLookup().findVirtual(String.class, |
| 137 | "hashCode", methodType(int.class)); |
| 138 | assertEquals("xy", (String) MH_concat.invokeExact("x", "y")); |
| 139 | assertEquals("xy".hashCode(), (int) MH_hashCode.invokeExact((Object)"xy")); |
| 140 | assertEquals("xy".hashCode(), (int) MH_hashCode_String.invokeExact("xy")); |
| 141 | // interface method: |
| 142 | MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class, |
| 143 | "subSequence", methodType(CharSequence.class, int.class, int.class)); |
| 144 | assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString()); |
| 145 | // constructor "internal method" must be accessed differently: |
| 146 | MethodType MT_newString = methodType(void.class); //()V for new String() |
| 147 | try { assertEquals("impossible", lookup() |
| 148 | .findVirtual(String.class, "<init>", MT_newString)); |
| 149 | } catch (NoSuchMethodException ex) { } // OK |
| 150 | MethodHandle MH_newString = publicLookup() |
| 151 | .findConstructor(String.class, MT_newString); |
| 152 | assertEquals("", (String) MH_newString.invokeExact()); |
| 153 | {} |
| 154 | } |
| 155 | |
| 156 | @Test public void testFindConstructor() throws Throwable { |
| 157 | {} |
| 158 | MethodHandle MH_newArrayList = publicLookup().findConstructor( |
| 159 | ArrayList.class, methodType(void.class, Collection.class)); |
| 160 | Collection orig = Arrays.asList("x", "y"); |
| 161 | Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig); |
| 162 | assert(orig != copy); |
| 163 | assertEquals(orig, copy); |
| 164 | // a variable-arity constructor: |
| 165 | MethodHandle MH_newProcessBuilder = publicLookup().findConstructor( |
| 166 | ProcessBuilder.class, methodType(void.class, String[].class)); |
| 167 | ProcessBuilder pb = (ProcessBuilder) |
| 168 | MH_newProcessBuilder.invoke("x", "y", "z"); |
| 169 | assertEquals("[x, y, z]", pb.command().toString()); |
| 170 | {} |
| 171 | } |
| 172 | |
| 173 | // for testFindSpecial |
| 174 | {} |
| 175 | static class Listie extends ArrayList { |
| 176 | public String toString() { return "[wee Listie]"; } |
| 177 | static Lookup lookup() { return MethodHandles.lookup(); } |
| 178 | } |
| 179 | {} |
| 180 | |
| 181 | @Test public void testFindSpecial() throws Throwable { |
| 182 | {} |
| 183 | // no access to constructor via invokeSpecial: |
| 184 | MethodHandle MH_newListie = Listie.lookup() |
| 185 | .findConstructor(Listie.class, methodType(void.class)); |
| 186 | Listie l = (Listie) MH_newListie.invokeExact(); |
| 187 | try { assertEquals("impossible", Listie.lookup().findSpecial( |
| 188 | Listie.class, "<init>", methodType(void.class), Listie.class)); |
| 189 | } catch (NoSuchMethodException ex) { } // OK |
| 190 | // access to super and self methods via invokeSpecial: |
| 191 | MethodHandle MH_super = Listie.lookup().findSpecial( |
| 192 | ArrayList.class, "toString" , methodType(String.class), Listie.class); |
| 193 | MethodHandle MH_this = Listie.lookup().findSpecial( |
| 194 | Listie.class, "toString" , methodType(String.class), Listie.class); |
| 195 | MethodHandle MH_duper = Listie.lookup().findSpecial( |
| 196 | Object.class, "toString" , methodType(String.class), Listie.class); |
| 197 | assertEquals("[]", (String) MH_super.invokeExact(l)); |
| 198 | assertEquals(""+l, (String) MH_this.invokeExact(l)); |
| 199 | assertEquals("[]", (String) MH_duper.invokeExact(l)); // ArrayList method |
| 200 | try { assertEquals("inaccessible", Listie.lookup().findSpecial( |
| 201 | String.class, "toString", methodType(String.class), Listie.class)); |
| 202 | } catch (IllegalAccessException ex) { } // OK |
| 203 | Listie subl = new Listie() { public String toString() { return "[subclass]"; } }; |
| 204 | assertEquals(""+l, (String) MH_this.invokeExact(subl)); // Listie method |
| 205 | {} |
| 206 | } |
| 207 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 208 | @Test public void testPermuteArguments() throws Throwable { |
| 209 | {{ |
| 210 | {} /// JAVADOC |
| 211 | MethodType intfn1 = methodType(int.class, int.class); |
| 212 | MethodType intfn2 = methodType(int.class, int.class, int.class); |
| 213 | MethodHandle sub = SUB_2;// ... {int x, int y => x-y} ...; |
| 214 | assert(sub.type().equals(intfn2)); |
| 215 | MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1); |
| 216 | MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0); |
| 217 | assert((int)rsub.invokeExact(1, 100) == 99); |
| 218 | MethodHandle add = ADD_2;// ... {int x, int y => x+y} ...; |
| 219 | assert(add.type().equals(intfn2)); |
| 220 | MethodHandle twice = permuteArguments(add, intfn1, 0, 0); |
| 221 | assert(twice.type().equals(intfn1)); |
| 222 | assert((int)twice.invokeExact(21) == 42); |
| 223 | }} |
| 224 | {{ |
| 225 | {} /// JAVADOC |
| 226 | MethodHandle cat = lookup().findVirtual(String.class, |
| 227 | "concat", methodType(String.class, String.class)); |
| 228 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
| 229 | MethodHandle d0 = dropArguments(cat, 0, String.class); |
| 230 | assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
| 231 | MethodHandle d1 = dropArguments(cat, 1, String.class); |
| 232 | assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
| 233 | MethodHandle d2 = dropArguments(cat, 2, String.class); |
| 234 | assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
| 235 | MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
| 236 | assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
| 237 | }} |
| 238 | } |
| 239 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 240 | @Test public void testDropArguments() throws Throwable { |
| 241 | {{ |
| 242 | {} /// JAVADOC |
| 243 | MethodHandle cat = lookup().findVirtual(String.class, |
| 244 | "concat", methodType(String.class, String.class)); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 245 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 246 | MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class); |
| 247 | MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2)); |
| 248 | assertEquals(bigType, d0.type()); |
| 249 | assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z")); |
| 250 | }} |
| 251 | {{ |
| 252 | {} /// JAVADOC |
| 253 | MethodHandle cat = lookup().findVirtual(String.class, |
| 254 | "concat", methodType(String.class, String.class)); |
| 255 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 256 | MethodHandle d0 = dropArguments(cat, 0, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 257 | assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 258 | MethodHandle d1 = dropArguments(cat, 1, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 259 | assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 260 | MethodHandle d2 = dropArguments(cat, 2, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 261 | assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 262 | MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 263 | assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 264 | }} |
| 265 | } |
| 266 | |
| 267 | @Test public void testFilterArguments() throws Throwable { |
| 268 | {{ |
| 269 | {} /// JAVADOC |
| 270 | MethodHandle cat = lookup().findVirtual(String.class, |
| 271 | "concat", methodType(String.class, String.class)); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 272 | MethodHandle upcase = lookup().findVirtual(String.class, |
| 273 | "toUpperCase", methodType(String.class)); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 274 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 275 | MethodHandle f0 = filterArguments(cat, 0, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 276 | assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 277 | MethodHandle f1 = filterArguments(cat, 1, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 278 | assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 279 | MethodHandle f2 = filterArguments(cat, 0, upcase, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 280 | assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 281 | }} |
| 282 | } |
| 283 | |
jrose | 1ccb8a7 | 2013-10-05 05:30:39 -0700 | [diff] [blame] | 284 | @Test public void testCollectArguments() throws Throwable { |
| 285 | {{ |
| 286 | {} /// JAVADOC |
| 287 | MethodHandle deepToString = publicLookup() |
| 288 | .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
| 289 | MethodHandle ts1 = deepToString.asCollector(String[].class, 1); |
| 290 | assertEquals("[strange]", (String) ts1.invokeExact("strange")); |
| 291 | MethodHandle ts2 = deepToString.asCollector(String[].class, 2); |
| 292 | assertEquals("[up, down]", (String) ts2.invokeExact("up", "down")); |
| 293 | MethodHandle ts3 = deepToString.asCollector(String[].class, 3); |
| 294 | MethodHandle ts3_ts2 = collectArguments(ts3, 1, ts2); |
| 295 | assertEquals("[top, [up, down], strange]", |
| 296 | (String) ts3_ts2.invokeExact("top", "up", "down", "strange")); |
| 297 | MethodHandle ts3_ts2_ts1 = collectArguments(ts3_ts2, 3, ts1); |
| 298 | assertEquals("[top, [up, down], [strange]]", |
| 299 | (String) ts3_ts2_ts1.invokeExact("top", "up", "down", "strange")); |
| 300 | MethodHandle ts3_ts2_ts3 = collectArguments(ts3_ts2, 1, ts3); |
| 301 | assertEquals("[top, [[up, down, strange], charm], bottom]", |
| 302 | (String) ts3_ts2_ts3.invokeExact("top", "up", "down", "strange", "charm", "bottom")); |
| 303 | }} |
| 304 | } |
| 305 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 306 | @Test public void testFoldArguments() throws Throwable { |
| 307 | {{ |
| 308 | {} /// JAVADOC |
| 309 | MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class, |
| 310 | "println", methodType(void.class, String.class)) |
| 311 | .bindTo(System.out); |
| 312 | MethodHandle cat = lookup().findVirtual(String.class, |
| 313 | "concat", methodType(String.class, String.class)); |
| 314 | assertEquals("boojum", (String) cat.invokeExact("boo", "jum")); |
| 315 | MethodHandle catTrace = foldArguments(cat, trace); |
| 316 | // also prints "boo": |
| 317 | assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum")); |
| 318 | }} |
| 319 | } |
| 320 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 321 | static void assertEquals(Object exp, Object act) { |
| 322 | if (verbosity > 0) |
| 323 | System.out.println("result: "+act); |
| 324 | Assert.assertEquals(exp, act); |
| 325 | } |
| 326 | |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 327 | @Test public void testMethodHandlesSummary() throws Throwable { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 328 | {{ |
| 329 | {} /// JAVADOC |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 330 | Object x, y; String s; int i; |
| 331 | MethodType mt; MethodHandle mh; |
| 332 | MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 333 | // mt is (char,char)String |
| 334 | mt = MethodType.methodType(String.class, char.class, char.class); |
| 335 | mh = lookup.findVirtual(String.class, "replace", mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 336 | s = (String) mh.invokeExact("daddy",'d','n'); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 337 | // invokeExact(Ljava/lang/String;CC)Ljava/lang/String; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 338 | assertEquals(s, "nanny"); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 339 | // weakly typed invocation (using MHs.invoke) |
| 340 | s = (String) mh.invokeWithArguments("sappy", 'p', 'v'); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 341 | assertEquals(s, "savvy"); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 342 | // mt is (Object[])List |
| 343 | mt = MethodType.methodType(java.util.List.class, Object[].class); |
| 344 | mh = lookup.findStatic(java.util.Arrays.class, "asList", mt); |
| 345 | assert(mh.isVarargsCollector()); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 346 | x = mh.invoke("one", "two"); |
| 347 | // invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 348 | assertEquals(x, java.util.Arrays.asList("one","two")); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 349 | // mt is (Object,Object,Object)Object |
| 350 | mt = MethodType.genericMethodType(3); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 351 | mh = mh.asType(mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 352 | x = mh.invokeExact((Object)1, (Object)2, (Object)3); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 353 | // invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 354 | assertEquals(x, java.util.Arrays.asList(1,2,3)); |
| 355 | // mt is ()int |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 356 | mt = MethodType.methodType(int.class); |
| 357 | mh = lookup.findVirtual(java.util.List.class, "size", mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 358 | i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3)); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 359 | // invokeExact(Ljava/util/List;)I |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 360 | assert(i == 3); |
| 361 | mt = MethodType.methodType(void.class, String.class); |
| 362 | mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt); |
| 363 | mh.invokeExact(System.out, "Hello, world."); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 364 | // invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 365 | {} |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 366 | }} |
| 367 | } |
| 368 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 369 | @Test public void testAsSpreader() throws Throwable { |
| 370 | {{ |
| 371 | {} /// JAVADOC |
| 372 | MethodHandle equals = publicLookup() |
| 373 | .findVirtual(String.class, "equals", methodType(boolean.class, Object.class)); |
| 374 | assert( (boolean) equals.invokeExact("me", (Object)"me")); |
| 375 | assert(!(boolean) equals.invokeExact("me", (Object)"thee")); |
| 376 | // spread both arguments from a 2-array: |
| 377 | MethodHandle eq2 = equals.asSpreader(Object[].class, 2); |
| 378 | assert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" })); |
| 379 | assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" })); |
jrose | 70feaa9 | 2013-10-05 05:30:39 -0700 | [diff] [blame] | 380 | // try to spread from anything but a 2-array: |
| 381 | for (int n = 0; n <= 10; n++) { |
| 382 | Object[] badArityArgs = (n == 2 ? null : new Object[n]); |
| 383 | try { assert((boolean) eq2.invokeExact(badArityArgs) && false); } |
| 384 | catch (IllegalArgumentException ex) { } // OK |
| 385 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 386 | // spread both arguments from a String array: |
| 387 | MethodHandle eq2s = equals.asSpreader(String[].class, 2); |
| 388 | assert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" })); |
| 389 | assert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" })); |
| 390 | // spread second arguments from a 1-array: |
| 391 | MethodHandle eq1 = equals.asSpreader(Object[].class, 1); |
| 392 | assert( (boolean) eq1.invokeExact("me", new Object[]{ "me" })); |
| 393 | assert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" })); |
| 394 | // spread no arguments from a 0-array or null: |
| 395 | MethodHandle eq0 = equals.asSpreader(Object[].class, 0); |
| 396 | assert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0])); |
| 397 | assert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null)); |
| 398 | // asSpreader and asCollector are approximate inverses: |
| 399 | for (int n = 0; n <= 2; n++) { |
| 400 | for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) { |
| 401 | MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n); |
| 402 | assert( (boolean) equals2.invokeWithArguments("me", "me")); |
| 403 | assert(!(boolean) equals2.invokeWithArguments("me", "thee")); |
| 404 | } |
| 405 | } |
| 406 | MethodHandle caToString = publicLookup() |
| 407 | .findStatic(Arrays.class, "toString", methodType(String.class, char[].class)); |
| 408 | assertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray())); |
| 409 | MethodHandle caString3 = caToString.asCollector(char[].class, 3); |
| 410 | assertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C')); |
| 411 | MethodHandle caToString2 = caString3.asSpreader(char[].class, 2); |
| 412 | assertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray())); |
| 413 | }} |
| 414 | } |
| 415 | |
| 416 | @Test public void testAsCollector() throws Throwable { |
| 417 | {{ |
| 418 | {} /// JAVADOC |
| 419 | MethodHandle deepToString = publicLookup() |
| 420 | .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
| 421 | assertEquals("[won]", (String) deepToString.invokeExact(new Object[]{"won"})); |
| 422 | MethodHandle ts1 = deepToString.asCollector(Object[].class, 1); |
| 423 | assertEquals(methodType(String.class, Object.class), ts1.type()); |
| 424 | //assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); //FAIL |
| 425 | assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"})); |
| 426 | // arrayType can be a subtype of Object[] |
| 427 | MethodHandle ts2 = deepToString.asCollector(String[].class, 2); |
| 428 | assertEquals(methodType(String.class, String.class, String.class), ts2.type()); |
| 429 | assertEquals("[two, too]", (String) ts2.invokeExact("two", "too")); |
| 430 | MethodHandle ts0 = deepToString.asCollector(Object[].class, 0); |
| 431 | assertEquals("[]", (String) ts0.invokeExact()); |
| 432 | // collectors can be nested, Lisp-style |
| 433 | MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2); |
| 434 | assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D"))); |
| 435 | // arrayType can be any primitive array type |
| 436 | MethodHandle bytesToString = publicLookup() |
| 437 | .findStatic(Arrays.class, "toString", methodType(String.class, byte[].class)) |
| 438 | .asCollector(byte[].class, 3); |
| 439 | assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3)); |
| 440 | MethodHandle longsToString = publicLookup() |
| 441 | .findStatic(Arrays.class, "toString", methodType(String.class, long[].class)) |
| 442 | .asCollector(long[].class, 1); |
| 443 | assertEquals("[123]", (String) longsToString.invokeExact((long)123)); |
| 444 | }} |
| 445 | } |
| 446 | |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame] | 447 | @SuppressWarnings("rawtypes") |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 448 | @Test public void testAsVarargsCollector() throws Throwable { |
| 449 | {{ |
| 450 | {} /// JAVADOC |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 451 | MethodHandle deepToString = publicLookup() |
| 452 | .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
| 453 | MethodHandle ts1 = deepToString.asVarargsCollector(Object[].class); |
| 454 | assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); |
| 455 | assertEquals("[won]", (String) ts1.invoke( new Object[]{"won"})); |
| 456 | assertEquals("[won]", (String) ts1.invoke( "won" )); |
| 457 | assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"})); |
| 458 | // findStatic of Arrays.asList(...) produces a variable arity method handle: |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 459 | MethodHandle asList = publicLookup() |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 460 | .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)); |
| 461 | assertEquals(methodType(List.class, Object[].class), asList.type()); |
| 462 | assert(asList.isVarargsCollector()); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 463 | assertEquals("[]", asList.invoke().toString()); |
| 464 | assertEquals("[1]", asList.invoke(1).toString()); |
| 465 | assertEquals("[two, too]", asList.invoke("two", "too").toString()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 466 | String[] argv = { "three", "thee", "tee" }; |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 467 | assertEquals("[three, thee, tee]", asList.invoke(argv).toString()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 468 | assertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString()); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 469 | List ls = (List) asList.invoke((Object)argv); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 470 | assertEquals(1, ls.size()); |
| 471 | assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0))); |
| 472 | }} |
| 473 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 474 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 475 | @Test public void testAsFixedArity() throws Throwable { |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 476 | {{ |
| 477 | {} /// JAVADOC |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 478 | MethodHandle asListVar = publicLookup() |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 479 | .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)) |
| 480 | .asVarargsCollector(Object[].class); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 481 | MethodHandle asListFix = asListVar.asFixedArity(); |
| 482 | assertEquals("[1]", asListVar.invoke(1).toString()); |
| 483 | Exception caught = null; |
| 484 | try { asListFix.invoke((Object)1); } |
| 485 | catch (Exception ex) { caught = ex; } |
| 486 | assert(caught instanceof ClassCastException); |
| 487 | assertEquals("[two, too]", asListVar.invoke("two", "too").toString()); |
| 488 | try { asListFix.invoke("two", "too"); } |
| 489 | catch (Exception ex) { caught = ex; } |
| 490 | assert(caught instanceof WrongMethodTypeException); |
| 491 | Object[] argv = { "three", "thee", "tee" }; |
| 492 | assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString()); |
| 493 | assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString()); |
| 494 | assertEquals(1, ((List) asListVar.invoke((Object)argv)).size()); |
| 495 | assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString()); |
| 496 | }} |
| 497 | } |
| 498 | |
| 499 | @Test public void testAsTypeCornerCases() throws Throwable { |
| 500 | {{ |
| 501 | {} /// JAVADOC |
| 502 | MethodHandle i2s = publicLookup() |
| 503 | .findVirtual(Integer.class, "toString", methodType(String.class)); |
| 504 | i2s = i2s.asType(i2s.type().unwrap()); |
| 505 | MethodHandle l2s = publicLookup() |
| 506 | .findVirtual(Long.class, "toString", methodType(String.class)); |
| 507 | l2s = l2s.asType(l2s.type().unwrap()); |
| 508 | |
| 509 | Exception caught = null; |
| 510 | try { i2s.asType(methodType(String.class, String.class)); } |
| 511 | catch (Exception ex) { caught = ex; } |
| 512 | assert(caught instanceof WrongMethodTypeException); |
| 513 | |
| 514 | i2s.asType(methodType(String.class, byte.class)); |
| 515 | i2s.asType(methodType(String.class, Byte.class)); |
| 516 | i2s.asType(methodType(String.class, Character.class)); |
| 517 | i2s.asType(methodType(String.class, Integer.class)); |
| 518 | l2s.asType(methodType(String.class, byte.class)); |
| 519 | l2s.asType(methodType(String.class, Byte.class)); |
| 520 | l2s.asType(methodType(String.class, Character.class)); |
| 521 | l2s.asType(methodType(String.class, Integer.class)); |
| 522 | l2s.asType(methodType(String.class, Long.class)); |
| 523 | |
| 524 | caught = null; |
| 525 | try { i2s.asType(methodType(String.class, Long.class)); } |
| 526 | catch (Exception ex) { caught = ex; } |
| 527 | assert(caught instanceof WrongMethodTypeException); |
| 528 | |
| 529 | MethodHandle i2sGen = i2s.asType(methodType(String.class, Object.class)); |
| 530 | MethodHandle l2sGen = l2s.asType(methodType(String.class, Object.class)); |
| 531 | |
| 532 | i2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int |
| 533 | i2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int |
| 534 | l2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int |
| 535 | l2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int |
| 536 | l2sGen.invoke(0x420000000L); |
| 537 | |
| 538 | caught = null; |
| 539 | try { i2sGen.invoke(0x420000000L); } // long -> Long -> Object -> Integer CCE |
| 540 | catch (Exception ex) { caught = ex; } |
| 541 | assert(caught instanceof ClassCastException); |
| 542 | |
| 543 | caught = null; |
| 544 | try { i2sGen.invoke("asdf"); } // String -> Object -> Integer CCE |
| 545 | catch (Exception ex) { caught = ex; } |
| 546 | assert(caught instanceof ClassCastException); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 547 | {} |
| 548 | }} |
| 549 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 550 | |
| 551 | @Test public void testMutableCallSite() throws Throwable { |
| 552 | {{ |
| 553 | {} /// JAVADOC |
| 554 | MutableCallSite name = new MutableCallSite(MethodType.methodType(String.class)); |
| 555 | MethodHandle MH_name = name.dynamicInvoker(); |
| 556 | MethodType MT_str1 = MethodType.methodType(String.class); |
| 557 | MethodHandle MH_upcase = MethodHandles.lookup() |
| 558 | .findVirtual(String.class, "toUpperCase", MT_str1); |
| 559 | MethodHandle worker1 = MethodHandles.filterReturnValue(MH_name, MH_upcase); |
| 560 | name.setTarget(MethodHandles.constant(String.class, "Rocky")); |
| 561 | assertEquals("ROCKY", (String) worker1.invokeExact()); |
| 562 | name.setTarget(MethodHandles.constant(String.class, "Fred")); |
| 563 | assertEquals("FRED", (String) worker1.invokeExact()); |
| 564 | // (mutation can be continued indefinitely) |
| 565 | /* |
| 566 | * </pre></blockquote> |
| 567 | * <p> |
| 568 | * The same call site may be used in several places at once. |
| 569 | * <blockquote><pre> |
| 570 | */ |
| 571 | MethodType MT_str2 = MethodType.methodType(String.class, String.class); |
| 572 | MethodHandle MH_cat = lookup().findVirtual(String.class, |
| 573 | "concat", methodType(String.class, String.class)); |
| 574 | MethodHandle MH_dear = MethodHandles.insertArguments(MH_cat, 1, ", dear?"); |
| 575 | MethodHandle worker2 = MethodHandles.filterReturnValue(MH_name, MH_dear); |
| 576 | assertEquals("Fred, dear?", (String) worker2.invokeExact()); |
| 577 | name.setTarget(MethodHandles.constant(String.class, "Wilma")); |
| 578 | assertEquals("WILMA", (String) worker1.invokeExact()); |
| 579 | assertEquals("Wilma, dear?", (String) worker2.invokeExact()); |
| 580 | {} |
| 581 | }} |
| 582 | } |
| 583 | |
| 584 | @Test public void testSwitchPoint() throws Throwable { |
| 585 | {{ |
| 586 | {} /// JAVADOC |
| 587 | MethodHandle MH_strcat = MethodHandles.lookup() |
| 588 | .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class)); |
| 589 | SwitchPoint spt = new SwitchPoint(); |
jrose | cd1d97b | 2011-06-03 11:20:20 -0700 | [diff] [blame] | 590 | assert(!spt.hasBeenInvalidated()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 591 | // the following steps may be repeated to re-use the same switch point: |
| 592 | MethodHandle worker1 = MH_strcat; |
| 593 | MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0); |
| 594 | MethodHandle worker = spt.guardWithTest(worker1, worker2); |
| 595 | assertEquals("method", (String) worker.invokeExact("met", "hod")); |
| 596 | SwitchPoint.invalidateAll(new SwitchPoint[]{ spt }); |
jrose | cd1d97b | 2011-06-03 11:20:20 -0700 | [diff] [blame] | 597 | assert(spt.hasBeenInvalidated()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 598 | assertEquals("hodmet", (String) worker.invokeExact("met", "hod")); |
| 599 | {} |
| 600 | }} |
| 601 | } |
| 602 | |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 603 | @Test public void testFoldArguments2() throws Throwable { |
| 604 | {{ |
| 605 | {} /// JAVADOC |
| 606 | // argument-based dispatch for methods of the form boolean x.___(y: String) |
| 607 | Lookup lookup = publicLookup(); |
| 608 | // first, a tracing hack: |
| 609 | MethodHandle println = lookup.findVirtual(java.io.PrintStream.class, "println", methodType(void.class, String.class)); |
| 610 | MethodHandle arrayToString = lookup.findStatic(Arrays.class, "toString", methodType(String.class, Object[].class)); |
| 611 | MethodHandle concat = lookup.findVirtual(String.class, "concat", methodType(String.class, String.class)); |
| 612 | MethodHandle arrayToString_DIS = filterReturnValue(arrayToString, concat.bindTo("DIS:")); |
| 613 | MethodHandle arrayToString_INV = filterReturnValue(arrayToString, concat.bindTo("INV:")); |
| 614 | MethodHandle printArgs_DIS = filterReturnValue(arrayToString_DIS, println.bindTo(System.out)).asVarargsCollector(Object[].class); |
| 615 | MethodHandle printArgs_INV = filterReturnValue(arrayToString_INV, println.bindTo(System.out)).asVarargsCollector(Object[].class); |
| 616 | // metaobject protocol: |
| 617 | MethodType mtype = methodType(boolean.class, String.class); |
| 618 | MethodHandle findVirtual = lookup.findVirtual(Lookup.class, |
| 619 | "findVirtual", methodType(MethodHandle.class, Class.class, String.class, MethodType.class)); |
| 620 | MethodHandle getClass = lookup.findVirtual(Object.class, |
| 621 | "getClass", methodType(Class.class)); |
| 622 | MethodHandle dispatch = findVirtual; |
| 623 | dispatch = filterArguments(dispatch, 1, getClass); |
| 624 | dispatch = insertArguments(dispatch, 3, mtype); |
| 625 | dispatch = dispatch.bindTo(lookup); |
| 626 | assertEquals(methodType(MethodHandle.class, Object.class, String.class), dispatch.type()); |
| 627 | MethodHandle invoker = invoker(mtype.insertParameterTypes(0, Object.class)); |
| 628 | // wrap tracing around the dispatch and invoke steps: |
| 629 | dispatch = foldArguments(dispatch, printArgs_DIS.asType(dispatch.type().changeReturnType(void.class))); |
| 630 | invoker = foldArguments(invoker, printArgs_INV.asType(invoker.type().changeReturnType(void.class))); |
| 631 | invoker = dropArguments(invoker, 2, String.class); // ignore selector |
| 632 | // compose the dispatcher and the invoker: |
| 633 | MethodHandle invokeDispatched = foldArguments(invoker, dispatch); |
| 634 | Object x = "football", y = new java.util.Scanner("bar"); |
| 635 | assert( (boolean) invokeDispatched.invokeExact(x, "startsWith", "foo")); |
| 636 | assert(!(boolean) invokeDispatched.invokeExact(x, "startsWith", "#")); |
| 637 | assert( (boolean) invokeDispatched.invokeExact(x, "endsWith", "all")); |
| 638 | assert(!(boolean) invokeDispatched.invokeExact(x, "endsWith", "foo")); |
| 639 | assert( (boolean) invokeDispatched.invokeExact(y, "hasNext", "[abc]+[rst]")); |
| 640 | assert(!(boolean) invokeDispatched.invokeExact(y, "hasNext", "[123]+[789]")); |
| 641 | }} |
| 642 | } |
| 643 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 644 | /* ---- TEMPLATE ---- |
| 645 | @Test public void testFoo() throws Throwable { |
| 646 | {{ |
| 647 | {} /// JAVADOC |
| 648 | {} |
| 649 | }} |
| 650 | } |
| 651 | */ |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 652 | } |