8205418: Assorted improvements to source code model

Improving tree positions, better error recovery, fixing Trees.getScope for possibly erroneous lambdas.

Reviewed-by: jjg, mcimadamore, vromero
This commit is contained in:
Jan Lahoda 2018-06-29 10:41:10 +02:00
parent 69191fc4cc
commit eaf0364068
14 changed files with 434 additions and 40 deletions

View file

@ -23,7 +23,7 @@
/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205913
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@ -51,6 +51,7 @@ import com.sun.source.tree.VariableTree;
import com.sun.source.tree.WhileLoopTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
@ -1037,6 +1038,105 @@ public class JavacParserTest extends TestCase {
assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors);
}
@Test
void testTypeParamsWithoutMethod() throws IOException {
assert tool != null;
String code = "package test; class Test { /**javadoc*/ |public <T> |}";
String[] parts = code.split("\\|");
code = parts[0] + parts[1] + parts[2];
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
Trees trees = Trees.instance(ct);
SourcePositions pos = trees.getSourcePositions();
CompilationUnitTree cut = ct.parse().iterator().next();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ErroneousTree err = (ErroneousTree) clazz.getMembers().get(0);
MethodTree method = (MethodTree) err.getErrorTrees().get(0);
final int methodStart = parts[0].length();
final int methodEnd = parts[0].length() + parts[1].length();
assertEquals("testTypeParamsWithoutMethod",
methodStart, pos.getStartPosition(cut, method));
assertEquals("testTypeParamsWithoutMethod",
methodEnd, pos.getEndPosition(cut, method));
TreePath path2Method = new TreePath(new TreePath(new TreePath(cut), clazz), method);
String javadoc = trees.getDocComment(path2Method);
if (!"javadoc".equals(javadoc)) {
throw new AssertionError("Expected javadoc not found, actual javadoc: " + javadoc);
}
}
@Test
void testAnalyzeParensWithComma1() throws IOException {
assert tool != null;
String code = "package test; class Test { FI fi = |(s, |";
String[] parts = code.split("\\|", 3);
code = parts[0] + parts[1] + parts[2];
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
Trees trees = Trees.instance(ct);
SourcePositions pos = trees.getSourcePositions();
CompilationUnitTree cut = ct.parse().iterator().next();
boolean[] found = new boolean[1];
new TreeScanner<Void, Void>() {
@Override
public Void visitLambdaExpression(LambdaExpressionTree tree, Void v) {
found[0] = true;
int lambdaStart = parts[0].length();
int lambdaEnd = parts[0].length() + parts[1].length();
assertEquals("testAnalyzeParensWithComma1",
lambdaStart, pos.getStartPosition(cut, tree));
assertEquals("testAnalyzeParensWithComma1",
lambdaEnd, pos.getEndPosition(cut, tree));
return null;
}
}.scan(cut, null);
assertTrue("testAnalyzeParensWithComma1", found[0]);
}
@Test
void testAnalyzeParensWithComma2() throws IOException {
assert tool != null;
String code = "package test; class Test { FI fi = |(s, o)|";
String[] parts = code.split("\\|", 3);
code = parts[0] + parts[1] + parts[2];
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
Trees trees = Trees.instance(ct);
SourcePositions pos = trees.getSourcePositions();
CompilationUnitTree cut = ct.parse().iterator().next();
boolean[] found = new boolean[1];
new TreeScanner<Void, Void>() {
@Override
public Void visitLambdaExpression(LambdaExpressionTree tree, Void v) {
found[0] = true;
int lambdaStart = parts[0].length();
int lambdaEnd = parts[0].length() + parts[1].length();
assertEquals("testAnalyzeParensWithComma2",
lambdaStart, pos.getStartPosition(cut, tree));
assertEquals("testAnalyzeParensWithComma2",
lambdaEnd, pos.getEndPosition(cut, tree));
return null;
}
}.scan(cut, null);
assertTrue("testAnalyzeParensWithComma2", found[0]);
}
void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)
@ -1082,6 +1182,12 @@ abstract class TestCase {
}
}
void assertTrue(String message, boolean bvalue) {
if (bvalue == false) {
fail(message);
}
}
void assertEquals(String message, int i, long l) {
if (i != l) {
fail(message + ":" + i + ":" + l);