8166420: Confusing error message when reading bad module declaration
Reviewed-by: jlahoda
This commit is contained in:
parent
e7c5d26299
commit
d6090047be
@ -45,6 +45,7 @@ import javax.lang.model.element.ElementVisitor;
|
||||
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 @@ public class JavaCompiler {
|
||||
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)));
|
||||
|
@ -70,6 +70,10 @@ public class JavacParser implements Parser {
|
||||
*/
|
||||
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;
|
||||
@ -135,13 +139,24 @@ public class JavacParser implements Parser {
|
||||
CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }}
|
||||
}
|
||||
|
||||
/** 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 @@ public class JavacParser implements Parser {
|
||||
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 @@ public class JavacParser implements Parser {
|
||||
} 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,7 +81,11 @@ public class ParserFactory {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1895,6 +1895,9 @@ compiler.err.premature.eof=\
|
||||
compiler.err.expected.module=\
|
||||
''module'' expected
|
||||
|
||||
compiler.err.expected.module.or.open=\
|
||||
''module'' or ''open'' expected
|
||||
|
||||
compiler.err.dot.class.expected=\
|
||||
''.class'' expected
|
||||
|
||||
|
@ -56,4 +56,9 @@ class ReplParserFactory extends ParserFactory {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {}
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
@ -52,4 +52,9 @@ class TrialParserFactory extends ParserFactory {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user