Skip to content

Commit

Permalink
Open declaration / Javadoc popup is confused by overloaded method wit…
Browse files Browse the repository at this point in the history
…h method reference (eclipse-jdt#1386)
  • Loading branch information
srikanth-sankaran authored Sep 18, 2023
1 parent 92fba49 commit 4e74cc8
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ protected int actFromTokenOrSynthetic(int previousAct) {
protected int intPtr;

protected int[] intStack;
public int lastAct ; //handle for multiple parsing goals
public int lastAct;
//error recovery management
protected int lastCheckPoint;
protected int lastErrorEndPosition;
Expand Down Expand Up @@ -7488,7 +7488,7 @@ protected void consumeRule(int act) {
break;

case 369 : if (DEBUG) { System.out.println("PatternList ::= PatternList COMMA Pattern"); } //$NON-NLS-1$
consumePatternList();
consumePatternList();
break;

case 371 : if (DEBUG) { System.out.println("PushLeftBrace ::="); } //$NON-NLS-1$
Expand Down Expand Up @@ -13075,12 +13075,13 @@ protected void parse() {

int errorPos = this.scanner.currentPosition - 1;
if (!this.hasReportedError) {
this.hasError = true;
if (act == ERROR_ACTION)
this.hasError = true;
}
int previousToken = this.currentToken;
switch (resumeOnSyntaxError()) {
case HALT:
act = ERROR_ACTION;
act = ERROR_ACTION; // this is suspect, but goes quite some way back in time ...
break ProcessTerminals;
case RESTART:
if (act == ERROR_ACTION && previousToken != 0) this.lastErrorEndPosition = errorPos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8369,6 +8369,27 @@ public void testGH1162_2() {
"Unhandled exception type X\n" +
"----------\n");
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=576252
// Open declaration / Javadoc popup is confused by overloaded method with method reference
public void testBug576252() {
this.runConformTest(
new String[] {
"LambdaTest.java",
"public class LambdaTest {\n" +
" public static void method(String value) {\n" +
" System.out.print(\"para\");\n" +
" }\n" +
"\n" +
" public static void method(java.util.function.Supplier<String> supplier) {\n" +
" System.out.print(supplier.get());\n" +
" }\n" +
"\n" +
" public static void main(String[] args) {\n" +
" LambdaTest.method(LambdaTest.class::toString);\n" +
" }\n" +
"}\n"},
"class LambdaTest");
}
public static Class testClass() {
return LambdaExpressionsTest.class;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3017,4 +3017,113 @@ public void testGH1195() throws Exception {
elements
);
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=576252
// Open declaration / Javadoc popup is confused by overloaded method with method reference
public void testBug576252() throws Exception {
this.wc = getWorkingCopy(
"Resolve/src/LambdaTest.java",
"public class LambdaTest {\n" +
" public static void method(String value) {\n" +
" System.out.print(\"para\");\n" +
" }\n" +
"\n" +
" public static void method(java.util.function.Supplier<String> supplier) {\n" +
" System.out.print(supplier.get());\n" +
" }\n" +
"\n" +
" public static void main(String[] args) {\n" +
" LambdaTest.method(LambdaTest.class::toString);\n" +
" System.out.print(\"extra\");\n" +
" }\n" +
"}\n");
String str = this.wc.getSource();
String selection = "method";
int start = str.lastIndexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.wc.codeSelect(start, length);
assertElementsEqual(
"Unexpected elements",
"method(java.util.function.Supplier<String>) [in LambdaTest [in [Working copy] LambdaTest.java [in <default> [in src [in Resolve]]]]]",
elements
);
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=578011
// Open Declaration (F3) not navigating to static method in same class
public void testBug578011() throws Exception {
this.wc = getWorkingCopy(
"Resolve/src/EclipseOpenDeclarationBug.java",
"import java.lang.reflect.Constructor;\n" +
"import java.util.HashSet;\n" +
"import java.util.Optional;\n" +
"import java.util.Set;\n" +
"\n" +
"public class EclipseOpenDeclarationBug {\n" +
" public EclipseOpenDeclarationBug(Class<?> cls) {\n" +
" Set<Constructor<?>> constructors = new HashSet<>();\n" +
"\n" +
" getPublicEmptyConstructor(cls).ifPresent(c -> {\n" +
" if(constructors.isEmpty()) {\n" +
" constructors.add(c);\n" +
" }\n" +
" });\n" +
"\n" +
" if(constructors.size() < 1) {\n" +
" throw new IllegalArgumentException(\"No suitable constructor found; provide an empty constructor or annotate one with @Inject: \" + cls);\n" +
" }\n" +
" }\n" +
"\n" +
" private static <T> Optional<Constructor<T>> getPublicEmptyConstructor(Class<T> cls) {\n" +
" try {\n" +
" return Optional.of(cls.getConstructor());\n" +
" }\n" +
" catch(NoSuchMethodException e) {\n" +
" return Optional.empty();\n" +
" }\n" +
" }\n" +
"}\n");
String str = this.wc.getSource();
String selection = "getPublicEmptyConstructor";
int start = str.indexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.wc.codeSelect(start, length);
assertElementsEqual(
"Unexpected elements",
"getPublicEmptyConstructor(Class<T>) [in EclipseOpenDeclarationBug [in [Working copy] EclipseOpenDeclarationBug.java [in <default> [in src [in Resolve]]]]]",
elements
);
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=546563
// [navigation] Open Declaration not working
public void testBug546563() throws Exception {
this.wc = getWorkingCopy(
"Resolve/src/Test.java",
"import java.util.Optional;\n" +
"\n" +
"public class Test {\n" +
"\n" +
" public void xyz() {\n" +
" getOptionalValue().ifPresent(val -> {\n" +
" int i = 1;\n" +
" System.out.print(val);\n" +
" });\n" +
" try {\n" +
" } catch (Exception e) {\n" +
" }\n" +
" }\n" +
"\n" +
" public Optional<String> getOptionalValue() {\n" +
" return Optional.empty();\n" +
" }\n" +
"}\n");
String str = this.wc.getSource();
String selection = "getOptionalValue";
int start = str.indexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.wc.codeSelect(start, length);
assertElementsEqual(
"Unexpected elements",
"getOptionalValue() [in Test [in [Working copy] Test.java [in <default> [in src [in Resolve]]]]]",
elements
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5509,7 +5509,7 @@ protected NameReference getUnspecifiedReference(boolean rejectTypeAnnotations) {
char[] token = this.identifierStack[this.identifierPtr];
long position = this.identifierPositionStack[this.identifierPtr--];
int start = (int) (position >>> 32), end = (int) position;
// in expression like new Foo(<completion>Collection.emptyList(), 1), the token becomes empty,
// in expression like new Foo(<completion>Collection.emptyList(), 1), the token becomes empty,
// but the positions represents the static type name identifier.
int adjustedStart = ((token.length == 0) ? start - 1 : start);
if (this.assistNode == null && adjustedStart <= this.cursorLocation && end >= this.cursorLocation) {
Expand Down Expand Up @@ -5811,9 +5811,9 @@ private boolean checkIfAtFirstArgumentOfConstructor() {
return true;
}
// We try to handle the situation where lParenPos represents a argument MessageSend's left paren in the AllocationExpression like
// Foo(|null, Collections.empty(), null). Here we try to calculate the constructor's left paren and match it with the
// Foo(|null, Collections.empty(), null). Here we try to calculate the constructor's left paren and match it with the
// assistNode position if the assistNode is a CompletionOnSingleNameReference or CompletionOnMessageSendName.

// following int literals represents
// 1 -> '('
// 3 -> 'new'
Expand Down Expand Up @@ -5898,18 +5898,6 @@ public MethodDeclaration parseSomeStatements(int start, int end, int fakeBlocksC
} finally {
this.nestedMethod[this.nestedType]--;
}
if (!this.hasError) {
int length;
if (this.astLengthPtr > -1 && (length = this.astLengthStack[this.astLengthPtr--]) != 0) {
System.arraycopy(
this.astStack,
(this.astPtr -= length) + 1,
fakeMethod.statements = new Statement[length],
0,
length);
}
}

return fakeMethod;
}
protected void popUntilCompletedAnnotationIfNecessary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,9 @@ public void parseBlockStatements(ConstructorDeclaration cd, CompilationUnitDecla
}

if (this.lastAct == ERROR_ACTION) {
cd.bits |= ASTNode.HasSyntaxErrors;
if (this.hasError) {
cd.bits |= ASTNode.HasSyntaxErrors;
}
return;
}

Expand Down Expand Up @@ -2058,7 +2060,9 @@ public void parseBlockStatements(
}

if (this.lastAct == ERROR_ACTION) {
initializer.bits |= ASTNode.HasSyntaxErrors;
if (this.hasError) {
initializer.bits |= ASTNode.HasSyntaxErrors;
}
return;
}

Expand Down Expand Up @@ -2118,7 +2122,9 @@ public void parseBlockStatements(MethodDeclaration md, CompilationUnitDeclaratio
}

if (this.lastAct == ERROR_ACTION) {
md.bits |= ASTNode.HasSyntaxErrors;
if (this.hasError) {
md.bits |= ASTNode.HasSyntaxErrors;
}
return;
}

Expand Down

0 comments on commit 4e74cc8

Please sign in to comment.