8166810: jlink should fail on extra arguments
authorsundar
Wed, 26 Oct 2016 10:42:55 +0530
changeset 41766 b5eab76a23a9
parent 41765 e830711d95ac
child 41767 0301970d37d3
8166810: jlink should fail on extra arguments Reviewed-by: jlaskey
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties
jdk/test/tools/jlink/JLinkTest.java
jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Tue Oct 25 14:55:29 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Wed Oct 26 10:42:55 2016 +0530
@@ -186,7 +186,7 @@
                    new PrintWriter(System.err, true));
         }
         try {
-            optionsHelper.handleOptions(this, args);
+            optionsHelper.handleOptionsNoUnhandled(this, args);
             if (options.help) {
                 optionsHelper.showHelp(PROGNAME);
                 return EXIT_OK;
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java	Tue Oct 25 14:55:29 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java	Wed Oct 26 10:42:55 2016 +0530
@@ -466,7 +466,21 @@
             return pp;
         }
 
+        // used by jimage. Return unhandled arguments like "create", "describe".
         public List<String> handleOptions(T task, String[] args) throws BadArgs {
+            return handleOptions(task, args, true);
+        }
+
+        // used by jlink. No unhandled arguments like "create", "describe".
+        void handleOptionsNoUnhandled(T task, String[] args) throws BadArgs {
+            handleOptions(task, args, false);
+        }
+
+        // shared code that handles options for both jlink and jimage. jimage uses arguments like
+        // "create", "describe" etc. as "task names". Those arguments are unhandled here and returned
+        // as "unhandled arguments list". jlink does not want such arguments. "collectUnhandled" flag
+        // tells whether to allow for unhandled arguments or not.
+        private List<String> handleOptions(T task, String[] args, boolean collectUnhandled) throws BadArgs {
             // findbugs warning, copy instead of keeping a reference.
             command = Arrays.copyOf(args, args.length);
 
@@ -499,10 +513,10 @@
             String[] arr = new String[filteredArgs.size()];
             args = filteredArgs.toArray(arr);
 
-            List<String> rest = new ArrayList<>();
+            List<String> rest = collectUnhandled? new ArrayList<>() : null;
             // process options
             for (int i = 0; i < args.length; i++) {
-                if (!args[i].isEmpty() && args[i].charAt(0) == '-') {
+                if (args[i].charAt(0) == '-') {
                     String name = args[i];
                     PlugOption pluginOption = null;
                     Option<T> option = getOption(name);
@@ -539,7 +553,12 @@
                         i = args.length;
                     }
                 } else {
-                    rest.add(args[i]);
+                    if (collectUnhandled) {
+                        rest.add(args[i]);
+                    } else {
+                        throw new BadArgs("err.orphan.argument", args[i]).
+                            showUsage(true);
+                    }
                 }
             }
             return rest;
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties	Tue Oct 25 14:55:29 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties	Wed Oct 26 10:42:55 2016 +0530
@@ -95,6 +95,7 @@
 err.dir.exists={0} already exists
 err.badpattern=bad pattern {0}
 err.unknown.option=unknown option: {0}
+err.orphan.argument=orphan argument: {0}
 err.missing.arg=no value given for {0}
 err.internal.error=internal error: {0} {1} {2}
 err.invalid.arg.for.option=invalid argument for option: {0}
--- a/jdk/test/tools/jlink/JLinkTest.java	Tue Oct 25 14:55:29 2016 -0700
+++ b/jdk/test/tools/jlink/JLinkTest.java	Wed Oct 26 10:42:55 2016 +0530
@@ -109,19 +109,16 @@
                     .modulePath(helper.defaultModulePath())
                     .output(helper.createNewImageDir(moduleName))
                     .addMods("leaf1")
-                    .option("")
                     .call().assertSuccess();
             JImageGenerator.getJLinkTask()
                     .modulePath(helper.defaultModulePath())
                     .addMods("leaf1")
                     .option("--output")
-                    .option("")
                     .call().assertFailure("Error: no value given for --output");
             JImageGenerator.getJLinkTask()
                     .modulePath("")
                     .output(helper.createNewImageDir(moduleName))
                     .addMods("leaf1")
-                    .option("")
                     .call().assertFailure("Error: no value given for --module-path");
         }
 
@@ -132,7 +129,6 @@
                     .modulePath(helper.defaultModulePath())
                     .output(helper.createNewImageDir(moduleName))
                     .addMods("m")
-                    .option("")
                     .call().assertSuccess();
             moduleName = "mod";
             jmod = helper.generateDefaultJModule(moduleName).assertSuccess();
@@ -140,7 +136,6 @@
                     .modulePath(helper.defaultModulePath())
                     .output(helper.createNewImageDir(moduleName))
                     .addMods("m")
-                    .option("")
                     .call().assertSuccess();
         }
 
@@ -282,18 +277,21 @@
             helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level invalid");
         }
 
-        // @file
+        // orphan argument - JDK-8166810
         {
-            Path path = Paths.get("embedded.properties");
-            Files.write(path, Collections.singletonList("--strip-debug --add-modules " +
-                    "toto.unknown --compress UNKNOWN\n"));
-            String[] userOptions = {"@", path.toAbsolutePath().toString()};
-            String moduleName = "configembeddednocompresscomposite2";
+            String[] userOptions = {"--compress", "2", "foo" };
+            String moduleName = "orphanarg1";
             helper.generateDefaultJModule(moduleName, "composite2");
-            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
-            helper.checkImage(imageDir, moduleName, null, null);
+            helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: orphan argument: foo");
         }
 
+        // orphan argument - JDK-8166810
+        {
+            String[] userOptions = {"--output", "foo", "bar" };
+            String moduleName = "orphanarg2";
+            helper.generateDefaultJModule(moduleName, "composite2");
+            helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: orphan argument: bar");
+        }
     }
 
     private static void testCompress(Helper helper, String moduleName, String... userOptions) throws IOException {
--- a/jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java	Tue Oct 25 14:55:29 2016 -0700
+++ b/jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java	Wed Oct 26 10:42:55 2016 +0530
@@ -433,13 +433,23 @@
 
         for (Object[] data : testData) {
             // create image for each test data
-            System.out.println("Invoking jlink with \"" + data[INCLUDE_LOCALES_OPTION] + "\"");
-            Result result = JImageGenerator.getJLinkTask()
+            Result result;
+            if (data[INCLUDE_LOCALES_OPTION].toString().isEmpty()) {
+                System.out.println("Invoking jlink with no --include-locales option");
+                result = JImageGenerator.getJLinkTask()
+                    .modulePath(helper.defaultModulePath())
+                    .output(helper.createNewImageDir(moduleName))
+                    .addMods((String) data[ADDMODS_OPTION])
+                    .call();
+            } else {
+                System.out.println("Invoking jlink with \"" + data[INCLUDE_LOCALES_OPTION] + "\"");
+                result = JImageGenerator.getJLinkTask()
                     .modulePath(helper.defaultModulePath())
                     .output(helper.createNewImageDir(moduleName))
                     .addMods((String) data[ADDMODS_OPTION])
                     .option((String) data[INCLUDE_LOCALES_OPTION])
                     .call();
+            }
 
             String errorMsg = (String) data[ERROR_MESSAGE];
             if (errorMsg.isEmpty()) {