Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/invoke/InvokeDynamicPrintArgs.java
41149 views
1
/*
2
* Copyright (c) 2010, 2019, 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
* @bug 7050328 8007035
26
* @summary smoke test for invokedynamic instructions
27
* @build indify.Indify
28
* @compile InvokeDynamicPrintArgs.java
29
* @run main/othervm
30
* indify.Indify
31
* --verify-specifier-count=8
32
* --expand-properties --classpath ${test.classes}
33
* --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output
34
* @run main/othervm
35
* -Djava.security.manager=allow
36
* indify.Indify
37
* --expand-properties --classpath ${test.classes}
38
* --java test.java.lang.invoke.InvokeDynamicPrintArgs --security-manager
39
*/
40
41
package test.java.lang.invoke;
42
43
import java.util.*;
44
import java.io.*;
45
46
import java.lang.invoke.*;
47
import java.security.*;
48
import static java.lang.invoke.MethodHandles.*;
49
import static java.lang.invoke.MethodType.*;
50
51
public class InvokeDynamicPrintArgs {
52
public static void main(String... av) throws Throwable {
53
if (av.length > 0 && av[0].equals("--check-output")) openBuf();
54
if (av.length > 0 && av[0].equals("--security-manager")) setSM();
55
System.out.println("Printing some argument lists, starting with a empty one:");
56
INDY_nothing().invokeExact(); // BSM specifier #0 = {bsm}
57
INDY_bar().invokeExact("bar arg", 1); // BSM specifier #1 = {bsm2, Void.class, "void type"}
58
INDY_bar2().invokeExact("bar2 arg", 222); // BSM specifier #1 = (same)
59
INDY_baz().invokeExact("baz arg", 2, 3.14); // BSM specifier #2 = {bsm2, 1234.5}
60
INDY_foo().invokeExact("foo arg"); // BSM specifier #0 = (same)
61
// Hence, BSM specifier count should be 3. See "--verify-specifier-count=3" above.
62
System.out.println("Done printing argument lists.");
63
closeBuf();
64
checkConstantRefs();
65
}
66
67
private static void checkConstantRefs() throws Throwable {
68
// check some constant references to its self class
69
assertEquals(MT_bsm(), MH_bsm().type());
70
assertEquals(MT_bsm2(), MH_bsm2().type());
71
assertEquals(MT_bsm(), non_MH_bsm().type());
72
}
73
private static void assertEquals(Object exp, Object act) {
74
if (exp == act || (exp != null && exp.equals(act))) return;
75
throw new AssertionError("not equal: "+exp+", "+act);
76
}
77
78
private static void setSM() {
79
Policy.setPolicy(new TestPolicy());
80
System.setSecurityManager(new SecurityManager());
81
}
82
83
private static PrintStream oldOut;
84
private static ByteArrayOutputStream buf;
85
private static void openBuf() {
86
oldOut = System.out;
87
buf = new ByteArrayOutputStream();
88
System.setOut(new PrintStream(buf));
89
}
90
private static void closeBuf() {
91
if (buf == null) return;
92
System.out.flush();
93
System.setOut(oldOut);
94
String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");
95
for (String line : haveLines) System.out.println(line);
96
Iterator<String> iter = Arrays.asList(haveLines).iterator();
97
for (String want : EXPECT_OUTPUT) {
98
String have = iter.hasNext() ? iter.next() : "[EOF]";
99
if (want.equals(have)) continue;
100
System.err.println("want line: "+want);
101
System.err.println("have line: "+have);
102
throw new AssertionError("unexpected output: "+have);
103
}
104
if (iter.hasNext())
105
throw new AssertionError("unexpected output: "+iter.next());
106
}
107
private static final String[] EXPECT_OUTPUT = {
108
"Printing some argument lists, starting with a empty one:",
109
"[test.java.lang.invoke.InvokeDynamicPrintArgs, nothing, ()void][]",
110
"[test.java.lang.invoke.InvokeDynamicPrintArgs, bar, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]",
111
"[test.java.lang.invoke.InvokeDynamicPrintArgs, bar2, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]",
112
"[test.java.lang.invoke.InvokeDynamicPrintArgs, baz, (String,int,double)void, 1234.5][baz arg, 2, 3.14]",
113
"[test.java.lang.invoke.InvokeDynamicPrintArgs, foo, (String)void][foo arg]",
114
"Done printing argument lists."
115
};
116
117
private static boolean doPrint = true;
118
private static void printArgs(Object bsmInfo, Object... args) {
119
String message = bsmInfo+Arrays.deepToString(args);
120
if (doPrint) System.out.println(message);
121
}
122
private static MethodHandle MH_printArgs() throws ReflectiveOperationException {
123
shouldNotCallThis();
124
return lookup().findStatic(lookup().lookupClass(),
125
"printArgs", methodType(void.class, Object.class, Object[].class));
126
}
127
128
private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
129
// ignore caller and name, but match the type:
130
Object bsmInfo = Arrays.asList(caller, name, type);
131
return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type));
132
}
133
private static MethodType MT_bsm() {
134
shouldNotCallThis();
135
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);
136
}
137
private static MethodHandle MH_bsm() throws ReflectiveOperationException {
138
shouldNotCallThis();
139
return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
140
}
141
private static MethodHandle non_MH_bsm() throws ReflectiveOperationException {
142
return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
143
}
144
145
/* Example of a constant call site with user-data.
146
* In this case, the user data is exactly the BSM data.
147
* Note that a CCS with user data must use the "hooked" constructor
148
* to bind the CCS itself into the resulting target.
149
* A normal constructor would not allow a circular relation
150
* between the CCS and its target.
151
*/
152
public static class PrintingCallSite extends ConstantCallSite {
153
final Lookup caller;
154
final String name;
155
final Object[] staticArgs;
156
157
PrintingCallSite(Lookup caller, String name, MethodType type, Object... staticArgs) throws Throwable {
158
super(type, MH_createTarget());
159
this.caller = caller;
160
this.name = name;
161
this.staticArgs = staticArgs;
162
}
163
164
public MethodHandle createTarget() {
165
try {
166
return lookup().bind(this, "runTarget", genericMethodType(0, true)).asType(type());
167
} catch (ReflectiveOperationException ex) {
168
throw new RuntimeException(ex);
169
}
170
}
171
172
public Object runTarget(Object... dynamicArgs) {
173
List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type()));
174
bsmInfo.addAll(Arrays.asList(staticArgs));
175
printArgs(bsmInfo, dynamicArgs);
176
return null;
177
}
178
179
private static MethodHandle MH_createTarget() throws ReflectiveOperationException {
180
shouldNotCallThis();
181
return lookup().findVirtual(lookup().lookupClass(), "createTarget", methodType(MethodHandle.class));
182
}
183
}
184
private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws Throwable {
185
// ignore caller and name, but match the type:
186
return new PrintingCallSite(caller, name, type, arg);
187
}
188
private static MethodType MT_bsm2() {
189
shouldNotCallThis();
190
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object[].class);
191
}
192
private static MethodHandle MH_bsm2() throws ReflectiveOperationException {
193
shouldNotCallThis();
194
return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2());
195
}
196
197
private static MethodHandle INDY_nothing() throws Throwable {
198
shouldNotCallThis();
199
return ((CallSite) MH_bsm().invoke(lookup(),
200
"nothing", methodType(void.class)
201
)).dynamicInvoker();
202
}
203
private static MethodHandle INDY_foo() throws Throwable {
204
shouldNotCallThis();
205
return ((CallSite) MH_bsm().invoke(lookup(),
206
"foo", methodType(void.class, String.class)
207
)).dynamicInvoker();
208
}
209
private static MethodHandle INDY_bar() throws Throwable {
210
shouldNotCallThis();
211
return ((CallSite) MH_bsm2().invoke(lookup(),
212
"bar", methodType(void.class, String.class, int.class)
213
, Void.class, "void type!", 1, 234.5F, 67.5, (long)89
214
)).dynamicInvoker();
215
}
216
private static MethodHandle INDY_bar2() throws Throwable {
217
shouldNotCallThis();
218
return ((CallSite) MH_bsm2().invoke(lookup(),
219
"bar2", methodType(void.class, String.class, int.class)
220
, Void.class, "void type!", 1, 234.5F, 67.5, (long)89
221
)).dynamicInvoker();
222
}
223
private static MethodHandle INDY_baz() throws Throwable {
224
shouldNotCallThis();
225
return ((CallSite) MH_bsm2().invoke(lookup(),
226
"baz", methodType(void.class, String.class, int.class, double.class)
227
, 1234.5
228
)).dynamicInvoker();
229
}
230
231
private static void shouldNotCallThis() {
232
// if this gets called, the transformation has not taken place
233
if (System.getProperty("InvokeDynamicPrintArgs.allow-untransformed") != null) return;
234
throw new AssertionError("this code should be statically transformed away by Indify");
235
}
236
237
static class TestPolicy extends Policy {
238
static final Policy DEFAULT_POLICY = Policy.getPolicy();
239
240
final PermissionCollection permissions = new Permissions();
241
TestPolicy() {
242
permissions.add(new java.io.FilePermission("<<ALL FILES>>", "read"));
243
}
244
public PermissionCollection getPermissions(ProtectionDomain domain) {
245
return permissions;
246
}
247
248
public PermissionCollection getPermissions(CodeSource codesource) {
249
return permissions;
250
}
251
252
public boolean implies(ProtectionDomain domain, Permission perm) {
253
return permissions.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
254
}
255
}
256
}
257
258