Skip to content
Open
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 @@ -316,11 +316,6 @@ public class Test {
for (Completion completion : completions) {
if (completion.getLabel().equals("Test") &&
"(int i) - generate".equals(completion.getLabelDetail())) {
//force full reparse:
byte[] content = primaryTestFO.asBytes();
try (OutputStream out = primaryTestFO.getOutputStream()) {
out.write(content);
}
assertEquals(null,
completion.getInsertText());
assertEquals("63-63:",
Expand All @@ -343,6 +338,124 @@ public Test(int i) {
assertTrue(found.get());
}

public void testOverrideItem() throws Exception {
System.err.println(System.getProperties().keySet());
AtomicBoolean found = new AtomicBoolean();
runJavaCollector(List.of(new FileDescription("test/Test.java",
"""
package test;

public class Test extends List {
acce|
}
class List {
public void accept(java.util.List l) {}
}
""")),
completions -> {
for (Completion completion : completions) {
if (completion.getLabel().equals("accept") &&
"(List l) - override".equals(completion.getLabelDetail())) {
assertEquals(null,
completion.getInsertText());
assertEquals("52-52:",
textEdit2String(completion.getTextEdit()));
assertEquals("""
48-48:
@Override
public void accept(java.util.List l) {
}
""".replace("\n", "\\n"),
completion.getAdditionalTextEdits()
.get()
.stream()
.map(JavaCompletionCollectorTest::textEdit2String)
.collect(Collectors.joining(", ")));
found.set(true);
}
}
});
assertTrue(found.get());
}

public void testGetterSetterItem() throws Exception {
System.err.println(System.getProperties().keySet());
AtomicBoolean found = new AtomicBoolean();
runJavaCollector(List.of(new FileDescription("test/Test.java",
"""
package test;

public class Test extends List {
java.util.List list;
get|
}
class List {}
""")),
completions -> {
for (Completion completion : completions) {
if (completion.getLabel().equals("getList") &&
"() - generate".equals(completion.getLabelDetail())) {
assertEquals(null,
completion.getInsertText());
assertEquals("77-77:",
textEdit2String(completion.getTextEdit()));
assertEquals("""
73-73:
public java.util.List getList() {
return list;
}
""".replace("\n", "\\n"),
completion.getAdditionalTextEdits()
.get()
.stream()
.map(JavaCompletionCollectorTest::textEdit2String)
.collect(Collectors.joining(", ")));
found.set(true);
}
}
});
assertTrue(found.get());
}

public void testInitializeAll() throws Exception {
System.err.println(System.getProperties().keySet());
AtomicBoolean found = new AtomicBoolean();
runJavaCollector(List.of(new FileDescription("test/Test.java",
"""
package test;

public class Test extends List {
private final java.util.List list;
Test|
}
class List {}
""")),
completions -> {
for (Completion completion : completions) {
if (completion.getLabel().equals("Test") &&
"(List list) - generate".equals(completion.getLabelDetail())) {
assertEquals(null,
completion.getInsertText());
assertEquals("91-91:",
textEdit2String(completion.getTextEdit()));
assertEquals("""
87-87:
public Test(java.util.List list) {
this.list = list;
}
""".replace("\n", "\\n"),
completion.getAdditionalTextEdits()
.get()
.stream()
.map(JavaCompletionCollectorTest::textEdit2String)
.collect(Collectors.joining(", ")));
found.set(true);
}
}
});
assertTrue(found.get());
}

private void runJavaCollector(List<FileDescription> files, Validator<List<Completion>> validator) throws Exception {
SourceUtilsTestUtil.prepareTest(new String[]{"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[]{new MIMEResolverImpl(), new MIMEDataProvider()});

Expand Down Expand Up @@ -387,6 +500,11 @@ private void runJavaCollector(List<FileDescription> files, Validator<List<Comple
cc.toPhase(JavaSource.Phase.RESOLVED);
}, true);
collector.collectCompletions(doc, caretPosition, ctx, completions::add);
//force full reparse, to ensure handles are used properly:
byte[] content = primaryTestFO.asBytes();
try (OutputStream out = primaryTestFO.getOutputStream()) {
out.write(content);
}
validator.validate(completions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sun.source.tree.*;
import com.sun.source.tree.Tree.Kind;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.tree.JCTree.JCLambda;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
Expand All @@ -34,6 +35,7 @@
import java.util.Set;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.QualifiedNameable;
import javax.lang.model.util.Elements;
import org.netbeans.api.java.source.GeneratorUtilities;
Expand Down Expand Up @@ -520,6 +522,9 @@ public Tree visitMemberSelect(MemberSelectTree tree, Object p) {
if (el == null) {
el = overlay.resolve(model, elements, qit.getFQN());
} else {
if (((Symbol) el).name.table.names.empty != elements.getName("")) {
throw new IllegalStateException("Element: " + el + " originates in a different javac instance than is the current one. Please use ElementHandles, TypeMirrorHandles and TreePathHandles to transfer instances of Element, TypeMirror and TreePath, respectivelly, between javac instances.");
}
if (el.getKind().isClass() || el.getKind().isInterface() || el.getKind() == ElementKind.PACKAGE) {
el = overlay.resolve(model, elements, ((QualifiedNameable) el).getQualifiedName().toString(), el, elements.getModuleOf(el));
}
Expand Down
Loading