8155955: packager needs to determine the root modules to create JRE image
authormchung
Sun, 19 Jun 2016 16:46:49 -0700
changeset 39125 e97953d38291
parent 39107 2a5697a98620
child 39126 1d9492ef71e9
8155955: packager needs to determine the root modules to create JRE image Reviewed-by: alanb
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/packager/AppRuntimeImageBuilder.java
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Wed Jul 05 21:52:00 2017 +0200
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Sun Jun 19 16:46:49 2016 -0700
@@ -39,15 +39,7 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.Date;
-import java.util.Formatter;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import jdk.internal.module.ConfigurableModuleFinder;
@@ -96,11 +88,9 @@
         }, "--help"),
         new Option<JlinkTask>(true, (task, opt, arg) -> {
             String[] dirs = arg.split(File.pathSeparator);
-            task.options.modulePath = new Path[dirs.length];
-            int i = 0;
-            for (String dir : dirs) {
-                task.options.modulePath[i++] = Paths.get(dir);
-            }
+            Arrays.stream(dirs)
+                  .map(Paths::get)
+                  .forEach(task.options.modulePath::add);
         }, "--modulepath", "--mp"),
         new Option<JlinkTask>(true, (task, opt, arg) -> {
             for (String mn : arg.split(",")) {
@@ -176,7 +166,7 @@
         String  saveoptsfile;
         boolean version;
         boolean fullVersion;
-        Path[] modulePath;
+        List<Path> modulePath = new ArrayList<>();
         Set<String> limitMods = new HashSet<>();
         Set<String> addMods = new HashSet<>();
         Path output;
@@ -203,7 +193,7 @@
                 return EXIT_OK;
             }
             if (taskHelper.getExistingImage() == null) {
-                if (options.modulePath == null || options.modulePath.length == 0) {
+                if (options.modulePath == null || options.modulePath.isEmpty()) {
                     throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true);
                 }
                 createImage();
@@ -245,7 +235,7 @@
      * Jlink API entry point.
      */
     public static void createImage(JlinkConfiguration config,
-            PluginsConfiguration plugins)
+                                   PluginsConfiguration plugins)
             throws Exception {
         Objects.requireNonNull(config);
         Objects.requireNonNull(config.getOutput());
@@ -254,10 +244,9 @@
         if (config.getModulepaths().isEmpty()) {
             throw new Exception("Empty module paths");
         }
-        Path[] arr = new Path[config.getModulepaths().size()];
-        arr = config.getModulepaths().toArray(arr);
+
         ModuleFinder finder
-                = newModuleFinder(arr, config.getLimitmods(), config.getModules());
+                = newModuleFinder(config.getModulepaths(), config.getLimitmods(), config.getModules());
 
         // First create the image provider
         ImageProvider imageProvider
@@ -332,10 +321,12 @@
         return addMods;
     }
 
-    private static ModuleFinder newModuleFinder(Path[] paths,
-            Set<String> limitMods,
-            Set<String> addMods) {
-        ModuleFinder finder = ModuleFinder.of(paths);
+    public static ModuleFinder newModuleFinder(List<Path> paths,
+                                               Set<String> limitMods,
+                                               Set<String> addMods)
+    {
+
+        ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[0]));
 
         // jmods are located at link-time
         if (finder instanceof ConfigurableModuleFinder) {
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/packager/AppRuntimeImageBuilder.java	Wed Jul 05 21:52:00 2017 +0200
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/packager/AppRuntimeImageBuilder.java	Sun Jun 19 16:46:49 2016 -0700
@@ -27,36 +27,19 @@
 
 
 import jdk.tools.jlink.Jlink;
-import jdk.tools.jlink.builder.ImageBuilder;
+import jdk.tools.jlink.builder.DefaultImageBuilder;
+import jdk.tools.jlink.internal.JlinkTask;
 import jdk.tools.jlink.plugin.Plugin;
-import jdk.tools.jlink.builder.*;
-import jdk.tools.jlink.plugin.ModulePool;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.StringReader;
+import java.lang.module.ModuleFinder;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
-import java.util.EnumSet;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
-import java.util.ResourceBundle;
 import java.util.Set;
-import java.util.StringTokenizer;
-import java.util.TreeSet;
-import java.util.stream.Collectors;
 
 /**
  * AppRuntimeImageBuilder is a private API used only by the Java Packager to generate
@@ -143,4 +126,14 @@
         Jlink jlink = new Jlink();
         jlink.build(jlinkConfig, pluginConfig);
     }
+
+    /**
+     * Returns a ModuleFinder that limits observability to the given root
+     * modules, their transitive dependences, plus a set of other modules.
+     */
+    public static ModuleFinder moduleFinder(List<Path> modulepaths,
+                                            Set<String> roots,
+                                            Set<String> otherModules) {
+        return JlinkTask.newModuleFinder(modulepaths, roots, otherModules);
+    }
 }