3131import org .eclipse .jdt .core .compiler .CharOperation ;
3232import org .eclipse .jdt .core .dom .AST ;
3333import org .eclipse .jdt .core .dom .ASTNode ;
34+ import org .eclipse .jdt .core .dom .AbstractTypeDeclaration ;
3435import org .eclipse .jdt .core .dom .Annotation ;
3536import org .eclipse .jdt .core .dom .AnnotationTypeMemberDeclaration ;
3637import org .eclipse .jdt .core .dom .Expression ;
5758import org .eclipse .jdt .core .dom .SuperMethodReference ;
5859import org .eclipse .jdt .core .dom .ThisExpression ;
5960import org .eclipse .jdt .core .dom .TypeMethodReference ;
61+ import org .eclipse .jdt .core .dom .VariableDeclarationFragment ;
6062import org .eclipse .jdt .core .search .IJavaSearchConstants ;
6163import org .eclipse .jdt .core .search .MethodDeclarationMatch ;
6264import org .eclipse .jdt .core .search .MethodReferenceMatch ;
6365import org .eclipse .jdt .core .search .SearchMatch ;
6466import org .eclipse .jdt .core .search .SearchPattern ;
67+ import org .eclipse .jdt .internal .codeassist .DOMCompletionUtils ;
6568import org .eclipse .jdt .internal .core .BinaryMethod ;
6669import org .eclipse .jdt .internal .core .SourceMethod ;
6770import 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