blob: 7810399c62dec5cbce949bfe05e635b4cea4d497 [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
jrose9633f5f2011-04-07 22:07:06 -070028 * @compile JavaDocExamplesTest.java
29 * @run junit/othervm 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 \
jroseada69fa2011-03-23 23:02:31 -070037 -Dtest.java.lang.invoke.JavaDocExamplesTest.verbosity=1 \
38 test.java.lang.invoke.JavaDocExamplesTest
jrose900bafd2010-10-30 21:08:23 -070039----
40*/
41
jroseada69fa2011-03-23 23:02:31 -070042package test.java.lang.invoke;
jrose900bafd2010-10-30 21:08:23 -070043
jroseada69fa2011-03-23 23:02:31 -070044import java.lang.invoke.*;
45import static java.lang.invoke.MethodHandles.*;
46import static java.lang.invoke.MethodType.*;
jrose900bafd2010-10-30 21:08:23 -070047
48import java.lang.reflect.*;
49import java.util.*;
50
51import org.junit.*;
52import static org.junit.Assert.*;
53import static org.junit.Assume.*;
54
55
56/**
57 * @author jrose
58 */
59public 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?
jroseada69fa2011-03-23 23:02:31 -070067 static int verbosity = Integer.getInteger("test.java.lang.invoke.JavaDocExamplesTest.verbosity", 0);
jrose900bafd2010-10-30 21:08:23 -070068
69{}
70static 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
jrosef15905c2011-02-11 01:26:32 -080076// form required if ReflectiveOperationException is intercepted:
jrose900bafd2010-10-30 21:08:23 -070077static final private MethodHandle CONCAT_2, HASHCODE_2;
78static {
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));
jrosef15905c2011-02-11 01:26:32 -080084 } catch (ReflectiveOperationException ex) {
jrose900bafd2010-10-30 21:08:23 -070085 throw new RuntimeException(ex);
86 }
87}
88{}
89
90 @Test public void testFindVirtual() throws Throwable {
91{}
92MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
93 "concat", methodType(String.class, String.class));
94MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
95 "hashCode", methodType(int.class));
96//assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y"));
jrose1c1bfac2010-11-22 22:41:31 -080097assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y"));
98assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y"));
99//assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy"));
100assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy"));
101assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy"));
jrose900bafd2010-10-30 21:08:23 -0700102{}
103 }
104 @Test public void testDropArguments() throws Throwable {
105 {{
106{} /// JAVADOC
107MethodHandle cat = lookup().findVirtual(String.class,
108 "concat", methodType(String.class, String.class));
jroseb90d2e72010-12-16 15:59:27 -0800109assertEquals("xy", (String) cat.invokeExact("x", "y"));
jroseada69fa2011-03-23 23:02:31 -0700110MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
111MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
112assertEquals(bigType, d0.type());
113assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
114 }}
115 {{
116{} /// JAVADOC
117MethodHandle cat = lookup().findVirtual(String.class,
118 "concat", methodType(String.class, String.class));
119assertEquals("xy", (String) cat.invokeExact("x", "y"));
jrose900bafd2010-10-30 21:08:23 -0700120MethodHandle d0 = dropArguments(cat, 0, String.class);
jroseb90d2e72010-12-16 15:59:27 -0800121assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
jrose900bafd2010-10-30 21:08:23 -0700122MethodHandle d1 = dropArguments(cat, 1, String.class);
jroseb90d2e72010-12-16 15:59:27 -0800123assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
jrose900bafd2010-10-30 21:08:23 -0700124MethodHandle d2 = dropArguments(cat, 2, String.class);
jroseb90d2e72010-12-16 15:59:27 -0800125assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
jrose900bafd2010-10-30 21:08:23 -0700126MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
jroseb90d2e72010-12-16 15:59:27 -0800127assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
jrose900bafd2010-10-30 21:08:23 -0700128 }}
129 }
130
131 @Test public void testFilterArguments() throws Throwable {
132 {{
133{} /// JAVADOC
134MethodHandle cat = lookup().findVirtual(String.class,
135 "concat", methodType(String.class, String.class));
jrose900bafd2010-10-30 21:08:23 -0700136MethodHandle upcase = lookup().findVirtual(String.class,
137 "toUpperCase", methodType(String.class));
jroseb90d2e72010-12-16 15:59:27 -0800138assertEquals("xy", (String) cat.invokeExact("x", "y"));
jrose900bafd2010-10-30 21:08:23 -0700139MethodHandle f0 = filterArguments(cat, 0, upcase);
jroseb90d2e72010-12-16 15:59:27 -0800140assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
jrose900bafd2010-10-30 21:08:23 -0700141MethodHandle f1 = filterArguments(cat, 1, upcase);
jroseb90d2e72010-12-16 15:59:27 -0800142assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
jrose900bafd2010-10-30 21:08:23 -0700143MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
jroseb90d2e72010-12-16 15:59:27 -0800144assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
jrose900bafd2010-10-30 21:08:23 -0700145 }}
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
jrosef108fc02011-02-11 01:26:24 -0800154 @Test public void testMethodHandlesSummary() throws Throwable {
jrose900bafd2010-10-30 21:08:23 -0700155 {{
156{} /// JAVADOC
jrosef108fc02011-02-11 01:26:24 -0800157Object x, y; String s; int i;
158MethodType mt; MethodHandle mh;
159MethodHandles.Lookup lookup = MethodHandles.lookup();
160// mt is (char,char)String
161mt = MethodType.methodType(String.class, char.class, char.class);
162mh = lookup.findVirtual(String.class, "replace", mt);
jrosef108fc02011-02-11 01:26:24 -0800163s = (String) mh.invokeExact("daddy",'d','n');
jroseadc650a2011-02-11 01:26:28 -0800164// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
jrosef108fc02011-02-11 01:26:24 -0800165assert(s.equals("nanny"));
166// weakly typed invocation (using MHs.invoke)
167s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
168assert(s.equals("savvy"));
169// mt is (Object[])List
170mt = MethodType.methodType(java.util.List.class, Object[].class);
171mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
172assert(mh.isVarargsCollector());
173x = mh.invokeGeneric("one", "two");
jroseadc650a2011-02-11 01:26:28 -0800174// invokeGeneric(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
jrosef108fc02011-02-11 01:26:24 -0800175assert(x.equals(java.util.Arrays.asList("one","two")));
176// mt is (Object,Object,Object)Object
177mt = MethodType.genericMethodType(3);
jroseadc650a2011-02-11 01:26:28 -0800178mh = mh.asType(mt);
jrosef108fc02011-02-11 01:26:24 -0800179x = mh.invokeExact((Object)1, (Object)2, (Object)3);
jroseadc650a2011-02-11 01:26:28 -0800180// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
jrosef108fc02011-02-11 01:26:24 -0800181assert(x.equals(java.util.Arrays.asList(1,2,3)));
182// mt is { => int}
183mt = MethodType.methodType(int.class);
184mh = lookup.findVirtual(java.util.List.class, "size", mt);
jrosef108fc02011-02-11 01:26:24 -0800185i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3));
jroseadc650a2011-02-11 01:26:28 -0800186// invokeExact(Ljava/util/List;)I
jrosef108fc02011-02-11 01:26:24 -0800187assert(i == 3);
188mt = MethodType.methodType(void.class, String.class);
189mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
190mh.invokeExact(System.out, "Hello, world.");
jroseadc650a2011-02-11 01:26:28 -0800191// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
jrosef108fc02011-02-11 01:26:24 -0800192{}
jrose900bafd2010-10-30 21:08:23 -0700193 }}
194 }
195
jrosef108fc02011-02-11 01:26:24 -0800196 @Test public void testAsVarargsCollector() throws Throwable {
197 {{
198{} /// JAVADOC
199MethodHandle asList = publicLookup()
200 .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
201 .asVarargsCollector(Object[].class);
202assertEquals("[]", asList.invokeGeneric().toString());
203assertEquals("[1]", asList.invokeGeneric(1).toString());
204assertEquals("[two, too]", asList.invokeGeneric("two", "too").toString());
205Object[] argv = { "three", "thee", "tee" };
206assertEquals("[three, thee, tee]", asList.invokeGeneric(argv).toString());
207List ls = (List) asList.invokeGeneric((Object)argv);
208assertEquals(1, ls.size());
209assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
210 }}
211 }
jrose900bafd2010-10-30 21:08:23 -0700212
jrosef108fc02011-02-11 01:26:24 -0800213 @Test public void testVarargsCollectorSuppression() throws Throwable {
214 {{
215{} /// JAVADOC
216MethodHandle vamh = publicLookup()
217 .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
218 .asVarargsCollector(Object[].class);
jroseadc650a2011-02-11 01:26:28 -0800219MethodHandle mh = MethodHandles.exactInvoker(vamh.type()).bindTo(vamh);
jrosef108fc02011-02-11 01:26:24 -0800220assert(vamh.type().equals(mh.type()));
221assertEquals("[1, 2, 3]", vamh.invokeGeneric(1,2,3).toString());
222boolean failed = false;
223try { mh.invokeGeneric(1,2,3); }
224catch (WrongMethodTypeException ex) { failed = true; }
225assert(failed);
226{}
227 }}
228 }
jrose900bafd2010-10-30 21:08:23 -0700229}