1 /* 2 * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.module; 27 28 import java.lang.module.Configuration; 29 import java.lang.module.ResolvedModule; 30 import java.util.HashMap; 31 import java.util.Map; 32 import java.util.Set; 33 import java.util.function.Function; 34 35 import jdk.internal.loader.ClassLoaders; 36 37 /** 38 * Supports the mapping of modules to class loaders. The set of modules mapped 39 * to the boot and platform class loaders is generated at build time from 40 * this source file. 41 */ 42 public final class ModuleLoaderMap { 43 44 /** 45 * Maps the system modules to the built-in class loaders. 46 */ 47 private static final class Mapper implements Function<String, ClassLoader> { 48 49 private static final ClassLoader PLATFORM_CLASSLOADER = 50 ClassLoaders.platformClassLoader(); 51 private static final ClassLoader APP_CLASSLOADER = 52 ClassLoaders.appClassLoader(); 53 54 private static final Integer PLATFORM_LOADER_INDEX = 1; 55 private static final Integer APP_LOADER_INDEX = 2; 56 57 /** 58 * Map from module to a class loader index. The index is resolved to the 59 * actual class loader in {@code apply}. 60 */ 61 private final Map<String, Integer> map; 62 63 /** 64 * Creates a Mapper to map module names in the given Configuration to 65 * built-in classloaders. 66 * 67 * As a proxy for the actual classloader, we store an easily archiveable 68 * index value in the internal map. The index is stored as a boxed value 69 * so that we can cheaply do identity comparisons during bootstrap. 70 */ 71 Mapper(Configuration cf) { 72 var map = new HashMap<String, Integer>(); 73 for (ResolvedModule resolvedModule : cf.modules()) { 74 String mn = resolvedModule.name(); 75 if (!Modules.bootModules.contains(mn)) { 76 if (Modules.platformModules.contains(mn)) { 77 map.put(mn, PLATFORM_LOADER_INDEX); 78 } else { 79 map.put(mn, APP_LOADER_INDEX); 80 } 81 } 82 } 83 this.map = map; 84 } 85 86 @Override 87 public ClassLoader apply(String name) { 88 Integer loader = map.get(name); 89 if (loader == APP_LOADER_INDEX) { 90 return APP_CLASSLOADER; 91 } else if (loader == PLATFORM_LOADER_INDEX) { 92 return PLATFORM_CLASSLOADER; 93 } else { // BOOT_LOADER_INDEX 94 return null; 95 } 96 } 97 } 98 99 /** 100 * Returns the names of the modules defined to the boot loader. 101 */ 102 public static Set<String> bootModules() { 103 return Modules.bootModules; 104 } 105 106 /** 107 * Returns the names of the modules defined to the platform loader. 108 */ 109 public static Set<String> platformModules() { 110 return Modules.platformModules; 111 } 112 113 /** 114 * Returns the names of the modules defined to the application loader which perform native access. 115 */ 116 public static Set<String> nativeAccessModules() { 117 return Modules.nativeAccessModules; 118 } 119 120 private static class Modules { 121 // list of boot modules is generated at build time. 122 private static final Set<String> bootModules = 123 Set.of(new String[] { "@@BOOT_MODULE_NAMES@@" }); 124 125 // list of platform modules is generated at build time. 126 private static final Set<String> platformModules = 127 Set.of(new String[] { "@@PLATFORM_MODULE_NAMES@@" }); 128 129 // list of jdk modules is generated at build time. 130 private static final Set<String> nativeAccessModules = 131 Set.of(new String[] { "@@NATIVE_ACCESS_MODULE_NAMES@@" }); 132 } 133 134 /** 135 * Returns a function to map modules in the given configuration to the 136 * built-in class loaders. 137 */ 138 static Function<String, ClassLoader> mappingFunction(Configuration cf) { 139 return new Mapper(cf); 140 } 141 142 /** 143 * When defining modules for a configuration, we only allow defining modules 144 * to the boot or platform classloader if the ClassLoader mapping function 145 * originate from here. 146 */ 147 public static boolean isBuiltinMapper(Function<String, ClassLoader> clf) { 148 return clf instanceof Mapper; 149 } 150 }