blob: 7093ea2005a1c5cf0403b3bdb153679fcb66466b [file] [log] [blame]
jrose900bafd2010-10-30 21:08:23 -07001/*
jrosef108fc02011-02-11 01:26:24 -08002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
jrose900bafd2010-10-30 21:08:23 -07003 * 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
jroseada69fa2011-03-23 23:02:31 -070027 * @summary example code used in javadoc for java.lang.invoke API
jrose900bafd2010-10-30 21:08:23 -070028 * @compile -XDallowTransitionalJSR292=no JavaDocExamplesTest.java
jroseada69fa2011-03-23 23:02:31 -070029 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles test.java.lang.invoke.JavaDocExamplesTest
jrose900bafd2010-10-30 21:08:23 -070030 */
31
32/*
33---- To run outside jtreg:
34$ $JAVA7X_HOME/bin/javac -cp $JUNIT4_JAR -d /tmp/Classes \
jroseada69fa2011-03-23 23:02:31 -070035 $DAVINCI/sources/jdk/test/java/lang/invoke/JavaDocExamplesTest.java
jrose900bafd2010-10-30 21:08:23 -070036$ $JAVA7X_HOME/bin/java -cp $JUNIT4_JAR:/tmp/Classes \
37 -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles \
jroseada69fa2011-03-23 23:02:31 -070038 -Dtest.java.lang.invoke.JavaDocExamplesTest.verbosity=1 \
39 test.java.lang.invoke.JavaDocExamplesTest
jrose900bafd2010-10-30 21:08:23 -070040----
41*/
42
jroseada69fa2011-03-23 23:02:31 -070043package test.java.lang.invoke;
jrose900bafd2010-10-30 21:08:23 -070044
jroseada69fa2011-03-23 23:02:31 -070045import java.lang.invoke.*;
46import static java.lang.invoke.MethodHandles.*;
47import static java.lang.invoke.MethodType.*;
jrose900bafd2010-10-30 21:08:23 -070048
49import java.lang.reflect.*;
50import java.util.*;
51
52import org.junit.*;
53import static org.junit.Assert.*;
54import static org.junit.Assume.*;
55
56
57/**
58 * @author jrose
59 */
60public 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?
jroseada69fa2011-03-23 23:02:31 -070068 static int verbosity = Integer.getInteger("test.java.lang.invoke.JavaDocExamplesTest.verbosity", 0);
jrose900bafd2010-10-30 21:08:23 -070069
70{}
71static 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
jrosef15905c2011-02-11 01:26:32 -080077// form required if ReflectiveOperationException is intercepted:
jrose900bafd2010-10-30 21:08:23 -070078static final private MethodHandle CONCAT_2, HASHCODE_2;
79static {
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));
jrosef15905c2011-02-11 01:26:32 -080085 } catch (ReflectiveOperationException ex) {
jrose900bafd2010-10-30 21:08:23 -070086 throw new RuntimeException(ex);
87 }
88}
89{}
90
91 @Test public void testFindVirtual() throws Throwable {
92{}
93MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
94 "concat", methodType(String.class, String.class));
95MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
96 "hashCode", methodType(int.class));
97//assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y"));
jrose1c1bfac2010-11-22 22:41:31 -080098assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y"));
99assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y"));
100//assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy"));
101assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy"));
102assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy"));
jrose900bafd2010-10-30 21:08:23 -0700103{}
104 }
105 @Test public void testDropArguments() throws Throwable {
106 {{
107{} /// JAVADOC
108MethodHandle cat = lookup().findVirtual(String.class,
109 "concat", methodType(String.class, String.class));
jroseb90d2e72010-12-16 15:59:27 -0800110assertEquals("xy", (String) cat.invokeExact("x", "y"));
jroseada69fa2011-03-23 23:02:31 -0700111MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
112MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
113assertEquals(bigType, d0.type());
114assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
115 }}
116 {{
117{} /// JAVADOC
118MethodHandle cat = lookup().findVirtual(String.class,
119 "concat", methodType(String.class, String.class));
120assertEquals("xy", (String) cat.invokeExact("x", "y"));
jrose900bafd2010-10-30 21:08:23 -0700121MethodHandle d0 = dropArguments(cat, 0, String.class);
jroseb90d2e72010-12-16 15:59:27 -0800122assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
jrose900bafd2010-10-30 21:08:23 -0700123MethodHandle d1 = dropArguments(cat, 1, String.class);
jroseb90d2e72010-12-16 15:59:27 -0800124assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
jrose900bafd2010-10-30 21:08:23 -0700125MethodHandle d2 = dropArguments(cat, 2, String.class);
jroseb90d2e72010-12-16 15:59:27 -0800126assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
jrose900bafd2010-10-30 21:08:23 -0700127MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
jroseb90d2e72010-12-16 15:59:27 -0800128assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
jrose900bafd2010-10-30 21:08:23 -0700129 }}
130 }
131
132 @Test public void testFilterArguments() throws Throwable {
133 {{
134{} /// JAVADOC
135MethodHandle cat = lookup().findVirtual(String.class,
136 "concat", methodType(String.class, String.class));
jrose900bafd2010-10-30 21:08:23 -0700137MethodHandle upcase = lookup().findVirtual(String.class,
138 "toUpperCase", methodType(String.class));
jroseb90d2e72010-12-16 15:59:27 -0800139assertEquals("xy", (String) cat.invokeExact("x", "y"));
jrose900bafd2010-10-30 21:08:23 -0700140MethodHandle f0 = filterArguments(cat, 0, upcase);
jroseb90d2e72010-12-16 15:59:27 -0800141assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
jrose900bafd2010-10-30 21:08:23 -0700142MethodHandle f1 = filterArguments(cat, 1, upcase);
jroseb90d2e72010-12-16 15:59:27 -0800143assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
jrose900bafd2010-10-30 21:08:23 -0700144MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
jroseb90d2e72010-12-16 15:59:27 -0800145assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
jrose900bafd2010-10-30 21:08:23 -0700146 }}
147 }
148
149 static void assertEquals(Object exp, Object act) {
150 if (verbosity > 0)
151 System.out.println("result: "+act);
152 Assert.assertEquals(exp, act);
153 }
154
jrosef108fc02011-02-11 01:26:24 -0800155 @Test public void testMethodHandlesSummary() throws Throwable {
jrose900bafd2010-10-30 21:08:23 -0700156 {{
157{} /// JAVADOC
jrosef108fc02011-02-11 01:26:24 -0800158Object x, y; String s; int i;
159MethodType mt; MethodHandle mh;
160MethodHandles.Lookup lookup = MethodHandles.lookup();
161// mt is (char,char)String
162mt = MethodType.methodType(String.class, char.class, char.class);
163mh = lookup.findVirtual(String.class, "replace", mt);
jrosef108fc02011-02-11 01:26:24 -0800164s = (String) mh.invokeExact("daddy",'d','n');
jroseadc650a2011-02-11 01:26:28 -0800165// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
jrosef108fc02011-02-11 01:26:24 -0800166assert(s.equals("nanny"));
167// weakly typed invocation (using MHs.invoke)
168s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
169assert(s.equals("savvy"));
170// mt is (Object[])List
171mt = MethodType.methodType(java.util.List.class, Object[].class);
172mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
173assert(mh.isVarargsCollector());
174x = mh.invokeGeneric("one", "two");
jroseadc650a2011-02-11 01:26:28 -0800175// invokeGeneric(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
jrosef108fc02011-02-11 01:26:24 -0800176assert(x.equals(java.util.Arrays.asList("one","two")));
177// mt is (Object,Object,Object)Object
178mt = MethodType.genericMethodType(3);
jroseadc650a2011-02-11 01:26:28 -0800179mh = mh.asType(mt);
jrosef108fc02011-02-11 01:26:24 -0800180x = mh.invokeExact((Object)1, (Object)2, (Object)3);
jroseadc650a2011-02-11 01:26:28 -0800181// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
jrosef108fc02011-02-11 01:26:24 -0800182assert(x.equals(java.util.Arrays.asList(1,2,3)));
183// mt is { => int}
184mt = MethodType.methodType(int.class);
185mh = lookup.findVirtual(java.util.List.class, "size", mt);
jrosef108fc02011-02-11 01:26:24 -0800186i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3));
jroseadc650a2011-02-11 01:26:28 -0800187// invokeExact(Ljava/util/List;)I
jrosef108fc02011-02-11 01:26:24 -0800188assert(i == 3);
189mt = MethodType.methodType(void.class, String.class);
190mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
191mh.invokeExact(System.out, "Hello, world.");
jroseadc650a2011-02-11 01:26:28 -0800192// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
jrosef108fc02011-02-11 01:26:24 -0800193{}
jrose900bafd2010-10-30 21:08:23 -0700194 }}
195 }
196
jrosef108fc02011-02-11 01:26:24 -0800197 @Test public void testAsVarargsCollector() throws Throwable {
198 {{
199{} /// JAVADOC
200MethodHandle asList = publicLookup()
201 .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
202 .asVarargsCollector(Object[].class);
203assertEquals("[]", asList.invokeGeneric().toString());
204assertEquals("[1]", asList.invokeGeneric(1).toString());
205assertEquals("[two, too]", asList.invokeGeneric("two", "too").toString());
206Object[] argv = { "three", "thee", "tee" };
207assertEquals("[three, thee, tee]", asList.invokeGeneric(argv).toString());
208List ls = (List) asList.invokeGeneric((Object)argv);
209assertEquals(1, ls.size());
210assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
211 }}
212 }
jrose900bafd2010-10-30 21:08:23 -0700213
jrosef108fc02011-02-11 01:26:24 -0800214 @Test public void testVarargsCollectorSuppression() throws Throwable {
215 {{
216{} /// JAVADOC
217MethodHandle vamh = publicLookup()
218 .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
219 .asVarargsCollector(Object[].class);
jroseadc650a2011-02-11 01:26:28 -0800220MethodHandle mh = MethodHandles.exactInvoker(vamh.type()).bindTo(vamh);
jrosef108fc02011-02-11 01:26:24 -0800221assert(vamh.type().equals(mh.type()));
222assertEquals("[1, 2, 3]", vamh.invokeGeneric(1,2,3).toString());
223boolean failed = false;
224try { mh.invokeGeneric(1,2,3); }
225catch (WrongMethodTypeException ex) { failed = true; }
226assert(failed);
227{}
228 }}
229 }
jrose900bafd2010-10-30 21:08:23 -0700230}