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