< prev index next >

src/jdk.jpackage/share/classes/jdk/jpackage/internal/JLinkBundlerHelper.java

Print this page

        

@@ -43,10 +43,15 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.Set;
 import java.util.Optional;
+import java.util.Arrays;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.lang.module.Configuration;
+import java.lang.module.ResolvedModule;
 import java.lang.module.ModuleDescriptor;
 import java.lang.module.ModuleFinder;
 import java.lang.module.ModuleReference;
 
 import jdk.tools.jlink.internal.packager.AppRuntimeImageBuilder;

@@ -180,11 +185,11 @@
         Set<String> limitModules =
                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
         Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
         Set<String> addModules = getValidModules(modulePath,
                 StandardBundlerParam.ADD_MODULES.fetchFrom(params),
-                limitModules, true);
+                limitModules);
 
 
         if (javaBasePath != null && javaBasePath.toFile().exists()) {
             result = getModuleVersion(javaBasePath.toFile(),
                     modulePath, addModules, limitModules);

@@ -228,14 +233,13 @@
 
         return result;
     }
 
     private static Set<String> getValidModules(List<Path> modulePath,
-            Set<String> addModules, Set<String> limitModules,
-            boolean forJRE) {
+            Set<String> addModules, Set<String> limitModules) {
         ModuleHelper moduleHelper = new ModuleHelper(
-                modulePath, addModules, limitModules, forJRE);
+                modulePath, addModules, limitModules);
         return removeInvalidModules(modulePath, moduleHelper.modules());
     }
 
     static void execute(Map<String, ? super Object> params,
             AbstractAppImageBuilder imageBuilder)

@@ -260,21 +264,20 @@
             mainJarType = ModFile.ModType.UnnamedJar;
         }
 
         // Modules
 
-        // The default for an unnamed jar is ALL_DEFAULT with the
-        // non-valid modules removed.
         if (mainJarType == ModFile.ModType.UnnamedJar) {
-            addModules.add(ModuleHelper.ALL_RUNTIME);
-        } else if (mainJarType == ModFile.ModType.Unknown ||
+            // The default for an unnamed jar is ALL_DEFAULT
+            addModules.add(ModuleHelper.ALL_DEFAULT);
+        } if (mainJarType == ModFile.ModType.Unknown ||
                 mainJarType == ModFile.ModType.ModularJar) {
             String mainModule = getMainModule(params);
             addModules.add(mainModule);
         }
         addModules.addAll(getValidModules(
-                modulePath, addModules, limitModules, false));
+                modulePath, addModules, limitModules));
 
         Log.verbose(MessageFormat.format(
                 I18N.getString("message.modules"), addModules.toString()));
 
         AppRuntimeImageBuilder appRuntimeBuilder = new AppRuntimeImageBuilder();

@@ -300,13 +303,13 @@
         Set<String> limitModules =
                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
         boolean stripNativeCommands =
                 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
         Path outputDir = imageBuilder.getRoot();
-        addModules.add(ModuleHelper.ALL_RUNTIME);
+        addModules.add(ModuleHelper.ALL_MODULE_PATH);
         Set<String> redistModules = getValidModules(modulePath,
-                addModules, limitModules, true);
+                addModules, limitModules);
         addModules.addAll(redistModules);
 
         Log.verbose(MessageFormat.format(
                 I18N.getString("message.modules"), addModules.toString()));
 

@@ -338,10 +341,55 @@
         }
 
         return result;
     }
 
