author | jrose |
Thu, 07 Apr 2011 22:07:06 -0700 | |
changeset 9033 | a88f5656f05d |
parent 8822 | 8145ab9f5f86 |
child 9752 | 88ab34b6da6d |
permissions | -rw-r--r-- |
7558 | 1 |
/* |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
2 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
7558 | 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 |
|
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
26 |
* @build indify.Indify |
7558 | 27 |
* @compile InvokeDynamicPrintArgs.java |
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
28 |
* @run main/othervm |
7558 | 29 |
* indify.Indify |
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
30 |
* --verify-specifier-count=3 |
7558 | 31 |
* --expand-properties --classpath ${test.classes} |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
32 |
* --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output |
7558 | 33 |
*/ |
34 |
||
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
35 |
package test.java.lang.invoke; |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
36 |
|
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
37 |
import org.junit.Test; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
38 |
|
7558 | 39 |
import java.util.*; |
40 |
import java.io.*; |
|
41 |
||
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
42 |
import java.lang.invoke.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
43 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
44 |
import static java.lang.invoke.MethodType.*; |
7558 | 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 |
||
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
60 |
@Test |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
61 |
public void testInvokeDynamicPrintArgs() throws IOException { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
62 |
System.err.println(System.getProperties()); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
63 |
String testClassPath = System.getProperty("build.test.classes.dir"); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
64 |
if (testClassPath == null) throw new RuntimeException(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
65 |
String[] args = new String[]{ |
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
66 |
"--verify-specifier-count=3", |
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
67 |
"--verbose", |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
68 |
"--expand-properties", "--classpath", testClassPath, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
69 |
"--java", "test.java.lang.invoke.InvokeDynamicPrintArgs", "--check-output" |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
70 |
}; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
71 |
System.err.println("Indify: "+Arrays.toString(args)); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
72 |
indify.Indify.main(args); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
73 |
} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
74 |
|
7558 | 75 |
private static PrintStream oldOut; |
76 |
private static ByteArrayOutputStream buf; |
|
77 |
private static void openBuf() { |
|
78 |
oldOut = System.out; |
|
79 |
buf = new ByteArrayOutputStream(); |
|
80 |
System.setOut(new PrintStream(buf)); |
|
81 |
} |
|
82 |
private static void closeBuf() { |
|
83 |
if (buf == null) return; |
|
84 |
System.out.flush(); |
|
85 |
System.setOut(oldOut); |
|
86 |
String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+"); |
|
87 |
for (String line : haveLines) System.out.println(line); |
|
88 |
Iterator<String> iter = Arrays.asList(haveLines).iterator(); |
|
89 |
for (String want : EXPECT_OUTPUT) { |
|
90 |
String have = iter.hasNext() ? iter.next() : "[EOF]"; |
|
91 |
if (want.equals(have)) continue; |
|
92 |
System.err.println("want line: "+want); |
|
93 |
System.err.println("have line: "+have); |
|
94 |
throw new AssertionError("unexpected output: "+have); |
|
95 |
} |
|
96 |
if (iter.hasNext()) |
|
97 |
throw new AssertionError("unexpected output: "+iter.next()); |
|
98 |
} |
|
99 |
private static final String[] EXPECT_OUTPUT = { |
|
100 |
"Printing some argument lists, starting with a empty one:", |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
101 |
"[test.java.lang.invoke.InvokeDynamicPrintArgs, nothing, ()void][]", |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
102 |
"[test.java.lang.invoke.InvokeDynamicPrintArgs, bar, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]", |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
103 |
"[test.java.lang.invoke.InvokeDynamicPrintArgs, bar2, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]", |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
104 |
"[test.java.lang.invoke.InvokeDynamicPrintArgs, baz, (String,int,double)void, 1234.5][baz arg, 2, 3.14]", |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8345
diff
changeset
|
105 |
"[test.java.lang.invoke.InvokeDynamicPrintArgs, foo, (String)void][foo arg]", |
7558 | 106 |
"Done printing argument lists." |
107 |
}; |
|
108 |
||
109 |
private static void printArgs(Object bsmInfo, Object... args) { |
|
110 |
System.out.println(bsmInfo+Arrays.deepToString(args)); |
|
111 |
} |
|
112 |
private static MethodHandle MH_printArgs() throws ReflectiveOperationException { |
|
113 |
shouldNotCallThis(); |
|
114 |
return lookup().findStatic(lookup().lookupClass(), |
|
115 |
"printArgs", methodType(void.class, Object.class, Object[].class)); |
|
116 |
} |
|
117 |
||
118 |
private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException { |
|
119 |
// ignore caller and name, but match the type: |
|
120 |
Object bsmInfo = Arrays.asList(caller, name, type); |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7558
diff
changeset
|
121 |
return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type)); |
7558 | 122 |
} |
123 |
private static MethodType MT_bsm() { |
|
124 |
shouldNotCallThis(); |
|
125 |
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class); |
|
126 |
} |
|
127 |
private static MethodHandle MH_bsm() throws ReflectiveOperationException { |
|
128 |
shouldNotCallThis(); |
|
129 |
return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm()); |
|
130 |
} |
|
131 |
||
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
132 |
private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws ReflectiveOperationException { |
7558 | 133 |
// ignore caller and name, but match the type: |
134 |
List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type)); |
|
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
135 |
bsmInfo.addAll(Arrays.asList((Object[])arg)); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7558
diff
changeset
|
136 |
return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type)); |
7558 | 137 |
} |
138 |
private static MethodType MT_bsm2() { |
|
139 |
shouldNotCallThis(); |
|
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
140 |
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object[].class); |
7558 | 141 |
} |
142 |
private static MethodHandle MH_bsm2() throws ReflectiveOperationException { |
|
143 |
shouldNotCallThis(); |
|
144 |
return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2()); |
|
145 |
} |
|
146 |
||
147 |
private static MethodHandle INDY_nothing() throws Throwable { |
|
148 |
shouldNotCallThis(); |
|
149 |
return ((CallSite) MH_bsm().invokeGeneric(lookup(), |
|
150 |
"nothing", methodType(void.class) |
|
151 |
)).dynamicInvoker(); |
|
152 |
} |
|
153 |
private static MethodHandle INDY_foo() throws Throwable { |
|
154 |
shouldNotCallThis(); |
|
155 |
return ((CallSite) MH_bsm().invokeGeneric(lookup(), |
|
156 |
"foo", methodType(void.class, String.class) |
|
157 |
)).dynamicInvoker(); |
|
158 |
} |
|
159 |
private static MethodHandle INDY_bar() throws Throwable { |
|
160 |
shouldNotCallThis(); |
|
161 |
return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
162 |
"bar", methodType(void.class, String.class, int.class) |
|
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
163 |
, Void.class, "void type!", 1, 234.5F, 67.5, (long)89 |
7558 | 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) |
|
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
170 |
, Void.class, "void type!", 1, 234.5F, 67.5, (long)89 |
7558 | 171 |
)).dynamicInvoker(); |
172 |
} |
|
173 |
private static MethodHandle INDY_baz() throws Throwable { |
|
174 |
shouldNotCallThis(); |
|
175 |
return ((CallSite) MH_bsm2().invokeGeneric(lookup(), |
|
176 |
"baz", methodType(void.class, String.class, int.class, double.class) |
|
177 |
, 1234.5 |
|
178 |
)).dynamicInvoker(); |
|
179 |
} |
|
180 |
||
181 |
private static void shouldNotCallThis() { |
|
182 |
// if this gets called, the transformation has not taken place |
|
183 |
if (System.getProperty("InvokeDynamicPrintArgs.allow-untransformed") != null) return; |
|
184 |
throw new AssertionError("this code should be statically transformed away by Indify"); |
|
185 |
} |
|
186 |
} |