blob: 1e29956e5fb4699efbc5311a6386b710e8e3f29a [file] [log] [blame]
jrose900bafd2010-10-30 21:08:23 -07001/*
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
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
43package test.java.dyn;
44
45import java.dyn.*;
46import static java.dyn.MethodHandles.*;
47import static java.dyn.MethodType.*;
48
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?
68 static int verbosity = Integer.getInteger("test.java.dyn.JavaDocExamplesTest.verbosity", 0);
69
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
77// form required if NoAccessException is intercepted:
78static 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));
85 } catch (NoAccessException ex) {
86 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));
110cat = cat.asType(methodType(Object.class, String.class, String.class)); /*(String)*/
111assertEquals("xy", /*(String)*/ cat.invokeExact("x", "y"));
112MethodHandle d0 = dropArguments(cat, 0, String.class);
113assertEquals("yz", /*(String)*/ d0.invokeExact("x", "y", "z"));
114MethodHandle d1 = dropArguments(cat, 1, String.class);
115assertEquals("xz", /*(String)*/ d1.invokeExact("x", "y", "z"));
116MethodHandle d2 = dropArguments(cat, 2, String.class);
117assertEquals("xy", /*(String)*/ d2.invokeExact("x", "y", "z"));
118MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
119assertEquals("xz", /*(String)*/ d12.invokeExact("x", 12, true, "z"));
120 }}
121 }
122
123 @Test public void testFilterArguments() throws Throwable {
124 {{
125{} /// JAVADOC
126MethodHandle cat = lookup().findVirtual(String.class,
127 "concat", methodType(String.class, String.class));
128cat = cat.asType(methodType(Object.class, String.class, String.class)); /*(String)*/
129MethodHandle upcase = lookup().findVirtual(String.class,
130 "toUpperCase", methodType(String.class));
131assertEquals("xy", /*(String)*/ cat.invokeExact("x", "y")); // xy
132MethodHandle f0 = filterArguments(cat, 0, upcase);
133assertEquals("Xy", /*(String)*/ f0.invokeExact("x", "y")); // Xy
134MethodHandle f1 = filterArguments(cat, 1, upcase);
135assertEquals("xY", /*(String)*/ f1.invokeExact("x", "y")); // xY
136MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
137assertEquals("XY", /*(String)*/ f2.invokeExact("x", "y")); // XY
138 }}
139 }
140
141 static void assertEquals(Object exp, Object act) {
142 if (verbosity > 0)
143 System.out.println("result: "+act);
144 Assert.assertEquals(exp, act);
145 }
146
147 @Test public void testVolatileCallSite() throws Throwable {
148 {{
149{} /// JAVADOC
150MethodHandle strcat = MethodHandles.lookup()
151 .findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
152MethodHandle trueCon = MethodHandles.constant(boolean.class, true);
153MethodHandle falseCon = MethodHandles.constant(boolean.class, false);
154VolatileCallSite switcher = new VolatileCallSite(trueCon, falseCon);
155// following steps may be repeated to re-use the same switcher:
156MethodHandle worker1 = strcat;
157MethodHandle worker2 = MethodHandles.permuteArguments(strcat, strcat.type(), 1, 0);
158MethodHandle worker = MethodHandles.guardWithTest(switcher.dynamicInvoker(), worker1, worker2);
159System.out.println((String) worker.invokeExact("met", "hod")); // method
160switcher.invalidate();
161System.out.println((String) worker.invokeExact("met", "hod")); // hodmet
162 }}
163 }
164
165static MethodHandle asList;
166 @Test public void testWithTypeHandler() throws Throwable {
167 {{
168{} /// JAVADOC
169MethodHandle makeEmptyList = MethodHandles.constant(List.class, Arrays.asList());
170MethodHandle asList = lookup()
171 .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class));
172
173JavaDocExamplesTest.asList = asList;
174/*
175static MethodHandle collectingTypeHandler(MethodHandle base, MethodType newType) {
176 return asList.asCollector(Object[].class, newType.parameterCount()).asType(newType);
177}
178*/
179
180MethodHandle collectingTypeHandler = lookup()
181 .findStatic(lookup().lookupClass(), "collectingTypeHandler",
182 methodType(MethodHandle.class, MethodHandle.class, MethodType.class));
183MethodHandle makeAnyList = makeEmptyList.withTypeHandler(collectingTypeHandler);
184
185System.out.println(makeAnyList.invokeGeneric());
186System.out.println(makeAnyList.invokeGeneric(1));
187System.out.println(makeAnyList.invokeGeneric("two", "too"));
188 }}
189 }
190
191static MethodHandle collectingTypeHandler(MethodHandle base, MethodType newType) {
192 //System.out.println("Converting "+asList+" to "+newType);
193 MethodHandle conv = asList.asCollector(Object[].class, newType.parameterCount()).asType(newType);
194 //System.out.println(" =>"+conv);
195 return conv;
196}
197
198}