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 | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 37 | -Dtest.java.lang.invoke.JavaDocExamplesTest.verbosity=1 \ |
| 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 | |
| 48 | import java.lang.reflect.*; |
| 49 | import java.util.*; |
| 50 | |
| 51 | import org.junit.*; |
| 52 | import static org.junit.Assert.*; |
| 53 | import static org.junit.Assume.*; |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * @author jrose |
| 58 | */ |
| 59 | public class JavaDocExamplesTest { |
| 60 | /** Wrapper for running the JUnit tests in this module. |
| 61 | * Put JUnit on the classpath! |
| 62 | */ |
| 63 | public static void main(String... ignore) { |
| 64 | org.junit.runner.JUnitCore.runClasses(JavaDocExamplesTest.class); |
| 65 | } |
| 66 | // How much output? |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 67 | static int verbosity = Integer.getInteger("test.java.lang.invoke.JavaDocExamplesTest.verbosity", 0); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 68 | |
| 69 | {} |
| 70 | static final private Lookup LOOKUP = lookup(); |
| 71 | // static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class, |
| 72 | // "concat", methodType(String.class, String.class)); |
| 73 | // static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class, |
| 74 | // "hashCode", methodType(int.class)); |
| 75 | |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 76 | // form required if ReflectiveOperationException is intercepted: |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 77 | static final private MethodHandle CONCAT_2, HASHCODE_2; |
| 78 | static { |
| 79 | try { |
| 80 | CONCAT_2 = LOOKUP.findVirtual(String.class, |
| 81 | "concat", methodType(String.class, String.class)); |
| 82 | HASHCODE_2 = LOOKUP.findVirtual(Object.class, |
| 83 | "hashCode", methodType(int.class)); |
jrose | f15905c | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 84 | } catch (ReflectiveOperationException ex) { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 85 | throw new RuntimeException(ex); |
| 86 | } |
| 87 | } |
| 88 | {} |
| 89 | |
| 90 | @Test public void testFindVirtual() throws Throwable { |
| 91 | {} |
| 92 | MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class, |
| 93 | "concat", methodType(String.class, String.class)); |
| 94 | MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class, |
| 95 | "hashCode", methodType(int.class)); |
| 96 | //assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y")); |
jrose | 1c1bfac | 2010-11-22 22:41:31 -0800 | [diff] [blame] | 97 | assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y")); |
| 98 | assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y")); |
| 99 | //assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy")); |
| 100 | assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy")); |
| 101 | assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 102 | {} |
| 103 | } |
| 104 | @Test public void testDropArguments() throws Throwable { |
| 105 | {{ |
| 106 | {} /// JAVADOC |
| 107 | MethodHandle cat = lookup().findVirtual(String.class, |
| 108 | "concat", methodType(String.class, String.class)); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 109 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 110 | MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class); |
| 111 | MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2)); |
| 112 | assertEquals(bigType, d0.type()); |
| 113 | assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z")); |
| 114 | }} |
| 115 | {{ |
| 116 | {} /// JAVADOC |
| 117 | MethodHandle cat = lookup().findVirtual(String.class, |
| 118 | "concat", methodType(String.class, String.class)); |
| 119 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 120 | MethodHandle d0 = dropArguments(cat, 0, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 121 | assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 122 | MethodHandle d1 = dropArguments(cat, 1, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 123 | assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 124 | MethodHandle d2 = dropArguments(cat, 2, String.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 125 | assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 126 | MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 127 | assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 128 | }} |
| 129 | } |
| 130 | |
| 131 | @Test public void testFilterArguments() throws Throwable { |
| 132 | {{ |
| 133 | {} /// JAVADOC |
| 134 | MethodHandle cat = lookup().findVirtual(String.class, |
| 135 | "concat", methodType(String.class, String.class)); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 136 | MethodHandle upcase = lookup().findVirtual(String.class, |
| 137 | "toUpperCase", methodType(String.class)); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 138 | assertEquals("xy", (String) cat.invokeExact("x", "y")); |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 139 | MethodHandle f0 = filterArguments(cat, 0, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 140 | assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 141 | MethodHandle f1 = filterArguments(cat, 1, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 142 | assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 143 | MethodHandle f2 = filterArguments(cat, 0, upcase, upcase); |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 144 | assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 145 | }} |
| 146 | } |
| 147 | |
| 148 | static void assertEquals(Object exp, Object act) { |
| 149 | if (verbosity > 0) |
| 150 | System.out.println("result: "+act); |
| 151 | Assert.assertEquals(exp, act); |
| 152 | } |
| 153 | |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 154 | @Test public void testMethodHandlesSummary() throws Throwable { |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 155 | {{ |
| 156 | {} /// JAVADOC |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 157 | Object x, y; String s; int i; |
| 158 | MethodType mt; MethodHandle mh; |
| 159 | MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 160 | // mt is (char,char)String |
| 161 | mt = MethodType.methodType(String.class, char.class, char.class); |
| 162 | mh = lookup.findVirtual(String.class, "replace", mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 163 | s = (String) mh.invokeExact("daddy",'d','n'); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 164 | // invokeExact(Ljava/lang/String;CC)Ljava/lang/String; |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 165 | assert(s.equals("nanny")); |
| 166 | // weakly typed invocation (using MHs.invoke) |
| 167 | s = (String) mh.invokeWithArguments("sappy", 'p', 'v'); |
| 168 | assert(s.equals("savvy")); |
| 169 | // mt is (Object[])List |
| 170 | mt = MethodType.methodType(java.util.List.class, Object[].class); |
| 171 | mh = lookup.findStatic(java.util.Arrays.class, "asList", mt); |
| 172 | assert(mh.isVarargsCollector()); |
| 173 | x = mh.invokeGeneric("one", "two"); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 174 | // invokeGeneric(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object; |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 175 | assert(x.equals(java.util.Arrays.asList("one","two"))); |
| 176 | // mt is (Object,Object,Object)Object |
| 177 | mt = MethodType.genericMethodType(3); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 178 | mh = mh.asType(mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 179 | x = mh.invokeExact((Object)1, (Object)2, (Object)3); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 180 | // invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 181 | assert(x.equals(java.util.Arrays.asList(1,2,3))); |
| 182 | // mt is { => int} |
| 183 | mt = MethodType.methodType(int.class); |
| 184 | mh = lookup.findVirtual(java.util.List.class, "size", mt); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 185 | i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3)); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 186 | // invokeExact(Ljava/util/List;)I |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 187 | assert(i == 3); |
| 188 | mt = MethodType.methodType(void.class, String.class); |
| 189 | mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt); |
| 190 | mh.invokeExact(System.out, "Hello, world."); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 191 | // invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 192 | {} |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 193 | }} |
| 194 | } |
| 195 | |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 196 | @Test public void testAsVarargsCollector() throws Throwable { |
| 197 | {{ |
| 198 | {} /// JAVADOC |
| 199 | MethodHandle asList = publicLookup() |
| 200 | .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)) |
| 201 | .asVarargsCollector(Object[].class); |
| 202 | assertEquals("[]", asList.invokeGeneric().toString()); |
| 203 | assertEquals("[1]", asList.invokeGeneric(1).toString()); |
| 204 | assertEquals("[two, too]", asList.invokeGeneric("two", "too").toString()); |
| 205 | Object[] argv = { "three", "thee", "tee" }; |
| 206 | assertEquals("[three, thee, tee]", asList.invokeGeneric(argv).toString()); |
| 207 | List ls = (List) asList.invokeGeneric((Object)argv); |
| 208 | assertEquals(1, ls.size()); |
| 209 | assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0))); |
| 210 | }} |
| 211 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 212 | |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 213 | @Test public void testVarargsCollectorSuppression() throws Throwable { |
| 214 | {{ |
| 215 | {} /// JAVADOC |
| 216 | MethodHandle vamh = publicLookup() |
| 217 | .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)) |
| 218 | .asVarargsCollector(Object[].class); |
jrose | adc650a | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 219 | MethodHandle mh = MethodHandles.exactInvoker(vamh.type()).bindTo(vamh); |
jrose | f108fc0 | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 220 | assert(vamh.type().equals(mh.type())); |
| 221 | assertEquals("[1, 2, 3]", vamh.invokeGeneric(1,2,3).toString()); |
| 222 | boolean failed = false; |
| 223 | try { mh.invokeGeneric(1,2,3); } |
| 224 | catch (WrongMethodTypeException ex) { failed = true; } |
| 225 | assert(failed); |
| 226 | {} |
| 227 | }} |
| 228 | } |
jrose | 900bafd | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 229 | } |