73 * maps the class name to its package name. If there a module defined to the
74 * Loader containing the package then the class loader attempts to load from
75 * that module. If the package is instead defined to a module in a "remote"
76 * ClassLoader then this class loader delegates directly to that class loader.
77 * The map of package name to remote class loader is created based on the
78 * modules read by modules defined to this class loader. If the package is not
79 * local or remote then this class loader will delegate to the parent class
80 * loader. This allows automatic modules (for example) to link to types in the
81 * unnamed module of the parent class loader.
82 *
83 * @see ModuleLayer#defineModulesWithOneLoader
84 * @see ModuleLayer#defineModulesWithManyLoaders
85 */
86
87 public final class Loader extends SecureClassLoader {
88
89 static {
90 ClassLoader.registerAsParallelCapable();
91 }
92
93 // the loader pool is in a pool, can be null
94 private final LoaderPool pool;
95
96 // parent ClassLoader, can be null
97 private final ClassLoader parent;
98
99 // maps a module name to a module reference
100 private final Map<String, ModuleReference> nameToModule;
101
102 // maps package name to a module loaded by this class loader
103 private final Map<String, LoadedModule> localPackageToModule;
104
105 // maps package name to a remote class loader, populated post initialization
106 private final Map<String, ClassLoader> remotePackageToLoader
107 = new ConcurrentHashMap<>();
108
109 // maps a module reference to a module reader, populated lazily
110 private final Map<ModuleReference, ModuleReader> moduleToReader
111 = new ConcurrentHashMap<>();
112
113 // ACC used when loading classes and resources
470 }
471
472
473 // -- finding/loading classes
474
475 /**
476 * Finds the class with the specified binary name.
477 */
478 @Override
479 protected Class<?> findClass(String cn) throws ClassNotFoundException {
480 Class<?> c = null;
481 LoadedModule loadedModule = findLoadedModule(cn);
482 if (loadedModule != null)
483 c = findClassInModuleOrNull(loadedModule, cn);
484 if (c == null)
485 throw new ClassNotFoundException(cn);
486 return c;
487 }
488
489 /**
490 * Finds the class with the specified binary name in a given module.
491 * This method returns {@code null} if the class cannot be found.
492 */
493 @Override
494 protected Class<?> findClass(String mn, String cn) {
495 Class<?> c = null;
496 LoadedModule loadedModule = findLoadedModule(cn);
497 if (loadedModule != null && loadedModule.name().equals(mn))
498 c = findClassInModuleOrNull(loadedModule, cn);
499 return c;
500 }
501
502 /**
503 * Loads the class with the specified binary name.
504 */
505 @Override
506 protected Class<?> loadClass(String cn, boolean resolve)
507 throws ClassNotFoundException
508 {
509 SecurityManager sm = System.getSecurityManager();
510 if (sm != null) {
|
73 * maps the class name to its package name. If there a module defined to the
74 * Loader containing the package then the class loader attempts to load from
75 * that module. If the package is instead defined to a module in a "remote"
76 * ClassLoader then this class loader delegates directly to that class loader.
77 * The map of package name to remote class loader is created based on the
78 * modules read by modules defined to this class loader. If the package is not
79 * local or remote then this class loader will delegate to the parent class
80 * loader. This allows automatic modules (for example) to link to types in the
81 * unnamed module of the parent class loader.
82 *
83 * @see ModuleLayer#defineModulesWithOneLoader
84 * @see ModuleLayer#defineModulesWithManyLoaders
85 */
86
87 public final class Loader extends SecureClassLoader {
88
89 static {
90 ClassLoader.registerAsParallelCapable();
91 }
92
93 // the pool this loader is a member of; can be null
94 private final LoaderPool pool;
95
96 // parent ClassLoader, can be null
97 private final ClassLoader parent;
98
99 // maps a module name to a module reference
100 private final Map<String, ModuleReference> nameToModule;
101
102 // maps package name to a module loaded by this class loader
103 private final Map<String, LoadedModule> localPackageToModule;
104
105 // maps package name to a remote class loader, populated post initialization
106 private final Map<String, ClassLoader> remotePackageToLoader
107 = new ConcurrentHashMap<>();
108
109 // maps a module reference to a module reader, populated lazily
110 private final Map<ModuleReference, ModuleReader> moduleToReader
111 = new ConcurrentHashMap<>();
112
113 // ACC used when loading classes and resources
470 }
471
472
473 // -- finding/loading classes
474
475 /**
476 * Finds the class with the specified binary name.
477 */
478 @Override
479 protected Class<?> findClass(String cn) throws ClassNotFoundException {
480 Class<?> c = null;
481 LoadedModule loadedModule = findLoadedModule(cn);
482 if (loadedModule != null)
483 c = findClassInModuleOrNull(loadedModule, cn);
484 if (c == null)
485 throw new ClassNotFoundException(cn);
486 return c;
487 }
488
489 /**
490 * Finds the class with the specified binary name in the given module.
491 * This method returns {@code null} if the class cannot be found.
492 */
493 @Override
494 protected Class<?> findClass(String mn, String cn) {
495 Class<?> c = null;
496 LoadedModule loadedModule = findLoadedModule(cn);
497 if (loadedModule != null && loadedModule.name().equals(mn))
498 c = findClassInModuleOrNull(loadedModule, cn);
499 return c;
500 }
501
502 /**
503 * Loads the class with the specified binary name.
504 */
505 @Override
506 protected Class<?> loadClass(String cn, boolean resolve)
507 throws ClassNotFoundException
508 {
509 SecurityManager sm = System.getSecurityManager();
510 if (sm != null) {
|