package sun.launcher;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Exports;
import java.lang.module.ModuleDescriptor.Opens;
import java.lang.module.ModuleDescriptor.Provides;
import java.lang.module.ModuleDescriptor.Requires;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.lang.module.ResolvedModule;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Locale.Category;
import java.util.Optional;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.internal.misc.VM;
import jdk.internal.module.ModuleBootstrap;
import jdk.internal.module.Modules;
import jdk.internal.platform.Container;
import jdk.internal.platform.Metrics;
public final class LauncherHelper {
private LauncherHelper() {}
private static final String JAVAFX_APPLICATION_MARKER =
"JavaFX-Application-Class";
private static final String JAVAFX_APPLICATION_CLASS_NAME =
"javafx.application.Application";
private static final String JAVAFX_FXHELPER_CLASS_NAME_SUFFIX =
"sun.launcher.LauncherHelper$FXHelper";
private static final String LAUNCHER_AGENT_CLASS = "Launcher-Agent-Class";
private static final String MAIN_CLASS = "Main-Class";
private static final String ADD_EXPORTS = "Add-Exports";
private static final String ADD_OPENS = "Add-Opens";
private static StringBuilder outBuf = new StringBuilder();
private static final String INDENT = " ";
private static final String VM_SETTINGS = "VM settings:";
private static final String PROP_SETTINGS = "Property settings:";
private static final String LOCALE_SETTINGS = "Locale settings:";
private static final String diagprop = "sun.java.launcher.diag";
static final boolean trace = VM.getSavedProperty(diagprop) != null;
private static final String defaultBundleName =
"sun.launcher.resources.launcher";
private static class ResourceBundleHolder {
private static final ResourceBundle RB =
ResourceBundle.getBundle(defaultBundleName);
}
private static PrintStream ostream;
private static Class<?> appClass;
@SuppressWarnings("fallthrough")
static void showSettings(boolean printToStderr, String optionFlag,
long initialHeapSize, long maxHeapSize, long stackSize) {
initOutput(printToStderr);
String opts[] = optionFlag.split(":");
String optStr = (opts.length > 1 && opts[1] != null)
? opts[1].trim()
: "all";
switch (optStr) {
case "vm":
printVmSettings(initialHeapSize, maxHeapSize, stackSize);
break;
case "properties":
printProperties();
break;
case "locale":
printLocale();
break;
case "system":
if (System.getProperty("os.name").contains("Linux")) {
printSystemMetrics();
break;
}
default:
printVmSettings(initialHeapSize, maxHeapSize, stackSize);
printProperties();
printLocale();
if (System.getProperty("os.name").contains("Linux")) {
printSystemMetrics();
}
break;
}
}
private static void printVmSettings(
long initialHeapSize, long maxHeapSize,
long stackSize) {
ostream.println(VM_SETTINGS);
if (stackSize != 0L) {
ostream.println(INDENT + "Stack Size: " +
SizePrefix.scaleValue(stackSize));
}
if (initialHeapSize != 0L) {
ostream.println(INDENT + "Min. Heap Size: " +
SizePrefix.scaleValue(initialHeapSize));
}
if (maxHeapSize != 0L) {
ostream.println(INDENT + "Max. Heap Size: " +
SizePrefix.scaleValue(maxHeapSize));
} else {
ostream.println(INDENT + "Max. Heap Size (Estimated): "
+ SizePrefix.scaleValue(Runtime.getRuntime().maxMemory()));
}
ostream.println(INDENT + "Using VM: "
+ System.getProperty("java.vm.name"));
ostream.println();
}
private static void printProperties() {
Properties p = System.getProperties();
ostream.println(PROP_SETTINGS);
List<String> sortedPropertyKeys = new ArrayList<>();
sortedPropertyKeys.addAll(p.stringPropertyNames());
Collections.sort(sortedPropertyKeys);
for (String x : sortedPropertyKeys) {
printPropertyValue(x, p.getProperty(x));
}
ostream.println();
}
private static boolean isPath(String key) {
return key.endsWith(".dirs") || key.endsWith(".path");
}
private static void printPropertyValue(String key, String value) {
ostream.print(INDENT + key + " = ");
if (key.equals("line.separator")) {
for (byte b : value.getBytes()) {
switch (b) {
case 0xd:
ostream.print("\\r ");
break;
case 0xa:
ostream.print("\\n ");
break;
default:
ostream.printf("0x%02X", b & 0xff);
break;
}
}
ostream.println();
return;
}
if (!isPath(key)) {
ostream.println(value);
return;
}
String[] values = value.split(System.getProperty("path.separator"));
boolean first = true;
for (String s : values) {
if (first) {
ostream.println(s);
first = false;
} else {
ostream.println(INDENT + INDENT + s);
}
}
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayName());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocales() {
Locale[] tlocales = Locale.getAvailableLocales();
final int len = tlocales == null ? 0 : tlocales.length;
if (len < 1 ) {
return;
}
Set<String> sortedSet = new TreeSet<>();
for (Locale l : tlocales) {
sortedSet.add(l.toString());
}
ostream.print(INDENT + "available locales = ");
Iterator<String> iter = sortedSet.iterator();
final int last = len - 1;
for (int i = 0 ; iter.hasNext() ; i++) {
String s = iter.next();
ostream.print(s);
if (i != last) {
ostream.print(", ");
}
if ((i + 1) % 8 == 0) {
ostream.println();
ostream.print(INDENT + INDENT);
}
}
}
public static void printSystemMetrics() {
Metrics c = Container.metrics();
ostream.println("Operating System Metrics:");
if (c == null) {
ostream.println(INDENT + "No metrics available for this platform");
return;
}
final long longRetvalNotSupported = -2;
ostream.println(INDENT + "Provider: " + c.getProvider());
ostream.println(INDENT + "Effective CPU Count: " + c.getEffectiveCpuCount());
ostream.println(formatCpuVal(c.getCpuPeriod(), INDENT + "CPU Period: ", longRetvalNotSupported));
ostream.println(formatCpuVal(c.getCpuQuota(), INDENT + "CPU Quota: ", longRetvalNotSupported));
ostream.println(formatCpuVal(c.getCpuShares(), INDENT + "CPU Shares: ", longRetvalNotSupported));
int cpus[] = c.getCpuSetCpus();
if (cpus != null) {
ostream.println(INDENT + "List of Processors, "
+ cpus.length + " total: ");
ostream.print(INDENT);
for (int i = 0; i < cpus.length; i++) {
ostream.print(cpus[i] + " ");
}
if (cpus.length > 0) {
ostream.println("");
}
} else {
ostream.println(INDENT + "List of Processors: N/A");
}
cpus = c.getEffectiveCpuSetCpus();
if (cpus != null) {
ostream.println(INDENT + "List of Effective Processors, "
+ cpus.length + " total: ");
ostream.print(INDENT);
for (int i = 0; i < cpus.length; i++) {
ostream.print(cpus[i] + " ");
}
if (cpus.length > 0) {
ostream.println("");
}
} else {
ostream.println(INDENT + "List of Effective Processors: N/A");
}
int mems[] = c.getCpuSetMems();
if (mems != null) {
ostream.println(INDENT + "List of Memory Nodes, "
+ mems.length + " total: ");
ostream.print(INDENT);
for (int i = 0; i < mems.length; i++) {
ostream.print(mems[i] + " ");
}
if (mems.length > 0) {
ostream.println("");
}
} else {
ostream.println(INDENT + "List of Memory Nodes: N/A");
}
mems = c.getEffectiveCpuSetMems();
if (mems != null) {
ostream.println(INDENT + "List of Available Memory Nodes, "
+ mems.length + " total: ");
ostream.print(INDENT);
for (int i = 0; i < mems.length; i++) {
ostream.print(mems[i] + " ");
}
if (mems.length > 0) {
ostream.println("");
}
} else {
ostream.println(INDENT + "List of Available Memory Nodes: N/A");
}
long limit = c.getMemoryLimit();
ostream.println(formatLimitString(limit, INDENT + "Memory Limit: ", longRetvalNotSupported));
limit = c.getMemorySoftLimit();
ostream.println(formatLimitString(limit, INDENT + "Memory Soft Limit: ", longRetvalNotSupported));
limit = c.getMemoryAndSwapLimit();
ostream.println(formatLimitString(limit, INDENT + "Memory & Swap Limit: ", longRetvalNotSupported));
ostream.println("");
}
private static String formatLimitString(long limit, String prefix, long unavailable) {
if (limit >= 0) {
return prefix + SizePrefix.scaleValue(limit);
} else if (limit == unavailable) {
return prefix + "N/A";
} else {
return prefix + "Unlimited";
}
}
private static String formatCpuVal(long cpuVal, String prefix, long unavailable) {
if (cpuVal >= 0) {
return prefix + cpuVal + "us";
} else if (cpuVal == unavailable) {
return prefix + "N/A";
} else {
return prefix + cpuVal;
}
}
private enum SizePrefix {
KILO(1024, "K"),
MEGA(1024 * 1024, "M"),
GIGA(1024 * 1024 * 1024, "G"),
TERA(1024L * 1024L * 1024L * 1024L, "T");
long size;
String abbrev;
SizePrefix(long size, String abbrev) {
this.size = size;
this.abbrev = abbrev;
}
private static String scale(long v, SizePrefix prefix) {
return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size),
2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev;
}
static String scaleValue(long v) {
if (v < MEGA.size) {
return scale(v, KILO);
} else if (v < GIGA.size) {
return scale(v, MEGA);
} else if (v < TERA.size) {
return scale(v, GIGA);
} else {
return scale(v, TERA);
}
}
}
private static String getLocalizedMessage(String key, Object... args) {
String msg = ResourceBundleHolder.RB.getString(key);
return (args != null) ? MessageFormat.format(msg, args) : msg;
}
static void initHelpMessage(String progname) {
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header",
(progname == null) ? "java" : progname ));
}
static void appendVmSelectMessage(String vm1, String vm2) {
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.vmselect",
vm1, vm2));
}
static void appendVmSynonymMessage(String vm1, String vm2) {
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.hotspot",
vm1, vm2));
}
static void printHelpMessage(boolean printToStderr) {
initOutput(printToStderr);
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.footer",
File.pathSeparator));
ostream.println(outBuf.toString());
}
static void printXUsageMessage(boolean printToStderr) {
initOutput(printToStderr);
ostream.println(getLocalizedMessage("java.launcher.X.usage",
File.pathSeparator));
if (System.getProperty("os.name").contains("OS X")) {
ostream.println(getLocalizedMessage("java.launcher.X.macosx.usage",
File.pathSeparator));
}
}
static void initOutput(boolean printToStderr) {
ostream = (printToStderr) ? System.err : System.out;
}
static void initOutput(PrintStream ps) {
ostream = ps;
}
static String getMainClassFromJar(String jarname) {
String mainValue;
try (JarFile jarFile = new JarFile(jarname)) {
Manifest manifest = jarFile.getManifest();
if (manifest == null) {
abort(null, "java.launcher.jar.error2", jarname);
}
Attributes mainAttrs = manifest.getMainAttributes();
if (mainAttrs == null) {
abort(null, "java.launcher.jar.error3", jarname);
}
mainValue = mainAttrs.getValue(MAIN_CLASS);
if (mainValue == null) {
abort(null, "java.launcher.jar.error3", jarname);
}
String agentClass = mainAttrs.getValue(LAUNCHER_AGENT_CLASS);
if (agentClass != null) {
ModuleLayer.boot().findModule("java.instrument").ifPresent(m -> {
try {
String cn = "sun.instrument.InstrumentationImpl";
Class<?> clazz = Class.forName(cn, false, null);
Method loadAgent = clazz.getMethod("loadAgent", String.class);
loadAgent.invoke(null, jarname);
} catch (Throwable e) {
if (e instanceof InvocationTargetException) e = e.getCause();
abort(e, "java.launcher.jar.error4", jarname);
}
});
}
String exports = mainAttrs.getValue(ADD_EXPORTS);
if (exports != null) {
addExportsOrOpens(exports, false);
}
String opens = mainAttrs.getValue(ADD_OPENS);
if (opens != null) {
addExportsOrOpens(opens, true);
}
if (mainAttrs.containsKey(
new Attributes.Name(JAVAFX_APPLICATION_MARKER))) {
FXHelper.setFXLaunchParameters(jarname, LM_JAR);
return FXHelper.class.getName();
}
return mainValue.trim();
} catch (IOException ioe) {
abort(ioe, "java.launcher.jar.error1", jarname);
}
return null;
}
static void addExportsOrOpens(String value, boolean open) {
for (String moduleAndPackage : value.split(" ")) {
String[] s = moduleAndPackage.trim().split("/");
if (s.length == 2) {
String mn = s[0];
String pn = s[1];
ModuleLayer.boot()
.findModule(mn)
.filter(m -> m.getDescriptor().packages().contains(pn))
.ifPresent(m -> {
if (open) {
Modules.addOpensToAllUnnamed(m, pn);
} else {
Modules.addExportsToAllUnnamed(m, pn);
}
});
}
}
}
private static final int LM_UNKNOWN = 0;
private static final int LM_CLASS = 1;
private static final int LM_JAR = 2;
private static final int LM_MODULE = 3;
private static final int LM_SOURCE = 4;
static void abort(Throwable t, String msgKey, Object... args) {
if (msgKey != null) {
ostream.println(getLocalizedMessage(msgKey, args));
}
if (trace) {
if (t != null) {
t.printStackTrace();
} else {
Thread.dumpStack();
}
}
System.exit(1);
}
@SuppressWarnings("fallthrough")
public static Class<?> checkAndLoadMain(boolean printToStderr,
int mode,
String what) {
initOutput(printToStderr);
Class<?> mainClass = null;
switch (mode) {
case LM_MODULE: case LM_SOURCE:
mainClass = loadModuleMainClass(what);
break;
default:
mainClass = loadMainClass(mode, what);
break;
}
appClass = mainClass;
if (JAVAFX_FXHELPER_CLASS_NAME_SUFFIX.equals(mainClass.getName()) ||
doesExtendFXApplication(mainClass)) {
FXHelper.setFXLaunchParameters(what, mode);
mainClass = FXHelper.class;
}
validateMainClass(mainClass);
return mainClass;
}
private static Class<?> loadModuleMainClass(String what) {
int i = what.indexOf('/');
String mainModule;
String mainClass;
if (i == -1) {
mainModule = what;
mainClass = null;
} else {
mainModule = what.substring(0, i);
mainClass = what.substring(i+1);
}
ModuleLayer layer = ModuleLayer.boot();
Optional<Module> om = layer.findModule(mainModule);
if (!om.isPresent()) {
throw new InternalError("Module " + mainModule + " not in boot Layer");
}
Module m = om.get();
if (mainClass == null) {
Optional<String> omc = m.getDescriptor().mainClass();
if (!omc.isPresent()) {
abort(null, "java.launcher.module.error1", mainModule);
}
mainClass = omc.get();
}
Class<?> c = null;
try {
c = Class.forName(m, mainClass);
if (c == null && System.getProperty("os.name", "").contains("OS X")
&& Normalizer.isNormalized(mainClass, Normalizer.Form.NFD)) {
String cn = Normalizer.normalize(mainClass, Normalizer.Form.NFC);
c = Class.forName(m, cn);
}
} catch (LinkageError le) {
abort(null, "java.launcher.module.error3", mainClass, m.getName(),
le.getClass().getName() + ": " + le.getLocalizedMessage());
}
if (c == null) {
abort(null, "java.launcher.module.error2", mainClass, mainModule);
}
System.setProperty("jdk.module.main.class", c.getName());
return c;
}
private static Class<?> loadMainClass(int mode, String what) {
String cn;
switch (mode) {
case LM_CLASS:
cn = what;
break;
case LM_JAR:
cn = getMainClassFromJar(what);
break;
default:
throw new InternalError("" + mode + ": Unknown launch mode");
}
cn = cn.replace('/', '.');
Class<?> mainClass = null;
ClassLoader scl = ClassLoader.getSystemClassLoader();
try {
try {
mainClass = Class.forName(cn, false, scl);
} catch (NoClassDefFoundError | ClassNotFoundException cnfe) {
if (System.getProperty("os.name", "").contains("OS X")
&& Normalizer.isNormalized(cn, Normalizer.Form.NFD)) {
try {
String ncn = Normalizer.normalize(cn, Normalizer.Form.NFC);
mainClass = Class.forName(ncn, false, scl);
} catch (NoClassDefFoundError | ClassNotFoundException cnfe1) {
abort(cnfe1, "java.launcher.cls.error1", cn,
cnfe1.getClass().getCanonicalName(), cnfe1.getMessage());
}
} else {
abort(cnfe, "java.launcher.cls.error1", cn,
cnfe.getClass().getCanonicalName(), cnfe.getMessage());
}
}
} catch (LinkageError le) {
abort(le, "java.launcher.cls.error6", cn,
le.getClass().getName() + ": " + le.getLocalizedMessage());
}
return mainClass;
}
public static Class<?> getApplicationClass() {
return appClass;
}
private static boolean doesExtendFXApplication(Class<?> mainClass) {
for (Class<?> sc = mainClass.getSuperclass(); sc != null;
sc = sc.getSuperclass()) {
if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) {
return true;
}
}
return false;
}
static void validateMainClass(Class<?> mainClass) {
Method mainMethod = null;
try {
mainMethod = mainClass.getMethod("main", String[].class);
} catch (NoSuchMethodException nsme) {
abort(null, "java.launcher.cls.error4", mainClass.getName(),
JAVAFX_APPLICATION_CLASS_NAME);
} catch (Throwable e) {
if (mainClass.getModule().isNamed()) {
abort(e, "java.launcher.module.error5",
mainClass.getName(), mainClass.getModule().getName(),
e.getClass().getName(), e.getLocalizedMessage());
} else {
abort(e, "java.launcher.cls.error7", mainClass.getName(),
e.getClass().getName(), e.getLocalizedMessage());
}
}
int mod = mainMethod.getModifiers();
if (!Modifier.isStatic(mod)) {
abort(null, "java.launcher.cls.error2", "static",
mainMethod.getDeclaringClass().getName());
}
if (mainMethod.getReturnType() != java.lang.Void.TYPE) {
abort(null, "java.launcher.cls.error3",
mainMethod.getDeclaringClass().getName());
}
}
private static final String encprop = "sun.jnu.encoding";
private static String encoding = null;
private static boolean isCharsetSupported = false;
static String makePlatformString(boolean printToStderr, byte[] inArray) {
initOutput(printToStderr);
if (encoding == null) {
encoding = System.getProperty(encprop);
isCharsetSupported = Charset.isSupported(encoding);
}
try {
String out = isCharsetSupported
? new String(inArray, encoding)
: new String(inArray);
return out;
} catch (UnsupportedEncodingException uee) {
abort(uee, null);
}
return null;
}
static String[] expandArgs(String[] argArray) {
List<StdArg> aList = new ArrayList<>();
for (String x : argArray) {
aList.add(new StdArg(x));
}
return expandArgs(aList);
}
static String[] expandArgs(List<StdArg> argList) {
ArrayList<String> out = new ArrayList<>();
if (trace) {
System.err.println("Incoming arguments:");
}
for (StdArg a : argList) {
if (trace) {
System.err.println(a);
}
if (a.needsExpansion) {
File x = new File(a.arg);
File parent = x.getParentFile();
String glob = x.getName();
if (parent == null) {
parent = new File(".");
}
try (DirectoryStream<Path> dstream =
Files.newDirectoryStream(parent.toPath(), glob)) {
int entries = 0;
for (Path p : dstream) {
out.add(p.normalize().toString());
entries++;
}
if (entries == 0) {
out.add(a.arg);
}
} catch (Exception e) {
out.add(a.arg);
if (trace) {
System.err.println("Warning: passing argument as-is " + a);
System.err.print(e);
}
}
} else {
out.add(a.arg);
}
}
String[] oarray = new String[out.size()];
out.toArray(oarray);
if (trace) {
System.err.println("Expanded arguments:");
for (String x : oarray) {
System.err.println(x);
}
}
return oarray;
}
private static class StdArg {
final String arg;
final boolean needsExpansion;
StdArg(String arg, boolean expand) {
this.arg = arg;
this.needsExpansion = expand;
}
StdArg(String in) {
this.arg = in.substring(1);
needsExpansion = in.charAt(0) == 'T';
}
public String toString() {
return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}';
}
}
static final class FXHelper {
private static final String JAVAFX_GRAPHICS_MODULE_NAME =
"javafx.graphics";
private static final String JAVAFX_LAUNCHER_CLASS_NAME =
"com.sun.javafx.application.LauncherImpl";
private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS";
private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR";
private static final String JAVAFX_LAUNCH_MODE_MODULE = "LM_MODULE";
private static String fxLaunchName = null;
private static String fxLaunchMode = null;
private static Class<?> fxLauncherClass = null;
private static Method fxLauncherMethod = null;
private static void setFXLaunchParameters(String what, int mode) {
Optional<Module> om = ModuleLayer.boot().findModule(JAVAFX_GRAPHICS_MODULE_NAME);
if (!om.isPresent()) {
abort(null, "java.launcher.cls.error5");
}
fxLauncherClass = Class.forName(om.get(), JAVAFX_LAUNCHER_CLASS_NAME);
if (fxLauncherClass == null) {
abort(null, "java.launcher.cls.error5");
}
try {
fxLauncherMethod = fxLauncherClass.getMethod("launchApplication",
String.class, String.class, String[].class);
int mod = fxLauncherMethod.getModifiers();
if (!Modifier.isStatic(mod)) {
abort(null, "java.launcher.javafx.error1");
}
if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) {
abort(null, "java.launcher.javafx.error1");
}
} catch (NoSuchMethodException ex) {
abort(ex, "java.launcher.cls.error5", ex);
}
fxLaunchName = what;
switch (mode) {
case LM_CLASS:
fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS;
break;
case LM_JAR:
fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR;
break;
case LM_MODULE:
fxLaunchMode = JAVAFX_LAUNCH_MODE_MODULE;
break;
default:
throw new InternalError(mode + ": Unknown launch mode");
}
}
public static void main(String... args) throws Exception {
if (fxLauncherMethod == null
|| fxLaunchMode == null
|| fxLaunchName == null) {
throw new RuntimeException("Invalid JavaFX launch parameters");
}
fxLauncherMethod.invoke(null,
new Object[] {fxLaunchName, fxLaunchMode, args});
}
}
static void listModules() {
initOutput(System.out);
ModuleBootstrap.limitedFinder().findAll().stream()
.sorted(new JrtFirstComparator())
.forEach(LauncherHelper::showModule);
}
static void showResolvedModules() {
initOutput(System.out);
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration cf = bootLayer.configuration();
cf.modules().stream()
.map(ResolvedModule::reference)
.sorted(new JrtFirstComparator())
.forEach(LauncherHelper::showModule);
}
static void describeModule(String moduleName) {
initOutput(System.out);
ModuleFinder finder = ModuleBootstrap.limitedFinder();
ModuleReference mref = finder.find(moduleName).orElse(null);
if (mref == null) {
abort(null, "java.launcher.module.error4", moduleName);
}
ModuleDescriptor md = mref.descriptor();
showModule(mref);
md.exports().stream()
.filter(e -> !e.isQualified())
.sorted(Comparator.comparing(Exports::source))
.map(e -> Stream.concat(Stream.of(e.source()),
toStringStream(e.modifiers()))
.collect(Collectors.joining(" ")))
.forEach(sourceAndMods -> ostream.format("exports %s%n", sourceAndMods));
for (Requires r : md.requires()) {
String nameAndMods = Stream.concat(Stream.of(r.name()),
toStringStream(r.modifiers()))
.collect(Collectors.joining(" "));
ostream.format("requires %s", nameAndMods);
finder.find(r.name())
.map(ModuleReference::descriptor)
.filter(ModuleDescriptor::isAutomatic)
.ifPresent(any -> ostream.print(" automatic"));
ostream.println();
}
for (String s : md.uses()) {
ostream.format("uses %s%n", s);
}
for (Provides ps : md.provides()) {
String names = ps.providers().stream().collect(Collectors.joining(" "));
ostream.format("provides %s with %s%n", ps.service(), names);
}
for (Exports e : md.exports()) {
if (e.isQualified()) {
String who = e.targets().stream().collect(Collectors.joining(" "));
ostream.format("qualified exports %s to %s%n", e.source(), who);
}
}
for (Opens opens: md.opens()) {
if (opens.isQualified())
ostream.print("qualified ");
String sourceAndMods = Stream.concat(Stream.of(opens.source()),
toStringStream(opens.modifiers()))
.collect(Collectors.joining(" "));
ostream.format("opens %s", sourceAndMods);
if (opens.isQualified()) {
String who = opens.targets().stream().collect(Collectors.joining(" "));
ostream.format(" to %s", who);
}
ostream.println();
}
Set<String> concealed = new TreeSet<>(md.packages());
md.exports().stream().map(Exports::source).forEach(concealed::remove);
md.opens().stream().map(Opens::source).forEach(concealed::remove);
concealed.forEach(p -> ostream.format("contains %s%n", p));
}
private static void showModule(ModuleReference mref) {
ModuleDescriptor md = mref.descriptor();
ostream.print(md.toNameAndVersion());
mref.location()
.filter(uri -> !isJrt(uri))
.ifPresent(uri -> ostream.format(" %s", uri));
if (md.isOpen())
ostream.print(" open");
if (md.isAutomatic())
ostream.print(" automatic");
ostream.println();
}
private static class JrtFirstComparator implements Comparator<ModuleReference> {
private final Comparator<ModuleReference> real;
JrtFirstComparator() {
this.real = Comparator.comparing(ModuleReference::descriptor);
}
@Override
public int compare(ModuleReference a, ModuleReference b) {
if (isJrt(a)) {
return isJrt(b) ? real.compare(a, b) : -1;
} else {
return isJrt(b) ? 1 : real.compare(a, b);
}
}
}
private static <T> Stream<String> toStringStream(Set<T> s) {
return s.stream().map(e -> e.toString().toLowerCase());
}
private static boolean isJrt(ModuleReference mref) {
return isJrt(mref.location().orElse(null));
}
private static boolean isJrt(URI uri) {
return (uri != null && uri.getScheme().equalsIgnoreCase("jrt"));
}
}