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