37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.NoSuchElementException;
46 import java.util.Objects;
47 import java.util.Optional;
48 import java.util.Set;
49 import java.util.function.Function;
50 import java.util.stream.Collectors;
51
52 import jdk.internal.loader.BootLoader;
53 import jdk.internal.loader.BuiltinClassLoader;
54 import jdk.internal.misc.JavaLangAccess;
55 import jdk.internal.misc.JavaLangModuleAccess;
56 import jdk.internal.misc.SharedSecrets;
57 import jdk.internal.perf.PerfCounter;
58
59 /**
60 * Initializes/boots the module system.
61 *
62 * The {@link #boot() boot} method is called early in the startup to initialize
63 * the module system. In summary, the boot method creates a Configuration by
64 * resolving a set of module names specified via the launcher (or equivalent)
65 * -m and --add-modules options. The modules are located on a module path that
66 * is constructed from the upgrade module path, system modules, and application
67 * module path. The Configuration is instantiated as the boot layer with each
68 * module in the configuration defined to a class loader.
69 */
70
71 public final class ModuleBootstrap {
72 private ModuleBootstrap() { }
73
74 private static final String JAVA_BASE = "java.base";
75
76 // the token for "all default modules"
155 PrintStream traceOutput = null;
156 String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
157 if (trace != null && Boolean.parseBoolean(trace))
158 traceOutput = System.out;
159
160
161 // Step 1: The observable system modules, either all system modules
162 // or the system modules pre-generated for the initial module (the
163 // initial module may be the unnamed module). If the system modules
164 // are pre-generated for the initial module then resolution can be
165 // skipped.
166
167 long t1 = System.nanoTime();
168
169 SystemModules systemModules = null;
170 ModuleFinder systemModuleFinder;
171
172 boolean haveModulePath = (appModulePath != null || upgradeModulePath != null);
173 boolean needResolution = true;
174
175 if (!haveModulePath && addModules.isEmpty() && limitModules.isEmpty()) {
176 systemModules = SystemModuleFinders.systemModules(mainModule);
177 if (systemModules != null && !isPatched && (traceOutput == null)) {
178 needResolution = false;
179 }
180 }
181 if (systemModules == null) {
182 // all system modules are observable
183 systemModules = SystemModuleFinders.allSystemModules();
184 }
185 if (systemModules != null) {
186 // images build
187 systemModuleFinder = SystemModuleFinders.of(systemModules);
188 } else {
189 // exploded build or testing
190 systemModules = new ExplodedSystemModules();
191 systemModuleFinder = SystemModuleFinders.ofSystem();
192 }
193
194 Counters.add("jdk.module.boot.1.systemModulesTime", t1);
195
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);
|
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.NoSuchElementException;
46 import java.util.Objects;
47 import java.util.Optional;
48 import java.util.Set;
49 import java.util.function.Function;
50 import java.util.stream.Collectors;
51
52 import jdk.internal.loader.BootLoader;
53 import jdk.internal.loader.BuiltinClassLoader;
54 import jdk.internal.misc.JavaLangAccess;
55 import jdk.internal.misc.JavaLangModuleAccess;
56 import jdk.internal.misc.SharedSecrets;
57 import jdk.internal.misc.VM;
58 import jdk.internal.perf.PerfCounter;
59
60 /**
61 * Initializes/boots the module system.
62 *
63 * The {@link #boot() boot} method is called early in the startup to initialize
64 * the module system. In summary, the boot method creates a Configuration by
65 * resolving a set of module names specified via the launcher (or equivalent)
66 * -m and --add-modules options. The modules are located on a module path that
67 * is constructed from the upgrade module path, system modules, and application
68 * module path. The Configuration is instantiated as the boot layer with each
69 * module in the configuration defined to a class loader.
70 */
71
72 public final class ModuleBootstrap {
73 private ModuleBootstrap() { }
74
75 private static final String JAVA_BASE = "java.base";
76
77 // the token for "all default modules"
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);
|