8201544: Improve javac command line parsing and error reporting
authorsdama
Thu, 14 Feb 2019 21:52:39 +0530
changeset 53755 4cd89541b492
parent 53754 bc8fb1fa55b5
child 53756 adfb1c71f52c
8201544: Improve javac command line parsing and error reporting Summary: Modified exception into an error message for invalid filenames on windows Reviewed-by: vromero, jjg Contributed-by: srinivas.dama@oracle.com
src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
test/langtools/tools/javac/diags/examples.not-yet.txt
test/langtools/tools/javac/options/T6986895.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java	Thu Feb 14 07:41:54 2019 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java	Thu Feb 14 21:52:39 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
 import java.io.PrintWriter;
 import java.lang.module.ModuleDescriptor;
 import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.text.Collator;
@@ -752,14 +753,18 @@
         @Override
         public void process(OptionHelper helper, String option) throws InvalidValueException {
             if (option.endsWith(".java") ) {
-                Path p = Paths.get(option);
-                if (!Files.exists(p)) {
-                    throw helper.newInvalidValueException(Errors.FileNotFound(p.toString()));
+                try {
+                    Path p = Paths.get(option);
+                    if (!Files.exists(p)) {
+                        throw helper.newInvalidValueException(Errors.FileNotFound(p.toString()));
+                    }
+                    if (!Files.isRegularFile(p)) {
+                        throw helper.newInvalidValueException(Errors.FileNotFile(p));
+                    }
+                    helper.addFile(p);
+                } catch (InvalidPathException ex) {
+                    throw helper.newInvalidValueException(Errors.InvalidPath(option));
                 }
-                if (!Files.isRegularFile(p)) {
-                    throw helper.newInvalidValueException(Errors.FileNotFile(p));
-                }
-                helper.addFile(p);
             } else {
                 helper.addClassName(option);
             }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Feb 14 07:41:54 2019 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Feb 14 21:52:39 2019 +0530
@@ -1977,6 +1977,11 @@
 compiler.warn.invalid.path=\
     Invalid filename: {0}
 
+# 0: string
+compiler.err.invalid.path=\
+    Invalid filename: {0}
+
+
 # 0: path
 compiler.warn.invalid.archive.file=\
     Unexpected file on path: {0}
--- a/test/langtools/tools/javac/diags/examples.not-yet.txt	Thu Feb 14 07:41:54 2019 -0800
+++ b/test/langtools/tools/javac/diags/examples.not-yet.txt	Thu Feb 14 21:52:39 2019 +0530
@@ -125,6 +125,7 @@
 compiler.misc.bad.const.pool.entry                      # constant pool entry has wrong type
 compiler.warn.access.to.member.from.serializable.lambda # in order to generate it we need to modify a restricted package
 compiler.warn.invalid.path                              # this warning is generated only in Windows systems
+compiler.err.invalid.path                               # this error is generated only in Windows systems
 compiler.note.multiple.elements                         # needs user code
 compiler.err.preview.feature.disabled.classfile         # preview feature support: needs compilation against classfile
 compiler.warn.preview.feature.use.classfile             # preview feature support: needs compilation against classfile
--- a/test/langtools/tools/javac/options/T6986895.java	Thu Feb 14 07:41:54 2019 -0800
+++ b/test/langtools/tools/javac/options/T6986895.java	Thu Feb 14 21:52:39 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 6986895
+ * @bug 8201544
  * @summary compiler gives misleading message for no input files
  * @modules jdk.compiler
  */
@@ -38,6 +39,8 @@
 
     String noSourceFiles = "no source files";
     String noSourceFilesOrClasses = "no source files or class names";
+    String invalidFileName = "Invalid filename";
+    boolean isWindows = System.getProperty("os.name").startsWith("Windows");
 
     void run() throws Exception {
         Locale prev = Locale.getDefault();
@@ -45,6 +48,8 @@
             Locale.setDefault(Locale.ENGLISH);
             test(noSourceFiles,           "-Werror");
             test(noSourceFilesOrClasses,  "-Werror", "-Xprint");
+            if (isWindows)
+                test(invalidFileName, "-Werror", "someNonExistingFile*.java");
         } finally {
             Locale.setDefault(prev);
         }