Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/plugins/StringSharingPluginTest.java
41152 views
1
/*
2
* Copyright (c) 2015, 2018, 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
* @summary Test StringSharingPluginTest
27
* @author Jean-Francois Denise
28
* @library ../../lib
29
* @modules java.base/jdk.internal.jimage
30
* java.base/jdk.internal.jimage.decompressor
31
* jdk.jlink/jdk.tools.jlink.internal
32
* jdk.jlink/jdk.tools.jlink.internal.plugins
33
* jdk.jlink/jdk.tools.jlink.plugin
34
* jdk.jlink/jdk.tools.jmod
35
* jdk.jlink/jdk.tools.jimage
36
* jdk.jdeps/com.sun.tools.classfile
37
* jdk.compiler
38
* @run build tests.*
39
* @run main StringSharingPluginTest
40
*/
41
42
import java.io.IOException;
43
import java.io.UncheckedIOException;
44
import java.nio.ByteBuffer;
45
import java.nio.ByteOrder;
46
import java.nio.file.Files;
47
import java.nio.file.Path;
48
import java.util.Arrays;
49
import java.util.HashMap;
50
import java.util.List;
51
import java.util.Map;
52
import java.util.function.Consumer;
53
54
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
55
import jdk.internal.jimage.decompressor.StringSharingDecompressor;
56
import jdk.tools.jlink.internal.ResourcePoolManager;
57
import jdk.tools.jlink.internal.StringTable;
58
import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
59
import jdk.tools.jlink.plugin.ResourcePoolEntry;
60
import jdk.tools.jlink.plugin.ResourcePool;
61
import jdk.tools.jlink.plugin.Plugin;
62
import tests.Helper;
63
import tests.JImageValidator;
64
65
public class StringSharingPluginTest {
66
67
private static int strID = 1;
68
69
public static void main(String[] args) throws Exception {
70
Helper helper = Helper.newHelper();
71
if (helper == null) {
72
// Skip test if the jmods directory is missing (e.g. exploded image)
73
System.err.println("Test not run, NO jmods directory");
74
return;
75
}
76
77
List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
78
Path compiledClasses = helper.generateModuleCompiledClasses(
79
helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes);
80
81
Map<String, Integer> map = new HashMap<>();
82
Map<Integer, String> reversedMap = new HashMap<>();
83
84
ResourcePoolManager resources = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
85
@Override
86
public int addString(String str) {
87
Integer id = map.get(str);
88
if (id == null) {
89
id = strID;
90
map.put(str, id);
91
reversedMap.put(id, str);
92
strID += 1;
93
}
94
return id;
95
}
96
97
@Override
98
public String getString(int id) {
99
throw new UnsupportedOperationException("Not supported yet.");
100
}
101
});
102
Consumer<Path> c = (p) -> {
103
// take only the .class resources.
104
if (Files.isRegularFile(p) && p.toString().endsWith(".class")
105
&& !p.toString().endsWith("module-info.class")) {
106
try {
107
byte[] content = Files.readAllBytes(p);
108
String path = p.toString().replace('\\', '/');
109
path = path.substring("/modules".length());
110
if (path.charAt(0) != '/') {
111
path = "/" + path;
112
}
113
ResourcePoolEntry res = ResourcePoolEntry.create(path, content);
114
resources.add(res);
115
} catch (Exception ex) {
116
throw new RuntimeException(ex);
117
}
118
}
119
};
120
try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) {
121
stream.forEach(c);
122
}
123
Plugin plugin = new StringSharingPlugin();
124
ResourcePoolManager resultMgr = new ResourcePoolManager(resources.byteOrder(), resources.getStringTable());
125
ResourcePool result = plugin.transform(resources.resourcePool(), resultMgr.resourcePoolBuilder());
126
127
if (result.isEmpty()) {
128
throw new AssertionError("No result");
129
}
130
131
result.entries().forEach(res -> {
132
if (res.path().endsWith(".class")) {
133
try {
134
byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.contentBytes(),
135
CompressedResourceHeader.getSize());
136
JImageValidator.readClass(uncompacted);
137
} catch (IOException exp) {
138
throw new UncheckedIOException(exp);
139
}
140
}
141
});
142
}
143
}
144
145