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 }
| 1 /*
2 * Copyright (c) 2015, 2020, 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 public 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 // Using boxed values allows us to use these as an adhoc enum, while keeping
55 // comparisons in Mapper.apply cheap during bootstrap since we can do
56 // identity comparisons rather than unbox the value. Not storing a reference
57 // to the ClassLoader directly ensures we can archive Mapper instances
58 private static final Integer PLATFORM_LOADER_INDEX = 1;
59 private static final Integer APP_LOADER_INDEX = 2;
60
61 private final HashMap<String, Integer> map;
62
63 Mapper(Configuration cf) {
64 var map = new HashMap<String, Integer>();
65 for (ResolvedModule resolvedModule : cf.modules()) {
66 String mn = resolvedModule.name();
67 if (!Modules.bootModules.contains(mn)) {
68 if (Modules.platformModules.contains(mn)) {
69 map.put(mn, PLATFORM_LOADER_INDEX);
70 } else {
71 map.put(mn, APP_LOADER_INDEX);
72 }
73 }
74 }
75 this.map = map;
76 }
77
78 @Override
79 public ClassLoader apply(String name) {
80 Integer loader = map.get(name);
81 if (loader == APP_LOADER_INDEX) {
82 return APP_CLASSLOADER;
83 } else if (loader == PLATFORM_LOADER_INDEX) {
84 return PLATFORM_CLASSLOADER;
85 } else { // BOOT_LOADER_INDEX
86 return null;
87 }
88 }
89 }
90
91 /**
92 * Returns the names of the modules defined to the boot loader.
93 */
94 public static Set<String> bootModules() {
95 return Modules.bootModules;
96 }
97
98 /**
99 * Returns the names of the modules defined to the platform loader.
100 */
101 public static Set<String> platformModules() {
102 return Modules.platformModules;
103 }
104
105 private static class Modules {
106 // list of boot modules is generated at build time.
107 private static final Set<String> bootModules =
108 Set.of(new String[] { "@@BOOT_MODULE_NAMES@@" });
109
110 // list of platform modules is generated at build time.
111 private static final Set<String> platformModules =
112 Set.of(new String[] { "@@PLATFORM_MODULE_NAMES@@" });
113 }
114
115 /**
116 * Returns a function to map modules in the given configuration to the
117 * built-in class loaders.
118 */
119 static Mapper mappingFunction(Configuration cf) {
120 return new Mapper(cf);
121 }
122 }
|