Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/loader/BootLoader.java
41159 views
1
/*
2
* Copyright (c) 2015, 2021, 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.loader;
26
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.lang.module.ModuleReference;
30
import java.net.MalformedURLException;
31
import java.net.URI;
32
import java.net.URL;
33
import java.nio.file.Files;
34
import java.nio.file.Path;
35
import java.security.AccessController;
36
import java.security.PrivilegedAction;
37
import java.util.Arrays;
38
import java.util.Enumeration;
39
import java.util.concurrent.ConcurrentHashMap;
40
import java.util.jar.JarInputStream;
41
import java.util.jar.Manifest;
42
import java.util.stream.Stream;
43
44
import jdk.internal.access.JavaLangAccess;
45
import jdk.internal.access.SharedSecrets;
46
import jdk.internal.module.Modules;
47
import jdk.internal.module.ServicesCatalog;
48
import jdk.internal.util.StaticProperty;
49
50
/**
51
* Find resources and packages in modules defined to the boot class loader or
52
* resources and packages on the "boot class path" specified via -Xbootclasspath/a.
53
*/
54
55
public class BootLoader {
56
private BootLoader() { }
57
58
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
59
60
// The unnamed module for the boot loader
61
private static final Module UNNAMED_MODULE;
62
private static final String JAVA_HOME = StaticProperty.javaHome();
63
64
static {
65
JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
66
UNNAMED_MODULE = jla.defineUnnamedModule(null);
67
jla.addEnableNativeAccess(UNNAMED_MODULE);
68
setBootLoaderUnnamedModule0(UNNAMED_MODULE);
69
}
70
71
// ClassLoaderValue map for the boot class loader
72
private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP
73
= new ConcurrentHashMap<>();
74
75
// native libraries loaded by the boot class loader
76
private static final NativeLibraries NATIVE_LIBS
77
= NativeLibraries.jniNativeLibraries(null);
78
79
/**
80
* Returns the unnamed module for the boot loader.
81
*/
82
public static Module getUnnamedModule() {
83
return UNNAMED_MODULE;
84
}
85
86
/**
87
* Returns the ServiceCatalog for modules defined to the boot class loader.
88
*/
89
public static ServicesCatalog getServicesCatalog() {
90
return ServicesCatalog.getServicesCatalog(ClassLoaders.bootLoader());
91
}
92
93
/**
94
* Returns the ClassLoaderValue map for the boot class loader.
95
*/
96
public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() {
97
return CLASS_LOADER_VALUE_MAP;
98
}
99
100
/**
101
* Returns NativeLibraries for the boot class loader.
102
*/
103
public static NativeLibraries getNativeLibraries() {
104
return NATIVE_LIBS;
105
}
106
107
/**
108
* Returns {@code true} if there is a class path associated with the
109
* BootLoader.
110
*/
111
public static boolean hasClassPath() {
112
return ClassLoaders.bootLoader().hasClassPath();
113
}
114
115
/**
116
* Registers a module with this class loader so that its classes
117
* (and resources) become visible via this class loader.
118
*/
119
public static void loadModule(ModuleReference mref) {
120
ClassLoaders.bootLoader().loadModule(mref);
121
}
122
123
/**
124
* Loads the Class object with the given name defined to the boot loader.
125
*/
126
public static Class<?> loadClassOrNull(String name) {
127
return JLA.findBootstrapClassOrNull(name);
128
}
129
130
/**
131
* Loads the Class object with the given name in the given module
132
* defined to the boot loader. Returns {@code null} if not found.
133
*/
134
public static Class<?> loadClass(Module module, String name) {
135
Class<?> c = loadClassOrNull(name);
136
if (c != null && c.getModule() == module) {
137
return c;
138
} else {
139
return null;
140
}
141
}
142
143
/**
144
* Loads a native library from the system library path.
145
*/
146
@SuppressWarnings("removal")
147
public static void loadLibrary(String name) {
148
if (System.getSecurityManager() == null) {
149
BootLoader.getNativeLibraries().loadLibrary(name);
150
} else {
151
AccessController.doPrivileged(new java.security.PrivilegedAction<>() {
152
public Void run() {
153
BootLoader.getNativeLibraries().loadLibrary(name);
154
return null;
155
}
156
});
157
}
158
}
159
160
/**
161
* Returns a URL to a resource in a module defined to the boot loader.
162
*/
163
public static URL findResource(String mn, String name) throws IOException {
164
return ClassLoaders.bootLoader().findResource(mn, name);
165
}
166
167
/**
168
* Returns an input stream to a resource in a module defined to the
169
* boot loader.
170
*/
171
public static InputStream findResourceAsStream(String mn, String name)
172
throws IOException
173
{
174
return ClassLoaders.bootLoader().findResourceAsStream(mn, name);
175
}
176
177
/**
178
* Returns the URL to the given resource in any of the modules
179
* defined to the boot loader and the boot class path.
180
*/
181
public static URL findResource(String name) {
182
return ClassLoaders.bootLoader().findResource(name);
183
}
184
185
/**
186
* Returns an Iterator to iterate over the resources of the given name
187
* in any of the modules defined to the boot loader.
188
*/
189
public static Enumeration<URL> findResources(String name) throws IOException {
190
return ClassLoaders.bootLoader().findResources(name);
191
}
192
193
/**
194
* Define a package for the given class to the boot loader, if not already
195
* defined.
196
*/
197
public static Package definePackage(Class<?> c) {
198
return getDefinedPackage(c.getPackageName());
199
}
200
201
/**
202
* Returns the Package of the given name defined to the boot loader or null
203
* if the package has not been defined.
204
*/
205
public static Package getDefinedPackage(String pn) {
206
Package pkg = ClassLoaders.bootLoader().getDefinedPackage(pn);
207
if (pkg == null) {
208
String location = getSystemPackageLocation(pn.replace('.', '/'));
209
if (location != null) {
210
pkg = PackageHelper.definePackage(pn.intern(), location);
211
}
212
}
213
return pkg;
214
}
215
216
/**
217
* Returns a stream of the packages defined to the boot loader.
218
*/
219
public static Stream<Package> packages() {
220
return Arrays.stream(getSystemPackageNames())
221
.map(name -> getDefinedPackage(name.replace('/', '.')));
222
}
223
224
/**
225
* Helper class to define {@code Package} objects for packages in modules
226
* defined to the boot loader.
227
*/
228
static class PackageHelper {
229
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
230
231
/**
232
* Define the {@code Package} with the given name. The specified
233
* location is a jrt URL to a named module in the run-time image,
234
* a file URL to a module in an exploded run-time image, or a file
235
* path to an entry on the boot class path (java agent Boot-Class-Path
236
* or -Xbootclasspath/a.
237
*
238
* <p> If the given location is a JAR file containing a manifest,
239
* the defined Package contains the versioning information from
240
* the manifest, if present.
241
*
242
* @param name package name
243
* @param location location where the package is (jrt URL or file URL
244
* for a named module in the run-time or exploded image;
245
* a file path for a package from -Xbootclasspath/a)
246
*/
247
static Package definePackage(String name, String location) {
248
Module module = findModule(location);
249
if (module != null) {
250
// named module from runtime image or exploded module
251
if (name.isEmpty())
252
throw new InternalError("empty package in " + location);
253
return JLA.definePackage(ClassLoaders.bootLoader(), name, module);
254
}
255
256
// package in unnamed module (-Xbootclasspath/a)
257
URL url = toFileURL(location);
258
Manifest man = url != null ? getManifest(location) : null;
259
260
return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url);
261
}
262
263
/**
264
* Finds the module at the given location defined to the boot loader.
265
* The module is either in runtime image or exploded image.
266
* Otherwise this method returns null.
267
*/
268
private static Module findModule(String location) {
269
String mn = null;
270
if (location.startsWith("jrt:/")) {
271
// named module in runtime image ("jrt:/".length() == 5)
272
mn = location.substring(5, location.length());
273
} else if (location.startsWith("file:/")) {
274
// named module in exploded image
275
Path path = Path.of(URI.create(location));
276
Path modulesDir = Path.of(JAVA_HOME, "modules");
277
if (path.startsWith(modulesDir)) {
278
mn = path.getFileName().toString();
279
}
280
}
281
282
// return the Module object for the module name. The Module may
283
// in the boot layer or a child layer for the case that the module
284
// is loaded into a running VM
285
if (mn != null) {
286
String name = mn;
287
return Modules.findLoadedModule(mn)
288
.orElseThrow(() -> new InternalError(name + " not loaded"));
289
} else {
290
return null;
291
}
292
}
293
294
/**
295
* Returns URL if the given location is a regular file path.
296
*/
297
@SuppressWarnings("removal")
298
private static URL toFileURL(String location) {
299
return AccessController.doPrivileged(new PrivilegedAction<>() {
300
public URL run() {
301
Path path = Path.of(location);
302
if (Files.isRegularFile(path)) {
303
try {
304
return path.toUri().toURL();
305
} catch (MalformedURLException e) {}
306
}
307
return null;
308
}
309
});
310
}
311
312
/**
313
* Returns the Manifest if the given location is a JAR file
314
* containing a manifest.
315
*/
316
@SuppressWarnings("removal")
317
private static Manifest getManifest(String location) {
318
return AccessController.doPrivileged(new PrivilegedAction<>() {
319
public Manifest run() {
320
Path jar = Path.of(location);
321
try (InputStream in = Files.newInputStream(jar);
322
JarInputStream jis = new JarInputStream(in, false)) {
323
return jis.getManifest();
324
} catch (IOException e) {
325
return null;
326
}
327
}
328
});
329
}
330
}
331
332
/**
333
* Returns an array of the binary name of the packages defined by
334
* the boot loader, in VM internal form (forward slashes instead of dot).
335
*/
336
private static native String[] getSystemPackageNames();
337
338
/**
339
* Returns the location of the package of the given name, if
340
* defined by the boot loader; otherwise {@code null} is returned.
341
*
342
* The location may be a module from the runtime image or exploded image,
343
* or from the boot class append path (i.e. -Xbootclasspath/a or
344
* BOOT-CLASS-PATH attribute specified in java agent).
345
*/
346
private static native String getSystemPackageLocation(String name);
347
private static native void setBootLoaderUnnamedModule0(Module module);
348
}
349
350