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/ModuleInfoExtender.java
41159 views
1
/*
2
* Copyright (c) 2014, 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
26
package jdk.internal.module;
27
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31
import java.lang.module.ModuleDescriptor.Version;
32
import java.util.ArrayList;
33
import java.util.Collections;
34
import java.util.List;
35
import java.util.Set;
36
37
import jdk.internal.org.objectweb.asm.Attribute;
38
import jdk.internal.org.objectweb.asm.ClassReader;
39
import jdk.internal.org.objectweb.asm.ClassVisitor;
40
import jdk.internal.org.objectweb.asm.ClassWriter;
41
import jdk.internal.org.objectweb.asm.ModuleVisitor;
42
import jdk.internal.org.objectweb.asm.Opcodes;
43
import jdk.internal.org.objectweb.asm.commons.ModuleHashesAttribute;
44
import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
45
import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
46
47
/**
48
* Utility class to extend a module-info.class with additional attributes.
49
*/
50
51
public final class ModuleInfoExtender {
52
53
// the input stream to read the original module-info.class
54
private final InputStream in;
55
56
// the packages in the ModulePackages attribute
57
private Set<String> packages;
58
59
// the value for the module version in the Module attribute
60
private Version version;
61
62
// the value of the ModuleMainClass attribute
63
private String mainClass;
64
65
// the value for the ModuleTarget attribute
66
private String targetPlatform;
67
68
// the hashes for the ModuleHashes attribute
69
private ModuleHashes hashes;
70
71
// the value of the ModuleResolution attribute
72
private ModuleResolution moduleResolution;
73
74
private ModuleInfoExtender(InputStream in) {
75
this.in = in;
76
}
77
78
/**
79
* Sets the packages for the ModulePackages attribute
80
*
81
* @apiNote This method does not check that the package names are legal
82
* package names or that the set of packages is a super set of the
83
* packages in the module.
84
*/
85
public ModuleInfoExtender packages(Set<String> packages) {
86
this.packages = Collections.unmodifiableSet(packages);
87
return this;
88
}
89
90
/**
91
* Sets the value for the module version in the Module attribute
92
*/
93
public ModuleInfoExtender version(Version version) {
94
this.version = version;
95
return this;
96
}
97
98
/**
99
* Sets the value of the ModuleMainClass attribute.
100
*
101
* @apiNote This method does not check that the main class is a legal
102
* class name in a named package.
103
*/
104
public ModuleInfoExtender mainClass(String mainClass) {
105
this.mainClass = mainClass;
106
return this;
107
}
108
109
/**
110
* Sets the value for the ModuleTarget attribute.
111
*/
112
public ModuleInfoExtender targetPlatform(String targetPlatform) {
113
this.targetPlatform = targetPlatform;
114
return this;
115
}
116
117
/**
118
* The ModuleHashes attribute will be emitted to the module-info with
119
* the hashes encapsulated in the given {@code ModuleHashes}
120
* object.
121
*/
122
public ModuleInfoExtender hashes(ModuleHashes hashes) {
123
this.hashes = hashes;
124
return this;
125
}
126
127
/**
128
* Sets the value for the ModuleResolution attribute.
129
*/
130
public ModuleInfoExtender moduleResolution(ModuleResolution mres) {
131
this.moduleResolution = mres;
132
return this;
133
}
134
135
/**
136
* Outputs the modified module-info.class to the given output stream.
137
* Once this method has been called then the Extender object should
138
* be discarded.
139
*/
140
public void write(OutputStream out) throws IOException {
141
// emit to the output stream
142
out.write(toByteArray());
143
}
144
145
/**
146
* Returns the bytes of the modified module-info.class.
147
* Once this method has been called then the Extender object should
148
* be discarded.
149
*/
150
public byte[] toByteArray() throws IOException {
151
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
152
+ ClassWriter.COMPUTE_FRAMES);
153
154
ClassReader cr = new ClassReader(in);
155
156
ClassVisitor cv = new ClassVisitor(Opcodes.ASM7, cw) {
157
@Override
158
public ModuleVisitor visitModule(String name, int flags, String version) {
159
Version v = ModuleInfoExtender.this.version;
160
String vs = (v != null) ? v.toString() : version;
161
ModuleVisitor mv = super.visitModule(name, flags, vs);
162
163
// ModuleMainClass attribute
164
if (mainClass != null) {
165
mv.visitMainClass(mainClass.replace('.', '/'));
166
}
167
168
// ModulePackages attribute
169
if (packages != null) {
170
packages.stream()
171
.sorted()
172
.forEach(pn -> mv.visitPackage(pn.replace('.', '/')));
173
}
174
175
return new ModuleVisitor(Opcodes.ASM7, mv) {
176
public void visitMainClass(String existingMainClass) {
177
// skip main class if there is a new value
178
if (mainClass == null) {
179
super.visitMainClass(existingMainClass);
180
}
181
}
182
public void visitPackage(String existingPackage) {
183
// skip packages if there is a new set of packages
184
if (packages == null) {
185
super.visitPackage(existingPackage);
186
}
187
}
188
};
189
}
190
@Override
191
public void visitAttribute(Attribute attr) {
192
String name = attr.type;
193
// drop existing attributes if there are replacements
194
if (name.equals(ClassFileConstants.MODULE_TARGET)
195
&& targetPlatform != null)
196
return;
197
if (name.equals(ClassFileConstants.MODULE_RESOLUTION)
198
&& moduleResolution != null)
199
return;
200
if (name.equals(ClassFileConstants.MODULE_HASHES)
201
&& hashes != null)
202
return;
203
204
super.visitAttribute(attr);
205
206
}
207
};
208
209
List<Attribute> attrs = new ArrayList<>();
210
attrs.add(new ModuleTargetAttribute());
211
attrs.add(new ModuleResolutionAttribute());
212
attrs.add(new ModuleHashesAttribute());
213
cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
214
215
// add ModuleTarget, ModuleResolution and ModuleHashes attributes
216
if (targetPlatform != null) {
217
cw.visitAttribute(new ModuleTargetAttribute(targetPlatform));
218
}
219
if (moduleResolution != null) {
220
int flags = moduleResolution.value();
221
cw.visitAttribute(new ModuleResolutionAttribute(flags));
222
}
223
if (hashes != null) {
224
String algorithm = hashes.algorithm();
225
List<String> names = new ArrayList<>();
226
List<byte[]> values = new ArrayList<>();
227
for (String name : hashes.names()) {
228
names.add(name);
229
values.add(hashes.hashFor(name));
230
}
231
cw.visitAttribute(new ModuleHashesAttribute(algorithm, names, values));
232
}
233
234
return cw.toByteArray();
235
}
236
237
/**
238
* Returns an {@code Extender} that may be used to add additional
239
* attributes to the module-info.class read from the given input
240
* stream.
241
*/
242
public static ModuleInfoExtender newExtender(InputStream in) {
243
return new ModuleInfoExtender(in);
244
}
245
246
}
247
248