< prev index next > src/java.base/share/classes/java/lang/ClassLoader.java
Print this page
/*
- * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, Azul Systems, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
import jdk.internal.loader.BuiltinClassLoader;
import jdk.internal.loader.ClassLoaders;
import jdk.internal.loader.NativeLibrary;
import jdk.internal.loader.NativeLibraries;
import jdk.internal.perf.PerfCounter;
+ import jdk.internal.misc.CDS;
import jdk.internal.misc.Unsafe;
import jdk.internal.misc.VM;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.CallerSensitiveAdapter;
import jdk.internal.reflect.Reflection;
case 3:
String msg = "getSystemClassLoader cannot be called during the system class loader instantiation";
throw new IllegalStateException(msg);
default:
// system fully initialized
- assert VM.isBooted() && scl != null;
- return scl;
+ assert VM.isBooted() && Holder.scl != null;
+ return Holder.scl;
}
}
static ClassLoader getBuiltinPlatformClassLoader() {
return ClassLoaders.platformClassLoader();
throw new InternalError("system class loader cannot be set at initLevel " +
VM.initLevel());
}
// detect recursive initialization
- if (scl != null) {
+ if (Holder.scl != null) {
throw new IllegalStateException("recursive invocation");
}
ClassLoader builtinLoader = getBuiltinAppClassLoader();
String cn = System.getProperty("java.system.class.loader");
if (cn != null) {
try {
// custom class loader is only supported to be loaded from unnamed module
Constructor<?> ctor = Class.forName(cn, false, builtinLoader)
.getDeclaredConstructor(ClassLoader.class);
- scl = (ClassLoader) ctor.newInstance(builtinLoader);
+ Holder.scl = (ClassLoader) ctor.newInstance(builtinLoader);
} catch (Exception e) {
Throwable cause = e;
if (e instanceof InvocationTargetException) {
cause = e.getCause();
if (cause instanceof Error) {
throw (RuntimeException) cause;
}
throw new Error(cause.getMessage(), cause);
}
} else {
- scl = builtinLoader;
+ Holder.scl = builtinLoader;
}
- return scl;
+ return Holder.scl;
}
// Returns the class's class loader, or null if none.
static ClassLoader getClassLoader(Class<?> caller) {
// This can be null if the VM is requesting it
}
// Circumvent security check since this is package-private
return caller.getClassLoader0();
}
- // The system class loader
- // @GuardedBy("ClassLoader.class")
- private static volatile ClassLoader scl;
+ // Holder has the field(s) that need to be initialized during JVM bootstrap even if
+ // the outer is aot-initialized.
+ private static class Holder {
+ // The system class loader
+ // @GuardedBy("ClassLoader.class")
+ private static volatile ClassLoader scl;
+ }
// -- Package --
/**
* Define a Package of the given Class object.
*/
private void resetArchivedStates() {
if (parallelLockMap != null) {
parallelLockMap.clear();
}
- packages.clear();
- package2certs.clear();
+
+ if (CDS.isDumpingPackages()) {
+ if (System.getProperty("cds.debug.archived.packages") != null) {
+ for (Map.Entry<String, NamedPackage> entry : packages.entrySet()) {
+ String key = entry.getKey();
+ NamedPackage value = entry.getValue();
+ System.out.println("Archiving " +
+ (value instanceof Package ? "Package" : "NamedPackage") +
+ " \"" + key + "\" for " + this);
+ }
+ }
+ } else {
+ packages.clear();
+ package2certs.clear();
+ }
classes.clear();
classLoaderValueMap = null;
+ libraries.clear();
}
}
/*
* A utility class that will enumerate over an array of enumerations.
< prev index next >