8165634: Support multiple --add-modules options on the command line
authorhseigel
Sat, 10 Sep 2016 08:27:40 -0400
changeset 41114 f83e9aebbab4
parent 41113 8ba3d8daf78d
child 41115 364a0c517151
8165634: Support multiple --add-modules options on the command line Reviewed-by: alanb Contributed-by: mandy.chung@oracle.com
jdk/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java
jdk/test/tools/launcher/modules/addmods/AddModsTest.java
--- a/jdk/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Fri Sep 09 11:25:33 2016 -0700
+++ b/jdk/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java	Sat Sep 10 08:27:40 2016 -0400
@@ -153,27 +153,24 @@
         boolean addAllDefaultModules = false;
         boolean addAllSystemModules = false;
         boolean addAllApplicationModules = false;
-        String propValue = getAndRemoveProperty("jdk.module.addmods");
-        if (propValue != null) {
-            for (String mod: propValue.split(",")) {
-                switch (mod) {
-                    case ALL_DEFAULT:
-                        addAllDefaultModules = true;
-                        break;
-                    case ALL_SYSTEM:
-                        addAllSystemModules = true;
-                        break;
-                    case ALL_MODULE_PATH:
-                        addAllApplicationModules = true;
-                        break;
-                    default :
-                        roots.add(mod);
-                }
+        for (String mod: getExtraAddModules()) {
+            switch (mod) {
+                case ALL_DEFAULT:
+                    addAllDefaultModules = true;
+                    break;
+                case ALL_SYSTEM:
+                    addAllSystemModules = true;
+                    break;
+                case ALL_MODULE_PATH:
+                    addAllApplicationModules = true;
+                    break;
+                default :
+                    roots.add(mod);
             }
         }
 
         // --limit-modules
-        propValue = getAndRemoveProperty("jdk.module.limitmods");
+        String propValue = getAndRemoveProperty("jdk.module.limitmods");
         if (propValue != null) {
             Set<String> mods = new HashSet<>();
             for (String mod: propValue.split(",")) {
@@ -392,6 +389,32 @@
         }
     }
 
+    /**
+     * Returns the set of module names specified via --add-modules options
+     * on the command line
+     */
+    private static Set<String> getExtraAddModules() {
+        String prefix = "jdk.module.addmods.";
+        int index = 0;
+
+        // the system property is removed after decoding
+        String value = getAndRemoveProperty(prefix + index);
+        if (value == null) {
+            return Collections.emptySet();
+        }
+
+        Set<String> modules = new HashSet<>();
+        while (value != null) {
+            for (String s : value.split(",")) {
+                if (s.length() > 0) modules.add(s);
+            }
+
+            index++;
+            value = getAndRemoveProperty(prefix + index);
+        }
+
+        return modules;
+    }
 
     /**
      * Process the --add-reads options to add any additional read edges that
@@ -514,7 +537,7 @@
 
             // value is <module>(,<module>)*
             if (map.containsKey(key))
-                fail(key + " specified more than once");
+                 fail(key + " specified more than once");
 
             Set<String> values = new HashSet<>();
             map.put(key, values);
--- a/jdk/test/tools/launcher/modules/addmods/AddModsTest.java	Fri Sep 09 11:25:33 2016 -0700
+++ b/jdk/test/tools/launcher/modules/addmods/AddModsTest.java	Sat Sep 10 08:27:40 2016 -0400
@@ -31,6 +31,7 @@
  * @summary Basic test for java --add-modules
  */
 
+import java.io.File;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
@@ -224,6 +225,27 @@
 
 
     /**
+     * Tests {@code --add-modules} be specified more than once.
+     */
+    public void testWithMultipleAddModules() throws Exception {
+
+        String modulepath = MODS1_DIR.toString() + File.pathSeparator +
+                                MODS2_DIR.toString();
+        int exitValue
+            = executeTestJava("--module-path", modulepath,
+            "--add-modules", LOGGER_MODULE,
+            "--add-modules", TEST_MODULE,
+            "-m", TEST_MID,
+            "logger.Logger")
+            .outputTo(System.out)
+            .errorTo(System.out)
+            .getExitValue();
+
+        assertTrue(exitValue == 0);
+    }
+
+
+    /**
      * Attempt to run with a bad module name specified to --add-modules
      */
     public void testRunWithBadAddMods() throws Exception {