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