Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/module/ModuleInfoWriter.java
41159 views
1
/*
2
* Copyright (c) 2015, 2020, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package jdk.internal.module;
26
27
import java.io.IOException;
28
import java.io.OutputStream;
29
import java.lang.module.ModuleDescriptor;
30
import java.nio.ByteBuffer;
31
import java.util.Map;
32
import java.util.stream.Stream;
33
34
import jdk.internal.org.objectweb.asm.ClassWriter;
35
import jdk.internal.org.objectweb.asm.ModuleVisitor;
36
import jdk.internal.org.objectweb.asm.Opcodes;
37
import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
38
import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
39
import static jdk.internal.org.objectweb.asm.Opcodes.*;
40
41
/**
42
* Utility class to write a ModuleDescriptor as a module-info.class.
43
*/
44
45
public final class ModuleInfoWriter {
46
47
private static final Map<ModuleDescriptor.Modifier, Integer>
48
MODULE_MODS_TO_FLAGS = Map.of(
49
ModuleDescriptor.Modifier.OPEN, ACC_OPEN,
50
ModuleDescriptor.Modifier.SYNTHETIC, ACC_SYNTHETIC,
51
ModuleDescriptor.Modifier.MANDATED, ACC_MANDATED
52
);
53
54
private static final Map<ModuleDescriptor.Requires.Modifier, Integer>
55
REQUIRES_MODS_TO_FLAGS = Map.of(
56
ModuleDescriptor.Requires.Modifier.TRANSITIVE, ACC_TRANSITIVE,
57
ModuleDescriptor.Requires.Modifier.STATIC, ACC_STATIC_PHASE,
58
ModuleDescriptor.Requires.Modifier.SYNTHETIC, ACC_SYNTHETIC,
59
ModuleDescriptor.Requires.Modifier.MANDATED, ACC_MANDATED
60
);
61
62
private static final Map<ModuleDescriptor.Exports.Modifier, Integer>
63
EXPORTS_MODS_TO_FLAGS = Map.of(
64
ModuleDescriptor.Exports.Modifier.SYNTHETIC, ACC_SYNTHETIC,
65
ModuleDescriptor.Exports.Modifier.MANDATED, ACC_MANDATED
66
);
67
68
private static final Map<ModuleDescriptor.Opens.Modifier, Integer>
69
OPENS_MODS_TO_FLAGS = Map.of(
70
ModuleDescriptor.Opens.Modifier.SYNTHETIC, ACC_SYNTHETIC,
71
ModuleDescriptor.Opens.Modifier.MANDATED, ACC_MANDATED
72
);
73
74
private static final String[] EMPTY_STRING_ARRAY = new String[0];
75
76
private ModuleInfoWriter() { }
77
78
/**
79
* Writes the given module descriptor to a module-info.class file,
80
* returning it in a byte array.
81
*/
82
private static byte[] toModuleInfo(ModuleDescriptor md,
83
ModuleResolution mres,
84
ModuleTarget target) {
85
ClassWriter cw = new ClassWriter(0);
86
cw.visit(Opcodes.V10, ACC_MODULE, "module-info", null, null, null);
87
88
int moduleFlags = md.modifiers().stream()
89
.map(MODULE_MODS_TO_FLAGS::get)
90
.reduce(0, (x, y) -> (x | y));
91
String vs = md.rawVersion().orElse(null);
92
ModuleVisitor mv = cw.visitModule(md.name(), moduleFlags, vs);
93
94
// requires
95
for (ModuleDescriptor.Requires r : md.requires()) {
96
int flags = r.modifiers().stream()
97
.map(REQUIRES_MODS_TO_FLAGS::get)
98
.reduce(0, (x, y) -> (x | y));
99
vs = r.rawCompiledVersion().orElse(null);
100
mv.visitRequire(r.name(), flags, vs);
101
}
102
103
// exports
104
for (ModuleDescriptor.Exports e : md.exports()) {
105
int flags = e.modifiers().stream()
106
.map(EXPORTS_MODS_TO_FLAGS::get)
107
.reduce(0, (x, y) -> (x | y));
108
String[] targets = e.targets().toArray(EMPTY_STRING_ARRAY);
109
mv.visitExport(e.source().replace('.', '/'), flags, targets);
110
}
111
112
// opens
113
for (ModuleDescriptor.Opens opens : md.opens()) {
114
int flags = opens.modifiers().stream()
115
.map(OPENS_MODS_TO_FLAGS::get)
116
.reduce(0, (x, y) -> (x | y));
117
String[] targets = opens.targets().toArray(EMPTY_STRING_ARRAY);
118
mv.visitOpen(opens.source().replace('.', '/'), flags, targets);
119
}
120
121
// uses
122
md.uses().stream().map(sn -> sn.replace('.', '/')).forEach(mv::visitUse);
123
124
// provides
125
for (ModuleDescriptor.Provides p : md.provides()) {
126
mv.visitProvide(p.service().replace('.', '/'),
127
p.providers()
128
.stream()
129
.map(pn -> pn.replace('.', '/'))
130
.toArray(String[]::new));
131
}
132
133
// add the ModulePackages attribute when there are packages that aren't
134
// exported or open
135
Stream<String> exported = md.exports().stream()
136
.map(ModuleDescriptor.Exports::source);
137
Stream<String> open = md.opens().stream()
138
.map(ModuleDescriptor.Opens::source);
139
long exportedOrOpen = Stream.concat(exported, open).distinct().count();
140
if (md.packages().size() > exportedOrOpen) {
141
md.packages().stream()
142
.map(pn -> pn.replace('.', '/'))
143
.forEach(mv::visitPackage);
144
}
145
146
// ModuleMainClass attribute
147
md.mainClass()
148
.map(mc -> mc.replace('.', '/'))
149
.ifPresent(mv::visitMainClass);
150
151
mv.visitEnd();
152
153
// write ModuleResolution attribute if specified
154
if (mres != null) {
155
cw.visitAttribute(new ModuleResolutionAttribute(mres.value()));
156
}
157
158
// write ModuleTarget attribute if there is a target platform
159
if (target != null && target.targetPlatform().length() > 0) {
160
cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform()));
161
}
162
163
cw.visitEnd();
164
return cw.toByteArray();
165
}
166
167
/**
168
* Writes a module descriptor to the given output stream as a
169
* module-info.class.
170
*/
171
public static void write(ModuleDescriptor descriptor,
172
ModuleResolution mres,
173
ModuleTarget target,
174
OutputStream out)
175
throws IOException
176
{
177
byte[] bytes = toModuleInfo(descriptor, mres, target);
178
out.write(bytes);
179
}
180
181
/**
182
* Writes a module descriptor to the given output stream as a
183
* module-info.class.
184
*/
185
public static void write(ModuleDescriptor descriptor,
186
ModuleResolution mres,
187
OutputStream out)
188
throws IOException
189
{
190
write(descriptor, mres, null, out);
191
}
192
193
/**
194
* Writes a module descriptor to the given output stream as a
195
* module-info.class.
196
*/
197
public static void write(ModuleDescriptor descriptor,
198
ModuleTarget target,
199
OutputStream out)
200
throws IOException
201
{
202
write(descriptor, null, target, out);
203
}
204
205
/**
206
* Writes a module descriptor to the given output stream as a
207
* module-info.class.
208
*/
209
public static void write(ModuleDescriptor descriptor, OutputStream out)
210
throws IOException
211
{
212
write(descriptor, null, null, out);
213
}
214
215
/**
216
* Returns a byte array containing the given module descriptor in
217
* module-info.class format.
218
*/
219
public static byte[] toBytes(ModuleDescriptor descriptor) {
220
return toModuleInfo(descriptor, null, null);
221
}
222
223
/**
224
* Returns a {@code ByteBuffer} containing the given module descriptor
225
* in module-info.class format.
226
*/
227
public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
228
byte[] bytes = toModuleInfo(descriptor, null, null);
229
return ByteBuffer.wrap(bytes);
230
}
231
}
232
233