author | jrose |
Thu, 16 Dec 2010 15:59:27 -0800 | |
changeset 7562 | a0ad195efe2c |
parent 7558 | e7a391a3bcfe |
child 8345 | 9e2483e6cfab |
permissions | -rw-r--r-- |
7558 | 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 |
* @library 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 InvokeDynamicPrintArgs --check-output |
|
33 |
*/ |
|
34 |
||
35 |
import java.util.*; |
|
36 |
import java.io.*; |
|
37 |
||
38 |
import java.dyn.*; |
|
39 |
import static java.dyn.MethodHandles.*; |
|
40 |
import static java.dyn.MethodType.*; |
|
41 |
||
42 |
public class InvokeDynamicPrintArgs { |
|
43 |
public static void main(String... av) throws Throwable { |
|
44 |
if (av.length > 0) openBuf(); // --check-output mode |
|
45 |
System.out.println("Printing some argument lists, starting with a empty one:"); |
|
46 |
INDY_nothing().invokeExact(); // BSM specifier #0 = {bsm} |
|
47 |
INDY_bar().invokeExact("bar arg", 1); // BSM specifier #1 = {bsm2, Void.class, "void type"} |
|
48 |
INDY_bar2().invokeExact("bar2 arg", 222); // BSM specifier #1 = (same) |
|
49 |
INDY_baz().invokeExact("baz arg", 2, 3.14); // BSM specifier #2 = {bsm2, 1234.5} |
|
50 |
INDY_foo().invokeExact("foo arg"); // BSM specifier #0 = (same) |
|
51 |
// Hence, BSM specifier count should be 3. See "--verify-specifier-count=3" above. |
|
52 |
System.out.println("Done printing argument lists."); |
|
53 |
closeBuf(); |
|
54 |
} |
|
55 |
||
56 |
private static PrintStream oldOut; |
|
57 |
private static ByteArrayOutputStream buf; |
|
58 |
private static void openBuf() { |
|
59 |
oldOut = System.out; |
|
60 |
buf = new ByteArrayOutputStream(); |
|
61 |
System.setOut(new PrintStream(buf)); |
|
62 |
} |
|
63 |
private static void closeBuf() { |
|
64 |
if (buf == null) return; |
|
65 |
System.out.flush(); |
|
66 |
System.setOut(oldOut); |
|
67 |
String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+"); |
|
68 |
for (String line : haveLines) System.out.println(line); |
|
69 |
Iterator<String> iter = Arrays.asList(haveLines).iterator(); |
|
70 |
for (String want : EXPECT_OUTPUT) { |
|
71 |
String have = iter.hasNext() ? iter.next() : "[EOF]"; |
|
72 |
if (want.equals(have)) continue; |
|
73 |
System.err.println("want line: "+want); |
|
74 |
System.err.println("have line: "+have); |
|
75 |
throw new AssertionError("unexpected output: "+have); |
|
76 |
} |
|
77 |
if (iter.hasNext()) |
|
78 |
throw new AssertionError("unexpected output: "+iter.next()); |
|
79 |
} |
|
80 |
private static final String[] EXPECT_OUTPUT = { |
|
81 |
"Printing some argument lists, starting with a empty one:", |
|
82 |
"[InvokeDynamicPrintArgs, nothing, ()void][]", |
|
83 |
"[InvokeDynamicPrintArgs, bar, (java.lang.String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]", |
|
84 |
"[InvokeDynamicPrintArgs, bar2, (java.lang.String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]", |
|
85 |
"[InvokeDynamicPrintArgs, baz, (java.lang.String,int,double)void, 1234.5][baz arg, 2, 3.14]", |
|
86 |
"[InvokeDynamicPrintArgs, foo, (java.lang.String)void][foo arg]", |
|
87 |
"Done printing argument lists." |
|
88 |
}; |
|
89 |
||
90 |
private static void printArgs(Object bsmInfo, Object... args) { |
|
91 |
System.out.println(bsmInfo+Arrays.deepToString(args)); |
|
92 |
} |
|
93 |
private static MethodHandle MH_printArgs() throws ReflectiveOperationException { |
|
94 |
shouldNotCallThis(); |
|
95 |
return lookup().findStatic(lookup().lookupClass(), |
|
96 |
"printArgs", methodType(void.class, Object.class, Object[].class)); |
|
97 |
} |
|
98 |
||
99 |
private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException { |
|
100 |
// ignore caller and name, but match the type: |
|
101 |
Object bsmInfo = Arrays.asList(caller, name, type); |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7558
diff
changeset
|
102 |
return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type)); |
7558 | 103 |
} |
104 |
private static MethodType MT_bsm() { |
|
105 |
shouldNotCallThis(); |
|
106 |
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class); |
|
107 |
} |
|
108 |
private static MethodHandle MH_bsm() throws ReflectiveOperationException { |
|
109 |
shouldNotCallThis(); |
|
110 |
return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm()); |
|
111 |
} |
|
112 |
||
113 |
private static CallSite bsm2(Lookup caller, String name, MethodType type, Object arg) throws ReflectiveOperationException { |
|
114 |
// ignore caller and name, but match the type: |
|
115 |
List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type)); |
|
116 |
if (arg instanceof Object[]) |
|
117 |
bsmInfo.addAll(Arrays.asList((Object[])arg)); |
|
118 |
else |
|
119 |
bsmInfo.add(arg); |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7558
diff
changeset
|
120 |
return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type)); |
7558 | 121 |
} |
122 |
private static MethodType MT_bsm2() { |
|
123 |
shouldNotCallThis(); |
|
124 |
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object.class); |
|
125 |
} |
|
126 |
private static MethodHandle MH_bsm2() throws ReflectiveOperationException { |
|
127 |
shouldNotCallThis(); |
|
128 |
return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2()); |
|
129 |
} |
|
130 |
||
131 |
private static MethodHandle INDY_nothing() throws Throwable { |
|
132 |
shouldNotCallThis(); |
|
133 |
return ((CallSite) MH_bsm().invokeGeneric(lookup(), |
|
134 |
"nothing", methodType(void.class) |
|
135 |
)).dynamicInvoker(); |
|
136 |
} |
|
137 |
private static MethodHandle INDY_foo() throws Throwable { |
|
138 |
shouldNotCallThis(); |
|
139 |
return ((CallSite) MH_bsm().invokeGeneric(lookup(), |
|
140 |
"foo", methodType(void.class, String.class) |
|
141 |
)).dynamicInvoker(); |
|
142 |
} |
|
143 |
private static MethodHandle INDY_bar() throws Throwable { |
|
144 |
shouldNotCallThis(); |
|
145 |
return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
146 |
"bar", methodType(void.class, String.class, int.class) |
|
147 |
, new Object[] { Void.class, "void type!", |
|
148 |
1, 234.5F, 67.5, (long)89 } |
|
149 |
)).dynamicInvoker(); |
|
150 |
} |
|
151 |
private static MethodHandle INDY_bar2() throws Throwable { |
|
152 |
shouldNotCallThis(); |
|
153 |
return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
154 |
"bar2", methodType(void.class, String.class, int.class) |
|
155 |
, new Object[] { Void.class, "void type!", |
|
156 |
1, 234.5F, 67.5, (long)89 } |
|
157 |
)).dynamicInvoker(); |
|
158 |
} |
|
159 |
private static MethodHandle INDY_baz() throws Throwable { |
|
160 |
shouldNotCallThis(); |
|
161 |
return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
162 |
"baz", methodType(void.class, String.class, int.class, double.class) |
|
163 |
, 1234.5 |
|
164 |
)).dynamicInvoker(); |
|
165 |
} |
|
166 |
||
167 |
private static void shouldNotCallThis() { |
|
168 |
// if this gets called, the transformation has not taken place |
|
169 |
if (System.getProperty("InvokeDynamicPrintArgs.allow-untransformed") != null) return; |
|
170 |
throw new AssertionError("this code should be statically transformed away by Indify"); |
|
171 |
} |
|
172 |
} |