1 /*
   2  * Copyright (c) 2014, 2016, 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.io.File;
  29 import java.io.PrintStream;
  30 import java.lang.module.Configuration;
  31 import java.lang.module.ModuleDescriptor;
  32 import java.lang.module.ModuleFinder;
  33 import java.lang.module.ModuleReference;
  34 import java.lang.module.ResolvedModule;
  35 import java.lang.reflect.Layer;
  36 import java.lang.reflect.Module;
  37 import java.net.URI;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.ArrayList;
  41 import java.util.Collections;
  42 import java.util.HashMap;
  43 import java.util.HashSet;
  44 import java.util.List;
  45 import java.util.Map;
  46 import java.util.Optional;
  47 import java.util.Set;
  48 import java.util.function.Function;
  49 
  50 import jdk.internal.loader.BootLoader;
  51 import jdk.internal.loader.BuiltinClassLoader;
  52 import jdk.internal.misc.SharedSecrets;
  53 import jdk.internal.perf.PerfCounter;
  54 
  55 /**
  56  * Initializes/boots the module system.
  57  *
  58  * The {@link #boot() boot} method is called early in the startup to initialize
  59  * the module system. In summary, the boot method creates a Configuration by
  60  * resolving a set of module names specified via the launcher (or equivalent)
  61  * -m and --add-modules options. The modules are located on a module path that
  62  * is constructed from the upgrade module path, system modules, and application
  63  * module path. The Configuration is instantiated as the boot Layer with each
  64  * module in the the configuration defined to one of the built-in class loaders.
  65  */
  66 
  67 public final class ModuleBootstrap {
  68     private ModuleBootstrap() { }
  69 
  70     private static final String JAVA_BASE = "java.base";
  71 
  72     private static final String JAVA_SE = "java.se";
  73 
  74     // the token for "all default modules"
  75     private static final String ALL_DEFAULT = "ALL-DEFAULT";
  76 
  77     // the token for "all unnamed modules"
  78     private static final String ALL_UNNAMED = "ALL-UNNAMED";
  79 
  80     // the token for "all system modules"
  81     private static final String ALL_SYSTEM = "ALL-SYSTEM";
  82 
  83     // the token for "all modules on the module path"
  84     private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
  85 
  86     // The ModulePatcher for the initial configuration
  87     private static final ModulePatcher patcher = initModulePatcher();
  88 
  89     // ModuleFinder for the initial configuration
  90     private static ModuleFinder initialFinder;
  91 
  92     /**
  93      * Returns the ModulePatcher for the initial configuration.
  94      */
  95     public static ModulePatcher patcher() {
  96         return patcher;
  97     }
  98 
  99     /**
 100      * Returns the ModuleFinder for the initial configuration
 101      */
 102     public static ModuleFinder finder() {
 103         assert initialFinder != null;
 104         return initialFinder;
 105     }
 106 
 107     /**
 108      * Initialize the module system, returning the boot Layer.
 109      *
 110      * @see java.lang.System#initPhase2()
 111      */
 112     public static Layer boot() {
 113 
 114         long t0 = System.nanoTime();
 115 
 116         // system modules (may be patched)
 117         ModuleFinder systemModules = ModuleFinder.ofSystem();
 118 
 119         PerfCounters.systemModulesTime.addElapsedTimeFrom(t0);
 120 
 121 
 122         long t1 = System.nanoTime();
 123 
 124         // Once we have the system modules then we define the base module to
 125         // the VM. We do this here so that java.base is defined as early as
 126         // possible and also that resources in the base module can be located
 127         // for error messages that may happen from here on.
 128         ModuleReference base = systemModules.find(JAVA_BASE).orElse(null);
 129         if (base == null)
 130             throw new InternalError(JAVA_BASE + " not found");
 131         URI baseUri = base.location().orElse(null);
 132         if (baseUri == null)
 133             throw new InternalError(JAVA_BASE + " does not have a location");
 134         BootLoader.loadModule(base);
 135         Modules.defineModule(null, base.descriptor(), baseUri);
 136 
 137         PerfCounters.defineBaseTime.addElapsedTimeFrom(t1);
 138 
 139 
 140         long t2 = System.nanoTime();
 141 
 142         // --upgrade-module-path option specified to launcher
 143         ModuleFinder upgradeModulePath
 144             = createModulePathFinder("jdk.module.upgrade.path");
 145         if (upgradeModulePath != null)
 146             systemModules = ModuleFinder.compose(upgradeModulePath, systemModules);
 147 
 148         // --module-path option specified to the launcher
 149         ModuleFinder appModulePath = createModulePathFinder("jdk.module.path");
 150 
 151         // The module finder: [--upgrade-module-path] system [--module-path]
 152         ModuleFinder finder = systemModules;
 153         if (appModulePath != null)
 154             finder = ModuleFinder.compose(finder, appModulePath);
 155 
 156         // The root modules to resolve
 157         Set<String> roots = new HashSet<>();
 158 
 159         // launcher -m option to specify the main/initial module
 160         String mainModule = System.getProperty("jdk.module.main");
 161         if (mainModule != null)
 162             roots.add(mainModule);
 163 
 164         // additional module(s) specified by --add-modules
 165         boolean addAllDefaultModules = false;
 166         boolean addAllSystemModules = false;
 167         boolean addAllApplicationModules = false;
 168         for (String mod: getExtraAddModules()) {
 169             switch (mod) {
 170                 case ALL_DEFAULT:
 171                     addAllDefaultModules = true;
 172                     break;
 173                 case ALL_SYSTEM:
 174                     addAllSystemModules = true;
 175                     break;
 176                 case ALL_MODULE_PATH:
 177                     addAllApplicationModules = true;
 178                     break;
 179                 default :
 180                     roots.add(mod);
 181             }
 182         }
 183 
 184         // --limit-modules
 185         String propValue = getAndRemoveProperty("jdk.module.limitmods");
 186         if (propValue != null) {
 187             Set<String> mods = new HashSet<>();
 188             for (String mod: propValue.split(",")) {
 189                 mods.add(mod);
 190             }
 191             finder = limitFinder(finder, mods, roots);
 192         }
 193 
 194         // If there is no initial module specified then assume that the initial
 195         // module is the unnamed module of the application class loader. This
 196         // is implemented by resolving "java.se" and all (non-java.*) modules
 197         // that export an API. If "java.se" is not observable then all java.*
 198         // modules are resolved. Modules that have the DO_NOT_RESOLVE_BY_DEFAULT
 199         // bit set in their ModuleResolution attribute flags are excluded from
 200         // the default set of roots.
 201         if (mainModule == null || addAllDefaultModules) {
 202             boolean hasJava = false;
 203             if (systemModules.find(JAVA_SE).isPresent()) {
 204                 // java.se is a system module
 205                 if (finder == systemModules || finder.find(JAVA_SE).isPresent()) {
 206                     // java.se is observable
 207                     hasJava = true;
 208                     roots.add(JAVA_SE);
 209                 }
 210             }
 211 
 212             for (ModuleReference mref : systemModules.findAll()) {
 213                 String mn = mref.descriptor().name();
 214                 if (hasJava && mn.startsWith("java."))
 215                     continue;
 216 
 217                 if (ModuleResolution.doNotResolveByDefault(mref))
 218                     continue;
 219 
 220                 // add as root if observable and exports at least one package
 221                 if ((finder == systemModules || finder.find(mn).isPresent())) {
 222                     ModuleDescriptor descriptor = mref.descriptor();
 223                     for (ModuleDescriptor.Exports e : descriptor.exports()) {
 224                         if (!e.isQualified()) {
 225                             roots.add(mn);
 226                             break;
 227                         }
 228                     }
 229                 }
 230             }
 231         }
 232 
 233         // If `--add-modules ALL-SYSTEM` is specified then all observable system
 234         // modules will be resolved.
 235         if (addAllSystemModules) {
 236             ModuleFinder f = finder;  // observable modules
 237             systemModules.findAll()
 238                 .stream()
 239                 .filter(mref -> !ModuleResolution.doNotResolveByDefault(mref))
 240                 .map(ModuleReference::descriptor)
 241                 .map(ModuleDescriptor::name)
 242                 .filter(mn -> f.find(mn).isPresent())  // observable
 243                 .forEach(mn -> roots.add(mn));
 244         }
 245 
 246         // If `--add-modules ALL-MODULE-PATH` is specified then all observable
 247         // modules on the application module path will be resolved.
 248         if (appModulePath != null && addAllApplicationModules) {
 249             ModuleFinder f = finder;  // observable modules
 250             appModulePath.findAll()
 251                 .stream()
 252                 .map(ModuleReference::descriptor)
 253                 .map(ModuleDescriptor::name)
 254                 .filter(mn -> f.find(mn).isPresent())  // observable
 255                 .forEach(mn -> roots.add(mn));
 256         }
 257 
 258         PerfCounters.optionsAndRootsTime.addElapsedTimeFrom(t2);
 259 
 260 
 261         long t3 = System.nanoTime();
 262 
 263         // determine if post resolution checks are needed
 264         boolean needPostResolutionChecks = true;
 265         if (baseUri.getScheme().equals("jrt")   // toLowerCase not needed here
 266                 && (upgradeModulePath == null)
 267                 && (appModulePath == null)
 268                 && (patcher.isEmpty())) {
 269             needPostResolutionChecks = false;
 270         }
 271 
 272         PrintStream traceOutput = null;
 273         if (Boolean.getBoolean("jdk.launcher.traceResolver"))
 274             traceOutput = System.out;
 275 
 276         // run the resolver to create the configuration
 277         Configuration cf = SharedSecrets.getJavaLangModuleAccess()
 278                 .resolveRequiresAndUses(finder,
 279                                         roots,
 280                                         needPostResolutionChecks,
 281                                         traceOutput);
 282 
 283         // time to create configuration
 284         PerfCounters.resolveTime.addElapsedTimeFrom(t3);
 285 
 286         // check module names and incubating status
 287         checkModuleNamesAndStatus(cf);
 288 
 289         // mapping of modules to class loaders
 290         Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
 291 
 292         // check that all modules to be mapped to the boot loader will be
 293         // loaded from the runtime image
 294         if (needPostResolutionChecks) {
 295             for (ResolvedModule resolvedModule : cf.modules()) {
 296                 ModuleReference mref = resolvedModule.reference();
 297                 String name = mref.descriptor().name();
 298                 ClassLoader cl = clf.apply(name);
 299                 if (cl == null) {
 300 
 301                     if (upgradeModulePath != null
 302                             && upgradeModulePath.find(name).isPresent())
 303                         fail(name + ": cannot be loaded from upgrade module path");
 304 
 305                     if (!systemModules.find(name).isPresent())
 306                         fail(name + ": cannot be loaded from application module path");
 307                 }
 308             }
 309 
 310             // check if module specified in --patch-module is present
 311             for (String mn: patcher.patchedModules()) {
 312                 if (!cf.findModule(mn).isPresent()) {
 313                     warnUnknownModule(PATCH_MODULE, mn);
 314                 }
 315             }
 316         }
 317 
 318         // if needed check that there are no split packages in the set of
 319         // resolved modules for the boot layer
 320         if (SystemModules.hasSplitPackages() || needPostResolutionChecks) {
 321                 Map<String, String> packageToModule = new HashMap<>();
 322                 for (ResolvedModule resolvedModule : cf.modules()) {
 323                     ModuleDescriptor descriptor =
 324                         resolvedModule.reference().descriptor();
 325                     String name = descriptor.name();
 326                     for (String p : descriptor.packages()) {
 327                         String other = packageToModule.putIfAbsent(p, name);
 328                         if (other != null) {
 329                             fail("Package " + p + " in both module "
 330                                  + name + " and module " + other);
 331                         }
 332                     }
 333                 }
 334             }
 335 
 336         long t4 = System.nanoTime();
 337 
 338         // define modules to VM/runtime
 339         Layer bootLayer = Layer.empty().defineModules(cf, clf);
 340 
 341         PerfCounters.layerCreateTime.addElapsedTimeFrom(t4);
 342 
 343 
 344         long t5 = System.nanoTime();
 345 
 346         // define the module to its class loader, except java.base
 347         for (ResolvedModule resolvedModule : cf.modules()) {
 348             ModuleReference mref = resolvedModule.reference();
 349             String name = mref.descriptor().name();
 350             ClassLoader cl = clf.apply(name);
 351             if (cl == null) {
 352                 if (!name.equals(JAVA_BASE)) BootLoader.loadModule(mref);
 353             } else {
 354                 ((BuiltinClassLoader)cl).loadModule(mref);
 355             }
 356         }
 357 
 358         PerfCounters.loadModulesTime.addElapsedTimeFrom(t5);
 359 
 360 
 361         // --add-reads, -add-exports/-add-opens
 362         addExtraReads(bootLayer);
 363         addExtraExportsAndOpens(bootLayer);
 364 
 365         // total time to initialize
 366         PerfCounters.bootstrapTime.addElapsedTimeFrom(t0);
 367 
 368         // remember the ModuleFinder
 369         initialFinder = finder;
 370 
 371         return bootLayer;
 372     }
 373 
 374     /**
 375      * Returns a ModuleFinder that limits observability to the given root
 376      * modules, their transitive dependences, plus a set of other modules.
 377      */
 378     private static ModuleFinder limitFinder(ModuleFinder finder,
 379                                             Set<String> roots,
 380                                             Set<String> otherMods)
 381     {
 382         // resolve all root modules
 383         Configuration cf = Configuration.empty()
 384                 .resolveRequires(finder,
 385                                  ModuleFinder.of(),
 386                                  roots);
 387 
 388         // module name -> reference
 389         Map<String, ModuleReference> map = new HashMap<>();
 390 
 391         // root modules and their transitive dependences
 392         cf.modules().stream()
 393             .map(ResolvedModule::reference)
 394             .forEach(mref -> map.put(mref.descriptor().name(), mref));
 395 
 396         // additional modules
 397         otherMods.stream()
 398             .map(finder::find)
 399             .flatMap(Optional::stream)
 400             .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
 401 
 402         // set of modules that are observable
 403         Set<ModuleReference> mrefs = new HashSet<>(map.values());
 404 
 405         return new ModuleFinder() {
 406             @Override
 407             public Optional<ModuleReference> find(String name) {
 408                 return Optional.ofNullable(map.get(name));
 409             }
 410             @Override
 411             public Set<ModuleReference> findAll() {
 412                 return mrefs;
 413             }
 414         };
 415     }
 416 
 417     /**
 418      * Creates a finder from the module path that is the value of the given
 419      * system property.
 420      */
 421     private static ModuleFinder createModulePathFinder(String prop) {
 422         String s = System.getProperty(prop);
 423         if (s == null) {
 424             return null;
 425         } else {
 426             String[] dirs = s.split(File.pathSeparator);
 427             Path[] paths = new Path[dirs.length];
 428             int i = 0;
 429             for (String dir: dirs) {
 430                 paths[i++] = Paths.get(dir);
 431             }
 432             return ModuleFinder.of(paths);
 433         }
 434     }
 435 
 436 
 437     /**
 438      * Initialize the module patcher for the initial configuration passed on the
 439      * value of the --patch-module options.
 440      */
 441     private static ModulePatcher initModulePatcher() {
 442         Map<String, List<String>> map = decode("jdk.module.patch.",
 443                                                File.pathSeparator,
 444                                                false);
 445         return new ModulePatcher(map);
 446     }
 447 
 448     /**
 449      * Returns the set of module names specified via --add-modules options
 450      * on the command line
 451      */
 452     private static Set<String> getExtraAddModules() {
 453         String prefix = "jdk.module.addmods.";
 454         int index = 0;
 455 
 456         // the system property is removed after decoding
 457         String value = getAndRemoveProperty(prefix + index);
 458         if (value == null) {
 459             return Collections.emptySet();
 460         }
 461 
 462         Set<String> modules = new HashSet<>();
 463         while (value != null) {
 464             for (String s : value.split(",")) {
 465                 if (s.length() > 0) modules.add(s);
 466             }
 467             index++;
 468             value = getAndRemoveProperty(prefix + index);
 469         }
 470 
 471         return modules;
 472     }
 473 
 474     /**
 475      * Process the --add-reads options to add any additional read edges that
 476      * are specified on the command-line.
 477      */
 478     private static void addExtraReads(Layer bootLayer) {
 479 
 480         // decode the command line options
 481         Map<String, List<String>> map = decode("jdk.module.addreads.");
 482         if (map.isEmpty())
 483             return;
 484 
 485         for (Map.Entry<String, List<String>> e : map.entrySet()) {
 486 
 487             // the key is $MODULE
 488             String mn = e.getKey();
 489             Optional<Module> om = bootLayer.findModule(mn);
 490             if (!om.isPresent()) {
 491                 warnUnknownModule(ADD_READS, mn);
 492                 continue;
 493             }
 494             Module m = om.get();
 495 
 496             // the value is the set of other modules (by name)
 497             for (String name : e.getValue()) {
 498                 if (ALL_UNNAMED.equals(name)) {
 499                     Modules.addReadsAllUnnamed(m);
 500                 } else {
 501                     om = bootLayer.findModule(name);
 502                     if (om.isPresent()) {
 503                         Modules.addReads(m, om.get());
 504                     } else {
 505                         warnUnknownModule(ADD_READS, name);
 506                     }
 507                 }
 508             }
 509         }
 510     }
 511 
 512     /**
 513      * Process the --add-exports and --add-opens options to export/open
 514      * additional packages specified on the command-line.
 515      */
 516     private static void addExtraExportsAndOpens(Layer bootLayer) {
 517 
 518         // --add-exports
 519         String prefix = "jdk.module.addexports.";
 520         Map<String, List<String>> extraExports = decode(prefix);
 521         if (!extraExports.isEmpty()) {
 522             addExtraExportsOrOpens(bootLayer, extraExports, false);
 523         }
 524 
 525         // --add-opens
 526         prefix = "jdk.module.addopens.";
 527         Map<String, List<String>> extraOpens = decode(prefix);
 528         if (!extraOpens.isEmpty()) {
 529             addExtraExportsOrOpens(bootLayer, extraOpens, true);
 530         }
 531     }
 532 
 533     private static void addExtraExportsOrOpens(Layer bootLayer,
 534                                                Map<String, List<String>> map,
 535                                                boolean opens)
 536     {
 537         String option = opens ? ADD_OPENS : ADD_EXPORTS;
 538         for (Map.Entry<String, List<String>> e : map.entrySet()) {
 539 
 540             // the key is $MODULE/$PACKAGE
 541             String key = e.getKey();
 542             String[] s = key.split("/");
 543             if (s.length != 2)
 544                 fail(unableToParse(option,  "<module>/<package>", key));
 545 
 546             String mn = s[0];
 547             String pn = s[1];
 548             if (mn.isEmpty() || pn.isEmpty())
 549                 fail(unableToParse(option,  "<module>/<package>", key));
 550 
 551             // The exporting module is in the boot layer
 552             Module m;
 553             Optional<Module> om = bootLayer.findModule(mn);
 554             if (!om.isPresent()) {
 555                 warnUnknownModule(option, mn);
 556                 continue;
 557             }
 558 
 559             m = om.get();
 560 
 561             if (!m.getDescriptor().packages().contains(pn)) {
 562                 warn("package " + pn + " not in " + mn);
 563                 continue;
 564             }
 565 
 566             // the value is the set of modules to export to (by name)
 567             for (String name : e.getValue()) {
 568                 boolean allUnnamed = false;
 569                 Module other = null;
 570                 if (ALL_UNNAMED.equals(name)) {
 571                     allUnnamed = true;
 572                 } else {
 573                     om = bootLayer.findModule(name);
 574                     if (om.isPresent()) {
 575                         other = om.get();
 576                     } else {
 577                         warnUnknownModule(option, name);
 578                         continue;
 579                     }
 580                 }
 581                 if (allUnnamed) {
 582                     if (opens) {
 583                         Modules.addOpensToAllUnnamed(m, pn);
 584                     } else {
 585                         Modules.addExportsToAllUnnamed(m, pn);
 586                     }
 587                 } else {
 588                     if (opens) {
 589                         Modules.addOpens(m, pn, other);
 590                     } else {
 591                         Modules.addExports(m, pn, other);
 592                     }
 593                 }
 594 
 595             }
 596         }
 597     }
 598 
 599     /**
 600      * Decodes the values of --add-reads, -add-exports, --add-opens or
 601      * --patch-modules options that are encoded in system properties.
 602      *
 603      * @param prefix the system property prefix
 604      * @praam regex the regex for splitting the RHS of the option value
 605      */
 606     private static Map<String, List<String>> decode(String prefix,
 607                                                     String regex,
 608                                                     boolean allowDuplicates) {
 609         int index = 0;
 610         // the system property is removed after decoding
 611         String value = getAndRemoveProperty(prefix + index);
 612         if (value == null)
 613             return Collections.emptyMap();
 614 
 615         Map<String, List<String>> map = new HashMap<>();
 616 
 617         while (value != null) {
 618 
 619             int pos = value.indexOf('=');
 620             if (pos == -1)
 621                 fail(unableToParse(option(prefix), "<module>=<value>", value));
 622             if (pos == 0)
 623                 fail(unableToParse(option(prefix), "<module>=<value>", value));
 624 
 625             // key is <module> or <module>/<package>
 626             String key = value.substring(0, pos);
 627 
 628             String rhs = value.substring(pos+1);
 629             if (rhs.isEmpty())
 630                 fail(unableToParse(option(prefix), "<module>=<value>", value));
 631 
 632             // value is <module>(,<module>)* or <file>(<pathsep><file>)*
 633             if (!allowDuplicates && map.containsKey(key))
 634                 fail(key + " specified more than once in " + option(prefix));
 635             List<String> values = map.computeIfAbsent(key, k -> new ArrayList<>());
 636             int ntargets = 0;
 637             for (String s : rhs.split(regex)) {
 638                 if (s.length() > 0) {
 639                     values.add(s);
 640                     ntargets++;
 641                 }
 642             }
 643             if (ntargets == 0)
 644                 fail("Target must be specified: " + option(prefix) + " " + value);
 645 
 646             index++;
 647             value = getAndRemoveProperty(prefix + index);
 648         }
 649 
 650         return map;
 651     }
 652 
 653     /**
 654      * Decodes the values of --add-reads, -add-exports or --add-opens
 655      * which use the "," to separate the RHS of the option value.
 656      */
 657     private static Map<String, List<String>> decode(String prefix) {
 658         return decode(prefix, ",", true);
 659     }
 660 
 661     /**
 662      * Gets and remove the named system property
 663      */
 664     private static String getAndRemoveProperty(String key) {
 665         return (String)System.getProperties().remove(key);
 666     }
 667 
 668     /**
 669      * Checks the names and resolution bit of each module in the configuration,
 670      * emitting warnings if needed.
 671      */
 672     private static void checkModuleNamesAndStatus(Configuration cf) {
 673         String incubating = null;
 674         for (ResolvedModule rm : cf.modules()) {
 675             ModuleReference mref = rm.reference();
 676             String mn = mref.descriptor().name();
 677 
 678             // emit warning if module name ends with a non-Java letter
 679             if (!Checks.hasLegalModuleNameLastCharacter(mn))
 680                 warn("Module name \"" + mn + "\" may soon be illegal");
 681 
 682             // emit warning if the WARN_INCUBATING module resolution bit set
 683             if (ModuleResolution.hasIncubatingWarning(mref)) {
 684                 if (incubating == null) {
 685                     incubating = mn;
 686                 } else {
 687                     incubating += ", " + mn;
 688                 }
 689             }
 690         }
 691         if (incubating != null)
 692             warn("Using incubator modules: " + incubating);
 693     }
 694 
 695     /**
 696      * Throws a RuntimeException with the given message
 697      */
 698     static void fail(String m) {
 699         throw new RuntimeException(m);
 700     }
 701 
 702     static void warn(String m) {
 703         System.err.println("WARNING: " + m);
 704     }
 705 
 706     static void warnUnknownModule(String option, String mn) {
 707         warn("Unknown module: " + mn + " specified in " + option);
 708     }
 709 
 710     static String unableToParse(String option, String text, String value) {
 711         return "Unable to parse " +  option + " " + text + ": " + value;
 712     }
 713 
 714     private static final String ADD_MODULES  = "--add-modules";
 715     private static final String ADD_EXPORTS  = "--add-exports";
 716     private static final String ADD_OPENS    = "--add-opens";
 717     private static final String ADD_READS    = "--add-reads";
 718     private static final String PATCH_MODULE = "--patch-module";
 719 
 720 
 721     /*
 722      * Returns the command-line option name corresponds to the specified
 723      * system property prefix.
 724      */
 725     static String option(String prefix) {
 726         switch (prefix) {
 727             case "jdk.module.addexports.":
 728                 return ADD_EXPORTS;
 729             case "jdk.module.addopens.":
 730                 return ADD_OPENS;
 731             case "jdk.module.addreads.":
 732                 return ADD_READS;
 733             case "jdk.module.patch.":
 734                 return PATCH_MODULE;
 735             case "jdk.module.addmods.":
 736                 return ADD_MODULES;
 737             default:
 738                 throw new IllegalArgumentException(prefix);
 739         }
 740     }
 741 
 742     static class PerfCounters {
 743 
 744         static PerfCounter systemModulesTime
 745             = PerfCounter.newPerfCounter("jdk.module.bootstrap.systemModulesTime");
 746         static PerfCounter defineBaseTime
 747             = PerfCounter.newPerfCounter("jdk.module.bootstrap.defineBaseTime");
 748         static PerfCounter optionsAndRootsTime
 749             = PerfCounter.newPerfCounter("jdk.module.bootstrap.optionsAndRootsTime");
 750         static PerfCounter resolveTime
 751             = PerfCounter.newPerfCounter("jdk.module.bootstrap.resolveTime");
 752         static PerfCounter layerCreateTime
 753             = PerfCounter.newPerfCounter("jdk.module.bootstrap.layerCreateTime");
 754         static PerfCounter loadModulesTime
 755             = PerfCounter.newPerfCounter("jdk.module.bootstrap.loadModulesTime");
 756         static PerfCounter bootstrapTime
 757             = PerfCounter.newPerfCounter("jdk.module.bootstrap.totalTime");
 758     }
 759 }