8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
authorsundar
Wed, 28 Sep 2016 22:13:14 +0530
changeset 41212 54a8bb41b174
parent 41211 744278856e58
child 41213 129d3db3b466
8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options Reviewed-by: mchung, jlaskey
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
jdk/test/tools/jlink/JLinkTest.java
jdk/test/tools/lib/tests/JImageGenerator.java
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Wed Sep 28 19:21:42 2016 +0300
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Wed Sep 28 22:13:14 2016 +0530
@@ -86,6 +86,9 @@
             task.options.help = true;
         }, "--help", "-h"),
         new Option<JlinkTask>(true, (task, opt, arg) -> {
+            // if used multiple times, the last one wins!
+            // So, clear previous values, if any.
+            task.options.modulePath.clear();
             String[] dirs = arg.split(File.pathSeparator);
             int i = 0;
             Arrays.stream(dirs)
@@ -93,6 +96,9 @@
                   .forEach(task.options.modulePath::add);
         }, "--module-path", "-p"),
         new Option<JlinkTask>(true, (task, opt, arg) -> {
+            // if used multiple times, the last one wins!
+            // So, clear previous values, if any.
+            task.options.limitMods.clear();
             for (String mn : arg.split(",")) {
                 if (mn.isEmpty()) {
                     throw taskHelper.newBadArgs("err.mods.must.be.specified",
--- a/jdk/test/tools/jlink/JLinkTest.java	Wed Sep 28 19:21:42 2016 +0300
+++ b/jdk/test/tools/jlink/JLinkTest.java	Wed Sep 28 22:13:14 2016 +0530
@@ -150,6 +150,43 @@
         }
 
         {
+            String moduleName = "m_8165735"; // JDK-8165735
+            helper.generateDefaultJModule(moduleName+"dependency").assertSuccess();
+            Path jmod = helper.generateDefaultJModule(moduleName, moduleName+"dependency").assertSuccess();
+            JImageGenerator.getJLinkTask()
+                    .modulePath(helper.defaultModulePath())
+                    .repeatedModulePath(".") // second --module-path overrides the first one
+                    .output(helper.createNewImageDir(moduleName))
+                    .addMods(moduleName)
+                    // second --module-path does not have that module
+                    .call().assertFailure("Error: Module m_8165735 not found");
+
+            JImageGenerator.getJLinkTask()
+                    .modulePath(".") // first --module-path overridden later
+                    .repeatedModulePath(helper.defaultModulePath())
+                    .output(helper.createNewImageDir(moduleName))
+                    .addMods(moduleName)
+                    // second --module-path has that module
+                    .call().assertSuccess();
+
+            JImageGenerator.getJLinkTask()
+                    .modulePath(helper.defaultModulePath())
+                    .output(helper.createNewImageDir(moduleName))
+                    .limitMods(moduleName)
+                    .repeatedLimitMods("java.base") // second --limit-modules overrides first
+                    .addMods(moduleName)
+                    .call().assertFailure("Error: Module m_8165735dependency not found, required by m_8165735");
+
+            JImageGenerator.getJLinkTask()
+                    .modulePath(helper.defaultModulePath())
+                    .output(helper.createNewImageDir(moduleName))
+                    .limitMods("java.base")
+                    .repeatedLimitMods(moduleName) // second --limit-modules overrides first
+                    .addMods(moduleName)
+                    .call().assertSuccess();
+        }
+
+        {
             // Help
             StringWriter writer = new StringWriter();
             jdk.tools.jlink.internal.Main.run(new String[]{"--help"}, new PrintWriter(writer));
--- a/jdk/test/tools/lib/tests/JImageGenerator.java	Wed Sep 28 19:21:42 2016 +0300
+++ b/jdk/test/tools/lib/tests/JImageGenerator.java	Wed Sep 28 22:13:14 2016 +0530
@@ -564,6 +564,10 @@
         private final List<String> limitMods = new ArrayList<>();
         private final List<String> options = new ArrayList<>();
         private String modulePath;
+        // if you want to specifiy repeated --module-path option
+        private String repeatedModulePath;
+        // if you want to specifiy repeated --limit-modules option
+        private String repeatedLimitMods;
         private Path output;
         private Path existing;
 
@@ -572,6 +576,11 @@
             return this;
         }
 
+        public JLinkTask repeatedModulePath(String modulePath) {
+            this.repeatedModulePath = modulePath;
+            return this;
+        }
+
         public JLinkTask addJars(Path jars) {
             this.jars.add(jars);
             return this;
@@ -597,6 +606,11 @@
             return this;
         }
 
+        public JLinkTask repeatedLimitMods(String modules) {
+            this.repeatedLimitMods = modules;
+            return this;
+        }
+
         public JLinkTask output(Path output) {
             this.output = output;
             return this;
@@ -639,6 +653,10 @@
                 options.add(LIMIT_MODULES_OPTION);
                 options.add(limitMods.stream().collect(Collectors.joining(",")));
             }
+            if (repeatedLimitMods != null) {
+                options.add(LIMIT_MODULES_OPTION);
+                options.add(repeatedLimitMods);
+            }
             if (!jars.isEmpty() || !jmods.isEmpty()) {
                 options.add(MODULE_PATH_OPTION);
                 options.add(modulePath());
@@ -647,6 +665,10 @@
                 options.add(MODULE_PATH_OPTION);
                 options.add(modulePath);
             }
+            if (repeatedModulePath != null) {
+                options.add(MODULE_PATH_OPTION);
+                options.add(repeatedModulePath);
+            }
             if (!pluginModulePath.isEmpty()) {
                 options.add(PLUGIN_MODULE_PATH);
                 options.add(toPath(pluginModulePath));