Path: blob/master/src/java.base/share/classes/jdk/internal/module/ModuleLoaderMap.java
41159 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.module;2627import java.lang.module.Configuration;28import java.lang.module.ResolvedModule;29import java.util.HashMap;30import java.util.Map;31import java.util.Set;32import java.util.function.Function;3334import jdk.internal.loader.ClassLoaders;3536/**37* Supports the mapping of modules to class loaders. The set of modules mapped38* to the boot and platform class loaders is generated at build time from39* this source file.40*/41public final class ModuleLoaderMap {4243/**44* Maps the system modules to the built-in class loaders.45*/46private static final class Mapper implements Function<String, ClassLoader> {4748private static final ClassLoader PLATFORM_CLASSLOADER =49ClassLoaders.platformClassLoader();50private static final ClassLoader APP_CLASSLOADER =51ClassLoaders.appClassLoader();5253private static final Integer PLATFORM_LOADER_INDEX = 1;54private static final Integer APP_LOADER_INDEX = 2;5556/**57* Map from module to a class loader index. The index is resolved to the58* actual class loader in {@code apply}.59*/60private final Map<String, Integer> map;6162/**63* Creates a Mapper to map module names in the given Configuration to64* built-in classloaders.65*66* As a proxy for the actual classloader, we store an easily archiveable67* index value in the internal map. The index is stored as a boxed value68* so that we can cheaply do identity comparisons during bootstrap.69*/70Mapper(Configuration cf) {71var map = new HashMap<String, Integer>();72for (ResolvedModule resolvedModule : cf.modules()) {73String mn = resolvedModule.name();74if (!Modules.bootModules.contains(mn)) {75if (Modules.platformModules.contains(mn)) {76map.put(mn, PLATFORM_LOADER_INDEX);77} else {78map.put(mn, APP_LOADER_INDEX);79}80}81}82this.map = map;83}8485@Override86public ClassLoader apply(String name) {87Integer loader = map.get(name);88if (loader == APP_LOADER_INDEX) {89return APP_CLASSLOADER;90} else if (loader == PLATFORM_LOADER_INDEX) {91return PLATFORM_CLASSLOADER;92} else { // BOOT_LOADER_INDEX93return null;94}95}96}9798/**99* Returns the names of the modules defined to the boot loader.100*/101public static Set<String> bootModules() {102return Modules.bootModules;103}104105/**106* Returns the names of the modules defined to the platform loader.107*/108public static Set<String> platformModules() {109return Modules.platformModules;110}111112private static class Modules {113// list of boot modules is generated at build time.114private static final Set<String> bootModules =115Set.of(new String[] { "@@BOOT_MODULE_NAMES@@" });116117// list of platform modules is generated at build time.118private static final Set<String> platformModules =119Set.of(new String[] { "@@PLATFORM_MODULE_NAMES@@" });120}121122/**123* Returns a function to map modules in the given configuration to the124* built-in class loaders.125*/126static Function<String, ClassLoader> mappingFunction(Configuration cf) {127return new Mapper(cf);128}129130/**131* When defining modules for a configuration, we only allow defining modules132* to the boot or platform classloader if the ClassLoader mapping function133* originate from here.134*/135public static boolean isBuiltinMapper(Function<String, ClassLoader> clf) {136return clf instanceof Mapper;137}138}139140141