Skip to content

Go to definition doesn't find results on methods inside an anonymous class #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
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 @@ -173,11 +173,16 @@ public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonito
}
}
if (!monitor.isCanceled() && help.getActiveSignature() == null && node instanceof Block) {
String methodName = getMethodName(node, unit, contextInfomation);
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).getParameters().size() >= activeParameter) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
IMethod m = JDTUtils.resolveMethod(proposal, javaProject);
if (m != null && m.getElementName().equals(methodName)) {
help.setActiveSignature(i);
help.setActiveParameter(activeParameter);
return help;
}
}
}
}
Expand Down Expand Up @@ -270,34 +275,9 @@ private ASTNode getNode(ICompilationUnit unit, int[] contextInfomation, IProgres
return node;
}
if (node instanceof Block) {
try {
int pos = contextInfomation[0];
IBuffer buffer = unit.getBuffer();
while (pos >= 0) {
char ch = buffer.getChar(pos);
if (ch == '(' || Character.isWhitespace(ch)) {
pos--;
} else {
break;
}
}
int end = pos + 1;
while (pos >= 0) {
char ch = buffer.getChar(pos);
if (Character.isJavaIdentifierPart(ch)) {
pos--;
} else {
break;
}
}
int start = pos + 1;
String name = unit.getSource().substring(start, end);
IStatus status = JavaConventionsUtil.validateMethodName(name, unit);
if (status.isOK()) {
return node;
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
String name = getMethodName(node, unit, contextInfomation);
if (name != null) {
return node;
}
}
if (node instanceof Expression) {
Expand All @@ -310,6 +290,41 @@ private ASTNode getNode(ICompilationUnit unit, int[] contextInfomation, IProgres
return null;
}

private String getMethodName(ASTNode node, ICompilationUnit unit, int[] contextInfomation) {
if (node instanceof Block) {
try {
int pos = contextInfomation[0];
IBuffer buffer = unit.getBuffer();
while (pos >= 0) {
char ch = buffer.getChar(pos);
if (ch == '(' || Character.isWhitespace(ch)) {
pos--;
} else {
break;
}
}
int end = pos + 1;
while (pos >= 0) {
char ch = buffer.getChar(pos);
if (Character.isJavaIdentifierPart(ch)) {
pos--;
} else {
break;
}
}
int start = pos + 1;
String name = unit.getSource().substring(start, end);
IStatus status = JavaConventionsUtil.validateMethodName(name, unit);
if (status.isOK()) {
return name;
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
return null;
}

/*
* Calculate the heuristic information about the start offset of method and current parameter position. The parameter position is 0-based.
* If cannot find the methods start offset after max search bound, -1 will be return.
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<unit id="org.eclipse.equinox.sdk.feature.group" version="0.0.0"/>
<unit id="org.eclipse.jdt.source.feature.group" version="0.0.0"/>
<unit id="org.eclipse.sdk.feature.group" version="0.0.0"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.22-I-builds/I20210915-0700/"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.22-I-builds/I20210921-1800/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.xtend.sdk.feature.group" version="0.0.0"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sample;

import java.util.List;
import java.util.ArrayList;
public class App2 {
public void test() {
List<String> arr = new ArrayList<>() {
String s2 = "abc";
private void test() {
int len = s2.length();
System.out.println(s2);
System.out.println(len);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ public void testMethodInAnonymousClass() throws Exception {
assertEquals(18, definitions.get(0).getRange().getStart().getCharacter());
}

// https://github.com/eclipse/eclipse.jdt.ls/issues/1813
@Test
public void testMethodInAnonymousClass2() throws Exception {
String className = "org.sample.App2";
int line = 10;
int column = 24;
importProjects("eclipse/java11");
IProject java11 = WorkspaceHelper.getProject("java11");
String uri = ClassFileUtil.getURI(java11, className);
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
List<? extends Location> definitions = handler.definition(new TextDocumentPositionParams(identifier, new Position(line, column)), monitor);
assertNotNull(definitions);
assertEquals("No definition found for " + className, 1, definitions.size());
assertNotNull(definitions.get(0).getUri());
assertEquals(7, definitions.get(0).getRange().getStart().getLine());
assertEquals(10, definitions.get(0).getRange().getStart().getCharacter());
}

@Test
public void testJdkClasses() throws Exception {
// because for test, we are using fake rt.jar(rtstubs.jar), the issue of issue https://bugs.eclipse.org/bugs/show_bug.cgi?id=541573 will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public void testSignatureHelp_singleMethod() throws JavaModelException {

SignatureHelp help = getSignatureHelp(cu, 4, 39);
assertNotNull(help);
assertEquals(1, help.getSignatures().size());
assertEquals("foo(String s) : int", help.getSignatures().get(0).getLabel());
assertTrue(help.getSignatures().get(0).getDocumentation().getLeft().length() > 0);
assertEquals(13, help.getSignatures().size());
assertEquals("foo(String s) : int", help.getSignatures().get(help.getActiveSignature()).getLabel());
assertTrue(help.getSignatures().get(help.getActiveSignature()).getDocumentation().getLeft().length() > 0);
assertEquals((Integer) 0, help.getActiveParameter());
}

Expand All @@ -112,7 +112,7 @@ public void testSignatureHelp_multipeMethod() throws JavaModelException {

SignatureHelp help = getSignatureHelp(cu, 5, 42);
assertNotNull(help);
assertEquals(3, help.getSignatures().size());
assertEquals(15, help.getSignatures().size());
assertEquals((Integer) 1, help.getActiveParameter());
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(int s, String s) : int");
}
Expand Down Expand Up @@ -167,7 +167,7 @@ public void testSignatureHelp_parameters() throws JavaModelException {
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
SignatureHelp help = getSignatureHelp(cu, 3, 12);
assertNotNull(help);
assertEquals(2, help.getSignatures().size());
assertEquals(14, help.getSignatures().size());
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(String s, boolean bar) : void");
}

Expand All @@ -187,7 +187,7 @@ public void testSignatureHelp_activeSignature() throws JavaModelException {
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
SignatureHelp help = getSignatureHelp(cu, 3, 12);
assertNotNull(help);
assertEquals(3, help.getSignatures().size());
assertEquals(15, help.getSignatures().size());
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(String s, String b) : void");
}

Expand Down Expand Up @@ -325,7 +325,7 @@ public void testSignatureHelp_varargs() throws JavaModelException {
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().startsWith("asList(T... "));
help = getSignatureHelp(cu, 5, 19);
assertNotNull(help);
assertEquals(1, help.getSignatures().size());
assertEquals(2, help.getSignatures().size());
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("demo(String s, String... s2) : void"));
}

Expand All @@ -336,7 +336,7 @@ public void testSignatureHelp_varargs2() throws JavaModelException {
ICompilationUnit cu = type.getCompilationUnit();
SignatureHelp help = getSignatureHelp(cu, 4, 16);
assertNotNull(help);
assertEquals(2, help.getSignatures().size());
assertEquals(3, help.getSignatures().size());
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("run(Class<?> clazz, String... args) : void"));
}

Expand All @@ -347,7 +347,7 @@ public void testSignatureHelp_lambda() throws JavaModelException {
ICompilationUnit cu = type.getCompilationUnit();
SignatureHelp help = getSignatureHelp(cu, 8, 14);
assertNotNull(help);
assertEquals(1, help.getSignatures().size());
assertEquals(12, help.getSignatures().size());
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("test(Function<String,String> f) : void"));
}

Expand Down Expand Up @@ -411,7 +411,8 @@ public void testSignatureHelp_stringLiteral() throws JavaModelException {
private void testStringLiteral(ICompilationUnit cu, int line, int character) {
SignatureHelp help = getSignatureHelp(cu, line, character);
assertNotNull(help);
assertEquals(1, help.getSignatures().size());
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=575149
// assertEquals(1, help.getSignatures().size());
assertNotNull(help.getActiveParameter());
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("foo(String p, int x) : void"));
}
Expand Down