jrose | 900bafd | 2010-10-30 21:08:23 -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 | 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 |
| 29 | * @run junit/othervm test.java.lang.invoke.JavaDocExamplesTest |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | /* |
| 33 | ---- To run outside jtreg: |
| 34 | $ $JAVA7X_HOME/bin/javac -cp $JUNIT4_JAR -d /tmp/Classes \ |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 35 | $DAVINCI/sources/jdk/test/java/lang/invoke/JavaDocExamplesTest.java |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 36 | $ $JAVA7X_HOME/bin/java -cp $JUNIT4_JAR:/tmp/Classes \ |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 37 | -DJavaDocExamplesTest.verbosity=1 \ |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 38 | test.java.lang.invoke.JavaDocExamplesTest |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 39 | ---- |
| 40 | */ |
| 41 | |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 42 | package test.java.lang.invoke; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 43 | |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 44 | import java.lang.invoke.*; |
| 45 | import static java.lang.invoke.MethodHandles.*; |
| 46 | import static java.lang.invoke.MethodType.*; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 47 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 48 | import java.util.*; |
| 49 | |
| 50 | import org.junit.*; |
| 51 | import static org.junit.Assert.*; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /** |
| 55 | * @author jrose |
| 56 | */ |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame^] | 57 | @SuppressWarnings("LocalVariableHidesMemberVariable") |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 58 | public class JavaDocExamplesTest { |
| 59 | /** Wrapper for running the JUnit tests in this module. |
| 60 | * Put JUnit on the classpath! |
| 61 | */ |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 62 | public static void main(String... ignore) throws Throwable { |
| 63 | System.out.println("can run this as:"); |
| 64 | System.out.println("$ java org.junit.runner.JUnitCore "+JavaDocExamplesTest.class.getName()); |
| 65 | new JavaDocExamplesTest().run(); |
| 66 | } |
| 67 | public void run() throws Throwable { |
| 68 | testFindVirtual(); |
| 69 | testPermuteArguments(); |
| 70 | testDropArguments(); |
| 71 | testFilterArguments(); |
| 72 | testFoldArguments(); |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 73 | testFoldArguments2(); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 74 | testMethodHandlesSummary(); |
| 75 | testAsSpreader(); |
| 76 | testAsCollector(); |
| 77 | testAsVarargsCollector(); |
| 78 | testAsFixedArity(); |
| 79 | testAsTypeCornerCases(); |
| 80 | testMutableCallSite(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 81 | } |
| 82 | // How much output? |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 83 | static final Class<?> THIS_CLASS = JavaDocExamplesTest.class; |
| 84 | static int verbosity = Integer.getInteger(THIS_CLASS.getSimpleName()+".verbosity", 0); |
| 85 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 86 | |
| 87 | {} |
| 88 | static final private Lookup LOOKUP = lookup(); |
| 89 | // static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class, |
| 90 | // "concat", methodType(String.class, String.class)); |
| 91 | // static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class, |
| 92 | // "hashCode", methodType(int.class)); |
| 93 | |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 94 | // form required if ReflectiveOperationException is intercepted: |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 95 | static final private MethodHandle CONCAT_2, HASHCODE_2, ADD_2, SUB_2; |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 96 | static { |
| 97 | try { |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 98 | Class<?> THIS_CLASS = LOOKUP.lookupClass(); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 99 | CONCAT_2 = LOOKUP.findVirtual(String.class, |
| 100 | "concat", methodType(String.class, String.class)); |
| 101 | HASHCODE_2 = LOOKUP.findVirtual(Object.class, |
| 102 | "hashCode", methodType(int.class)); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 103 | ADD_2 = LOOKUP.findStatic(THIS_CLASS, "add", methodType(int.class, int.class, int.class)); |
| 104 | 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] | 105 | } catch (ReflectiveOperationException ex) { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 106 | throw new RuntimeException(ex); |
| 107 | } |
| 108 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 109 | static int add(int x, int y) { return x + y; } |
| 110 | static int sub(int x, int y) { return x - y; } |
| 111 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 112 | {} |
| 113 | |
| 114 | @Test public void testFindVirtual() throws Throwable { |
| 115 | {} |
| 116 | MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class, |
| 117 | "concat", methodType(String.class, String.class)); |
| 118 | MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class, |
| 119 | "hashCode", methodType(int.class)); |
| 120 | //assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y")); |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame] | 121 | assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y")); |
| 122 | assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y")); |
| 123 | //assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy")); |
| 124 | assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy")); |
| 125 | assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 126 | {} |
| 127 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 128 | |
| 129 | @Test public void testPermuteArguments() throws Throwable { |
| 130 | {{ |
| 131 | {} /// JAVADOC |
| 132 | MethodType intfn1 = methodType(int.class, int.class); |
| 133 | MethodType intfn2 = methodType(int.class, int.class, int.class); |
| 134 | MethodHandle sub = SUB_2;// ... {int x, int y => x-y} ...; |
| 135 | assert(sub.type().equals(intfn2)); |
| 136 | MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1); |
| 137 | MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0); |
| 138 | assert((int)rsub.invokeExact(1, 100) == 99); |
| 139 | MethodHandle add = ADD_2;// ... {int x, int y => x+y} ...; |
| 140 | assert(add.type().equals(intfn2)); |
| 141 | MethodHandle twice = permuteArguments(add, intfn1, 0, 0); |
| 142 | assert(twice.type().equals(intfn1)); |
| 143 | assert((int)twice.invokeExact(21) == 42); |
| 144 | }} |
| 145 | {{ |
| 146 | {} /// JAVADOC |
| 147 | MethodHandle cat = lookup().findVirtual(String.class, |
| 148 | "concat", methodType(String.class, String.class)); |
| 149 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
| 150 | MethodHandle d0 = dropArguments(cat, 0, String.class); |
| 151 | assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
| 152 | MethodHandle d1 = dropArguments(cat, 1, String.class); |
| 153 | assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
| 154 | MethodHandle d2 = dropArguments(cat, 2, String.class); |
| 155 | assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
| 156 | MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
| 157 | assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
| 158 | }} |
| 159 | } |
| 160 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 161 | @Test public void testDropArguments() throws Throwable { |
| 162 | {{ |
| 163 | {} /// JAVADOC |
| 164 | MethodHandle cat = lookup().findVirtual(String.class, |
| 165 | "concat", methodType(String.class, String.class)); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 166 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 167 | MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class); |
| 168 | MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2)); |
| 169 | assertEquals(bigType, d0.type()); |
| 170 | assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z")); |
| 171 | }} |
| 172 | {{ |
| 173 | {} /// JAVADOC |
| 174 | MethodHandle cat = lookup().findVirtual(String.class, |
| 175 | "concat", methodType(String.class, String.class)); |
| 176 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 177 | MethodHandle d0 = dropArguments(cat, 0, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 178 | assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 179 | MethodHandle d1 = dropArguments(cat, 1, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 180 | assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 181 | MethodHandle d2 = dropArguments(cat, 2, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 182 | assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 183 | MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 184 | assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 185 | }} |
| 186 | } |
| 187 | |
| 188 | @Test public void testFilterArguments() throws Throwable { |
| 189 | {{ |
| 190 | {} /// JAVADOC |
| 191 | MethodHandle cat = lookup().findVirtual(String.class, |
| 192 | "concat", methodType(String.class, String.class)); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 193 | MethodHandle upcase = lookup().findVirtual(String.class, |
| 194 | "toUpperCase", methodType(String.class)); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 195 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 196 | MethodHandle f0 = filterArguments(cat, 0, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 197 | assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 198 | MethodHandle f1 = filterArguments(cat, 1, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 199 | assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 200 | MethodHandle f2 = filterArguments(cat, 0, upcase, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 201 | assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 202 | }} |
| 203 | } |
| 204 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 205 | @Test public void testFoldArguments() throws Throwable { |
| 206 | {{ |
| 207 | {} /// JAVADOC |
| 208 | MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class, |
| 209 | "println", methodType(void.class, String.class)) |
| 210 | .bindTo(System.out); |
| 211 | MethodHandle cat = lookup().findVirtual(String.class, |
| 212 | "concat", methodType(String.class, String.class)); |
| 213 | assertEquals("boojum", (String) cat.invokeExact("boo", "jum")); |
| 214 | MethodHandle catTrace = foldArguments(cat, trace); |
| 215 | // also prints "boo": |
| 216 | assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum")); |
| 217 | }} |
| 218 | } |
| 219 | |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 220 | static void assertEquals(Object exp, Object act) { |
| 221 | if (verbosity > 0) |
| 222 | System.out.println("result: "+act); |
| 223 | Assert.assertEquals(exp, act); |
| 224 | } |
| 225 | |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 226 | @Test public void testMethodHandlesSummary() throws Throwable { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 227 | {{ |
| 228 | {} /// JAVADOC |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 229 | Object x, y; String s; int i; |
| 230 | MethodType mt; MethodHandle mh; |
| 231 | MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 232 | // mt is (char,char)String |
| 233 | mt = MethodType.methodType(String.class, char.class, char.class); |
| 234 | mh = lookup.findVirtual(String.class, "replace", mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 235 | s = (String) mh.invokeExact("daddy",'d','n'); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 236 | // invokeExact(Ljava/lang/String;CC)Ljava/lang/String; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 237 | assertEquals(s, "nanny"); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 238 | // weakly typed invocation (using MHs.invoke) |
| 239 | s = (String) mh.invokeWithArguments("sappy", 'p', 'v'); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 240 | assertEquals(s, "savvy"); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 241 | // mt is (Object[])List |
| 242 | mt = MethodType.methodType(java.util.List.class, Object[].class); |
| 243 | mh = lookup.findStatic(java.util.Arrays.class, "asList", mt); |
| 244 | assert(mh.isVarargsCollector()); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 245 | x = mh.invoke("one", "two"); |
| 246 | // invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 247 | assertEquals(x, java.util.Arrays.asList("one","two")); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 248 | // mt is (Object,Object,Object)Object |
| 249 | mt = MethodType.genericMethodType(3); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 250 | mh = mh.asType(mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 251 | x = mh.invokeExact((Object)1, (Object)2, (Object)3); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 252 | // invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 253 | assertEquals(x, java.util.Arrays.asList(1,2,3)); |
| 254 | // mt is ()int |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 255 | mt = MethodType.methodType(int.class); |
| 256 | mh = lookup.findVirtual(java.util.List.class, "size", mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 257 | i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3)); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 258 | // invokeExact(Ljava/util/List;)I |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 259 | assert(i == 3); |
| 260 | mt = MethodType.methodType(void.class, String.class); |
| 261 | mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt); |
| 262 | mh.invokeExact(System.out, "Hello, world."); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 263 | // invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 264 | {} |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 265 | }} |
| 266 | } |
| 267 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 268 | @Test public void testAsSpreader() throws Throwable { |
| 269 | {{ |
| 270 | {} /// JAVADOC |
| 271 | MethodHandle equals = publicLookup() |
| 272 | .findVirtual(String.class, "equals", methodType(boolean.class, Object.class)); |
| 273 | assert( (boolean) equals.invokeExact("me", (Object)"me")); |
| 274 | assert(!(boolean) equals.invokeExact("me", (Object)"thee")); |
| 275 | // spread both arguments from a 2-array: |
| 276 | MethodHandle eq2 = equals.asSpreader(Object[].class, 2); |
| 277 | assert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" })); |
| 278 | assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" })); |
| 279 | // spread both arguments from a String array: |
| 280 | MethodHandle eq2s = equals.asSpreader(String[].class, 2); |
| 281 | assert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" })); |
| 282 | assert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" })); |
| 283 | // spread second arguments from a 1-array: |
| 284 | MethodHandle eq1 = equals.asSpreader(Object[].class, 1); |
| 285 | assert( (boolean) eq1.invokeExact("me", new Object[]{ "me" })); |
| 286 | assert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" })); |
| 287 | // spread no arguments from a 0-array or null: |
| 288 | MethodHandle eq0 = equals.asSpreader(Object[].class, 0); |
| 289 | assert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0])); |
| 290 | assert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null)); |
| 291 | // asSpreader and asCollector are approximate inverses: |
| 292 | for (int n = 0; n <= 2; n++) { |
| 293 | for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) { |
| 294 | MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n); |
| 295 | assert( (boolean) equals2.invokeWithArguments("me", "me")); |
| 296 | assert(!(boolean) equals2.invokeWithArguments("me", "thee")); |
| 297 | } |
| 298 | } |
| 299 | MethodHandle caToString = publicLookup() |
| 300 | .findStatic(Arrays.class, "toString", methodType(String.class, char[].class)); |
| 301 | assertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray())); |
| 302 | MethodHandle caString3 = caToString.asCollector(char[].class, 3); |
| 303 | assertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C')); |
| 304 | MethodHandle caToString2 = caString3.asSpreader(char[].class, 2); |
| 305 | assertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray())); |
| 306 | }} |
| 307 | } |
| 308 | |
| 309 | @Test public void testAsCollector() throws Throwable { |
| 310 | {{ |
| 311 | {} /// JAVADOC |
| 312 | MethodHandle deepToString = publicLookup() |
| 313 | .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
| 314 | assertEquals("[won]", (String) deepToString.invokeExact(new Object[]{"won"})); |
| 315 | MethodHandle ts1 = deepToString.asCollector(Object[].class, 1); |
| 316 | assertEquals(methodType(String.class, Object.class), ts1.type()); |
| 317 | //assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); //FAIL |
| 318 | assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"})); |
| 319 | // arrayType can be a subtype of Object[] |
| 320 | MethodHandle ts2 = deepToString.asCollector(String[].class, 2); |
| 321 | assertEquals(methodType(String.class, String.class, String.class), ts2.type()); |
| 322 | assertEquals("[two, too]", (String) ts2.invokeExact("two", "too")); |
| 323 | MethodHandle ts0 = deepToString.asCollector(Object[].class, 0); |
| 324 | assertEquals("[]", (String) ts0.invokeExact()); |
| 325 | // collectors can be nested, Lisp-style |
| 326 | MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2); |
| 327 | assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D"))); |
| 328 | // arrayType can be any primitive array type |
| 329 | MethodHandle bytesToString = publicLookup() |
| 330 | .findStatic(Arrays.class, "toString", methodType(String.class, byte[].class)) |
| 331 | .asCollector(byte[].class, 3); |
| 332 | assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3)); |
| 333 | MethodHandle longsToString = publicLookup() |
| 334 | .findStatic(Arrays.class, "toString", methodType(String.class, long[].class)) |
| 335 | .asCollector(long[].class, 1); |
| 336 | assertEquals("[123]", (String) longsToString.invokeExact((long)123)); |
| 337 | }} |
| 338 | } |
| 339 | |
jrose | 4949452 | 2012-01-18 17:34:29 -0800 | [diff] [blame^] | 340 | @SuppressWarnings("rawtypes") |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 341 | @Test public void testAsVarargsCollector() throws Throwable { |
| 342 | {{ |
| 343 | {} /// JAVADOC |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 344 | MethodHandle deepToString = publicLookup() |
| 345 | .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
| 346 | MethodHandle ts1 = deepToString.asVarargsCollector(Object[].class); |
| 347 | assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); |
| 348 | assertEquals("[won]", (String) ts1.invoke( new Object[]{"won"})); |
| 349 | assertEquals("[won]", (String) ts1.invoke( "won" )); |
| 350 | assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"})); |
| 351 | // findStatic of Arrays.asList(...) produces a variable arity method handle: |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 352 | MethodHandle asList = publicLookup() |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 353 | .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)); |
| 354 | assertEquals(methodType(List.class, Object[].class), asList.type()); |
| 355 | assert(asList.isVarargsCollector()); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 356 | assertEquals("[]", asList.invoke().toString()); |
| 357 | assertEquals("[1]", asList.invoke(1).toString()); |
| 358 | assertEquals("[two, too]", asList.invoke("two", "too").toString()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 359 | String[] argv = { "three", "thee", "tee" }; |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 360 | assertEquals("[three, thee, tee]", asList.invoke(argv).toString()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 361 | assertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString()); |
jrose | 79e0a6c | 2011-05-12 19:27:33 -0700 | [diff] [blame] | 362 | List ls = (List) asList.invoke((Object)argv); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 363 | assertEquals(1, ls.size()); |
| 364 | assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0))); |
| 365 | }} |
| 366 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 367 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 368 | @Test public void testAsFixedArity() throws Throwable { |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 369 | {{ |
| 370 | {} /// JAVADOC |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 371 | MethodHandle asListVar = publicLookup() |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 372 | .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)) |
| 373 | .asVarargsCollector(Object[].class); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 374 | MethodHandle asListFix = asListVar.asFixedArity(); |
| 375 | assertEquals("[1]", asListVar.invoke(1).toString()); |
| 376 | Exception caught = null; |
| 377 | try { asListFix.invoke((Object)1); } |
| 378 | catch (Exception ex) { caught = ex; } |
| 379 | assert(caught instanceof ClassCastException); |
| 380 | assertEquals("[two, too]", asListVar.invoke("two", "too").toString()); |
| 381 | try { asListFix.invoke("two", "too"); } |
| 382 | catch (Exception ex) { caught = ex; } |
| 383 | assert(caught instanceof WrongMethodTypeException); |
| 384 | Object[] argv = { "three", "thee", "tee" }; |
| 385 | assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString()); |
| 386 | assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString()); |
| 387 | assertEquals(1, ((List) asListVar.invoke((Object)argv)).size()); |
| 388 | assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString()); |
| 389 | }} |
| 390 | } |
| 391 | |
| 392 | @Test public void testAsTypeCornerCases() throws Throwable { |
| 393 | {{ |
| 394 | {} /// JAVADOC |
| 395 | MethodHandle i2s = publicLookup() |
| 396 | .findVirtual(Integer.class, "toString", methodType(String.class)); |
| 397 | i2s = i2s.asType(i2s.type().unwrap()); |
| 398 | MethodHandle l2s = publicLookup() |
| 399 | .findVirtual(Long.class, "toString", methodType(String.class)); |
| 400 | l2s = l2s.asType(l2s.type().unwrap()); |
| 401 | |
| 402 | Exception caught = null; |
| 403 | try { i2s.asType(methodType(String.class, String.class)); } |
| 404 | catch (Exception ex) { caught = ex; } |
| 405 | assert(caught instanceof WrongMethodTypeException); |
| 406 | |
| 407 | i2s.asType(methodType(String.class, byte.class)); |
| 408 | i2s.asType(methodType(String.class, Byte.class)); |
| 409 | i2s.asType(methodType(String.class, Character.class)); |
| 410 | i2s.asType(methodType(String.class, Integer.class)); |
| 411 | l2s.asType(methodType(String.class, byte.class)); |
| 412 | l2s.asType(methodType(String.class, Byte.class)); |
| 413 | l2s.asType(methodType(String.class, Character.class)); |
| 414 | l2s.asType(methodType(String.class, Integer.class)); |
| 415 | l2s.asType(methodType(String.class, Long.class)); |
| 416 | |
| 417 | caught = null; |
| 418 | try { i2s.asType(methodType(String.class, Long.class)); } |
| 419 | catch (Exception ex) { caught = ex; } |
| 420 | assert(caught instanceof WrongMethodTypeException); |
| 421 | |
| 422 | MethodHandle i2sGen = i2s.asType(methodType(String.class, Object.class)); |
| 423 | MethodHandle l2sGen = l2s.asType(methodType(String.class, Object.class)); |
| 424 | |
| 425 | i2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int |
| 426 | i2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int |
| 427 | l2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int |
| 428 | l2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int |
| 429 | l2sGen.invoke(0x420000000L); |
| 430 | |
| 431 | caught = null; |
| 432 | try { i2sGen.invoke(0x420000000L); } // long -> Long -> Object -> Integer CCE |
| 433 | catch (Exception ex) { caught = ex; } |
| 434 | assert(caught instanceof ClassCastException); |
| 435 | |
| 436 | caught = null; |
| 437 | try { i2sGen.invoke("asdf"); } // String -> Object -> Integer CCE |
| 438 | catch (Exception ex) { caught = ex; } |
| 439 | assert(caught instanceof ClassCastException); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 440 | {} |
| 441 | }} |
| 442 | } |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 443 | |
| 444 | @Test public void testMutableCallSite() throws Throwable { |
| 445 | {{ |
| 446 | {} /// JAVADOC |
| 447 | MutableCallSite name = new MutableCallSite(MethodType.methodType(String.class)); |
| 448 | MethodHandle MH_name = name.dynamicInvoker(); |
| 449 | MethodType MT_str1 = MethodType.methodType(String.class); |
| 450 | MethodHandle MH_upcase = MethodHandles.lookup() |
| 451 | .findVirtual(String.class, "toUpperCase", MT_str1); |
| 452 | MethodHandle worker1 = MethodHandles.filterReturnValue(MH_name, MH_upcase); |
| 453 | name.setTarget(MethodHandles.constant(String.class, "Rocky")); |
| 454 | assertEquals("ROCKY", (String) worker1.invokeExact()); |
| 455 | name.setTarget(MethodHandles.constant(String.class, "Fred")); |
| 456 | assertEquals("FRED", (String) worker1.invokeExact()); |
| 457 | // (mutation can be continued indefinitely) |
| 458 | /* |
| 459 | * </pre></blockquote> |
| 460 | * <p> |
| 461 | * The same call site may be used in several places at once. |
| 462 | * <blockquote><pre> |
| 463 | */ |
| 464 | MethodType MT_str2 = MethodType.methodType(String.class, String.class); |
| 465 | MethodHandle MH_cat = lookup().findVirtual(String.class, |
| 466 | "concat", methodType(String.class, String.class)); |
| 467 | MethodHandle MH_dear = MethodHandles.insertArguments(MH_cat, 1, ", dear?"); |
| 468 | MethodHandle worker2 = MethodHandles.filterReturnValue(MH_name, MH_dear); |
| 469 | assertEquals("Fred, dear?", (String) worker2.invokeExact()); |
| 470 | name.setTarget(MethodHandles.constant(String.class, "Wilma")); |
| 471 | assertEquals("WILMA", (String) worker1.invokeExact()); |
| 472 | assertEquals("Wilma, dear?", (String) worker2.invokeExact()); |
| 473 | {} |
| 474 | }} |
| 475 | } |
| 476 | |
| 477 | @Test public void testSwitchPoint() throws Throwable { |
| 478 | {{ |
| 479 | {} /// JAVADOC |
| 480 | MethodHandle MH_strcat = MethodHandles.lookup() |
| 481 | .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class)); |
| 482 | SwitchPoint spt = new SwitchPoint(); |
jrose | cd1d97b | 2011-06-03 11:20:20 -0700 | [diff] [blame] | 483 | assert(!spt.hasBeenInvalidated()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 484 | // the following steps may be repeated to re-use the same switch point: |
| 485 | MethodHandle worker1 = MH_strcat; |
| 486 | MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0); |
| 487 | MethodHandle worker = spt.guardWithTest(worker1, worker2); |
| 488 | assertEquals("method", (String) worker.invokeExact("met", "hod")); |
| 489 | SwitchPoint.invalidateAll(new SwitchPoint[]{ spt }); |
jrose | cd1d97b | 2011-06-03 11:20:20 -0700 | [diff] [blame] | 490 | assert(spt.hasBeenInvalidated()); |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 491 | assertEquals("hodmet", (String) worker.invokeExact("met", "hod")); |
| 492 | {} |
| 493 | }} |
| 494 | } |
| 495 | |
jrose | b4be026 | 2011-07-16 15:44:33 -0700 | [diff] [blame] | 496 | @Test public void testFoldArguments2() throws Throwable { |
| 497 | {{ |
| 498 | {} /// JAVADOC |
| 499 | // argument-based dispatch for methods of the form boolean x.___(y: String) |
| 500 | Lookup lookup = publicLookup(); |
| 501 | // first, a tracing hack: |
| 502 | MethodHandle println = lookup.findVirtual(java.io.PrintStream.class, "println", methodType(void.class, String.class)); |
| 503 | MethodHandle arrayToString = lookup.findStatic(Arrays.class, "toString", methodType(String.class, Object[].class)); |
| 504 | MethodHandle concat = lookup.findVirtual(String.class, "concat", methodType(String.class, String.class)); |
| 505 | MethodHandle arrayToString_DIS = filterReturnValue(arrayToString, concat.bindTo("DIS:")); |
| 506 | MethodHandle arrayToString_INV = filterReturnValue(arrayToString, concat.bindTo("INV:")); |
| 507 | MethodHandle printArgs_DIS = filterReturnValue(arrayToString_DIS, println.bindTo(System.out)).asVarargsCollector(Object[].class); |
| 508 | MethodHandle printArgs_INV = filterReturnValue(arrayToString_INV, println.bindTo(System.out)).asVarargsCollector(Object[].class); |
| 509 | // metaobject protocol: |
| 510 | MethodType mtype = methodType(boolean.class, String.class); |
| 511 | MethodHandle findVirtual = lookup.findVirtual(Lookup.class, |
| 512 | "findVirtual", methodType(MethodHandle.class, Class.class, String.class, MethodType.class)); |
| 513 | MethodHandle getClass = lookup.findVirtual(Object.class, |
| 514 | "getClass", methodType(Class.class)); |
| 515 | MethodHandle dispatch = findVirtual; |
| 516 | dispatch = filterArguments(dispatch, 1, getClass); |
| 517 | dispatch = insertArguments(dispatch, 3, mtype); |
| 518 | dispatch = dispatch.bindTo(lookup); |
| 519 | assertEquals(methodType(MethodHandle.class, Object.class, String.class), dispatch.type()); |
| 520 | MethodHandle invoker = invoker(mtype.insertParameterTypes(0, Object.class)); |
| 521 | // wrap tracing around the dispatch and invoke steps: |
| 522 | dispatch = foldArguments(dispatch, printArgs_DIS.asType(dispatch.type().changeReturnType(void.class))); |
| 523 | invoker = foldArguments(invoker, printArgs_INV.asType(invoker.type().changeReturnType(void.class))); |
| 524 | invoker = dropArguments(invoker, 2, String.class); // ignore selector |
| 525 | // compose the dispatcher and the invoker: |
| 526 | MethodHandle invokeDispatched = foldArguments(invoker, dispatch); |
| 527 | Object x = "football", y = new java.util.Scanner("bar"); |
| 528 | assert( (boolean) invokeDispatched.invokeExact(x, "startsWith", "foo")); |
| 529 | assert(!(boolean) invokeDispatched.invokeExact(x, "startsWith", "#")); |
| 530 | assert( (boolean) invokeDispatched.invokeExact(x, "endsWith", "all")); |
| 531 | assert(!(boolean) invokeDispatched.invokeExact(x, "endsWith", "foo")); |
| 532 | assert( (boolean) invokeDispatched.invokeExact(y, "hasNext", "[abc]+[rst]")); |
| 533 | assert(!(boolean) invokeDispatched.invokeExact(y, "hasNext", "[123]+[789]")); |
| 534 | }} |
| 535 | } |
| 536 | |
jrose | 9b82ad6 | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 537 | /* ---- TEMPLATE ---- |
| 538 | @Test public void testFoo() throws Throwable { |
| 539 | {{ |
| 540 | {} /// JAVADOC |
| 541 | {} |
| 542 | }} |
| 543 | } |
| 544 | */ |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 545 | } |