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