< prev index next >

src/java.base/share/classes/java/lang/ClassLoader.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2013, 2024, 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
--- 1,7 ---
  /*
!  * 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

*** 56,10 ***
--- 56,11 ---
  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;

*** 1838,12 ***
              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;
          }
      }
  
      static ClassLoader getBuiltinPlatformClassLoader() {
          return ClassLoaders.platformClassLoader();
--- 1839,12 ---
              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() && Holder.scl != null;
!                 return Holder.scl;
          }
      }
  
      static ClassLoader getBuiltinPlatformClassLoader() {
          return ClassLoaders.platformClassLoader();

*** 1864,22 ***
              throw new InternalError("system class loader cannot be set at initLevel " +
                                      VM.initLevel());
          }
  
          // detect recursive initialization
!         if (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);
              } catch (Exception e) {
                  Throwable cause = e;
                  if (e instanceof InvocationTargetException) {
                      cause = e.getCause();
                      if (cause instanceof Error) {
--- 1865,22 ---
              throw new InternalError("system class loader cannot be set at initLevel " +
                                      VM.initLevel());
          }
  
          // detect recursive initialization
!         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);
!                 Holder.scl = (ClassLoader) ctor.newInstance(builtinLoader);
              } catch (Exception e) {
                  Throwable cause = e;
                  if (e instanceof InvocationTargetException) {
                      cause = e.getCause();
                      if (cause instanceof Error) {

*** 1890,13 ***
                      throw (RuntimeException) cause;
                  }
                  throw new Error(cause.getMessage(), cause);
              }
          } else {
!             scl = builtinLoader;
          }
!         return 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
--- 1891,13 ---
                      throw (RuntimeException) cause;
                  }
                  throw new Error(cause.getMessage(), cause);
              }
          } else {
!             Holder.scl = builtinLoader;
          }
!         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

*** 1905,13 ***
          }
          // Circumvent security check since this is package-private
          return caller.getClassLoader0();
      }
  
!     // The system class loader
!     // @GuardedBy("ClassLoader.class")
!     private static volatile ClassLoader scl;
  
      // -- Package --
  
      /**
       * Define a Package of the given Class object.
--- 1906,17 ---
          }
          // Circumvent security check since this is package-private
          return caller.getClassLoader0();
      }
  
!     // 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.

*** 2567,14 ***
       */
      private void resetArchivedStates() {
          if (parallelLockMap != null) {
              parallelLockMap.clear();
          }
!         packages.clear();
!         package2certs.clear();
          classes.clear();
          classLoaderValueMap = null;
      }
  }
  
  /*
   * A utility class that will enumerate over an array of enumerations.
--- 2572,28 ---
       */
      private void resetArchivedStates() {
          if (parallelLockMap != null) {
              parallelLockMap.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 >