40 import java.security.AccessController;
41 import java.security.CodeSigner;
42 import java.security.CodeSource;
43 import java.security.Permission;
44 import java.security.PermissionCollection;
45 import java.security.PrivilegedAction;
46 import java.security.PrivilegedActionException;
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.List;
53 import java.util.Map;
54 import java.util.Optional;
55 import java.util.concurrent.ConcurrentHashMap;
56 import java.util.jar.Attributes;
57 import java.util.jar.Manifest;
58 import java.util.stream.Stream;
59
60 import jdk.internal.module.ModulePatcher.PatchedModuleReader;
61 import jdk.internal.misc.VM;
62
63
64 /**
65 * The platform or application class loader. Resources loaded from modules
66 * defined to the boot class loader are also loaded via an instance of this
67 * ClassLoader type.
68 *
69 * <p> This ClassLoader supports loading of classes and resources from modules.
70 * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
71 * method. Defining a module to this ClassLoader has the effect of making the
72 * types in the module visible. </p>
73 *
74 * <p> This ClassLoader also supports loading of classes and resources from a
75 * class path of URLs that are specified to the ClassLoader at construction
76 * time. The class path may expand at runtime (the Class-Path attribute in JAR
77 * files or via instrumentation agents). </p>
78 *
79 * <p> The delegation model used by this ClassLoader differs to the regular
80 * delegation model. When requested to load a class then this ClassLoader first
81 * maps the class name to its package name. If there is a module defined to a
118 URL url = null;
119 if (mref.location().isPresent()) {
120 try {
121 url = mref.location().get().toURL();
122 } catch (MalformedURLException | IllegalArgumentException e) { }
123 }
124 this.loader = loader;
125 this.mref = mref;
126 this.codeSourceURL = url;
127 }
128
129 BuiltinClassLoader loader() { return loader; }
130 ModuleReference mref() { return mref; }
131 String name() { return mref.descriptor().name(); }
132 URL codeSourceURL() { return codeSourceURL; }
133 }
134
135
136 // maps package name to loaded module for modules in the boot layer
137 private static final Map<String, LoadedModule> packageToModule
138 = new ConcurrentHashMap<>(1024);
139
140 // maps a module name to a module reference
141 private final Map<String, ModuleReference> nameToModule;
142
143 // maps a module reference to a module reader
144 private final Map<ModuleReference, ModuleReader> moduleToReader;
145
146 // cache of resource name -> list of URLs.
147 // used only for resources that are not in module packages
148 private volatile SoftReference<Map<String, List<URL>>> resourceCache;
149
150 /**
151 * Create a new instance.
152 */
153 BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) {
154 // ensure getParent() returns null when the parent is the boot loader
155 super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
156
157 this.parent = parent;
158 this.ucp = ucp;
905 String path = p.getName();
906 if (path.endsWith(File.separator)) {
907 path += "-";
908 p = new FilePermission(path, "read");
909 }
910 }
911 perms.add(p);
912 }
913 } catch (IOException ioe) { }
914
915 return perms;
916 }
917
918
919 // -- miscellaneous supporting methods
920
921 /**
922 * Returns the ModuleReader for the given module.
923 */
924 private ModuleReader moduleReaderFor(ModuleReference mref) {
925 return moduleToReader.computeIfAbsent(mref, m -> createModuleReader(mref));
926 }
927
928 /**
929 * Creates a ModuleReader for the given module.
930 */
931 private ModuleReader createModuleReader(ModuleReference mref) {
932 try {
933 return mref.open();
934 } catch (IOException e) {
935 // Return a null module reader to avoid a future class load
936 // attempting to open the module again.
937 return new NullModuleReader();
938 }
939 }
940
941 /**
942 * A ModuleReader that doesn't read any resources.
943 */
944 private static class NullModuleReader implements ModuleReader {
945 @Override
|
40 import java.security.AccessController;
41 import java.security.CodeSigner;
42 import java.security.CodeSource;
43 import java.security.Permission;
44 import java.security.PermissionCollection;
45 import java.security.PrivilegedAction;
46 import java.security.PrivilegedActionException;
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.List;
53 import java.util.Map;
54 import java.util.Optional;
55 import java.util.concurrent.ConcurrentHashMap;
56 import java.util.jar.Attributes;
57 import java.util.jar.Manifest;
58 import java.util.stream.Stream;
59
60 import jdk.internal.misc.VM;
61 import jdk.internal.module.ModulePatcher.PatchedModuleReader;
62 import jdk.internal.module.SystemModules;
63
64
65 /**
66 * The platform or application class loader. Resources loaded from modules
67 * defined to the boot class loader are also loaded via an instance of this
68 * ClassLoader type.
69 *
70 * <p> This ClassLoader supports loading of classes and resources from modules.
71 * Modules are defined to the ClassLoader by invoking the {@link #loadModule}
72 * method. Defining a module to this ClassLoader has the effect of making the
73 * types in the module visible. </p>
74 *
75 * <p> This ClassLoader also supports loading of classes and resources from a
76 * class path of URLs that are specified to the ClassLoader at construction
77 * time. The class path may expand at runtime (the Class-Path attribute in JAR
78 * files or via instrumentation agents). </p>
79 *
80 * <p> The delegation model used by this ClassLoader differs to the regular
81 * delegation model. When requested to load a class then this ClassLoader first
82 * maps the class name to its package name. If there is a module defined to a
119 URL url = null;
120 if (mref.location().isPresent()) {
121 try {
122 url = mref.location().get().toURL();
123 } catch (MalformedURLException | IllegalArgumentException e) { }
124 }
125 this.loader = loader;
126 this.mref = mref;
127 this.codeSourceURL = url;
128 }
129
130 BuiltinClassLoader loader() { return loader; }
131 ModuleReference mref() { return mref; }
132 String name() { return mref.descriptor().name(); }
133 URL codeSourceURL() { return codeSourceURL; }
134 }
135
136
137 // maps package name to loaded module for modules in the boot layer
138 private static final Map<String, LoadedModule> packageToModule
139 = new ConcurrentHashMap<>(SystemModules.PACKAGES_IN_BOOT_LAYER);
140
141 // maps a module name to a module reference
142 private final Map<String, ModuleReference> nameToModule;
143
144 // maps a module reference to a module reader
145 private final Map<ModuleReference, ModuleReader> moduleToReader;
146
147 // cache of resource name -> list of URLs.
148 // used only for resources that are not in module packages
149 private volatile SoftReference<Map<String, List<URL>>> resourceCache;
150
151 /**
152 * Create a new instance.
153 */
154 BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) {
155 // ensure getParent() returns null when the parent is the boot loader
156 super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
157
158 this.parent = parent;
159 this.ucp = ucp;
906 String path = p.getName();
907 if (path.endsWith(File.separator)) {
908 path += "-";
909 p = new FilePermission(path, "read");
910 }
911 }
912 perms.add(p);
913 }
914 } catch (IOException ioe) { }
915
916 return perms;
917 }
918
919
920 // -- miscellaneous supporting methods
921
922 /**
923 * Returns the ModuleReader for the given module.
924 */
925 private ModuleReader moduleReaderFor(ModuleReference mref) {
926 return moduleToReader.computeIfAbsent(mref, m -> createModuleReader(m));
927 }
928
929 /**
930 * Creates a ModuleReader for the given module.
931 */
932 private ModuleReader createModuleReader(ModuleReference mref) {
933 try {
934 return mref.open();
935 } catch (IOException e) {
936 // Return a null module reader to avoid a future class load
937 // attempting to open the module again.
938 return new NullModuleReader();
939 }
940 }
941
942 /**
943 * A ModuleReader that doesn't read any resources.
944 */
945 private static class NullModuleReader implements ModuleReader {
946 @Override
|