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 // IOI hack -- force these two modules (if they exist) to be 89 // loaded from the platform loader. 90 // 91 // Otherwise these two modules will be loaded by the app loader, but 92 // jdk.internal.vm.compiler (loaded by the platform loader) will resolve 93 // some classes in these two modules. This causes some complications with 94 // -XX:+PreloadSharedClasses. 95 if ("org.graalvm.sdk".equals(name)) { 96 return PLATFORM_CLASSLOADER; 97 } 98 if ("org.graalvm.truffle".equals(name)) { 99 return PLATFORM_CLASSLOADER; 100 } 101 Integer loader = map.get(name); 102 if (loader == APP_LOADER_INDEX) { 103 return APP_CLASSLOADER; 104 } else if (loader == PLATFORM_LOADER_INDEX) { 105 return PLATFORM_CLASSLOADER; 106 } else { // BOOT_LOADER_INDEX 107 return null; 108 } 109 } 110 } 111 112 /** 113 * Returns the names of the modules defined to the boot loader. 114 */ 115 public static Set<String> bootModules() { 116 return Modules.bootModules; 117 } 118 119 /** 120 * Returns the names of the modules defined to the platform loader. 121 */ 122 public static Set<String> platformModules() { 123 return Modules.platformModules; 124 } 125 126 /** 127 * Returns the names of the modules defined to the application loader which perform native access. 128 */ 129 public static Set<String> nativeAccessModules() { 130 return Modules.nativeAccessModules; 131 } 132 133 private static class Modules { 134 // list of boot modules is generated at build time. 135 private static final Set<String> bootModules = 136 Set.of(new String[] { "@@BOOT_MODULE_NAMES@@" }); 137 138 // list of platform modules is generated at build time. 139 private static final Set<String> platformModules = 140 Set.of(new String[] { "@@PLATFORM_MODULE_NAMES@@" }); 141 142 // list of jdk modules is generated at build time. 143 private static final Set<String> nativeAccessModules = 144 Set.of(new String[] { "@@NATIVE_ACCESS_MODULE_NAMES@@" }); 145 } 146 147 /** 148 * Returns a function to map modules in the given configuration to the 149 * built-in class loaders. 150 */ 151 static Function<String, ClassLoader> mappingFunction(Configuration cf) { 152 return new Mapper(cf); 153 } 154 155 /** 156 * When defining modules for a configuration, we only allow defining modules 157 * to the boot or platform classloader if the ClassLoader mapping function 158 * originate from here. 159 */ 160 public static boolean isBuiltinMapper(Function<String, ClassLoader> clf) { 161 return clf instanceof Mapper; 162 } 163 }