1 /*
   2  * Copyright (c) 2007, 2023, 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 sun.launcher;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.io.UnsupportedEncodingException;
  32 import java.lang.module.Configuration;
  33 import java.lang.module.ModuleDescriptor;
  34 import java.lang.module.ModuleDescriptor.Exports;
  35 import java.lang.module.ModuleDescriptor.Opens;
  36 import java.lang.module.ModuleDescriptor.Provides;
  37 import java.lang.module.ModuleDescriptor.Requires;
  38 import java.lang.module.ModuleFinder;
  39 import java.lang.module.ModuleReference;
  40 import java.lang.module.ResolvedModule;
  41 import java.lang.reflect.Constructor;
  42 import java.lang.reflect.InvocationTargetException;
  43 import java.lang.reflect.Method;
  44 import java.lang.reflect.Modifier;
  45 import java.math.BigDecimal;
  46 import java.math.RoundingMode;
  47 import java.net.URI;
  48 import java.nio.charset.Charset;
  49 import java.nio.file.DirectoryStream;
  50 import java.nio.file.Files;
  51 import java.nio.file.Path;
  52 import java.text.MessageFormat;
  53 import java.text.Normalizer;
  54 import java.util.ArrayList;
  55 import java.util.Arrays;
  56 import java.util.Comparator;
  57 import java.util.Iterator;
  58 import java.util.List;
  59 import java.util.Locale;
  60 import java.util.Locale.Category;
  61 import java.util.Map;
  62 import java.util.Optional;
  63 import java.util.Properties;
  64 import java.util.ResourceBundle;
  65 import java.util.Set;
  66 import java.util.TreeSet;
  67 import java.util.function.Function;
  68 import java.util.jar.Attributes;
  69 import java.util.jar.JarFile;
  70 import java.util.jar.Manifest;
  71 import java.util.stream.Collectors;
  72 import java.util.stream.Stream;
  73 
  74 import jdk.internal.misc.MainMethodFinder;
  75 import jdk.internal.misc.PreviewFeatures;
  76 import jdk.internal.misc.VM;
  77 import jdk.internal.module.ModuleBootstrap;
  78 import jdk.internal.module.Modules;
  79 import jdk.internal.platform.Container;
  80 import jdk.internal.platform.Metrics;
  81 import jdk.internal.util.OperatingSystem;
  82 import sun.util.calendar.ZoneInfoFile;
  83 
  84 /**
  85  * A utility package for the java(1), javaw(1) launchers.
  86  * The following are helper methods that the native launcher uses
  87  * to perform checks etc. using JNI, see src/share/bin/java.c
  88  */
  89 public final class LauncherHelper {
  90 
  91     // No instantiation
  92     private LauncherHelper() {}
  93 
  94     // used to identify JavaFX applications
  95     private static final String JAVAFX_APPLICATION_MARKER =
  96             "JavaFX-Application-Class";
  97     private static final String JAVAFX_APPLICATION_CLASS_NAME =
  98             "javafx.application.Application";
  99     private static final String JAVAFX_FXHELPER_CLASS_NAME_SUFFIX =
 100             "sun.launcher.LauncherHelper$FXHelper";
 101     private static final String LAUNCHER_AGENT_CLASS = "Launcher-Agent-Class";
 102     private static final String MAIN_CLASS = "Main-Class";
 103     private static final String ADD_EXPORTS = "Add-Exports";
 104     private static final String ADD_OPENS = "Add-Opens";
 105     private static final String ENABLE_NATIVE_ACCESS = "Enable-Native-Access";
 106 
 107     private static StringBuilder outBuf = new StringBuilder();
 108 
 109     private static final String INDENT = "    ";
 110     private static final String VM_SETTINGS     = "VM settings:";
 111     private static final String PROP_SETTINGS   = "Property settings:";
 112     private static final String LOCALE_SETTINGS = "Locale settings:";
 113 
 114     // sync with java.c and jdk.internal.misc.VM
 115     private static final String diagprop = "sun.java.launcher.diag";
 116     static final boolean trace = VM.getSavedProperty(diagprop) != null;
 117 
 118     private static final String defaultBundleName =
 119             "sun.launcher.resources.launcher";
 120 
 121     private static class ResourceBundleHolder {
 122         private static final ResourceBundle RB =
 123                 ResourceBundle.getBundle(defaultBundleName);
 124     }
 125     private static PrintStream ostream;
 126     private static Class<?> appClass; // application class, for GUI/reporting purposes
 127 
 128     enum Option { DEFAULT, ALL, LOCALE, PROPERTIES, SECURITY,
 129         SECURITY_ALL, SECURITY_PROPERTIES, SECURITY_PROVIDERS,
 130         SECURITY_TLS, SYSTEM, VM };
 131 
 132     /*
 133      * A method called by the launcher to print out the standard settings.
 134      * -XshowSettings prints details of all supported components in non-verbose
 135      * mode. -XshowSettings:all prints all settings in verbose mode.
 136      * Specific settings information may be obtained by using suboptions.
 137      *
 138      * Suboption values include "all", "locale", "properties", "security",
 139      * "system" (Linux only) and "vm". A error message is printed for an
 140      * unknown suboption value and the VM launch aborts.
 141      *
 142      * printToStderr: choose between stdout and stderr
 143      *
 144      * optionFlag: specifies which options to print default is all other
 145      *    possible values are vm, properties, locale.
 146      *
 147      * initialHeapSize: in bytes, as set by the launcher, a zero-value indicates
 148      *    this code should determine this value, using a suitable method or
 149      *    the line could be omitted.
 150      *
 151      * maxHeapSize: in bytes, as set by the launcher, a zero-value indicates
 152      *    this code should determine this value, using a suitable method.
 153      *
 154      * stackSize: in bytes, as set by the launcher, a zero-value indicates
 155      *    this code determine this value, using a suitable method or omit the
 156      *    line entirely.
 157      */
 158     @SuppressWarnings("fallthrough")
 159     static void showSettings(boolean printToStderr, String optionFlag,
 160             long initialHeapSize, long maxHeapSize, long stackSize) {
 161 
 162         initOutput(printToStderr);
 163         Option component = validateOption(optionFlag);
 164         switch (component) {
 165             case ALL -> printAllSettings(initialHeapSize, maxHeapSize, stackSize, true);
 166             case LOCALE -> printLocale(true);
 167             case PROPERTIES -> printProperties();
 168             case SECURITY,
 169                  SECURITY_ALL,
 170                  SECURITY_PROPERTIES,
 171                  SECURITY_PROVIDERS,
 172                  SECURITY_TLS -> SecuritySettings.printSecuritySettings(component, ostream, true);
 173             case SYSTEM -> printSystemMetrics();
 174             case VM -> printVmSettings(initialHeapSize, maxHeapSize, stackSize);
 175             case DEFAULT -> printAllSettings(initialHeapSize, maxHeapSize, stackSize, false);
 176         }
 177     }
 178 
 179     /*
 180      * Validate that the -XshowSettings value is allowed
 181      * If a valid option is parsed, return enum corresponding
 182      * to that option. Abort if a bad option is parsed.
 183      */
 184     private static Option validateOption(String optionFlag) {
 185         if (optionFlag.equals("-XshowSettings")) {
 186             return Option.DEFAULT;
 187         }
 188 
 189         if (optionFlag.equals("-XshowSetings:")) {
 190             abort(null, "java.launcher.bad.option", ":");
 191         }
 192 
 193         Map<String, Option> validOpts = Arrays.stream(Option.values())
 194                 .filter(o -> !o.equals(Option.DEFAULT)) // non-valid option
 195                 .collect(Collectors.toMap(o -> o.name()
 196                         .toLowerCase(Locale.ROOT)
 197                         .replace("_", ":"), Function.identity()));
 198 
 199         String optStr = optionFlag.substring("-XshowSettings:".length());
 200         Option component = validOpts.get(optStr);
 201         if (component == null) {
 202             abort(null, "java.launcher.bad.option", optStr);
 203         }
 204         return component;
 205     }
 206 
 207     /*
 208      * Print settings for all supported components.
 209      * verbose value used to determine if verbose information
 210      * should be printed for components that support printing
 211      * in verbose or non-verbose mode.
 212      */
 213     private static void printAllSettings(long initialHeapSize, long maxHeapSize,
 214                                          long stackSize, boolean verbose) {
 215         printVmSettings(initialHeapSize, maxHeapSize, stackSize);
 216         printProperties();
 217         printLocale(verbose);
 218         SecuritySettings.printSecuritySettings(
 219                     Option.SECURITY_ALL, ostream, verbose);
 220         if (OperatingSystem.isLinux()) {
 221             printSystemMetrics();
 222         }
 223     }
 224 
 225     private static void printVmSettings(
 226             long initialHeapSize, long maxHeapSize,
 227             long stackSize) {
 228 
 229         ostream.println(VM_SETTINGS);
 230         if (stackSize != 0L) {
 231             ostream.println(INDENT + "Stack Size: " +
 232                     SizePrefix.scaleValue(stackSize));
 233         }
 234         if (initialHeapSize != 0L) {
 235              ostream.println(INDENT + "Min. Heap Size: " +
 236                     SizePrefix.scaleValue(initialHeapSize));
 237         }
 238         if (maxHeapSize != 0L) {
 239             ostream.println(INDENT + "Max. Heap Size: " +
 240                     SizePrefix.scaleValue(maxHeapSize));
 241         } else {
 242             ostream.println(INDENT + "Max. Heap Size (Estimated): "
 243                     + SizePrefix.scaleValue(Runtime.getRuntime().maxMemory()));
 244         }
 245         ostream.println(INDENT + "Using VM: "
 246                 + System.getProperty("java.vm.name"));
 247         ostream.println();
 248     }
 249 
 250     /*
 251      * prints the properties subopt/section
 252      */
 253     private static void printProperties() {
 254         Properties p = System.getProperties();
 255         ostream.println(PROP_SETTINGS);
 256         for (String key : p.stringPropertyNames().stream().sorted().toList()) {
 257             printPropertyValue(key, p.getProperty(key));
 258         }
 259         ostream.println();
 260     }
 261 
 262     private static boolean isPath(String key) {
 263         return key.endsWith(".dirs") || key.endsWith(".path");
 264     }
 265 
 266     private static void printPropertyValue(String key, String value) {
 267         ostream.print(INDENT + key + " = ");
 268         if (key.equals("line.separator")) {
 269             for (byte b : value.getBytes()) {
 270                 switch (b) {
 271                     case 0xd:
 272                         ostream.print("\\r ");
 273                         break;
 274                     case 0xa:
 275                         ostream.print("\\n ");
 276                         break;
 277                     default:
 278                         // print any bizarre line separators in hex, but really
 279                         // shouldn't happen.
 280                         ostream.printf("0x%02X", b & 0xff);
 281                         break;
 282                 }
 283             }
 284             ostream.println();
 285             return;
 286         }
 287         if (!isPath(key)) {
 288             ostream.println(value);
 289             return;
 290         }
 291         String[] values = value.split(System.getProperty("path.separator"));
 292         boolean first = true;
 293         for (String s : values) {
 294             if (first) { // first line treated specially
 295                 ostream.println(s);
 296                 first = false;
 297             } else { // following lines prefix with indents
 298                 ostream.println(INDENT + INDENT + s);
 299             }
 300         }
 301     }
 302 
 303     /*
 304      * prints the locale subopt/section
 305      */
 306     private static void printLocale(boolean verbose) {
 307         Locale locale = Locale.getDefault();
 308         if (verbose) {
 309             ostream.println(LOCALE_SETTINGS);
 310         } else {
 311             ostream.println("Locale settings summary:");
 312             ostream.println(INDENT + "Use \"-XshowSettings:locale\" " +
 313                     "option for verbose locale settings options");
 314         }
 315         ostream.println(INDENT + "default locale = " +
 316                 locale.getDisplayName());
 317         ostream.println(INDENT + "default display locale = " +
 318                 Locale.getDefault(Category.DISPLAY).getDisplayName());
 319         ostream.println(INDENT + "default format locale = " +
 320                 Locale.getDefault(Category.FORMAT).getDisplayName());
 321         ostream.println(INDENT + "tzdata version = " +
 322                 ZoneInfoFile.getVersion());
 323         if (verbose) {
 324             printLocales();
 325         }
 326         ostream.println();
 327     }
 328 
 329     private static void printLocales() {
 330         Locale[] tlocales = Locale.getAvailableLocales();
 331         final int len = tlocales == null ? 0 : tlocales.length;
 332         if (len < 1 ) {
 333             return;
 334         }
 335         // Locale does not implement Comparable so we convert it to String
 336         // and sort it for pretty printing.
 337         Set<String> sortedSet = new TreeSet<>();
 338         for (Locale l : tlocales) {
 339             sortedSet.add(l.toString());
 340         }
 341 
 342         ostream.print(INDENT + "available locales = ");
 343         Iterator<String> iter = sortedSet.iterator();
 344         final int last = len - 1;
 345         for (int i = 0 ; iter.hasNext() ; i++) {
 346             String s = iter.next();
 347             ostream.print(s);
 348             if (i != last) {
 349                 ostream.print(", ");
 350             }
 351             // print columns of 8
 352             if ((i + 1) % 8 == 0) {
 353                 ostream.println();
 354                 ostream.print(INDENT + INDENT);
 355             }
 356         }
 357         ostream.println();
 358     }
 359 
 360     private static void printSystemMetrics() {
 361         Metrics c = Container.metrics();
 362 
 363         ostream.println("Operating System Metrics:");
 364 
 365         if (c == null) {
 366             ostream.println(INDENT + "No metrics available for this platform");
 367             return;
 368         }
 369 
 370         final long longRetvalNotSupported = -2;
 371 
 372         ostream.println(INDENT + "Provider: " + c.getProvider());
 373         ostream.println(INDENT + "Effective CPU Count: " + c.getEffectiveCpuCount());
 374         ostream.println(formatCpuVal(c.getCpuPeriod(), INDENT + "CPU Period: ", longRetvalNotSupported));
 375         ostream.println(formatCpuVal(c.getCpuQuota(), INDENT + "CPU Quota: ", longRetvalNotSupported));
 376         ostream.println(formatCpuVal(c.getCpuShares(), INDENT + "CPU Shares: ", longRetvalNotSupported));
 377 
 378         int cpus[] = c.getCpuSetCpus();
 379         if (cpus != null) {
 380             ostream.println(INDENT + "List of Processors, "
 381                     + cpus.length + " total: ");
 382 
 383             ostream.print(INDENT);
 384             for (int i = 0; i < cpus.length; i++) {
 385                 ostream.print(cpus[i] + " ");
 386             }
 387             if (cpus.length > 0) {
 388                 ostream.println("");
 389             }
 390         } else {
 391             ostream.println(INDENT + "List of Processors: N/A");
 392         }
 393 
 394         cpus = c.getEffectiveCpuSetCpus();
 395         if (cpus != null) {
 396             ostream.println(INDENT + "List of Effective Processors, "
 397                     + cpus.length + " total: ");
 398 
 399             ostream.print(INDENT);
 400             for (int i = 0; i < cpus.length; i++) {
 401                 ostream.print(cpus[i] + " ");
 402             }
 403             if (cpus.length > 0) {
 404                 ostream.println("");
 405             }
 406         } else {
 407             ostream.println(INDENT + "List of Effective Processors: N/A");
 408         }
 409 
 410         int mems[] = c.getCpuSetMems();
 411         if (mems != null) {
 412             ostream.println(INDENT + "List of Memory Nodes, "
 413                     + mems.length + " total: ");
 414 
 415             ostream.print(INDENT);
 416             for (int i = 0; i < mems.length; i++) {
 417                 ostream.print(mems[i] + " ");
 418             }
 419             if (mems.length > 0) {
 420                 ostream.println("");
 421             }
 422         } else {
 423             ostream.println(INDENT + "List of Memory Nodes: N/A");
 424         }
 425 
 426         mems = c.getEffectiveCpuSetMems();
 427         if (mems != null) {
 428             ostream.println(INDENT + "List of Available Memory Nodes, "
 429                     + mems.length + " total: ");
 430 
 431             ostream.print(INDENT);
 432             for (int i = 0; i < mems.length; i++) {
 433                 ostream.print(mems[i] + " ");
 434             }
 435             if (mems.length > 0) {
 436                 ostream.println("");
 437             }
 438         } else {
 439             ostream.println(INDENT + "List of Available Memory Nodes: N/A");
 440         }
 441 
 442         long limit = c.getMemoryLimit();
 443         ostream.println(formatLimitString(limit, INDENT + "Memory Limit: ", longRetvalNotSupported));
 444 
 445         limit = c.getMemorySoftLimit();
 446         ostream.println(formatLimitString(limit, INDENT + "Memory Soft Limit: ", longRetvalNotSupported));
 447 
 448         limit = c.getMemoryAndSwapLimit();
 449         ostream.println(formatLimitString(limit, INDENT + "Memory & Swap Limit: ", longRetvalNotSupported));
 450 
 451         limit = c.getPidsMax();
 452         ostream.println(formatLimitString(limit, INDENT + "Maximum Processes Limit: ",
 453                                           longRetvalNotSupported, false));
 454         ostream.println("");
 455     }
 456 
 457     private static String formatLimitString(long limit, String prefix, long unavailable) {
 458         return formatLimitString(limit, prefix, unavailable, true);
 459     }
 460 
 461     private static String formatLimitString(long limit, String prefix, long unavailable, boolean scale) {
 462         if (limit >= 0) {
 463             if (scale) {
 464                 return prefix + SizePrefix.scaleValue(limit);
 465             } else {
 466                 return prefix + limit;
 467             }
 468         } else if (limit == unavailable) {
 469             return prefix + "N/A";
 470         } else {
 471             return prefix + "Unlimited";
 472         }
 473     }
 474 
 475     private static String formatCpuVal(long cpuVal, String prefix, long unavailable) {
 476         if (cpuVal >= 0) {
 477             return prefix + cpuVal + "us";
 478         } else if (cpuVal == unavailable) {
 479             return prefix + "N/A";
 480         } else {
 481             return prefix + cpuVal;
 482         }
 483     }
 484 
 485     private enum SizePrefix {
 486 
 487         KILO(1024, "K"),
 488         MEGA(1024 * 1024, "M"),
 489         GIGA(1024 * 1024 * 1024, "G"),
 490         TERA(1024L * 1024L * 1024L * 1024L, "T");
 491         long size;
 492         String abbrev;
 493 
 494         SizePrefix(long size, String abbrev) {
 495             this.size = size;
 496             this.abbrev = abbrev;
 497         }
 498 
 499         private static String scale(long v, SizePrefix prefix) {
 500             return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size),
 501                     2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev;
 502         }
 503         /*
 504          * scale the incoming values to a human readable form, represented as
 505          * K, M, G and T, see java.c parse_size for the scaled values and
 506          * suffixes. The lowest possible scaled value is Kilo.
 507          */
 508         static String scaleValue(long v) {
 509             if (v < MEGA.size) {
 510                 return scale(v, KILO);
 511             } else if (v < GIGA.size) {
 512                 return scale(v, MEGA);
 513             } else if (v < TERA.size) {
 514                 return scale(v, GIGA);
 515             } else {
 516                 return scale(v, TERA);
 517             }
 518         }
 519     }
 520 
 521     /**
 522      * A private helper method to get a localized message and also
 523      * apply any arguments that we might pass.
 524      */
 525     private static String getLocalizedMessage(String key, Object... args) {
 526         String msg = ResourceBundleHolder.RB.getString(key);
 527         return (args != null) ? MessageFormat.format(msg, args) : msg;
 528     }
 529 
 530     /**
 531      * The java -help message is split into 3 parts, an invariant, followed
 532      * by a set of platform dependent variant messages, finally an invariant
 533      * set of lines.
 534      * This method initializes the help message for the first time, and also
 535      * assembles the invariant header part of the message.
 536      */
 537     static void initHelpMessage(String progname) {
 538         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header",
 539                 (progname == null) ? "java" : progname ));
 540     }
 541 
 542     /**
 543      * Appends the vm selection messages to the header, already created.
 544      * initHelpSystem must already be called.
 545      */
 546     static void appendVmSelectMessage(String vm1, String vm2) {
 547         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.vmselect",
 548                 vm1, vm2));
 549     }
 550 
 551     /**
 552      * Appends the vm synonym message to the header, already created.
 553      * initHelpSystem must be called before using this method.
 554      */
 555     static void appendVmSynonymMessage(String vm1, String vm2) {
 556         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.hotspot",
 557                 vm1, vm2));
 558     }
 559 
 560     /**
 561      * Appends the last invariant part to the previously created messages,
 562      * and finishes up the printing to the desired output stream.
 563      * initHelpSystem must be called before using this method.
 564      */
 565     static void printHelpMessage(boolean printToStderr) {
 566         initOutput(printToStderr);
 567         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.footer",
 568                 File.pathSeparator));
 569         ostream.println(outBuf.toString());
 570     }
 571 
 572     /**
 573      * Prints the Xusage text to the desired output stream.
 574      */
 575     static void printXUsageMessage(boolean printToStderr) {
 576         initOutput(printToStderr);
 577         ostream.println(getLocalizedMessage("java.launcher.X.usage",
 578                 File.pathSeparator));
 579         if (OperatingSystem.isMacOS()) {
 580             ostream.println(getLocalizedMessage("java.launcher.X.macosx.usage",
 581                         File.pathSeparator));
 582         }
 583     }
 584 
 585     static void initOutput(boolean printToStderr) {
 586         ostream =  (printToStderr) ? System.err : System.out;
 587     }
 588 
 589     static void initOutput(PrintStream ps) {
 590         ostream = ps;
 591     }
 592 
 593     static String getMainClassFromJar(String jarname) {
 594         String mainValue;
 595         try (JarFile jarFile = new JarFile(jarname)) {
 596             Manifest manifest = jarFile.getManifest();
 597             if (manifest == null) {
 598                 abort(null, "java.launcher.jar.error2", jarname);
 599             }
 600             Attributes mainAttrs = manifest.getMainAttributes();
 601             if (mainAttrs == null) {
 602                 abort(null, "java.launcher.jar.error3", jarname);
 603             }
 604 
 605             // Main-Class
 606             mainValue = mainAttrs.getValue(MAIN_CLASS);
 607             if (mainValue == null) {
 608                 abort(null, "java.launcher.jar.error3", jarname);
 609             }
 610 
 611             // Launcher-Agent-Class (only check for this when Main-Class present)
 612             String agentClass = mainAttrs.getValue(LAUNCHER_AGENT_CLASS);
 613             if (agentClass != null) {
 614                 ModuleLayer.boot().findModule("java.instrument").ifPresent(m -> {
 615                     try {
 616                         String cn = "sun.instrument.InstrumentationImpl";
 617                         Class<?> clazz = Class.forName(cn, false, null);
 618                         Method loadAgent = clazz.getMethod("loadAgent", String.class);
 619                         loadAgent.invoke(null, jarname);
 620                     } catch (Throwable e) {
 621                         if (e instanceof InvocationTargetException) e = e.getCause();
 622                         abort(e, "java.launcher.jar.error4", jarname);
 623                     }
 624                 });
 625             }
 626 
 627             // Add-Exports and Add-Opens
 628             String exports = mainAttrs.getValue(ADD_EXPORTS);
 629             if (exports != null) {
 630                 addExportsOrOpens(exports, false);
 631             }
 632             String opens = mainAttrs.getValue(ADD_OPENS);
 633             if (opens != null) {
 634                 addExportsOrOpens(opens, true);
 635             }
 636             String enableNativeAccess = mainAttrs.getValue(ENABLE_NATIVE_ACCESS);
 637             if (enableNativeAccess != null) {
 638                 if (!enableNativeAccess.equals("ALL-UNNAMED")) {
 639                     throw new IllegalArgumentException("Only ALL-UNNAMED allowed as value for " + ENABLE_NATIVE_ACCESS);
 640                 }
 641                 Modules.addEnableNativeAccessToAllUnnamed();
 642             }
 643 
 644             /*
 645              * Hand off to FXHelper if it detects a JavaFX application
 646              * This must be done after ensuring a Main-Class entry
 647              * exists to enforce compliance with the jar specification
 648              */
 649             if (mainAttrs.containsKey(
 650                     new Attributes.Name(JAVAFX_APPLICATION_MARKER))) {
 651                 FXHelper.setFXLaunchParameters(jarname, LM_JAR);
 652                 return FXHelper.class.getName();
 653             }
 654 
 655             return mainValue.trim();
 656         } catch (IOException ioe) {
 657             abort(ioe, "java.launcher.jar.error1", jarname);
 658         }
 659         return null;
 660     }
 661 
 662     /**
 663      * Process the Add-Exports or Add-Opens value. The value is
 664      * {@code <module>/<package> ( <module>/<package>)*}.
 665      */
 666     static void addExportsOrOpens(String value, boolean open) {
 667         for (String moduleAndPackage : value.split(" ")) {
 668             String[] s = moduleAndPackage.trim().split("/");
 669             if (s.length == 2) {
 670                 String mn = s[0];
 671                 String pn = s[1];
 672                 ModuleLayer.boot()
 673                     .findModule(mn)
 674                     .filter(m -> m.getDescriptor().packages().contains(pn))
 675                     .ifPresent(m -> {
 676                         if (open) {
 677                             Modules.addOpensToAllUnnamed(m, pn);
 678                         } else {
 679                             Modules.addExportsToAllUnnamed(m, pn);
 680                         }
 681                     });
 682             }
 683         }
 684     }
 685 
 686     // From src/share/bin/java.c:
 687     //   enum LaunchMode { LM_UNKNOWN = 0, LM_CLASS, LM_JAR, LM_MODULE, LM_SOURCE }
 688 
 689     private static final int LM_UNKNOWN = 0;
 690     private static final int LM_CLASS   = 1;
 691     private static final int LM_JAR     = 2;
 692     private static final int LM_MODULE  = 3;
 693     private static final int LM_SOURCE  = 4;
 694 
 695     static void abort(Throwable t, String msgKey, Object... args) {
 696         if (msgKey != null) {
 697             ostream.println(getLocalizedMessage(msgKey, args));
 698         }
 699         if (trace) {
 700             if (t != null) {
 701                 t.printStackTrace();
 702             } else {
 703                 Thread.dumpStack();
 704             }
 705         }
 706         System.exit(1);
 707     }
 708 
 709     /**
 710      * This method:
 711      * 1. Loads the main class from the module or class path
 712      * 2. Checks the public static void main method.
 713      * 3. If the main class extends FX Application then call on FXHelper to
 714      * perform the launch.
 715      *
 716      * @param printToStderr if set, all output will be routed to stderr
 717      * @param mode LaunchMode as determined by the arguments passed on the
 718      *             command line
 719      * @param what the module name[/class], JAR file, or the main class
 720      *             depending on the mode
 721      *
 722      * @return the application's main class
 723      */
 724     @SuppressWarnings("fallthrough")
 725     public static Class<?> checkAndLoadMain(boolean printToStderr,
 726                                             int mode,
 727                                             String what) {
 728         initOutput(printToStderr);
 729 
 730         Class<?> mainClass = null;
 731         switch (mode) {
 732             case LM_MODULE: case LM_SOURCE:
 733                 mainClass = loadModuleMainClass(what);
 734                 break;
 735             default:
 736                 mainClass = loadMainClass(mode, what);
 737                 break;
 738         }
 739 
 740         // record the real main class for UI purposes
 741         // neither method above can return null, they will abort()
 742         appClass = mainClass;
 743 
 744         /*
 745          * Check if FXHelper can launch it using the FX launcher. In an FX app,
 746          * the main class may or may not have a main method, so do this before
 747          * validating the main class.
 748          */
 749         if (JAVAFX_FXHELPER_CLASS_NAME_SUFFIX.equals(mainClass.getName()) ||
 750             doesExtendFXApplication(mainClass)) {
 751             // Will abort() if there are problems with FX runtime
 752             FXHelper.setFXLaunchParameters(what, mode);
 753             mainClass = FXHelper.class;
 754         }
 755 
 756         validateMainClass(mainClass);
 757         return mainClass;
 758     }
 759 
 760     /**
 761      * Returns the main class for a module. The query is either a module name
 762      * or module-name/main-class. For the former then the module's main class
 763      * is obtained from the module descriptor (MainClass attribute).
 764      */
 765     private static Class<?> loadModuleMainClass(String what) {
 766         int i = what.indexOf('/');
 767         String mainModule;
 768         String mainClass;
 769         if (i == -1) {
 770             mainModule = what;
 771             mainClass = null;
 772         } else {
 773             mainModule = what.substring(0, i);
 774             mainClass = what.substring(i+1);
 775         }
 776 
 777         // main module is in the boot layer
 778         ModuleLayer layer = ModuleLayer.boot();
 779         Optional<Module> om = layer.findModule(mainModule);
 780         if (om.isEmpty()) {
 781             // should not happen
 782             throw new InternalError("Module " + mainModule + " not in boot Layer");
 783         }
 784         Module m = om.get();
 785 
 786         // get main class
 787         if (mainClass == null) {
 788             Optional<String> omc = m.getDescriptor().mainClass();
 789             if (omc.isEmpty()) {
 790                 abort(null, "java.launcher.module.error1", mainModule);
 791             }
 792             mainClass = omc.get();
 793         }
 794 
 795         // load the class from the module
 796         Class<?> c = null;
 797         try {
 798             c = Class.forName(m, mainClass);
 799             if (c == null && OperatingSystem.isMacOS()
 800                     && Normalizer.isNormalized(mainClass, Normalizer.Form.NFD)) {
 801 
 802                 String cn = Normalizer.normalize(mainClass, Normalizer.Form.NFC);
 803                 c = Class.forName(m, cn);
 804             }
 805         } catch (LinkageError le) {
 806             abort(null, "java.launcher.module.error3", mainClass, m.getName(),
 807                     le.getClass().getName() + ": " + le.getLocalizedMessage());
 808         }
 809         if (c == null) {
 810             abort(null, "java.launcher.module.error2", mainClass, mainModule);
 811         }
 812 
 813         System.setProperty("jdk.module.main.class", c.getName());
 814         return c;
 815     }
 816 
 817     /**
 818      * Loads the main class from the class path (LM_CLASS or LM_JAR).
 819      */
 820     private static Class<?> loadMainClass(int mode, String what) {
 821         // get the class name
 822         String cn;
 823         switch (mode) {
 824             case LM_CLASS:
 825                 cn = what;
 826                 break;
 827             case LM_JAR:
 828                 cn = getMainClassFromJar(what);
 829                 break;
 830             default:
 831                 // should never happen
 832                 throw new InternalError("" + mode + ": Unknown launch mode");
 833         }
 834 
 835         // load the main class
 836         cn = cn.replace('/', '.');
 837         Class<?> mainClass = null;
 838         ClassLoader scl = ClassLoader.getSystemClassLoader();
 839         try {
 840             try {
 841                 mainClass = Class.forName(cn, false, scl);
 842             } catch (NoClassDefFoundError | ClassNotFoundException cnfe) {
 843                 if (OperatingSystem.isMacOS()
 844                         && Normalizer.isNormalized(cn, Normalizer.Form.NFD)) {
 845                     try {
 846                         // On Mac OS X since all names with diacritical marks are
 847                         // given as decomposed it is possible that main class name
 848                         // comes incorrectly from the command line and we have
 849                         // to re-compose it
 850                         String ncn = Normalizer.normalize(cn, Normalizer.Form.NFC);
 851                         mainClass = Class.forName(ncn, false, scl);
 852                     } catch (NoClassDefFoundError | ClassNotFoundException cnfe1) {
 853                         abort(cnfe1, "java.launcher.cls.error1", cn,
 854                                 cnfe1.getClass().getCanonicalName(), cnfe1.getMessage());
 855                     }
 856                 } else {
 857                     abort(cnfe, "java.launcher.cls.error1", cn,
 858                             cnfe.getClass().getCanonicalName(), cnfe.getMessage());
 859                 }
 860             }
 861         } catch (LinkageError le) {
 862             abort(le, "java.launcher.cls.error6", cn,
 863                     le.getClass().getName() + ": " + le.getLocalizedMessage());
 864         }
 865         return mainClass;
 866     }
 867 
 868     /*
 869      * Accessor method called by the launcher after getting the main class via
 870      * checkAndLoadMain(). The "application class" is the class that is finally
 871      * executed to start the application and in this case is used to report
 872      * the correct application name, typically for UI purposes.
 873      */
 874     public static Class<?> getApplicationClass() {
 875         return appClass;
 876     }
 877 
 878     /*
 879      * Check if the given class is a JavaFX Application class. This is done
 880      * in a way that does not cause the Application class to load or throw
 881      * ClassNotFoundException if the JavaFX runtime is not available.
 882      */
 883     private static boolean doesExtendFXApplication(Class<?> mainClass) {
 884         for (Class<?> sc = mainClass.getSuperclass(); sc != null;
 885                 sc = sc.getSuperclass()) {
 886             if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) {
 887                 return true;
 888             }
 889         }
 890         return false;
 891     }
 892 
 893     /*
 894      * main type flags
 895      */
 896     private static final int MAIN_WITHOUT_ARGS = 1;
 897     private static final int MAIN_NONSTATIC = 2;
 898     private static int mainType = 0;
 899 
 900     /*
 901      * Return type so that launcher invokes the correct main
 902      */
 903     public static int getMainType() {
 904         return mainType;
 905     }
 906 
 907     private static void setMainType(Method mainMethod) {
 908         int mods = mainMethod.getModifiers();
 909         boolean isStatic = Modifier.isStatic(mods);
 910         boolean noArgs = mainMethod.getParameterCount() == 0;
 911         mainType = (isStatic ? 0 : MAIN_NONSTATIC) | (noArgs ? MAIN_WITHOUT_ARGS : 0);
 912     }
 913 
 914     // Check the existence and signature of main and abort if incorrect
 915     static void validateMainClass(Class<?> mainClass) {
 916         Method mainMethod = null;
 917         try {
 918             mainMethod = MainMethodFinder.findMainMethod(mainClass);
 919         } catch (NoSuchMethodException nsme) {
 920             // invalid main or not FX application, abort with an error
 921             abort(null, "java.launcher.cls.error4", mainClass.getName(),
 922                   JAVAFX_APPLICATION_CLASS_NAME);
 923         } catch (Throwable e) {
 924             if (mainClass.getModule().isNamed()) {
 925                 abort(e, "java.launcher.module.error5",
 926                       mainClass.getName(), mainClass.getModule().getName(),
 927                       e.getClass().getName(), e.getLocalizedMessage());
 928             } else {
 929                 abort(e, "java.launcher.cls.error7", mainClass.getName(),
 930                       e.getClass().getName(), e.getLocalizedMessage());
 931             }
 932         }
 933 
 934         setMainType(mainMethod);
 935 
 936         /*
 937          * findMainMethod (above) will choose the correct method, based
 938          * on its name and parameter type, however, we still have to
 939          * ensure that the method is static (non-preview) and returns a void.
 940          */
 941         int mods = mainMethod.getModifiers();
 942         boolean isStatic = Modifier.isStatic(mods);
 943         boolean isPublic = Modifier.isPublic(mods);
 944         boolean noArgs = mainMethod.getParameterCount() == 0;
 945 
 946         if (!PreviewFeatures.isEnabled()) {
 947             if (!isStatic || !isPublic || noArgs) {
 948                 abort(null, "java.launcher.cls.error2", "static",
 949                       mainMethod.getDeclaringClass().getName());
 950             }
 951         }
 952 
 953         if (!isStatic) {
 954             if (mainClass.isMemberClass() && !Modifier.isStatic(mainClass.getModifiers())) {
 955                 abort(null, "java.launcher.cls.error9",
 956                         mainMethod.getDeclaringClass().getName());
 957             }
 958             try {
 959                 Constructor<?> constructor = mainClass.getDeclaredConstructor();
 960                 if (Modifier.isPrivate(constructor.getModifiers())) {
 961                     abort(null, "java.launcher.cls.error8",
 962                           mainMethod.getDeclaringClass().getName());
 963                 }
 964             } catch (Throwable ex) {
 965                 abort(null, "java.launcher.cls.error8",
 966                       mainMethod.getDeclaringClass().getName());
 967             }
 968         }
 969 
 970         if (mainMethod.getReturnType() != java.lang.Void.TYPE) {
 971             abort(null, "java.launcher.cls.error3",
 972                   mainMethod.getDeclaringClass().getName());
 973         }
 974     }
 975 
 976     private static final String encprop = "sun.jnu.encoding";
 977     private static String encoding = null;
 978     private static boolean isCharsetSupported = false;
 979 
 980     /*
 981      * converts a c or a byte array to a platform specific string,
 982      * previously implemented as a native method in the launcher.
 983      */
 984     static String makePlatformString(boolean printToStderr, byte[] inArray) {
 985         initOutput(printToStderr);
 986         if (encoding == null) {
 987             encoding = System.getProperty(encprop);
 988             isCharsetSupported = Charset.isSupported(encoding);
 989         }
 990         try {
 991             String out = isCharsetSupported
 992                     ? new String(inArray, encoding)
 993                     : new String(inArray);
 994             return out;
 995         } catch (UnsupportedEncodingException uee) {
 996             abort(uee, null);
 997         }
 998         return null; // keep the compiler happy
 999     }
