40 import java.util.ArrayDeque;
41 import java.util.Collections;
42 import java.util.Deque;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Objects;
48 import java.util.Optional;
49 import java.util.Set;
50 import java.util.Spliterator;
51 import java.util.function.Consumer;
52 import java.util.function.Supplier;
53 import java.util.stream.Stream;
54 import java.util.stream.StreamSupport;
55
56 import jdk.internal.jimage.ImageReader;
57 import jdk.internal.jimage.SystemImageReader;
58 import jdk.internal.access.JavaNetUriAccess;
59 import jdk.internal.access.SharedSecrets;
60 import jdk.internal.util.StaticProperty;
61 import jdk.internal.module.ModuleHashes.HashSupplier;
62
63 /**
64 * The factory for SystemModules objects and for creating ModuleFinder objects
65 * that find modules in the runtime image.
66 *
67 * This class supports initializing the module system when the runtime is an
68 * images build, an exploded build, or an images build with java.base patched
69 * by an exploded java.base. It also supports a testing mode that re-parses
70 * the module-info.class resources in the run-time image.
71 */
72
73 public final class SystemModuleFinders {
74 private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
75
76 private static final boolean USE_FAST_PATH;
77 static {
78 String value = System.getProperty("jdk.system.module.finder.disableFastPath");
79 if (value == null) {
187 SystemModules systemModules = allSystemModules();
188 if (systemModules != null) {
189 finder = of(systemModules);
190 }
191 }
192
193 // fall back to parsing the module-info.class files in image
194 if (finder == null) {
195 finder = ofModuleInfos();
196 }
197
198 cachedSystemModuleFinder = finder;
199 return finder;
200
201 }
202
203 // exploded build (do not cache module finder)
204 Path dir = Path.of(home, "modules");
205 if (!Files.isDirectory(dir))
206 throw new InternalError("Unable to detect the run-time image");
207 return ModulePath.of(ModuleBootstrap.patcher(), dir);
208 }
209
210 /**
211 * Parses the {@code module-info.class} of all modules in the runtime image and
212 * returns a ModuleFinder to find the modules.
213 *
214 * @apiNote The returned ModuleFinder is thread safe.
215 */
216 private static ModuleFinder ofModuleInfos() {
217 // parse the module-info.class in every module
218 Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
219 Map<String, byte[]> nameToHash = new HashMap<>();
220
221 allModuleAttributes().forEach(attrs -> {
222 nameToAttributes.put(attrs.descriptor().name(), attrs);
223 ModuleHashes hashes = attrs.recordedHashes();
224 if (hashes != null) {
225 for (String name : hashes.names()) {
226 nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
227 }
|
40 import java.util.ArrayDeque;
41 import java.util.Collections;
42 import java.util.Deque;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Objects;
48 import java.util.Optional;
49 import java.util.Set;
50 import java.util.Spliterator;
51 import java.util.function.Consumer;
52 import java.util.function.Supplier;
53 import java.util.stream.Stream;
54 import java.util.stream.StreamSupport;
55
56 import jdk.internal.jimage.ImageReader;
57 import jdk.internal.jimage.SystemImageReader;
58 import jdk.internal.access.JavaNetUriAccess;
59 import jdk.internal.access.SharedSecrets;
60 import jdk.internal.misc.PreviewFeatures;
61 import jdk.internal.util.StaticProperty;
62 import jdk.internal.module.ModuleHashes.HashSupplier;
63
64 /**
65 * The factory for SystemModules objects and for creating ModuleFinder objects
66 * that find modules in the runtime image.
67 *
68 * This class supports initializing the module system when the runtime is an
69 * images build, an exploded build, or an images build with java.base patched
70 * by an exploded java.base. It also supports a testing mode that re-parses
71 * the module-info.class resources in the run-time image.
72 */
73
74 public final class SystemModuleFinders {
75 private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
76
77 private static final boolean USE_FAST_PATH;
78 static {
79 String value = System.getProperty("jdk.system.module.finder.disableFastPath");
80 if (value == null) {
188 SystemModules systemModules = allSystemModules();
189 if (systemModules != null) {
190 finder = of(systemModules);
191 }
192 }
193
194 // fall back to parsing the module-info.class files in image
195 if (finder == null) {
196 finder = ofModuleInfos();
197 }
198
199 cachedSystemModuleFinder = finder;
200 return finder;
201
202 }
203
204 // exploded build (do not cache module finder)
205 Path dir = Path.of(home, "modules");
206 if (!Files.isDirectory(dir))
207 throw new InternalError("Unable to detect the run-time image");
208 return ModulePath.of(ModuleBootstrap.patcher(), PreviewFeatures.isEnabled(), dir);
209 }
210
211 /**
212 * Parses the {@code module-info.class} of all modules in the runtime image and
213 * returns a ModuleFinder to find the modules.
214 *
215 * @apiNote The returned ModuleFinder is thread safe.
216 */
217 private static ModuleFinder ofModuleInfos() {
218 // parse the module-info.class in every module
219 Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
220 Map<String, byte[]> nameToHash = new HashMap<>();
221
222 allModuleAttributes().forEach(attrs -> {
223 nameToAttributes.put(attrs.descriptor().name(), attrs);
224 ModuleHashes hashes = attrs.recordedHashes();
225 if (hashes != null) {
226 for (String name : hashes.names()) {
227 nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
228 }
|