< prev index next >

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

Print this page




  47 import java.security.PrivilegedExceptionAction;
  48 import java.security.SecureClassLoader;
  49 import java.util.ArrayList;
  50 import java.util.Collections;
  51 import java.util.Enumeration;
  52 import java.util.Iterator;
  53 import java.util.List;
  54 import java.util.Map;
  55 import java.util.NoSuchElementException;
  56 import java.util.Optional;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Function;
  59 import java.util.jar.Attributes;
  60 import java.util.jar.Manifest;
  61 import java.util.stream.Stream;
  62 
  63 import jdk.internal.access.SharedSecrets;
  64 import jdk.internal.misc.VM;
  65 import jdk.internal.module.ModulePatcher.PatchedModuleReader;
  66 import jdk.internal.module.Resources;
  67 import jdk.internal.vm.annotation.Stable;
  68 
  69 
  70 /**
  71  * The platform or application class loader. Resources loaded from modules
  72  * defined to the boot class loader are also loaded via an instance of this
  73  * ClassLoader type.
  74  *
  75  * <p> This ClassLoader supports loading of classes and resources from modules.
  76  * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
  77  * method. Defining a module to this ClassLoader has the effect of making the
  78  * types in the module visible. </p>
  79  *
  80  * <p> This ClassLoader also supports loading of classes and resources from a
  81  * class path of URLs that are specified to the ClassLoader at construction
  82  * time. The class path may expand at runtime (the Class-Path attribute in JAR
  83  * files or via instrumentation agents). </p>
  84  *
  85  * <p> The delegation model used by this ClassLoader differs to the regular
  86  * delegation model. When requested to load a class then this ClassLoader first
  87  * maps the class name to its package name. If there is a module defined to a


 101         if (!ClassLoader.registerAsParallelCapable())
 102             throw new InternalError("Unable to register as parallel capable");
 103     }
 104 
 105     // parent ClassLoader
 106     private final BuiltinClassLoader parent;
 107 
 108     // the URL class path, or null if there is no class path
 109     private final URLClassPath ucp;
 110 
 111 
 112     /**
 113      * A module defined/loaded by a built-in class loader.
 114      *
 115      * A LoadedModule encapsulates a ModuleReference along with its CodeSource
 116      * URL to avoid needing to create this URL when defining classes.
 117      */
 118     private static class LoadedModule {
 119         private final BuiltinClassLoader loader;
 120         private final ModuleReference mref;
 121         private final URI uri;                      // may be null
 122         private @Stable URL codeSourceURL;          // may be null
 123 
 124         LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
 125             URL url = null;
 126             this.uri = mref.location().orElse(null);
 127 
 128             // for non-jrt schemes we need to resolve the codeSourceURL
 129             // eagerly during bootstrap since the handler might be
 130             // overridden
 131             if (uri != null && !"jrt".equals(uri.getScheme())) {
 132                 url = createURL(uri);
 133             }
 134             this.loader = loader;
 135             this.mref = mref;
 136             this.codeSourceURL = url;
 137         }
 138 
 139         BuiltinClassLoader loader() { return loader; }
 140         ModuleReference mref() { return mref; }
 141         String name() { return mref.descriptor().name(); }
 142 
 143         URL codeSourceURL() {
 144             URL url = codeSourceURL;
 145             if (url == null && uri != null) {
 146                 codeSourceURL = url = createURL(uri);
 147             }
 148             return url;
 149         }
 150 
 151         private URL createURL(URI uri) {
 152             URL url = null;
 153             try {
 154                 url = uri.toURL();
 155             } catch (MalformedURLException | IllegalArgumentException e) {
 156             }
 157             return url;
 158         }
 159     }
 160 
 161 
 162     // maps package name to loaded module for modules in the boot layer
 163     private static final Map<String, LoadedModule> packageToModule
 164         = new ConcurrentHashMap<>(1024);
 165 
 166     // maps a module name to a module reference
 167     private final Map<String, ModuleReference> nameToModule;
 168 
 169     // maps a module reference to a module reader
 170     private final Map<ModuleReference, ModuleReader> moduleToReader;
 171 
 172     // cache of resource name -> list of URLs.
 173     // used only for resources that are not in module packages
 174     private volatile SoftReference<Map<String, List<URL>>> resourceCache;
 175 
 176     /**
 177      * Create a new instance.
 178      */




  47 import java.security.PrivilegedExceptionAction;
  48 import java.security.SecureClassLoader;
  49 import java.util.ArrayList;
  50 import java.util.Collections;
  51 import java.util.Enumeration;
  52 import java.util.Iterator;
  53 import java.util.List;
  54 import java.util.Map;
  55 import java.util.NoSuchElementException;
  56 import java.util.Optional;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Function;
  59 import java.util.jar.Attributes;
  60 import java.util.jar.Manifest;
  61 import java.util.stream.Stream;
  62 
  63 import jdk.internal.access.SharedSecrets;
  64 import jdk.internal.misc.VM;
  65 import jdk.internal.module.ModulePatcher.PatchedModuleReader;
  66 import jdk.internal.module.Resources;

  67 
  68 
  69 /**
  70  * The platform or application class loader. Resources loaded from modules
  71  * defined to the boot class loader are also loaded via an instance of this
  72  * ClassLoader type.
  73  *
  74  * <p> This ClassLoader supports loading of classes and resources from modules.
  75  * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
  76  * method. Defining a module to this ClassLoader has the effect of making the
  77  * types in the module visible. </p>
  78  *
  79  * <p> This ClassLoader also supports loading of classes and resources from a
  80  * class path of URLs that are specified to the ClassLoader at construction
  81  * time. The class path may expand at runtime (the Class-Path attribute in JAR
  82  * files or via instrumentation agents). </p>
  83  *
  84  * <p> The delegation model used by this ClassLoader differs to the regular
  85  * delegation model. When requested to load a class then this ClassLoader first
  86  * maps the class name to its package name. If there is a module defined to a


 100         if (!ClassLoader.registerAsParallelCapable())
 101             throw new InternalError("Unable to register as parallel capable");
 102     }
 103 
 104     // parent ClassLoader
 105     private final BuiltinClassLoader parent;
 106 
 107     // the URL class path, or null if there is no class path
 108     private final URLClassPath ucp;
 109 
 110 
 111     /**
 112      * A module defined/loaded by a built-in class loader.
 113      *
 114      * A LoadedModule encapsulates a ModuleReference along with its CodeSource
 115      * URL to avoid needing to create this URL when defining classes.
 116      */
 117     private static class LoadedModule {
 118         private final BuiltinClassLoader loader;
 119         private final ModuleReference mref;
 120         private final URL codeSourceURL;          // may be null

 121 
 122         LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
 123             URL url = null;
 124             if (mref.location().isPresent()) {
 125                 try {
 126                     url = mref.location().get().toURL();
 127                 } catch (MalformedURLException | IllegalArgumentException e) { }



 128             }
 129             this.loader = loader;
 130             this.mref = mref;
 131             this.codeSourceURL = url;
 132         }
 133 
 134         BuiltinClassLoader loader() { return loader; }
 135         ModuleReference mref() { return mref; }
 136         String name() { return mref.descriptor().name(); }
 137         URL codeSourceURL() { return codeSourceURL; }
















 138     }
 139 
 140 
 141     // maps package name to loaded module for modules in the boot layer
 142     private static final Map<String, LoadedModule> packageToModule
 143         = new ConcurrentHashMap<>(1024);
 144 
 145     // maps a module name to a module reference
 146     private final Map<String, ModuleReference> nameToModule;
 147 
 148     // maps a module reference to a module reader
 149     private final Map<ModuleReference, ModuleReader> moduleToReader;
 150 
 151     // cache of resource name -> list of URLs.
 152     // used only for resources that are not in module packages
 153     private volatile SoftReference<Map<String, List<URL>>> resourceCache;
 154 
 155     /**
 156      * Create a new instance.
 157      */


< prev index next >