< prev index next >

src/java.base/share/classes/jdk/internal/loader/ClassLoaders.java

Print this page
8198480: Improve ClassLoaders static init block
Reviewed-by: alanb, mchung


  37 import jdk.internal.misc.SharedSecrets;
  38 import jdk.internal.misc.VM;
  39 
  40 /**
  41  * Creates and provides access to the built-in platform and application class
  42  * loaders. It also creates the class loader that is used to locate resources
  43  * in modules defined to the boot class loader.
  44  */
  45 
  46 public class ClassLoaders {
  47 
  48     private ClassLoaders() { }
  49 
  50     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  51 
  52     // the built-in class loaders
  53     private static final BootClassLoader BOOT_LOADER;
  54     private static final PlatformClassLoader PLATFORM_LOADER;
  55     private static final AppClassLoader APP_LOADER;
  56 
  57     /**
  58      * Creates the built-in class loaders
  59      */
  60     static {
  61 
  62         // -Xbootclasspath/a or -javaagent with Boot-Class-Path attribute
  63         URLClassPath bcp = null;
  64         String s = VM.getSavedProperty("jdk.boot.class.path.append");
  65         if (s != null && s.length() > 0)
  66             bcp = new URLClassPath(s, true);


  67 
  68         // we have a class path if -cp is specified or -m is not specified.
  69         // If neither is specified then default to -cp <working directory>
  70         // If -cp is not specified and -m is specified, the value of
  71         // java.class.path is an empty string, then no class path.
  72         String mainMid = System.getProperty("jdk.module.main");
  73         String cp = System.getProperty("java.class.path");
  74         if (mainMid == null) {
  75             // no main module specified so class path required
  76             if (cp == null) {
  77                 cp = "";
  78             }
  79         } else {
  80             // main module specified, ignore empty class path
  81             if (cp != null && cp.length() == 0) {
  82                 cp = null;
  83             }
  84         }
  85         URLClassPath ucp = new URLClassPath(cp, false);
  86 
  87         // create the class loaders
  88         BOOT_LOADER = new BootClassLoader(bcp);
  89         PLATFORM_LOADER = new PlatformClassLoader(BOOT_LOADER);
  90         APP_LOADER = new AppClassLoader(PLATFORM_LOADER, ucp);
  91     }
  92 
  93     /**
  94      * Returns the class loader that is used to find resources in modules
  95      * defined to the boot class loader.
  96      *
  97      * @apiNote This method is not public, it should instead be used via
  98      * the BootLoader class that provides a restricted API to this class
  99      * loader.
 100      */
 101     static BuiltinClassLoader bootLoader() {
 102         return BOOT_LOADER;
 103     }
 104 
 105     /**
 106      * Returns the platform class loader.
 107      */
 108     public static ClassLoader platformClassLoader() {
 109         return PLATFORM_LOADER;




  37 import jdk.internal.misc.SharedSecrets;
  38 import jdk.internal.misc.VM;
  39 
  40 /**
  41  * Creates and provides access to the built-in platform and application class
  42  * loaders. It also creates the class loader that is used to locate resources
  43  * in modules defined to the boot class loader.
  44  */
  45 
  46 public class ClassLoaders {
  47 
  48     private ClassLoaders() { }
  49 
  50     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  51 
  52     // the built-in class loaders
  53     private static final BootClassLoader BOOT_LOADER;
  54     private static final PlatformClassLoader PLATFORM_LOADER;
  55     private static final AppClassLoader APP_LOADER;
  56 
  57     // Creates the built-in class loaders.


  58     static {

  59         // -Xbootclasspath/a or -javaagent with Boot-Class-Path attribute
  60         String append = VM.getSavedProperty("jdk.boot.class.path.append");
  61         BOOT_LOADER =
  62             new BootClassLoader((append != null && append.length() > 0)
  63                 ? new URLClassPath(append, true)
  64                 : null);
  65         PLATFORM_LOADER = new PlatformClassLoader(BOOT_LOADER);
  66 
  67         // A class path is required when no initial module is specified.
  68         // In this case the class path defaults to "", meaning the current
  69         // working directory.  When an initial module is specified, on the
  70         // contrary, we drop this historic interpretation of the empty
  71         // string and instead treat it as unspecified.
  72         String cp = System.getProperty("java.class.path");
  73         if (cp == null || cp.length() == 0) {
  74             String initialModuleName = System.getProperty("jdk.module.main");
  75             cp = (initialModuleName == null) ? "" : null;







  76         }
  77         URLClassPath ucp = new URLClassPath(cp, false);




  78         APP_LOADER = new AppClassLoader(PLATFORM_LOADER, ucp);
  79     }
  80 
  81     /**
  82      * Returns the class loader that is used to find resources in modules
  83      * defined to the boot class loader.
  84      *
  85      * @apiNote This method is not public, it should instead be used via
  86      * the BootLoader class that provides a restricted API to this class
  87      * loader.
  88      */
  89     static BuiltinClassLoader bootLoader() {
  90         return BOOT_LOADER;
  91     }
  92 
  93     /**
  94      * Returns the platform class loader.
  95      */
  96     public static ClassLoader platformClassLoader() {
  97         return PLATFORM_LOADER;


< prev index next >