Skip to content

Commit f498f57

Browse files
snjezargrunber
authored andcommitted
Go to definition doesn't find results on methods inside an anonymous class
Eclipse bugs: https://bugs.eclipse.org/bugs/show_bug.cgi?id=575149 https://bugs.eclipse.org/bugs/show_bug.cgi?id=576121 Signed-off-by: Snjezana Peco <[email protected]>
1 parent bb519f5 commit f498f57

File tree

5 files changed

+92
-42
lines changed

5 files changed

+92
-42
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SignatureHelpHandler.java

+46-31
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,16 @@ public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonito
173173
}
174174
}
175175
if (!monitor.isCanceled() && help.getActiveSignature() == null && node instanceof Block) {
176+
String methodName = getMethodName(node, unit, contextInfomation);
176177
for (int i = 0; i < infos.size(); i++) {
177178
if (infos.get(i).getParameters().size() >= activeParameter) {
178-
help.setActiveSignature(i);
179-
help.setActiveParameter(activeParameter);
180-
return help;
179+
CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
180+
IMethod m = JDTUtils.resolveMethod(proposal, javaProject);
181+
if (m != null && m.getElementName().equals(methodName)) {
182+
help.setActiveSignature(i);
183+
help.setActiveParameter(activeParameter);
184+
return help;
185+
}
181186
}
182187
}
183188
}
@@ -270,34 +275,9 @@ private ASTNode getNode(ICompilationUnit unit, int[] contextInfomation, IProgres
270275
return node;
271276
}
272277
if (node instanceof Block) {
273-
try {
274-
int pos = contextInfomation[0];
275-
IBuffer buffer = unit.getBuffer();
276-
while (pos >= 0) {
277-
char ch = buffer.getChar(pos);
278-
if (ch == '(' || Character.isWhitespace(ch)) {
279-
pos--;
280-
} else {
281-
break;
282-
}
283-
}
284-
int end = pos + 1;
285-
while (pos >= 0) {
286-
char ch = buffer.getChar(pos);
287-
if (Character.isJavaIdentifierPart(ch)) {
288-
pos--;
289-
} else {
290-
break;
291-
}
292-
}
293-
int start = pos + 1;
294-
String name = unit.getSource().substring(start, end);
295-
IStatus status = JavaConventionsUtil.validateMethodName(name, unit);
296-
if (status.isOK()) {
297-
return node;
298-
}
299-
} catch (CoreException e) {
300-
JavaLanguageServerPlugin.logException(e.getMessage(), e);
278+
String name = getMethodName(node, unit, contextInfomation);
279+
if (name != null) {
280+
return node;
301281
}
302282
}
303283
if (node instanceof Expression) {
@@ -310,6 +290,41 @@ private ASTNode getNode(ICompilationUnit unit, int[] contextInfomation, IProgres
310290
return null;
311291
}
312292

293+
private String getMethodName(ASTNode node, ICompilationUnit unit, int[] contextInfomation) {
294+
if (node instanceof Block) {
295+
try {
296+
int pos = contextInfomation[0];
297+
IBuffer buffer = unit.getBuffer();
298+
while (pos >= 0) {
299+
char ch = buffer.getChar(pos);
300+
if (ch == '(' || Character.isWhitespace(ch)) {
301+
pos--;
302+
} else {
303+
break;
304+
}
305+
}
306+
int end = pos + 1;
307+
while (pos >= 0) {
308+
char ch = buffer.getChar(pos);
309+
if (Character.isJavaIdentifierPart(ch)) {
310+
pos--;
311+
} else {
312+
break;
313+
}
314+
}
315+
int start = pos + 1;
316+
String name = unit.getSource().substring(start, end);
317+
IStatus status = JavaConventionsUtil.validateMethodName(name, unit);
318+
if (status.isOK()) {
319+
return name;
320+
}
321+
} catch (JavaModelException e) {
322+
JavaLanguageServerPlugin.logException(e.getMessage(), e);
323+
}
324+
}
325+
return null;
326+
}
327+
313328
/*
314329
* Calculate the heuristic information about the start offset of method and current parameter position. The parameter position is 0-based.
315330
* If cannot find the methods start offset after max search bound, -1 will be return.

org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<unit id="org.eclipse.equinox.sdk.feature.group" version="0.0.0"/>
3232
<unit id="org.eclipse.jdt.source.feature.group" version="0.0.0"/>
3333
<unit id="org.eclipse.sdk.feature.group" version="0.0.0"/>
34-
<repository location="https://download.eclipse.org/eclipse/updates/4.22-I-builds/I20210915-0700/"/>
34+
<repository location="https://download.eclipse.org/eclipse/updates/4.22-I-builds/I20210921-1800/"/>
3535
</location>
3636
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
3737
<unit id="org.eclipse.xtend.sdk.feature.group" version="0.0.0"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.sample;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
public class App2 {
6+
public void test() {
7+
List<String> arr = new ArrayList<>() {
8+
String s2 = "abc";
9+
private void test() {
10+
int len = s2.length();
11+
System.out.println(s2);
12+
System.out.println(len);
13+
}
14+
};
15+
}
16+
}

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/NavigateToDefinitionHandlerTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ public void testMethodInAnonymousClass() throws Exception {
110110
assertEquals(18, definitions.get(0).getRange().getStart().getCharacter());
111111
}
112112

113+
// https://github.com/eclipse/eclipse.jdt.ls/issues/1813
114+
@Test
115+
public void testMethodInAnonymousClass2() throws Exception {
116+
String className = "org.sample.App2";
117+
int line = 10;
118+
int column = 24;
119+
importProjects("eclipse/java11");
120+
IProject java11 = WorkspaceHelper.getProject("java11");
121+
String uri = ClassFileUtil.getURI(java11, className);
122+
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
123+
List<? extends Location> definitions = handler.definition(new TextDocumentPositionParams(identifier, new Position(line, column)), monitor);
124+
assertNotNull(definitions);
125+
assertEquals("No definition found for " + className, 1, definitions.size());
126+
assertNotNull(definitions.get(0).getUri());
127+
assertEquals(7, definitions.get(0).getRange().getStart().getLine());
128+
assertEquals(10, definitions.get(0).getRange().getStart().getCharacter());
129+
}
130+
113131
@Test
114132
public void testJdkClasses() throws Exception {
115133
// 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

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SignatureHelpHandlerTest.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public void testSignatureHelp_singleMethod() throws JavaModelException {
9191

9292
SignatureHelp help = getSignatureHelp(cu, 4, 39);
9393
assertNotNull(help);
94-
assertEquals(1, help.getSignatures().size());
95-
assertEquals("foo(String s) : int", help.getSignatures().get(0).getLabel());
96-
assertTrue(help.getSignatures().get(0).getDocumentation().getLeft().length() > 0);
94+
assertEquals(13, help.getSignatures().size());
95+
assertEquals("foo(String s) : int", help.getSignatures().get(help.getActiveSignature()).getLabel());
96+
assertTrue(help.getSignatures().get(help.getActiveSignature()).getDocumentation().getLeft().length() > 0);
9797
assertEquals((Integer) 0, help.getActiveParameter());
9898
}
9999

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

113113
SignatureHelp help = getSignatureHelp(cu, 5, 42);
114114
assertNotNull(help);
115-
assertEquals(3, help.getSignatures().size());
115+
assertEquals(15, help.getSignatures().size());
116116
assertEquals((Integer) 1, help.getActiveParameter());
117117
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(int s, String s) : int");
118118
}
@@ -167,7 +167,7 @@ public void testSignatureHelp_parameters() throws JavaModelException {
167167
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
168168
SignatureHelp help = getSignatureHelp(cu, 3, 12);
169169
assertNotNull(help);
170-
assertEquals(2, help.getSignatures().size());
170+
assertEquals(14, help.getSignatures().size());
171171
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(String s, boolean bar) : void");
172172
}
173173

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

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

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

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

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

0 commit comments

Comments
 (0)