Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Void, Void>() {
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" +
Expand Down Expand Up @@ -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<CompilationInfo> callback) throws Exception {
FileObject srcDir = FileUtil.createMemoryFileSystem().getRoot();
FileObject src = srcDir.createData("Test.java");
try (Writer out = new OutputStreamWriter(src.getOutputStream())) {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand Down