155
156 PrintStream traceOutput = null;
157 String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
158 if (trace != null && Boolean.parseBoolean(trace))
159 traceOutput = System.out;
160
161
162 // Step 1: The observable system modules, either all system modules
163 // or the system modules pre-generated for the initial module (the
164 // initial module may be the unnamed module). If the system modules
165 // are pre-generated for the initial module then resolution can be
166 // skipped.
167
168 long t1 = System.nanoTime();
169
170 SystemModules systemModules = null;
171 ModuleFinder systemModuleFinder;
172
173 boolean haveModulePath = (appModulePath != null || upgradeModulePath != null);
174 boolean needResolution = true;
175
176 // If the java heap was archived at CDS dump time and the environment
177 // at dump time matches the current environment then use the archived
178 // system modules and finder.
179 ArchivedModuleGraph archivedModuleGraph = ArchivedModuleGraph.get(mainModule);
180 if (archivedModuleGraph != null
181 && !haveModulePath
182 && addModules.isEmpty()
183 && limitModules.isEmpty()
184 && !isPatched) {
185 systemModules = archivedModuleGraph.systemModules();
186 systemModuleFinder = archivedModuleGraph.finder();
187 needResolution = (traceOutput != null);
188 } else {
189 boolean canArchive = false;
190 if (!haveModulePath && addModules.isEmpty() && limitModules.isEmpty()) {
191 systemModules = SystemModuleFinders.systemModules(mainModule);
192 if (systemModules != null && !isPatched) {
193 needResolution = (traceOutput != null);
194 canArchive = true;
195 }
196 }
197 if (systemModules == null) {
198 // all system modules are observable
199 systemModules = SystemModuleFinders.allSystemModules();
200 }
201 if (systemModules != null) {
202 // images build
203 systemModuleFinder = SystemModuleFinders.of(systemModules);
204 } else {
205 // exploded build or testing
206 systemModules = new ExplodedSystemModules();
207 systemModuleFinder = SystemModuleFinders.ofSystem();
208 }
209
210 // Module graph can be archived at CDS dump time. Only allow the
211 // unnamed module case for now.
212 if (canArchive && (mainModule == null)) {
213 ArchivedModuleGraph.archive(mainModule, systemModules, systemModuleFinder);
214 }
215 }
216
217 Counters.add("jdk.module.boot.1.systemModulesTime", t1);
218
219
220 // Step 2: Define and load java.base. This patches all classes loaded
221 // to date so that they are members of java.base. Once java.base is
222 // loaded then resources in java.base are available for error messages
223 // needed from here on.
224
225 long t2 = System.nanoTime();
226
227 ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
228 if (base == null)
229 throw new InternalError(JAVA_BASE + " not found");
230 URI baseUri = base.location().orElse(null);
231 if (baseUri == null)
232 throw new InternalError(JAVA_BASE + " does not have a location");
233 BootLoader.loadModule(base);
234 Modules.defineModule(null, base.descriptor(), baseUri);
336 }
337 } else {
338 // no resolution case
339 finder = systemModuleFinder;
340 roots = null;
341 }
342
343 Counters.add("jdk.module.boot.3.optionsAndRootsTime", t3);
344
345 // Step 4: Resolve the root modules, with service binding, to create
346 // the configuration for the boot layer. If resolution is not needed
347 // then create the configuration for the boot layer from the
348 // readability graph created at link time.
349
350 long t4 = System.nanoTime();
351
352 Configuration cf;
353 if (needResolution) {
354 cf = JLMA.resolveAndBind(finder, roots, traceOutput);
355 } else {
356 Map<String, Set<String>> map = systemModules.moduleReads();
357 cf = JLMA.newConfiguration(systemModuleFinder, map);
358 }
359
360 // check that modules specified to --patch-module are resolved
361 if (isPatched) {
362 patcher.patchedModules()
363 .stream()
364 .filter(mn -> !cf.findModule(mn).isPresent())
365 .forEach(mn -> warnUnknownModule(PATCH_MODULE, mn));
366 }
367
368 Counters.add("jdk.module.boot.4.resolveTime", t4);
369
370
371 // Step 5: Map the modules in the configuration to class loaders.
372 // The static configuration provides the mapping of standard and JDK
373 // modules to the boot and platform loaders. All other modules (JDK
374 // tool modules, and both explicit and automatic modules on the
375 // application module path) are defined to the application class
376 // loader.
377
378 long t5 = System.nanoTime();
417
418 // Step 7: Miscellaneous
419
420 // check incubating status
421 if (systemModules.hasIncubatorModules() || haveModulePath) {
422 checkIncubatingStatus(cf);
423 }
424
425 // --add-reads, --add-exports/--add-opens, and --illegal-access
426 long t7 = System.nanoTime();
427 addExtraReads(bootLayer);
428 boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
429 addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
430 Counters.add("jdk.module.boot.7.adjustModulesTime", t7);
431
432 // save module finders for later use
433 if (savedModuleFinder != null) {
434 unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
435 if (savedModuleFinder != finder)
436 limitedFinder = new SafeModuleFinder(finder);
437 }
438
439 // total time to initialize
440 Counters.add("jdk.module.boot.totalTime", t0);
441 Counters.publish();
442
443 return bootLayer;
444 }
445
446 /**
447 * Load/register the modules to the built-in class loaders.
448 */
449 private static void loadModules(Configuration cf,
450 Function<String, ClassLoader> clf) {
451 for (ResolvedModule resolvedModule : cf.modules()) {
452 ModuleReference mref = resolvedModule.reference();
453 String name = resolvedModule.name();
454 ClassLoader loader = clf.apply(name);
455 if (loader == null) {
456 // skip java.base as it is already loaded
|
155
156 PrintStream traceOutput = null;
157 String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
158 if (trace != null && Boolean.parseBoolean(trace))
159 traceOutput = System.out;
160
161
162 // Step 1: The observable system modules, either all system modules
163 // or the system modules pre-generated for the initial module (the
164 // initial module may be the unnamed module). If the system modules
165 // are pre-generated for the initial module then resolution can be
166 // skipped.
167
168 long t1 = System.nanoTime();
169
170 SystemModules systemModules = null;
171 ModuleFinder systemModuleFinder;
172
173 boolean haveModulePath = (appModulePath != null || upgradeModulePath != null);
174 boolean needResolution = true;
175 boolean canArchive = false;
176
177 // If the java heap was archived at CDS dump time and the environment
178 // at dump time matches the current environment then use the archived
179 // system modules and finder.
180 ArchivedModuleGraph archivedModuleGraph = ArchivedModuleGraph.get(mainModule);
181 if (archivedModuleGraph != null
182 && !haveModulePath
183 && addModules.isEmpty()
184 && limitModules.isEmpty()
185 && !isPatched) {
186 systemModules = archivedModuleGraph.systemModules();
187 systemModuleFinder = archivedModuleGraph.finder();
188 needResolution = (traceOutput != null);
189 } else {
190 if (!haveModulePath && addModules.isEmpty() && limitModules.isEmpty()) {
191 systemModules = SystemModuleFinders.systemModules(mainModule);
192 if (systemModules != null && !isPatched) {
193 needResolution = (traceOutput != null);
194 canArchive = true;
195 }
196 }
197 if (systemModules == null) {
198 // all system modules are observable
199 systemModules = SystemModuleFinders.allSystemModules();
200 }
201 if (systemModules != null) {
202 // images build
203 systemModuleFinder = SystemModuleFinders.of(systemModules);
204 } else {
205 // exploded build or testing
206 systemModules = new ExplodedSystemModules();
207 systemModuleFinder = SystemModuleFinders.ofSystem();
208 }
209 }
210
211 Counters.add("jdk.module.boot.1.systemModulesTime", t1);
212
213
214 // Step 2: Define and load java.base. This patches all classes loaded
215 // to date so that they are members of java.base. Once java.base is
216 // loaded then resources in java.base are available for error messages
217 // needed from here on.
218
219 long t2 = System.nanoTime();
220
221 ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
222 if (base == null)
223 throw new InternalError(JAVA_BASE + " not found");
224 URI baseUri = base.location().orElse(null);
225 if (baseUri == null)
226 throw new InternalError(JAVA_BASE + " does not have a location");
227 BootLoader.loadModule(base);
228 Modules.defineModule(null, base.descriptor(), baseUri);
330 }
331 } else {
332 // no resolution case
333 finder = systemModuleFinder;
334 roots = null;
335 }
336
337 Counters.add("jdk.module.boot.3.optionsAndRootsTime", t3);
338
339 // Step 4: Resolve the root modules, with service binding, to create
340 // the configuration for the boot layer. If resolution is not needed
341 // then create the configuration for the boot layer from the
342 // readability graph created at link time.
343
344 long t4 = System.nanoTime();
345
346 Configuration cf;
347 if (needResolution) {
348 cf = JLMA.resolveAndBind(finder, roots, traceOutput);
349 } else {
350 if (archivedModuleGraph != null) {
351 cf = archivedModuleGraph.configuration();
352 } else {
353 Map<String, Set<String>> map = systemModules.moduleReads();
354 cf = JLMA.newConfiguration(systemModuleFinder, map);
355 }
356 }
357
358 // check that modules specified to --patch-module are resolved
359 if (isPatched) {
360 patcher.patchedModules()
361 .stream()
362 .filter(mn -> !cf.findModule(mn).isPresent())
363 .forEach(mn -> warnUnknownModule(PATCH_MODULE, mn));
364 }
365
366 Counters.add("jdk.module.boot.4.resolveTime", t4);
367
368
369 // Step 5: Map the modules in the configuration to class loaders.
370 // The static configuration provides the mapping of standard and JDK
371 // modules to the boot and platform loaders. All other modules (JDK
372 // tool modules, and both explicit and automatic modules on the
373 // application module path) are defined to the application class
374 // loader.
375
376 long t5 = System.nanoTime();
415
416 // Step 7: Miscellaneous
417
418 // check incubating status
419 if (systemModules.hasIncubatorModules() || haveModulePath) {
420 checkIncubatingStatus(cf);
421 }
422
423 // --add-reads, --add-exports/--add-opens, and --illegal-access
424 long t7 = System.nanoTime();
425 addExtraReads(bootLayer);
426 boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
427 addIllegalAccess(upgradeModulePath, systemModules, bootLayer, extraExportsOrOpens);
428 Counters.add("jdk.module.boot.7.adjustModulesTime", t7);
429
430 // save module finders for later use
431 if (savedModuleFinder != null) {
432 unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
433 if (savedModuleFinder != finder)
434 limitedFinder = new SafeModuleFinder(finder);
435 }
436
437 // Module graph can be archived at CDS dump time. Only allow the
438 // unnamed module case for now.
439 if (canArchive && (mainModule == null)) {
440 ArchivedModuleGraph.archive(mainModule, systemModules,
441 systemModuleFinder, cf);
442 }
443
444 // total time to initialize
445 Counters.add("jdk.module.boot.totalTime", t0);
446 Counters.publish();
447
448 return bootLayer;
449 }
450
451 /**
452 * Load/register the modules to the built-in class loaders.
453 */
454 private static void loadModules(Configuration cf,
455 Function<String, ClassLoader> clf) {
456 for (ResolvedModule resolvedModule : cf.modules()) {
457 ModuleReference mref = resolvedModule.reference();
458 String name = resolvedModule.name();
459 ClassLoader loader = clf.apply(name);
460 if (loader == null) {
461 // skip java.base as it is already loaded
|