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/ClassLoaders.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
26
package jdk.internal.loader;
27
28
import java.io.IOException;
29
import java.net.URL;
30
import java.nio.file.InvalidPathException;
31
import java.nio.file.Path;
32
import java.security.CodeSource;
33
import java.security.PermissionCollection;
34
import java.util.jar.Manifest;
35
36
import jdk.internal.access.JavaLangAccess;
37
import jdk.internal.access.SharedSecrets;
38
import jdk.internal.misc.VM;
39
import jdk.internal.module.ServicesCatalog;
40
41
/**
42
* Creates and provides access to the built-in platform and application class
43
* loaders. It also creates the class loader that is used to locate resources
44
* in modules defined to the boot class loader.
45
*/
46
47
public class ClassLoaders {
48
49
private ClassLoaders() { }
50
51
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
52
53
// the built-in class loaders
54
private static final BootClassLoader BOOT_LOADER;
55
private static final PlatformClassLoader PLATFORM_LOADER;
56
private static final AppClassLoader APP_LOADER;
57
58
// Sets the ServicesCatalog for the specified loader using archived objects.
59
private static void setArchivedServicesCatalog(ClassLoader loader) {
60
ServicesCatalog catalog = ArchivedClassLoaders.get().servicesCatalog(loader);
61
ServicesCatalog.putServicesCatalog(loader, catalog);
62
}
63
64
// Creates the built-in class loaders.
65
static {
66
ArchivedClassLoaders archivedClassLoaders = ArchivedClassLoaders.get();
67
if (archivedClassLoaders != null) {
68
// assert VM.getSavedProperty("jdk.boot.class.path.append") == null
69
BOOT_LOADER = (BootClassLoader) archivedClassLoaders.bootLoader();
70
setArchivedServicesCatalog(BOOT_LOADER);
71
PLATFORM_LOADER = (PlatformClassLoader) archivedClassLoaders.platformLoader();
72
setArchivedServicesCatalog(PLATFORM_LOADER);
73
} else {
74
// -Xbootclasspath/a or -javaagent with Boot-Class-Path attribute
75
String append = VM.getSavedProperty("jdk.boot.class.path.append");
76
URLClassPath ucp = (append != null && !append.isEmpty())
77
? new URLClassPath(append, true)
78
: null;
79
BOOT_LOADER = new BootClassLoader(ucp);
80
PLATFORM_LOADER = new PlatformClassLoader(BOOT_LOADER);
81
}
82
// A class path is required when no initial module is specified.
83
// In this case the class path defaults to "", meaning the current
84
// working directory. When an initial module is specified, on the
85
// contrary, we drop this historic interpretation of the empty
86
// string and instead treat it as unspecified.
87
String cp = System.getProperty("java.class.path");
88
if (cp == null || cp.isEmpty()) {
89
String initialModuleName = System.getProperty("jdk.module.main");
90
cp = (initialModuleName == null) ? "" : null;
91
}
92
URLClassPath ucp = new URLClassPath(cp, false);
93
if (archivedClassLoaders != null) {
94
APP_LOADER = (AppClassLoader) archivedClassLoaders.appLoader();
95
setArchivedServicesCatalog(APP_LOADER);
96
APP_LOADER.setClassPath(ucp);
97
} else {
98
APP_LOADER = new AppClassLoader(PLATFORM_LOADER, ucp);
99
ArchivedClassLoaders.archive();
100
}
101
}
102
103
/**
104
* Returns the class loader that is used to find resources in modules
105
* defined to the boot class loader.
106
*
107
* @apiNote This method is not public, it should instead be used via
108
* the BootLoader class that provides a restricted API to this class
109
* loader.
110
*/
111
static BuiltinClassLoader bootLoader() {
112
return BOOT_LOADER;
113
}
114
115
/**
116
* Returns the platform class loader.
117
*/
118
public static ClassLoader platformClassLoader() {
119
return PLATFORM_LOADER;
120
}
121
122
/**
123
* Returns the application class loader.
124
*/
125
public static ClassLoader appClassLoader() {
126
return APP_LOADER;
127
}
128
129
/**
130
* The class loader that is used to find resources in modules defined to
131
* the boot class loader. It is not used for class loading.
132
*/
133
private static class BootClassLoader extends BuiltinClassLoader {
134
BootClassLoader(URLClassPath bcp) {
135
super(null, null, bcp);
136
}
137
138
@Override
139
protected Class<?> loadClassOrNull(String cn, boolean resolve) {
140
return JLA.findBootstrapClassOrNull(cn);
141
}
142
};
143
144
/**
145
* The platform class loader, a unique type to make it easier to distinguish
146
* from the application class loader.
147
*/
148
private static class PlatformClassLoader extends BuiltinClassLoader {
149
static {
150
if (!ClassLoader.registerAsParallelCapable())
151
throw new InternalError();
152
}
153
154
PlatformClassLoader(BootClassLoader parent) {
155
super("platform", parent, null);
156
}
157
}
158
159
/**
160
* The application class loader that is a {@code BuiltinClassLoader} with
161
* customizations to be compatible with long standing behavior.
162
*/
163
private static class AppClassLoader extends BuiltinClassLoader {
164
static {
165
if (!ClassLoader.registerAsParallelCapable())
166
throw new InternalError();
167
}
168
169
AppClassLoader(BuiltinClassLoader parent, URLClassPath ucp) {
170
super("app", parent, ucp);
171
}
172
173
@Override
174
protected Class<?> loadClass(String cn, boolean resolve)
175
throws ClassNotFoundException
176
{
177
// for compatibility reasons, say where restricted package list has
178
// been updated to list API packages in the unnamed module.
179
@SuppressWarnings("removal")
180
SecurityManager sm = System.getSecurityManager();
181
if (sm != null) {
182
int i = cn.lastIndexOf('.');
183
if (i != -1) {
184
sm.checkPackageAccess(cn.substring(0, i));
185
}
186
}
187
188
return super.loadClass(cn, resolve);
189
}
190
191
@Override
192
protected PermissionCollection getPermissions(CodeSource cs) {
193
PermissionCollection perms = super.getPermissions(cs);
194
perms.add(new RuntimePermission("exitVM"));
195
return perms;
196
}
197
198
/**
199
* Called by the VM to support dynamic additions to the class path
200
*
201
* @see java.lang.instrument.Instrumentation#appendToSystemClassLoaderSearch
202
*/
203
void appendToClassPathForInstrumentation(String path) {
204
appendClassPath(path);
205
}
206
207
/**
208
* Called by the VM to support define package for AppCDS
209
*/
210
protected Package defineOrCheckPackage(String pn, Manifest man, URL url) {
211
return super.defineOrCheckPackage(pn, man, url);
212
}
213
214
/**
215
* Called by the VM, during -Xshare:dump
216
*/
217
private void resetArchivedStates() {
218
setClassPath(null);
219
}
220
}
221
222
/**
223
* Attempts to convert the given string to a file URL.
224
*
225
* @apiNote This is called by the VM
226
*/
227
@Deprecated
228
private static URL toFileURL(String s) {
229
try {
230
// Use an intermediate File object to construct a URI/URL without
231
// authority component as URLClassPath can't handle URLs with a UNC
232
// server name in the authority component.
233
return Path.of(s).toRealPath().toFile().toURI().toURL();
234
} catch (InvalidPathException | IOException ignore) {
235
// malformed path string or class path element does not exist
236
return null;
237
}
238
}
239
}
240
241