From 740223b7abb236ca4d60c03219ff8a105f261490 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Mon, 8 Jun 2020 07:38:35 +0200 Subject: [PATCH] Do not blindly cast to IndexedClassDecl while reassigning anonymous class numbers, as the class decl may not be IndexedClassDecl. --- .../source/parsing/PartialReparseTest.java | 37 ++++++++++++++++++- .../lib/nbjavac/services/NBParserFactory.java | 4 +- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/PartialReparseTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/PartialReparseTest.java index 29e03faaabb7..4a928972187f 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/PartialReparseTest.java +++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/PartialReparseTest.java @@ -19,6 +19,7 @@ package org.netbeans.modules.java.source.parsing; import com.sun.source.tree.LineMap; +import com.sun.source.tree.NewClassTree; import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; import com.sun.source.util.TreePath; @@ -32,9 +33,11 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import java.util.stream.Collectors; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; import javax.swing.JEditorPane; import javax.swing.text.Document; import javax.tools.Diagnostic; @@ -208,6 +211,31 @@ public void testAnonymous() throws Exception { "final int j = 5;\n"); } + public void testAnonymousName() throws Exception { + doRunTest("package test;\n" + + "public class Test {\n" + + " private Object o = new Object() {};\n" + + " private void test() {\n" + + " new Object() {\n" + + " };" + + " final int i = 5;\n" + + " ^^\n" + + " }" + + "}", + "final int j = 5;\n", + info -> { + new TreePathScanner() { + public Void visitNewClass(NewClassTree tree, Void p) { + if (getCurrentPath().getParentPath().getLeaf().getKind() == Kind.METHOD) { + TypeElement el = (TypeElement) info.getTrees().getElement(new TreePath(getCurrentPath(), tree.getClassBody())); + assertEquals("test.Test$2", info.getElements().getBinaryName(el).toString()); + } + return super.visitNewClass(tree, p); + } + }.scan(info.getCompilationUnit(), null); + }); + } + public void testAnonymousFullReparse1() throws Exception { doVerifyFullReparse("package test;\n" + "public class Test {\n" + @@ -302,6 +330,10 @@ public void testConstructorEnum2() throws Exception { } private void doRunTest(String code, String inject) throws Exception { + doRunTest(code, inject, info -> {}); + } + + private void doRunTest(String code, String inject, Consumer callback) throws Exception { FileObject srcDir = FileUtil.createMemoryFileSystem().getRoot(); FileObject src = srcDir.createData("Test.java"); try (Writer out = new OutputStreamWriter(src.getOutputStream())) { @@ -313,7 +345,8 @@ private void doRunTest(String code, String inject) throws Exception { Object[] topLevel = new Object[1]; source.runUserActionTask(cc -> { cc.toPhase(Phase.RESOLVED); - topLevel[0] = cc.getCompilationUnit(); + topLevel[0] = cc.getCompilationUnit(); + callback.accept(cc); }, true); int startReplace = code.indexOf('^'); int endReplace = code.indexOf('^', startReplace + 1) + 1; @@ -328,6 +361,7 @@ private void doRunTest(String code, String inject) throws Exception { actualTree.set(dumpTree(cc)); actualDiagnostics.set(dumpDiagnostics(cc)); actualLineMap.set(dumpLineMap(cc)); + callback.accept(cc); }, true); ec.saveDocument(); ec.close(); @@ -340,6 +374,7 @@ private void doRunTest(String code, String inject) throws Exception { expectedTree.set(dumpTree(cc)); expectedDiagnostics.set(dumpDiagnostics(cc)); expectedLineMap.set(dumpLineMap(cc)); + callback.accept(cc); }, true); assertEquals(expectedTree.get(), actualTree.get()); assertEquals(expectedDiagnostics.get(), actualDiagnostics.get()); diff --git a/java/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBParserFactory.java b/java/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBParserFactory.java index 98d858bfb3a3..447e5ba3b5a6 100644 --- a/java/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBParserFactory.java +++ b/java/lib.nbjavac/src/org/netbeans/lib/nbjavac/services/NBParserFactory.java @@ -275,7 +275,7 @@ public void newAnonScope(final Name name, final int startNumber) { @Override public void visitClassDef(JCClassDecl tree) { - if (tree.name == names.empty) { + if (tree.name == names.empty && tree instanceof IndexedClassDecl) { ((IndexedClassDecl) tree).index = this.anonScopes.peek().assignNumber(); } newAnonScope(tree.name); @@ -284,7 +284,7 @@ public void visitClassDef(JCClassDecl tree) { } finally { this.anonScopes.pop(); } - if (!this.anonScopes.isEmpty() && this.anonScopes.peek().localClass && tree.name != names.empty) { + if (!this.anonScopes.isEmpty() && this.anonScopes.peek().localClass && tree.name != names.empty && tree instanceof IndexedClassDecl) { ((IndexedClassDecl) tree).index = this.anonScopes.peek().assignLocalNumber(tree.name); } }