1 /* |
|
2 * Copyright (c) 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* @test |
|
25 * @summary smoke test for invokedynamic instructions |
|
26 * @build indify.Indify |
|
27 * @compile InvokeDynamicPrintArgs.java |
|
28 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic |
|
29 * indify.Indify |
|
30 * --verify-specifier-count=3 --transitionalJSR292=false |
|
31 * --expand-properties --classpath ${test.classes} |
|
32 * --java test.java.dyn.InvokeDynamicPrintArgs --check-output |
|
33 */ |
|
34 |
|
35 package test.java.dyn; |
|
36 |
|
37 import org.junit.Test; |
|
38 |
|
39 import java.util.*; |
|
40 import java.io.*; |
|
41 |
|
42 import java.dyn.*; |
|
43 import static java.dyn.MethodHandles.*; |
|
44 import static java.dyn.MethodType.*; |
|
45 |
|
46 public class InvokeDynamicPrintArgs { |
|
47 public static void main(String... av) throws Throwable { |
|
48 if (av.length > 0) openBuf(); // --check-output mode |
|
49 System.out.println("Printing some argument lists, starting with a empty one:"); |
|
50 INDY_nothing().invokeExact(); // BSM specifier #0 = {bsm} |
|
51 INDY_bar().invokeExact("bar arg", 1); // BSM specifier #1 = {bsm2, Void.class, "void type"} |
|
52 INDY_bar2().invokeExact("bar2 arg", 222); // BSM specifier #1 = (same) |
|
53 INDY_baz().invokeExact("baz arg", 2, 3.14); // BSM specifier #2 = {bsm2, 1234.5} |
|
54 INDY_foo().invokeExact("foo arg"); // BSM specifier #0 = (same) |
|
55 // Hence, BSM specifier count should be 3. See "--verify-specifier-count=3" above. |
|
56 System.out.println("Done printing argument lists."); |
|
57 closeBuf(); |
|
58 } |
|
59 |
|
60 @Test |
|
61 public void testInvokeDynamicPrintArgs() throws IOException { |
|
62 System.err.println(System.getProperties()); |
|
63 String testClassPath = System.getProperty("build.test.classes.dir"); |
|
64 if (testClassPath == null) throw new RuntimeException(); |
|
65 String[] args = new String[]{ |
|
66 "--verify-specifier-count=3", "--transitionalJSR292=false", |
|
67 "--expand-properties", "--classpath", testClassPath, |
|
68 "--java", "test.java.dyn.InvokeDynamicPrintArgs", "--check-output" |
|
69 }; |
|
70 System.err.println("Indify: "+Arrays.toString(args)); |
|
71 indify.Indify.main(args); |
|
72 } |
|
73 |
|
74 private static PrintStream oldOut; |
|
75 private static ByteArrayOutputStream buf; |
|
76 private static void openBuf() { |
|
77 oldOut = System.out; |
|
78 buf = new ByteArrayOutputStream(); |
|
79 System.setOut(new PrintStream(buf)); |
|
80 } |
|
81 private static void closeBuf() { |
|
82 if (buf == null) return; |
|
83 System.out.flush(); |
|
84 System.setOut(oldOut); |
|
85 String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+"); |
|
86 for (String line : haveLines) System.out.println(line); |
|
87 Iterator<String> iter = Arrays.asList(haveLines).iterator(); |
|
88 for (String want : EXPECT_OUTPUT) { |
|
89 String have = iter.hasNext() ? iter.next() : "[EOF]"; |
|
90 if (want.equals(have)) continue; |
|
91 System.err.println("want line: "+want); |
|
92 System.err.println("have line: "+have); |
|
93 throw new AssertionError("unexpected output: "+have); |
|
94 } |
|
95 if (iter.hasNext()) |
|
96 throw new AssertionError("unexpected output: "+iter.next()); |
|
97 } |
|
98 private static final String[] EXPECT_OUTPUT = { |
|
99 "Printing some argument lists, starting with a empty one:", |
|
100 "[test.java.dyn.InvokeDynamicPrintArgs, nothing, ()void][]", |
|
101 "[test.java.dyn.InvokeDynamicPrintArgs, bar, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]", |
|
102 "[test.java.dyn.InvokeDynamicPrintArgs, bar2, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]", |
|
103 "[test.java.dyn.InvokeDynamicPrintArgs, baz, (String,int,double)void, 1234.5][baz arg, 2, 3.14]", |
|
104 "[test.java.dyn.InvokeDynamicPrintArgs, foo, (String)void][foo arg]", |
|
105 "Done printing argument lists." |
|
106 }; |
|
107 |
|
108 private static void printArgs(Object bsmInfo, Object... args) { |
|
109 System.out.println(bsmInfo+Arrays.deepToString(args)); |
|
110 } |
|
111 private static MethodHandle MH_printArgs() throws ReflectiveOperationException { |
|
112 shouldNotCallThis(); |
|
113 return lookup().findStatic(lookup().lookupClass(), |
|
114 "printArgs", methodType(void.class, Object.class, Object[].class)); |
|
115 } |
|
116 |
|
117 private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException { |
|
118 // ignore caller and name, but match the type: |
|
119 Object bsmInfo = Arrays.asList(caller, name, type); |
|
120 return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type)); |
|
121 } |
|
122 private static MethodType MT_bsm() { |
|
123 shouldNotCallThis(); |
|
124 return methodType(CallSite.class, Lookup.class, String.class, MethodType.class); |
|
125 } |
|
126 private static MethodHandle MH_bsm() throws ReflectiveOperationException { |
|
127 shouldNotCallThis(); |
|
128 return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm()); |
|
129 } |
|
130 |
|
131 private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws ReflectiveOperationException { |
|
132 // ignore caller and name, but match the type: |
|
133 List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type)); |
|
134 bsmInfo.addAll(Arrays.asList((Object[])arg)); |
|
135 return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type)); |
|
136 } |
|
137 private static MethodType MT_bsm2() { |
|
138 shouldNotCallThis(); |
|
139 return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object[].class); |
|
140 } |
|
141 private static MethodHandle MH_bsm2() throws ReflectiveOperationException { |
|
142 shouldNotCallThis(); |
|
143 return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2()); |
|
144 } |
|
145 |
|
146 private static MethodHandle INDY_nothing() throws Throwable { |
|
147 shouldNotCallThis(); |
|
148 return ((CallSite) MH_bsm().invokeGeneric(lookup(), |
|
149 "nothing", methodType(void.class) |
|
150 )).dynamicInvoker(); |
|
151 } |
|
152 private static MethodHandle INDY_foo() throws Throwable { |
|
153 shouldNotCallThis(); |
|
154 return ((CallSite) MH_bsm().invokeGeneric(lookup(), |
|
155 "foo", methodType(void.class, String.class) |
|
156 )).dynamicInvoker(); |
|
157 } |
|
158 private static MethodHandle INDY_bar() throws Throwable { |
|
159 shouldNotCallThis(); |
|
160 return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
161 "bar", methodType(void.class, String.class, int.class) |
|
162 , new Object[] { Void.class, "void type!", |
|
163 1, 234.5F, 67.5, (long)89 } |
|
164 )).dynamicInvoker(); |
|
165 } |
|
166 private static MethodHandle INDY_bar2() throws Throwable { |
|
167 shouldNotCallThis(); |
|
168 return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
169 "bar2", methodType(void.class, String.class, int.class) |
|
170 , new Object[] { Void.class, "void type!", |
|
171 1, 234.5F, 67.5, (long)89 } |
|
172 )).dynamicInvoker(); |
|
173 } |
|
174 private static MethodHandle INDY_baz() throws Throwable { |
|
175 shouldNotCallThis(); |
|
176 return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
177 "baz", methodType(void.class, String.class, int.class, double.class) |
|
178 , 1234.5 |
|
179 )).dynamicInvoker(); |
|
180 } |
|
181 |
|
182 private static void shouldNotCallThis() { |
|
183 // if this gets called, the transformation has not taken place |
|
184 if (System.getProperty("InvokeDynamicPrintArgs.allow-untransformed") != null) return; |
|
185 throw new AssertionError("this code should be statically transformed away by Indify"); |
|
186 } |
|
187 } |
|