+    /*
+     * Returns the set of modules that would be visible by default for
+     * a non-modular-aware application consisting of the given elements.
+     */
+    private static Set<String> getDefaultModules(
+            Path[] paths, String[] addModules) {
+
+        // the modules in the run-time image that export an API
+        Stream<String> systemRoots = ModuleFinder.ofSystem().findAll().stream()
+                .map(ModuleReference::descriptor)
+                .filter(descriptor -> exportsAPI(descriptor))
+                .map(ModuleDescriptor::name);
+
+        Set<String> roots;
+        if (addModules == null || addModules.length == 0) {
+            roots = systemRoots.collect(Collectors.toSet());
+        } else {
+            var extraRoots =  Stream.of(addModules);
+            roots = Stream.concat(systemRoots,
+                    extraRoots).collect(Collectors.toSet());
+        }
+
+        ModuleFinder finder = ModuleFinder.ofSystem();
+        if (paths != null && paths.length > 0) {
+            finder = ModuleFinder.compose(finder, ModuleFinder.of(paths));
+        }
+        return Configuration.empty()
+                .resolveAndBind(finder, ModuleFinder.of(), roots)
+                .modules()
+                .stream()
+                .map(ResolvedModule::name)
+                .collect(Collectors.toSet());
+    } 
+
+    /*
+     * Returns true if the given module exports an API to all module.
+     */
+    private static boolean exportsAPI(ModuleDescriptor descriptor) {
+        return descriptor.exports()
+                .stream()
+                .filter(e -> !e.isQualified())
+                .findAny()
+                .isPresent();
+    }
+
     private static Set<String> removeInvalidModules(
             List<Path> modulePath, Set<String> modules) {
         Set<String> result = new LinkedHashSet<String>();
         ModuleManager mm = new ModuleManager(modulePath);
         List<ModFile> lmodfiles =

@@ -396,75 +444,61 @@
     private static class ModuleHelper {
         // The token for "all modules on the module path".
         private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
 
         // The token for "all valid runtime modules".
-        static final String ALL_RUNTIME = "ALL-RUNTIME";
+        static final String ALL_DEFAULT = "ALL-DEFAULT";
 
         private final Set<String> modules = new HashSet<>();
         private enum Macros {None, AllModulePath, AllRuntime}
 
-        ModuleHelper(List<Path> paths, Set<String> roots,
-                Set<String> limitMods, boolean forJRE) {
-            Macros macro = Macros.None;
+        ModuleHelper(List<Path> paths, Set<String> addModules,
+                Set<String> limitModules) {
+            boolean addAllModulePath = false;
+            boolean addDefaultMods = false;
 
-            for (Iterator<String> iterator = roots.iterator();
+            for (Iterator<String> iterator = addModules.iterator();
                     iterator.hasNext();) {
                 String module = iterator.next();
 
                 switch (module) {
                     case ALL_MODULE_PATH:
                         iterator.remove();
-                        macro = Macros.AllModulePath;
+                        addAllModulePath = true;
                         break;
-                    case ALL_RUNTIME:
+                    case ALL_DEFAULT:
                         iterator.remove();
-                        macro = Macros.AllRuntime;
+                        addDefaultMods = true;
                         break;
                     default:
                         this.modules.add(module);
                 }
             }
 
-            switch (macro) {
-                case AllModulePath:
+            if (addAllModulePath) {
                     this.modules.addAll(getModuleNamesFromPath(paths));
-                    break;
-                case AllRuntime:
-                    Set<Module> runtimeModules =
-                            ModuleLayer.boot().modules();
-                    for (Module m : runtimeModules) {
-                        String name = m.getName();
-                        if (forJRE && isModuleExcludedFromJRE(name)) {
-                            continue;  // JRE does not include this module
-                        }
-                        this.modules.add(name);
-                    }
-                    break;
+            } else if (addDefaultMods) {
+                this.modules.addAll(getDefaultModules(
+                        paths.toArray(new Path[0]),
+                        addModules.toArray(new String[0])));
             }
         }
 
         Set<String> modules() {
             return modules;
         }
 
-        private boolean isModuleExcludedFromJRE(String name) {
-            return false;  // not excluding any modules from JRE at this time
-        }
-
         private static Set<String> getModuleNamesFromPath(List<Path> Value) {
                 Set<String> result = new LinkedHashSet<String>();
                 ModuleManager mm = new ModuleManager(Value);
-                List<ModFile> modFiles =
-                        mm.getModules(
+            List<ModFile> modFiles = mm.getModules(
                                 EnumSet.of(ModuleManager.SearchType.ModularJar,
                                 ModuleManager.SearchType.Jmod,
                                 ModuleManager.SearchType.ExplodedModule));
 
                 for (ModFile modFile : modFiles) {
                     result.add(modFile.getModName());
                 }
-
                 return result;
         }
     }
 }
< prev index next >