8166420: Confusing error message when reading bad module declaration
Reviewed-by: jlahoda
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java Mon Feb 06 11:11:43 2017 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java Mon Feb 06 18:14:51 2017 +0530
@@ -45,6 +45,7 @@
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
+import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardLocation;
import com.sun.source.util.TaskEvent;
@@ -623,7 +624,8 @@
keepComments = true;
genEndPos = true;
}
- Parser parser = parserFactory.newParser(content, keepComments(), genEndPos, lineDebugInfo);
+ Parser parser = parserFactory.newParser(content, keepComments(), genEndPos,
+ lineDebugInfo, filename.isNameCompatible("module-info", Kind.SOURCE));
tree = parser.parseCompilationUnit();
if (verbose) {
log.printVerbose("parsing.done", Long.toString(elapsed(msec)));
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Feb 06 11:11:43 2017 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Feb 06 18:14:51 2017 +0530
@@ -70,6 +70,10 @@
*/
private static final int infixPrecedenceLevels = 10;
+ /** Is the parser instantiated to parse a module-info file ?
+ */
+ private final boolean parseModuleInfo;
+
/** The scanner used for lexical analysis.
*/
protected Lexer S;
@@ -138,10 +142,21 @@
/** Construct a parser from a given scanner, tree factory and log.
*/
protected JavacParser(ParserFactory fac,
+ Lexer S,
+ boolean keepDocComments,
+ boolean keepLineMap,
+ boolean keepEndPositions) {
+ this(fac, S, keepDocComments, keepLineMap, keepEndPositions, false);
+
+ }
+ /** Construct a parser from a given scanner, tree factory and log.
+ */
+ protected JavacParser(ParserFactory fac,
Lexer S,
boolean keepDocComments,
boolean keepLineMap,
- boolean keepEndPositions) {
+ boolean keepEndPositions,
+ boolean parseModuleInfo) {
this.S = S;
nextToken(); // prime the pump
this.F = fac.F;
@@ -165,6 +180,7 @@
this.allowUnderscoreIdentifier = source.allowUnderscoreIdentifier();
this.allowPrivateInterfaceMethods = source.allowPrivateInterfaceMethods();
this.keepDocComments = keepDocComments;
+ this.parseModuleInfo = parseModuleInfo;
docComments = newDocCommentTable(keepDocComments, fac);
this.keepLineMap = keepLineMap;
this.errorTree = F.Erroneous();
@@ -3347,8 +3363,13 @@
} else {
errs = List.of(mods);
}
- return toP(F.Exec(syntaxError(pos, errs, "expected3",
- CLASS, INTERFACE, ENUM)));
+ final JCErroneous erroneousTree;
+ if (parseModuleInfo) {
+ erroneousTree = syntaxError(pos, errs, "expected.module.or.open");
+ } else {
+ erroneousTree = syntaxError(pos, errs, "expected3", CLASS, INTERFACE, ENUM);
+ }
+ return toP(F.Exec(erroneousTree));
}
}
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ParserFactory.java Mon Feb 06 11:11:43 2017 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ParserFactory.java Mon Feb 06 18:14:51 2017 +0530
@@ -81,7 +81,11 @@
}
public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) {
+ return newParser(input, keepDocComments, keepEndPos, keepLineMap, false);
+ }
+
+ public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap, boolean parseModuleInfo) {
Lexer lexer = scannerFactory.newScanner(input, keepDocComments);
- return new JavacParser(this, lexer, keepDocComments, keepLineMap, keepEndPos);
+ return new JavacParser(this, lexer, keepDocComments, keepLineMap, keepEndPos, parseModuleInfo);
}
}
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Mon Feb 06 11:11:43 2017 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Mon Feb 06 18:14:51 2017 +0530
@@ -1895,6 +1895,9 @@
compiler.err.expected.module=\
''module'' expected
+compiler.err.expected.module.or.open=\
+ ''module'' or ''open'' expected
+
compiler.err.dot.class.expected=\
''.class'' expected
--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/ReplParserFactory.java Mon Feb 06 11:11:43 2017 +0100
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/ReplParserFactory.java Mon Feb 06 18:14:51 2017 +0530
@@ -56,4 +56,9 @@
com.sun.tools.javac.parser.Lexer lexer = scannerFactory.newScanner(input, keepDocComments);
return new ReplParser(this, lexer, keepDocComments, keepLineMap, keepEndPos);
}
+
+ @Override
+ public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap, boolean parseModuleInfo) {
+ return newParser(input, keepDocComments, keepEndPos, keepLineMap);
+ }
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/UnexpectedTokenInModuleInfo/module-info.java Mon Feb 06 18:14:51 2017 +0530
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2017, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.expected.module.or.open
+
+weak module m {}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/modules/UnexpectedTokenInModuleInfoTest.java Mon Feb 06 18:14:51 2017 +0530
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2017, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8166420
+ * @summary Confusing error message when reading bad module declaration
+ * @library /tools/lib
+ * @modules
+ * jdk.compiler/com.sun.tools.javac.api
+ * jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
+ * @run main UnexpectedTokenInModuleInfoTest
+ */
+
+import java.nio.file.*;
+import java.util.List;
+import java.util.Arrays;
+
+import toolbox.JavacTask;
+import toolbox.Task;
+
+public class UnexpectedTokenInModuleInfoTest extends ModuleTestBase {
+ public static void main(String... args) throws Exception {
+ UnexpectedTokenInModuleInfoTest t = new UnexpectedTokenInModuleInfoTest();
+ t.runTests();
+ }
+
+ @Test
+ public void testSingleModule(Path base) throws Exception {
+ Path src = base.resolve("src");
+ tb.writeFile(src.resolve("module-info.java"), "weak module m { }");
+
+ List<String> output = new JavacTask(tb)
+ .options("-XDrawDiagnostics")
+ .files(src.resolve("module-info.java"))
+ .run(Task.Expect.FAIL)
+ .writeAll()
+ .getOutputLines(Task.OutputKind.DIRECT);
+
+ List<String> expected = Arrays.asList("module-info.java:1:1: compiler.err.expected.module.or.open",
+ "1 error");
+ if (!output.containsAll(expected)) {
+ throw new Exception("Expected output not found");
+ }
+ }
+}
\ No newline at end of file
--- a/langtools/test/tools/javac/parser/extend/TrialParserFactory.java Mon Feb 06 11:11:43 2017 +0100
+++ b/langtools/test/tools/javac/parser/extend/TrialParserFactory.java Mon Feb 06 18:14:51 2017 +0530
@@ -52,4 +52,9 @@
com.sun.tools.javac.parser.Lexer lexer = scannerFactory.newScanner(input, keepDocComments);
return new TrialParser(this, lexer, keepDocComments, keepLineMap, keepEndPos);
}
+
+ @Override
+ public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap, boolean parseModuleInfo) {
+ return newParser(input, keepDocComments, keepEndPos, keepLineMap);
+ }
}