8256411: Based anonymous classes have a weird end position

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2020-12-08 08:38:24 +00:00
parent 0b6b6eb59a
commit 51ac37686c
3 changed files with 39 additions and 4 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac

@ -2555,7 +2555,7 @@ public class Attr extends JCTree.Visitor {
((JCIdent) clazzid).name);
EndPosTable endPosTable = this.env.toplevel.endPositions;
endPosTable.storeEnd(clazzid1, tree.getEndPosition(endPosTable));
endPosTable.storeEnd(clazzid1, clazzid.getEndPosition(endPosTable));
if (clazz.hasTag(ANNOTATED_TYPE)) {
JCAnnotatedType annoType = (JCAnnotatedType) clazz;
List<JCAnnotation> annos = annoType.annotations;

@ -23,7 +23,7 @@
/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@ -84,6 +84,7 @@ import javax.tools.ToolProvider;
import com.sun.source.tree.CaseTree;
import com.sun.source.util.TreePathScanner;
import java.util.Objects;
public class JavacParserTest extends TestCase {
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
@ -1611,6 +1612,40 @@ public class JavacParserTest extends TestCase {
""");
}
@Test //JDK-8256411
void testBasedAnonymous() throws IOException {
String code = """
package t;
class Test {
class I {}
static Object I = new Test().new I() {};
}
""";
StringWriter out = new StringWriter();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(out, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
Trees trees = Trees.instance(ct);
SourcePositions sp = trees.getSourcePositions();
ct.analyze();
List<String> span = new ArrayList<>();
new TreeScanner<Void, Void>() {
public Void visitClass(ClassTree ct, Void v) {
if (ct.getExtendsClause() != null) {
int start = (int) sp.getStartPosition(cut,
ct.getExtendsClause());
int end = (int) sp.getEndPosition(cut,
ct.getExtendsClause());
span.add(code.substring(start, end));
}
return super.visitClass(ct, v);
}
}.scan(cut, null);
if (!Objects.equals(span, Arrays.asList("I"))) {
throw new AssertionError("Unexpected span: " + span);
}
}
void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)

@ -127,8 +127,8 @@ public class TreeEndPosTest {
}
static void testUninitializedVariable() throws IOException {
compile(JavaSource.createJavaSource("Object o = new A().new B(); class A { }",
"B()"));
compile(JavaSource.createJavaSource("Object o = new A().new BT(); class A { }",
"BT"));
}
static void testMissingAnnotationValue() throws IOException {
compile(JavaSource.createJavaSource("@Foo(\"vvvv\")",