Skip to content

Commit d820c42

Browse files
datho7561robstryker
authored andcommitted
Handle DeclarationOfReferencedMethodsPattern in search
Should fix `JavaSearchTests.testDeclarationsOfSentMessages02` Signed-off-by: David Thompson <[email protected]>
1 parent 08a7cb0 commit d820c42

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/matching/DOMMethodLocator.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.eclipse.jdt.core.compiler.CharOperation;
3232
import org.eclipse.jdt.core.dom.AST;
3333
import org.eclipse.jdt.core.dom.ASTNode;
34+
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
3435
import org.eclipse.jdt.core.dom.Annotation;
3536
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
3637
import org.eclipse.jdt.core.dom.Expression;
@@ -57,11 +58,13 @@
5758
import org.eclipse.jdt.core.dom.SuperMethodReference;
5859
import org.eclipse.jdt.core.dom.ThisExpression;
5960
import org.eclipse.jdt.core.dom.TypeMethodReference;
61+
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
6062
import org.eclipse.jdt.core.search.IJavaSearchConstants;
6163
import org.eclipse.jdt.core.search.MethodDeclarationMatch;
6264
import org.eclipse.jdt.core.search.MethodReferenceMatch;
6365
import org.eclipse.jdt.core.search.SearchMatch;
6466
import org.eclipse.jdt.core.search.SearchPattern;
67+
import org.eclipse.jdt.internal.codeassist.DOMCompletionUtils;
6568
import org.eclipse.jdt.internal.core.BinaryMethod;
6669
import org.eclipse.jdt.internal.core.SourceMethod;
6770
import org.eclipse.jdt.internal.core.search.BasicSearchEngine;
@@ -189,17 +192,26 @@ public LocatorResponse match(MethodInvocation node, NodeSetWrapper nodeSet, Matc
189192
return toResponse(IMPOSSIBLE_MATCH);
190193
}
191194
}
195+
if (!matchesExpectedJavaElement(node)) {
196+
return toResponse(IMPOSSIBLE_MATCH);
197+
}
192198
return toResponse(nodeSet.addMatch(node, level), true);
193199
}
194200
@Override
195201
public LocatorResponse match(MethodRef node, NodeSetWrapper nodeSet, MatchLocator locator) {
202+
if (!matchesExpectedJavaElement(node)) {
203+
return toResponse(IMPOSSIBLE_MATCH);
204+
}
196205
int level = this.matchReference(node.getName(), node.parameters());
197206
if( level == IMPOSSIBLE_MATCH )
198207
return toResponse(IMPOSSIBLE_MATCH);
199208
return toResponse(nodeSet.addMatch(node, level), true);
200209
}
201210
@Override
202211
public LocatorResponse match(MethodReference node, NodeSetWrapper nodeSet, MatchLocator locator) {
212+
if (!matchesExpectedJavaElement(node)) {
213+
return toResponse(IMPOSSIBLE_MATCH);
214+
}
203215
if (this.locator.pattern.fineGrain == 0
204216
|| (this.locator.pattern.fineGrain & IJavaSearchConstants.METHOD_REFERENCE_EXPRESSION) != 0
205217
|| (node instanceof SuperMethodReference && (this.locator.pattern.fineGrain & IJavaSearchConstants.SUPER_REFERENCE) != 0)
@@ -222,6 +234,9 @@ public LocatorResponse match(MethodReference node, NodeSetWrapper nodeSet, Match
222234
}
223235
@Override
224236
public LocatorResponse match(org.eclipse.jdt.core.dom.Expression expression, NodeSetWrapper nodeSet, MatchLocator locator) {
237+
if (!matchesExpectedJavaElement(expression)) {
238+
return toResponse(IMPOSSIBLE_MATCH);
239+
}
225240
int level = IMPOSSIBLE_MATCH;
226241
if (expression instanceof SuperMethodInvocation node) {
227242
if (this.pattern.fineGrain != 0 && (this.pattern.fineGrain & IJavaSearchConstants.SUPER_REFERENCE) == 0) {
@@ -249,6 +264,9 @@ public LocatorResponse match(Name node, NodeSetWrapper nodeSet, MatchLocator loc
249264
SuperMethodReference.NAME_PROPERTY == node.getLocationInParent()) {
250265
return toResponse(IMPOSSIBLE_MATCH);
251266
}
267+
if (!matchesExpectedJavaElement(node)) {
268+
return toResponse(IMPOSSIBLE_MATCH);
269+
}
252270

253271
if (node.getLocationInParent() == MemberValuePair.NAME_PROPERTY
254272
&& this.locator.pattern.parameterCount == 0
@@ -1258,4 +1276,35 @@ public LocatorResponse match(LambdaExpression node, NodeSetWrapper nodeSet, Matc
12581276
}
12591277
return toResponse(IMPOSSIBLE_MATCH);
12601278
}
1279+
1280+
private boolean matchesExpectedJavaElement(ASTNode currentNode) {
1281+
// only applies to DeclarationOfReferencedMethodsPattern
1282+
if (!(this.pattern instanceof DeclarationOfReferencedMethodsPattern referencedMethodsPattern)) {
1283+
return true;
1284+
}
1285+
ASTNode elementNode = DOMCompletionUtils.findParent(currentNode, new int[] {ASTNode.METHOD_DECLARATION, ASTNode.VARIABLE_DECLARATION_FRAGMENT, ASTNode.TYPE_DECLARATION, ASTNode.ENUM_DECLARATION, ASTNode.ANNOTATION_TYPE_DECLARATION, ASTNode.RECORD_DECLARATION});
1286+
if (elementNode == null) {
1287+
return false;
1288+
}
1289+
IBinding parentBinding = switch (elementNode) {
1290+
case MethodDeclaration methodDecl -> {
1291+
yield methodDecl.resolveBinding();
1292+
}
1293+
case AbstractTypeDeclaration typeDecl -> {
1294+
yield typeDecl.resolveBinding();
1295+
}
1296+
case VariableDeclarationFragment varFragment -> {
1297+
yield varFragment.resolveBinding();
1298+
}
1299+
default -> { yield null; }
1300+
};
1301+
if (parentBinding == null) {
1302+
return false;
1303+
}
1304+
// attempt to locate the expected enclosing element in the parent elements
1305+
IJavaElement cursor = parentBinding.getJavaElement();
1306+
while (cursor != null && !referencedMethodsPattern.enclosingElement.equals(cursor))
1307+
cursor = cursor.getParent();
1308+
return cursor != null;
1309+
}
12611310
}

0 commit comments

Comments
 (0)