1000 
1001     static String[] expandArgs(String[] argArray) {
1002         List<StdArg> aList = new ArrayList<>();
1003         for (String x : argArray) {
1004             aList.add(new StdArg(x));
1005         }
1006         return expandArgs(aList);
1007     }
1008 
1009     static String[] expandArgs(List<StdArg> argList) {
1010         ArrayList<String> out = new ArrayList<>();
1011         if (trace) {
1012             System.err.println("Incoming arguments:");
1013         }
1014         for (StdArg a : argList) {
1015             if (trace) {
1016                 System.err.println(a);
1017             }
1018             if (a.needsExpansion) {
1019                 File x = new File(a.arg);
1020                 File parent = x.getParentFile();
1021                 String glob = x.getName();
1022                 if (parent == null) {
1023                     parent = new File(".");
1024                 }
1025                 try (DirectoryStream<Path> dstream =
1026                         Files.newDirectoryStream(parent.toPath(), glob)) {
1027                     int entries = 0;
1028                     for (Path p : dstream) {
1029                         out.add(p.normalize().toString());
1030                         entries++;
1031                     }
1032                     if (entries == 0) {
1033                         out.add(a.arg);
1034                     }
1035                 } catch (Exception e) {
1036                     out.add(a.arg);
1037                     if (trace) {
1038                         System.err.println("Warning: passing argument as-is " + a);
1039                         System.err.print(e);
1040                     }
1041                 }
1042             } else {
1043                 out.add(a.arg);
1044             }
1045         }
1046         String[] oarray = new String[out.size()];
1047         out.toArray(oarray);
1048 
1049         if (trace) {
1050             System.err.println("Expanded arguments:");
1051             for (String x : oarray) {
1052                 System.err.println(x);
1053             }
1054         }
1055         return oarray;
1056     }
1057 
1058     /* duplicate of the native StdArg struct */
1059     private static class StdArg {
1060         final String arg;
1061         final boolean needsExpansion;
1062         StdArg(String arg, boolean expand) {
1063             this.arg = arg;
1064             this.needsExpansion = expand;
1065         }
1066         // protocol: first char indicates whether expansion is required
1067         // 'T' = true ; needs expansion
1068         // 'F' = false; needs no expansion
1069         StdArg(String in) {
1070             this.arg = in.substring(1);
1071             needsExpansion = in.charAt(0) == 'T';
1072         }
1073         public String toString() {
1074             return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}';
1075         }
1076     }
1077 
1078     static final class FXHelper {
1079 
1080         private static final String JAVAFX_GRAPHICS_MODULE_NAME =
1081                 "javafx.graphics";
1082 
1083         private static final String JAVAFX_LAUNCHER_CLASS_NAME =
1084                 "com.sun.javafx.application.LauncherImpl";
1085 
1086         /*
1087          * The launch method used to invoke the JavaFX launcher. These must
1088          * match the strings used in the launchApplication method.
1089          *
1090          * Command line                 JavaFX-App-Class  Launch mode  FX Launch mode
1091          * java -cp fxapp.jar FXClass   N/A               LM_CLASS     "LM_CLASS"
1092          * java -cp somedir FXClass     N/A               LM_CLASS     "LM_CLASS"
1093          * java -jar fxapp.jar          Present           LM_JAR       "LM_JAR"
1094          * java -jar fxapp.jar          Not Present       LM_JAR       "LM_JAR"
1095          * java -m module/class [1]     N/A               LM_MODULE    "LM_MODULE"
1096          * java -m module               N/A               LM_MODULE    "LM_MODULE"
1097          *
1098          * [1] - JavaFX-Application-Class is ignored when modular args are used, even
1099          * if present in a modular jar
1100          */
1101         private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS";
1102         private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR";
1103         private static final String JAVAFX_LAUNCH_MODE_MODULE = "LM_MODULE";
1104 
1105         /*
1106          * FX application launcher and launch method, so we can launch
1107          * applications with no main method.
1108          */
1109         private static String fxLaunchName = null;
1110         private static String fxLaunchMode = null;
1111 
1112         private static Class<?> fxLauncherClass    = null;
1113         private static Method   fxLauncherMethod   = null;
1114 
1115         /*
1116          * Set the launch params according to what was passed to LauncherHelper
1117          * so we can use the same launch mode for FX. Abort if there is any
1118          * issue with loading the FX runtime or with the launcher method.
1119          */
1120         private static void setFXLaunchParameters(String what, int mode) {
1121 
1122             // find the module with the FX launcher
1123             Optional<Module> om = ModuleLayer.boot().findModule(JAVAFX_GRAPHICS_MODULE_NAME);
1124             if (om.isEmpty()) {
1125                 abort(null, "java.launcher.cls.error5");
1126             }
1127 
1128             // load the FX launcher class
1129             fxLauncherClass = Class.forName(om.get(), JAVAFX_LAUNCHER_CLASS_NAME);
1130             if (fxLauncherClass == null) {
1131                 abort(null, "java.launcher.cls.error5");
1132             }
1133 
1134             try {
1135                 /*
1136                  * signature must be:
1137                  * public static void launchApplication(String launchName,
1138                  *     String launchMode, String[] args);
1139                  */
1140                 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication",
1141                         String.class, String.class, String[].class);
1142 
1143                 // verify launcher signature as we do when validating the main method
1144                 int mod = fxLauncherMethod.getModifiers();
1145                 if (!Modifier.isStatic(mod)) {
1146                     abort(null, "java.launcher.javafx.error1");
1147                 }
1148                 if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) {
1149                     abort(null, "java.launcher.javafx.error1");
1150                 }
1151             } catch (NoSuchMethodException ex) {
1152                 abort(ex, "java.launcher.cls.error5", ex);
1153             }
1154 
1155             fxLaunchName = what;
1156             switch (mode) {
1157                 case LM_CLASS:
1158                     fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS;
1159                     break;
1160                 case LM_JAR:
1161                     fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR;
1162                     break;
1163                 case LM_MODULE:
1164                     fxLaunchMode = JAVAFX_LAUNCH_MODE_MODULE;
1165                     break;
1166                 default:
1167                     // should not have gotten this far...
1168                     throw new InternalError(mode + ": Unknown launch mode");
1169             }
1170         }
1171 
1172         public static void main(String... args) throws Exception {
1173             if (fxLauncherMethod == null
1174                     || fxLaunchMode == null
1175                     || fxLaunchName == null) {
1176                 throw new RuntimeException("Invalid JavaFX launch parameters");
1177             }
1178             // launch appClass via fxLauncherMethod
1179             fxLauncherMethod.invoke(null,
1180                     new Object[] {fxLaunchName, fxLaunchMode, args});
1181         }
1182     }
1183 
1184     /**
1185      * Called by the launcher to list the observable modules.
1186      */
1187     static void listModules() {
1188         initOutput(System.out);
1189 
1190         ModuleBootstrap.limitedFinder().findAll().stream()
1191             .sorted(new JrtFirstComparator())
1192             .forEach(LauncherHelper::showModule);
1193     }
1194 
1195     /**
1196      * Called by the launcher to show the resolved modules
1197      */
1198     static void showResolvedModules() {
1199         initOutput(System.out);
1200 
1201         ModuleLayer bootLayer = ModuleLayer.boot();
1202         Configuration cf = bootLayer.configuration();
1203 
1204         cf.modules().stream()
1205             .map(ResolvedModule::reference)
1206             .sorted(new JrtFirstComparator())
1207             .forEach(LauncherHelper::showModule);
1208     }
1209 
1210     /**
1211      * Called by the launcher to describe a module
1212      */
1213     static void describeModule(String moduleName) {
1214         initOutput(System.out);
1215 
1216         ModuleFinder finder = ModuleBootstrap.limitedFinder();
1217         ModuleReference mref = finder.find(moduleName).orElse(null);
1218         if (mref == null) {
1219             abort(null, "java.launcher.module.error4", moduleName);
1220         }
1221         ModuleDescriptor md = mref.descriptor();
1222 
1223         // one-line summary
1224         showModule(mref);
1225 
1226         // unqualified exports (sorted by package)
1227         md.exports().stream()
1228             .filter(e -> !e.isQualified())
1229             .sorted(Comparator.comparing(Exports::source))
1230             .map(e -> Stream.concat(Stream.of(e.source()),
1231                                     toStringStream(e.modifiers()))
1232                     .collect(Collectors.joining(" ")))
1233             .forEach(sourceAndMods -> ostream.format("exports %s%n", sourceAndMods));
1234 
1235         // dependences
1236         for (Requires r : md.requires()) {
1237             String nameAndMods = Stream.concat(Stream.of(r.name()),
1238                                                toStringStream(r.modifiers()))
1239                     .collect(Collectors.joining(" "));
1240             ostream.format("requires %s", nameAndMods);
1241             finder.find(r.name())
1242                 .map(ModuleReference::descriptor)
1243                 .filter(ModuleDescriptor::isAutomatic)
1244                 .ifPresent(any -> ostream.print(" automatic"));
1245             ostream.println();
1246         }
1247 
1248         // service use and provides
1249         for (String s : md.uses()) {
1250             ostream.format("uses %s%n", s);
1251         }
1252         for (Provides ps : md.provides()) {
1253             String names = ps.providers().stream().collect(Collectors.joining(" "));
1254             ostream.format("provides %s with %s%n", ps.service(), names);
1255 
1256         }
1257 
1258         // qualified exports
1259         for (Exports e : md.exports()) {
1260             if (e.isQualified()) {
1261                 String who = e.targets().stream().collect(Collectors.joining(" "));
1262                 ostream.format("qualified exports %s to %s%n", e.source(), who);
1263             }
1264         }
1265 
1266         // open packages
1267         for (Opens opens: md.opens()) {
1268             if (opens.isQualified())
1269                 ostream.print("qualified ");
1270             String sourceAndMods = Stream.concat(Stream.of(opens.source()),
1271                                                  toStringStream(opens.modifiers()))
1272                     .collect(Collectors.joining(" "));
1273             ostream.format("opens %s", sourceAndMods);
1274             if (opens.isQualified()) {
1275                 String who = opens.targets().stream().collect(Collectors.joining(" "));
1276                 ostream.format(" to %s", who);
1277             }
1278             ostream.println();
1279         }
1280 
1281         // non-exported/non-open packages
1282         Set<String> concealed = new TreeSet<>(md.packages());
1283         md.exports().stream().map(Exports::source).forEach(concealed::remove);
1284         md.opens().stream().map(Opens::source).forEach(concealed::remove);
1285         concealed.forEach(p -> ostream.format("contains %s%n", p));
1286     }
1287 
1288     /**
1289      * Prints a single line with the module name, version and modifiers
1290      */
1291     private static void showModule(ModuleReference mref) {
1292         ModuleDescriptor md = mref.descriptor();
1293         ostream.print(md.toNameAndVersion());
1294         mref.location()
1295                 .filter(uri -> !isJrt(uri))
1296                 .ifPresent(uri -> ostream.format(" %s", uri));
1297         if (md.isOpen())
1298             ostream.print(" open");
1299         if (md.isAutomatic())
1300             ostream.print(" automatic");
1301         ostream.println();
1302     }
1303 
1304     /**
1305      * A ModuleReference comparator that considers modules in the run-time
1306      * image to be less than modules than not in the run-time image.
1307      */
1308     private static class JrtFirstComparator implements Comparator<ModuleReference> {
1309         private final Comparator<ModuleReference> real;
1310 
1311         JrtFirstComparator() {
1312             this.real = Comparator.comparing(ModuleReference::descriptor);
1313         }
1314 
1315         @Override
1316         public int compare(ModuleReference a, ModuleReference b) {
1317             if (isJrt(a)) {
1318                 return isJrt(b) ? real.compare(a, b) : -1;
1319             } else {
1320                 return isJrt(b) ? 1 : real.compare(a, b);
1321             }
1322         }
1323     }
1324 
1325     private static <T> Stream<String> toStringStream(Set<T> s) {
1326         return s.stream().map(e -> e.toString().toLowerCase(Locale.ROOT));
1327     }
1328 
1329     private static boolean isJrt(ModuleReference mref) {
1330         return isJrt(mref.location().orElse(null));
1331     }
1332 
1333     private static boolean isJrt(URI uri) {
1334         return (uri != null && uri.getScheme().equalsIgnoreCase("jrt"));
1335     }
1336 
1337 }