Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/invoke/MethodHandleConstants.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 8022066
26
* @summary smoke test for method handle constants
27
* @build indify.Indify
28
* @compile MethodHandleConstants.java
29
* @run main/othervm
30
* indify.Indify
31
* --verify-specifier-count=4
32
* --expand-properties --classpath ${test.classes}
33
* --java test.java.lang.invoke.MethodHandleConstants --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.MethodHandleConstants --security-manager
39
*/
40
41
package test.java.lang.invoke;
42
43
import java.util.*;
44
import java.io.*;
45
import java.lang.invoke.*;
46
import java.security.*;
47
48
import static java.lang.invoke.MethodHandles.*;
49
import static java.lang.invoke.MethodType.*;
50
51
public class MethodHandleConstants {
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("Obtaining method handle constants:");
56
testCase(MH_String_replace_C2(), String.class, "replace", String.class, String.class, char.class, char.class);
57
testCase(MH_MethodHandle_invokeExact_SC2(), MethodHandle.class, "invokeExact", String.class, MethodHandle.class, String.class, char.class, char.class);
58
testCase(MH_MethodHandle_invoke_SC2(), MethodHandle.class, "invoke", String.class, MethodHandle.class, String.class, char.class, char.class);
59
testCase(MH_Class_forName_S(), Class.class, "forName", Class.class, String.class);
60
testCase(MH_Class_forName_SbCL(), Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class);
61
System.out.println("Done.");
62
closeBuf();
63
}
64
65
private static void testCase(MethodHandle mh, Class<?> defc, String name, Class<?> rtype, Class<?>... ptypes) throws Throwable {
66
System.out.println(mh);
67
// we include defc, because we assume it is a non-static MH:
68
MethodType mt = methodType(rtype, ptypes);
69
assertEquals(mh.type(), mt);
70
// FIXME: Use revealDirect to find out more
71
}
72
private static void assertEquals(Object exp, Object act) {
73
if (exp == act || (exp != null && exp.equals(act))) return;
74
throw new AssertionError("not equal: "+exp+", "+act);
75
}
76
77
private static void setSM() {
78
Policy.setPolicy(new TestPolicy());
79
System.setSecurityManager(new SecurityManager());
80
}
81
82
private static PrintStream oldOut;
83
private static ByteArrayOutputStream buf;
84
private static void openBuf() {
85
oldOut = System.out;
86
buf = new ByteArrayOutputStream();
87
System.setOut(new PrintStream(buf));
88
}
89
private static void closeBuf() {
90
if (buf == null) return;
91
System.out.flush();
92
System.setOut(oldOut);
93
String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");
94
for (String line : haveLines) System.out.println(line);
95
Iterator<String> iter = Arrays.asList(haveLines).iterator();
96
for (String want : EXPECT_OUTPUT) {
97
String have = iter.hasNext() ? iter.next() : "[EOF]";
98
if (want.equals(have)) continue;
99
System.err.println("want line: "+want);
100
System.err.println("have line: "+have);
101
throw new AssertionError("unexpected output: "+have);
102
}
103
if (iter.hasNext())
104
throw new AssertionError("unexpected output: "+iter.next());
105
}
106
private static final String[] EXPECT_OUTPUT = {
107
"Obtaining method handle constants:",
108
"MethodHandle(String,char,char)String",
109
"MethodHandle(MethodHandle,String,char,char)String",
110
"MethodHandle(MethodHandle,String,char,char)String",
111
"MethodHandle(String)Class",
112
"MethodHandle(String,boolean,ClassLoader)Class",
113
"Done."
114
};
115
116
// String.replace(String, char, char)
117
private static MethodType MT_String_replace_C2() {
118
shouldNotCallThis();
119
return methodType(String.class, char.class, char.class);
120
}
121
private static MethodHandle MH_String_replace_C2() throws ReflectiveOperationException {
122
shouldNotCallThis();
123
return lookup().findVirtual(String.class, "replace", MT_String_replace_C2());
124
}
125
126
// MethodHandle.invokeExact(...)
127
private static MethodType MT_MethodHandle_invokeExact_SC2() {
128
shouldNotCallThis();
129
return methodType(String.class, String.class, char.class, char.class);
130
}
131
private static MethodHandle MH_MethodHandle_invokeExact_SC2() throws ReflectiveOperationException {
132
shouldNotCallThis();
133
return lookup().findVirtual(MethodHandle.class, "invokeExact", MT_MethodHandle_invokeExact_SC2());
134
}
135
136
// MethodHandle.invoke(...)
137
private static MethodType MT_MethodHandle_invoke_SC2() {
138
shouldNotCallThis();
139
return methodType(String.class, String.class, char.class, char.class);
140
}
141
private static MethodHandle MH_MethodHandle_invoke_SC2() throws ReflectiveOperationException {
142
shouldNotCallThis();
143
return lookup().findVirtual(MethodHandle.class, "invoke", MT_MethodHandle_invoke_SC2());
144
}
145
146
// Class.forName(String)
147
private static MethodType MT_Class_forName_S() {
148
shouldNotCallThis();
149
return methodType(Class.class, String.class);
150
}
151
private static MethodHandle MH_Class_forName_S() throws ReflectiveOperationException {
152
shouldNotCallThis();
153
return lookup().findStatic(Class.class, "forName", MT_Class_forName_S());
154
}
155
156
// Class.forName(String, boolean, ClassLoader)
157
private static MethodType MT_Class_forName_SbCL() {
158
shouldNotCallThis();
159
return methodType(Class.class, String.class, boolean.class, ClassLoader.class);
160
}
161
private static MethodHandle MH_Class_forName_SbCL() throws ReflectiveOperationException {
162
shouldNotCallThis();
163
return lookup().findStatic(Class.class, "forName", MT_Class_forName_SbCL());
164
}
165
166
private static void shouldNotCallThis() {
167
// if this gets called, the transformation has not taken place
168
if (System.getProperty("MethodHandleConstants.allow-untransformed") != null) return;
169
throw new AssertionError("this code should be statically transformed away by Indify");
170
}
171
172
static class TestPolicy extends Policy {
173
static final Policy DEFAULT_POLICY = Policy.getPolicy();
174
175
final PermissionCollection permissions = new Permissions();
176
TestPolicy() {
177
permissions.add(new java.io.FilePermission("<<ALL FILES>>", "read"));
178
}
179
public PermissionCollection getPermissions(ProtectionDomain domain) {
180
return permissions;
181
}
182
183
public PermissionCollection getPermissions(CodeSource codesource) {
184
return permissions;
185
}
186
187
public boolean implies(ProtectionDomain domain, Permission perm) {
188
return permissions.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
189
}
190
}
191
}
192
193