1 /* 2 * Copyright (c) 2015, 2017, 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.HashSet; 32 import java.util.Map; 33 import java.util.Set; 34 import java.util.function.Function; 35 36 import jdk.internal.loader.ClassLoaders; 37 38 39 /** 40 * Supports the mapping of modules to class loaders. The set of modules mapped 41 * to the boot and platform class loaders is generated at build time from 42 * this source file. 43 */ 44 public final class ModuleLoaderMap { 45 46 /** 47 * Maps the system modules to the built-in class loaders. 48 */ 49 public static final class Mapper implements Function<String, ClassLoader> { 50 private final Map<String, ClassLoader> map; 51 52 Mapper(Map<String, ClassLoader> map) { 53 this.map = map; // defensive copy not needed 54 } 55 56 @Override 57 public ClassLoader apply(String name) { 58 return map.get(name); 59 } 60 } 61 62 /** 63 * Returns the names of the modules defined to the boot loader. 64 */ 65 public static Set<String> bootModules() { 66 // The list of boot modules generated at build time. 67 String[] BOOT_MODULES = new String[] { "@@BOOT_MODULE_NAMES@@" }; 68 Set<String> bootModules = new HashSet<>(BOOT_MODULES.length); 69 for (String mn : BOOT_MODULES) { 70 bootModules.add(mn); 71 } 72 return bootModules; 73 } 74 75 /** 76 * Returns the names of the modules defined to the platform loader. 77 */ 78 public static Set<String> platformModules() { 79 // The list of platform modules generated at build time. 80 String[] PLATFORM_MODULES = new String[] { "@@PLATFORM_MODULE_NAMES@@" }; 81 Set<String> platformModules = new HashSet<>(PLATFORM_MODULES.length); 82 for (String mn : PLATFORM_MODULES) { 83 platformModules.add(mn); 84 } 85 return platformModules; 86 } 87 88 /** 89 * Returns the function to map modules in the given configuration to the 90 * built-in class loaders. 91 */ 92 static Function<String, ClassLoader> mappingFunction(Configuration cf) { 93 Set<String> bootModules = bootModules(); 94 Set<String> platformModules = platformModules(); 95 96 ClassLoader platformClassLoader = ClassLoaders.platformClassLoader(); 97 ClassLoader appClassLoader = ClassLoaders.appClassLoader(); 98 99 Map<String, ClassLoader> map = new HashMap<>(); 100 for (ResolvedModule resolvedModule : cf.modules()) { 101 String mn = resolvedModule.name(); 102 if (!bootModules.contains(mn)) { 103 if (platformModules.contains(mn)) { 104 map.put(mn, platformClassLoader); 105 } else { 106 map.put(mn, appClassLoader); 107 } 108 } 109 } 110 return new Mapper(map); 111 } 112 }