119 }
120 }
121
122 /**
123 * Returns the ModuleFinder for the initial configuration.
124 *
125 * @apiNote Used to support "{@code java --list-modules}".
126 */
127 public static ModuleFinder limitedFinder() {
128 ModuleFinder finder = limitedFinder;
129 if (finder == null) {
130 return unlimitedFinder();
131 } else {
132 return finder;
133 }
134 }
135
136 /**
137 * Initialize the module system, returning the boot layer.
138 *
139 * @see java.lang.System#initPhase2()
140 */
141 public static ModuleLayer boot() throws Exception {
142
143 // Step 0: Command line options
144
145 long t0 = System.nanoTime();
146
147 ModuleFinder upgradeModulePath = finderFor("jdk.module.upgrade.path");
148 ModuleFinder appModulePath = finderFor("jdk.module.path");
149 boolean isPatched = patcher.hasPatches();
150
151 String mainModule = System.getProperty("jdk.module.main");
152 Set<String> addModules = addModules();
153 Set<String> limitModules = limitModules();
154
155 PrintStream traceOutput = null;
156 String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
157 if (trace != null && Boolean.parseBoolean(trace))
158 traceOutput = System.out;
159
196
197 // Step 2: Define and load java.base. This patches all classes loaded
198 // to date so that they are members of java.base. Once java.base is
199 // loaded then resources in java.base are available for error messages
200 // needed from here on.
201
202 long t2 = System.nanoTime();
203
204 ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
205 if (base == null)
206 throw new InternalError(JAVA_BASE + " not found");
207 URI baseUri = base.location().orElse(null);
208 if (baseUri == null)
209 throw new InternalError(JAVA_BASE + " does not have a location");
210 BootLoader.loadModule(base);
211 Modules.defineModule(null, base.descriptor(), baseUri);
212
213 Counters.add("jdk.module.boot.2.defineBaseTime", t2);
214
215
216 // Step 2a: If --validate-modules is specified then the VM needs to
217 // start with only system modules, all other options are ignored.
218
219 if (getAndRemoveProperty("jdk.module.validation") != null) {
220 return createBootLayerForValidation();
221 }
222
223
224 // Step 3: If resolution is needed then create the module finder and
225 // the set of root modules to resolve.
226
227 long t3 = System.nanoTime();
228
229 ModuleFinder savedModuleFinder = null;
230 ModuleFinder finder;
231 Set<String> roots;
232 if (needResolution) {
233
234 // upgraded modules override the modules in the run-time image
235 if (upgradeModulePath != null)
236 systemModuleFinder = ModuleFinder.compose(upgradeModulePath,
237 systemModuleFinder);
238
239 // The module finder: [--upgrade-module-path] system [--module-path]
240 if (appModulePath != null) {
403 addExtraReads(bootLayer);
404 boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
405 addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
406 Counters.add("jdk.module.boot.7.adjustModulesTime", t7);
407
408 // save module finders for later use
409 if (savedModuleFinder != null) {
410 unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
411 if (savedModuleFinder != finder)
412 limitedFinder = new SafeModuleFinder(finder);
413 }
414
415 // total time to initialize
416 Counters.add("jdk.module.boot.totalTime", t0);
417 Counters.publish();
418
419 return bootLayer;
420 }
421
422 /**
423 * Create a boot module layer for validation that resolves all
424 * system modules.
425 */
426 private static ModuleLayer createBootLayerForValidation() {
427 Set<String> allSystem = ModuleFinder.ofSystem().findAll()
428 .stream()
429 .map(ModuleReference::descriptor)
430 .map(ModuleDescriptor::name)
431 .collect(Collectors.toSet());
432
433 Configuration cf = SharedSecrets.getJavaLangModuleAccess()
434 .resolveAndBind(ModuleFinder.ofSystem(),
435 allSystem,
436 null);
437
438 Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
439 return ModuleLayer.empty().defineModules(cf, clf);
440 }
441
442 /**
443 * Load/register the modules to the built-in class loaders.
444 */
445 private static void loadModules(Configuration cf,
446 Function<String, ClassLoader> clf) {
447 for (ResolvedModule resolvedModule : cf.modules()) {
448 ModuleReference mref = resolvedModule.reference();
449 String name = resolvedModule.name();
450 ClassLoader loader = clf.apply(name);
451 if (loader == null) {
452 // skip java.base as it is already loaded
453 if (!name.equals(JAVA_BASE)) {
454 BootLoader.loadModule(mref);
455 }
456 } else if (loader instanceof BuiltinClassLoader) {
457 ((BuiltinClassLoader) loader).loadModule(mref);
458 }
459 }
460 }
461
462 /**
|
119 }
120 }
121
122 /**
123 * Returns the ModuleFinder for the initial configuration.
124 *
125 * @apiNote Used to support "{@code java --list-modules}".
126 */
127 public static ModuleFinder limitedFinder() {
128 ModuleFinder finder = limitedFinder;
129 if (finder == null) {
130 return unlimitedFinder();
131 } else {
132 return finder;
133 }
134 }
135
136 /**
137 * Initialize the module system, returning the boot layer.
138 *
139 * @see java.lang.System#initPhase2(boolean, boolean)
140 */
141 public static ModuleLayer boot() throws Exception {
142
143 // Step 0: Command line options
144
145 long t0 = System.nanoTime();
146
147 ModuleFinder upgradeModulePath = finderFor("jdk.module.upgrade.path");
148 ModuleFinder appModulePath = finderFor("jdk.module.path");
149 boolean isPatched = patcher.hasPatches();
150
151 String mainModule = System.getProperty("jdk.module.main");
152 Set<String> addModules = addModules();
153 Set<String> limitModules = limitModules();
154
155 PrintStream traceOutput = null;
156 String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
157 if (trace != null && Boolean.parseBoolean(trace))
158 traceOutput = System.out;
159
196
197 // Step 2: Define and load java.base. This patches all classes loaded
198 // to date so that they are members of java.base. Once java.base is
199 // loaded then resources in java.base are available for error messages
200 // needed from here on.
201
202 long t2 = System.nanoTime();
203
204 ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
205 if (base == null)
206 throw new InternalError(JAVA_BASE + " not found");
207 URI baseUri = base.location().orElse(null);
208 if (baseUri == null)
209 throw new InternalError(JAVA_BASE + " does not have a location");
210 BootLoader.loadModule(base);
211 Modules.defineModule(null, base.descriptor(), baseUri);
212
213 Counters.add("jdk.module.boot.2.defineBaseTime", t2);
214
215
216 // Step 2a: Scan all modules when --validate-modules specified
217
218 if (getAndRemoveProperty("jdk.module.validation") != null) {
219 int errors = ModulePathValidator.scanAllModules(System.out);
220 if (errors > 0) {
221 fail("Validation of module path failed");
222 }
223 }
224
225
226 // Step 3: If resolution is needed then create the module finder and
227 // the set of root modules to resolve.
228
229 long t3 = System.nanoTime();
230
231 ModuleFinder savedModuleFinder = null;
232 ModuleFinder finder;
233 Set<String> roots;
234 if (needResolution) {
235
236 // upgraded modules override the modules in the run-time image
237 if (upgradeModulePath != null)
238 systemModuleFinder = ModuleFinder.compose(upgradeModulePath,
239 systemModuleFinder);
240
241 // The module finder: [--upgrade-module-path] system [--module-path]
242 if (appModulePath != null) {
405 addExtraReads(bootLayer);
406 boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
407 addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
408 Counters.add("jdk.module.boot.7.adjustModulesTime", t7);
409
410 // save module finders for later use
411 if (savedModuleFinder != null) {
412 unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
413 if (savedModuleFinder != finder)
414 limitedFinder = new SafeModuleFinder(finder);
415 }
416
417 // total time to initialize
418 Counters.add("jdk.module.boot.totalTime", t0);
419 Counters.publish();
420
421 return bootLayer;
422 }
423
424 /**
425 * Load/register the modules to the built-in class loaders.
426 */
427 private static void loadModules(Configuration cf,
428 Function<String, ClassLoader> clf) {
429 for (ResolvedModule resolvedModule : cf.modules()) {
430 ModuleReference mref = resolvedModule.reference();
431 String name = resolvedModule.name();
432 ClassLoader loader = clf.apply(name);
433 if (loader == null) {
434 // skip java.base as it is already loaded
435 if (!name.equals(JAVA_BASE)) {
436 BootLoader.loadModule(mref);
437 }
438 } else if (loader instanceof BuiltinClassLoader) {
439 ((BuiltinClassLoader) loader).loadModule(mref);
440 }
441 }
442 }
443
444 /**
|