Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/tools/jdeps/jdkinternals/RemovedJDKInternals.java
41152 views
1
/*
2
* Copyright (c) 2016, 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
/*
25
* @test
26
* @bug 8153042
27
* @summary Tests JDK internal APIs that have been removed.
28
* @library ../lib
29
* @build CompilerUtils JdepsRunner JdepsUtil ModuleMetaData
30
* @modules jdk.jdeps/com.sun.tools.jdeps
31
* @run testng RemovedJDKInternals
32
*/
33
34
import java.io.IOException;
35
import java.nio.file.Path;
36
import java.nio.file.Paths;
37
import java.util.Arrays;
38
import java.util.LinkedHashMap;
39
import java.util.List;
40
import java.util.Map;
41
import java.util.regex.Matcher;
42
import java.util.regex.Pattern;
43
44
import com.sun.tools.jdeps.DepsAnalyzer;
45
import com.sun.tools.jdeps.Graph;
46
import org.testng.annotations.BeforeTest;
47
import org.testng.annotations.DataProvider;
48
import org.testng.annotations.Test;
49
50
import static org.testng.Assert.assertEquals;
51
import static org.testng.Assert.assertFalse;
52
import static org.testng.Assert.assertTrue;
53
54
public class RemovedJDKInternals {
55
private static final String TEST_SRC = System.getProperty("test.src");
56
57
private static final Path CLASSES_DIR = Paths.get("classes");
58
private static final Path PATCHES_DIR = Paths.get("patches");
59
60
private static final String JDK_UNSUPPORTED = "jdk.unsupported";
61
/**
62
* Compiles classes used by the test
63
*/
64
@BeforeTest
65
public void compileAll() throws Exception {
66
CompilerUtils.cleanDir(PATCHES_DIR);
67
CompilerUtils.cleanDir(CLASSES_DIR);
68
69
// compile sun.misc types
70
Path sunMiscSrc = Paths.get(TEST_SRC, "patches", JDK_UNSUPPORTED);
71
Path patchDir = PATCHES_DIR.resolve(JDK_UNSUPPORTED);
72
assertTrue(CompilerUtils.compile(sunMiscSrc, patchDir,
73
"--patch-module", JDK_UNSUPPORTED + "=" + sunMiscSrc.toString()));
74
75
// compile com.sun.image.codec.jpeg types
76
Path codecSrc = Paths.get(TEST_SRC, "patches", "java.desktop");
77
Path codecDest = PATCHES_DIR;
78
assertTrue(CompilerUtils.compile(codecSrc, codecDest));
79
80
// patch jdk.unsupported and set -cp to codec types
81
assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "p"),
82
CLASSES_DIR,
83
"--patch-module", "jdk.unsupported=" + patchDir,
84
"-cp", codecDest.toString()));
85
}
86
87
@DataProvider(name = "deps")
88
public Object[][] deps() {
89
return new Object[][] {
90
{ "classes", new ModuleMetaData("classes", false)
91
.reference("p.Main", "java.lang.Class", "java.base")
92
.reference("p.Main", "java.lang.Object", "java.base")
93
.reference("p.Main", "java.util.Iterator", "java.base")
94
.reference("p.S", "java.lang.Object", "java.base")
95
.jdkInternal("p.Main", "sun.reflect.ReflectionFactory", "jdk.unsupported")
96
.removedJdkInternal("p.Main", "sun.reflect.Reflection")
97
.removedJdkInternal("p.Main", "com.sun.image.codec.jpeg.JPEGCodec")
98
.removedJdkInternal("p.Main", "sun.misc.Service")
99
.removedJdkInternal("p.Main", "sun.misc.SoftCache")
100
},
101
};
102
}
103
104
@Test(dataProvider = "deps")
105
public void runTest(String name, ModuleMetaData data) throws Exception {
106
String cmd = String.format("jdeps -verbose:class %s%n", CLASSES_DIR);
107
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
108
jdeps.verbose("-verbose:class")
109
.addRoot(CLASSES_DIR);
110
111
DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();
112
assertTrue(analyzer.run());
113
jdeps.dumpOutput(System.err);
114
115
Graph<DepsAnalyzer.Node> g = analyzer.dependenceGraph();
116
// there are two node with p.Main as origin
117
// one for exported API and one for removed JDK internal
118
g.nodes().stream()
119
.filter(u -> u.source.equals(data.moduleName))
120
.forEach(u -> g.adjacentNodes(u).stream()
121
.forEach(v -> data.checkDependence(u.name, v.name, v.source, v.info)));
122
}
123
}
124
125
private static final List<String> REMOVED_APIS = List.of(
126
"com.sun.image.codec.jpeg.JPEGCodec",
127
"sun.misc.Service",
128
"sun.misc.SoftCache",
129
"sun.reflect.Reflection"
130
);
131
private static final String REMOVED_INTERNAL_API = "JDK removed internal API";
132
133
134
@Test
135
public void removedInternalJDKs() throws IOException {
136
// verify the JDK removed internal API
137
JdepsRunner summary = JdepsRunner.run("-summary", CLASSES_DIR.toString());
138
Arrays.stream(summary.output()).map(l -> l.split(" -> "))
139
.map(a -> a[1]).filter(n -> n.equals(REMOVED_INTERNAL_API))
140
.findFirst().orElseThrow();
141
142
JdepsRunner jdeps = JdepsRunner.run("-verbose:class", CLASSES_DIR.toString());
143
String output = jdeps.stdout.toString();
144
Map<String, String> result = findDeps(output);
145
for (String cn : result.keySet()) {
146
String name = result.get(cn);
147
if (REMOVED_APIS.contains(cn)) {
148
assertEquals(name, REMOVED_INTERNAL_API);
149
} else if (cn.startsWith("sun.reflect")){
150
assertEquals(name, "JDK internal API (jdk.unsupported)");
151
} else {
152
assertEquals(name, "java.base");
153
}
154
}
155
REMOVED_APIS.stream().map(result::containsKey).allMatch(b -> b);
156
}
157
158
// Pattern used to parse lines
159
private static final Pattern linePattern = Pattern.compile(".*\r?\n");
160
private static final Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
161
162
// Use the linePattern to break the given String into lines, applying
163
// the pattern to each line to see if we have a match
164
private static Map<String, String> findDeps(String out) {
165
Map<String, String> result = new LinkedHashMap<>();
166
Matcher lm = linePattern.matcher(out); // Line matcher
167
Matcher pm = null; // Pattern matcher
168
int lines = 0;
169
while (lm.find()) {
170
lines++;
171
CharSequence cs = lm.group(); // The current line
172
if (pm == null)
173
pm = pattern.matcher(cs);
174
else
175
pm.reset(cs);
176
if (pm.find())
177
result.put(pm.group(1), pm.group(2).trim());
178
if (lm.end() == out.length())
179
break;
180
}
181
return result;
182
}
183
184
private static final Map<String, String> REPLACEMENTS = Map.of(
185
"com.sun.image.codec.jpeg.JPEGCodec", "Use javax.imageio @since 1.4",
186
"sun.misc.Service", "Use java.util.ServiceLoader @since 1.6",
187
"sun.misc.SoftCache", "Removed. See http://openjdk.java.net/jeps/260",
188
"sun.reflect.Reflection", "Use java.lang.StackWalker @since 9",
189
"sun.reflect.ReflectionFactory", "See http://openjdk.java.net/jeps/260"
190
);
191
192
@Test
193
public void checkReplacement() {
194
JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.toString());
195
String[] output = jdeps.output();
196
int i = 0;
197
while (!output[i].contains("Suggested Replacement")) {
198
i++;
199
}
200
201
// must match the number of JDK internal APIs
202
int count = output.length-i-2;
203
assertEquals(count, REPLACEMENTS.size());
204
205
for (int j=i+2; j < output.length; j++) {
206
String line = output[j];
207
int pos = line.indexOf("Use ");
208
if (pos < 0)
209
pos = line.indexOf("Removed. ");
210
if (pos < 0)
211
pos = line.indexOf("See ");
212
213
assertTrue(pos > 0);
214
String name = line.substring(0, pos).trim();
215
String repl = line.substring(pos, line.length()).trim();
216
assertEquals(REPLACEMENTS.get(name), repl);
217
}
218
}
219
}
220
221