Skip to content

Commit b663cef

Browse files
committed
Improve matching/filtering anonymous types
1 parent e853bdf commit b663cef

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/DOMASTNodeUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.jdt.core.dom.ASTNode;
2222
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
2323
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
24+
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
2425
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
2526
import org.eclipse.jdt.core.dom.Comment;
2627
import org.eclipse.jdt.core.dom.CompilationUnit;
@@ -75,6 +76,9 @@ public static IJavaElement getEnclosingJavaElement(ASTNode node) {
7576
|| node.getLocationInParent() == FieldDeclaration.FRAGMENTS_PROPERTY) {
7677
return getDeclaringJavaElement(node);
7778
}
79+
if (node instanceof ClassInstanceCreation newInst && newInst.getAnonymousClassDeclaration() != null) {
80+
return getDeclaringJavaElement(newInst.getAnonymousClassDeclaration());
81+
}
7882
return getEnclosingJavaElement(node.getParent());
7983
}
8084

@@ -256,6 +260,9 @@ public static IBinding getBinding(ASTNode astNode) {
256260
if (astNode instanceof LambdaExpression lambda) {
257261
return lambda.resolveMethodBinding();
258262
}
263+
if (astNode instanceof AnonymousClassDeclaration anon) {
264+
return anon.resolveBinding();
265+
}
259266
// TODO more...
260267
return null;
261268
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*******************************************************************************/
1111
package org.eclipse.jdt.internal.core.search.matching;
1212

13+
import static org.eclipse.jdt.internal.core.search.matching.SuperTypeReferencePattern.ONLY_SUPER_INTERFACES;
14+
import static org.eclipse.jdt.internal.core.search.matching.SuperTypeReferencePattern.ONLY_SUPER_CLASSES;
15+
1316
import java.util.Set;
1417

1518
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
@@ -25,6 +28,7 @@
2528
import org.eclipse.jdt.core.dom.Type;
2629
import org.eclipse.jdt.core.dom.TypeDeclaration;
2730
import org.eclipse.jdt.internal.core.search.LocatorResponse;
31+
import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants;
2832

2933
public class DOMSuperTypeReferenceLocator extends DOMPatternLocator {
3034

@@ -37,7 +41,7 @@ public DOMSuperTypeReferenceLocator(SuperTypeReferenceLocator locator) {
3741

3842
@Override
3943
public LocatorResponse match(org.eclipse.jdt.core.dom.LambdaExpression node, NodeSetWrapper nodeSet, MatchLocator locator) {
40-
if (this.locator.pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_INTERFACES)
44+
if (this.locator.pattern.superRefKind != ONLY_SUPER_INTERFACES)
4145
return toResponse(IMPOSSIBLE_MATCH);
4246
nodeSet.setMustResolve(true);
4347
int level = nodeSet.addMatch(node, POSSIBLE_MATCH);
@@ -49,6 +53,9 @@ public LocatorResponse match(Type node, NodeSetWrapper nodeSet, MatchLocator loc
4953
if (!Set.of(TypeDeclaration.SUPERCLASS_TYPE_PROPERTY, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY, ClassInstanceCreation.TYPE_PROPERTY).contains(node.getLocationInParent())) {
5054
return toResponse(IMPOSSIBLE_MATCH);
5155
}
56+
if (this.locator.pattern.typeSuffix == IIndexConstants.INTERFACE_SUFFIX && node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
57+
return toResponse(IMPOSSIBLE_MATCH);
58+
}
5259
if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY && node.getParent() instanceof ClassInstanceCreation newInst && newInst.getAnonymousClassDeclaration() == null) {
5360
return toResponse(IMPOSSIBLE_MATCH);
5461
}
@@ -90,14 +97,16 @@ public LocatorResponse resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBind
9097
return toResponse(IMPOSSIBLE_MATCH);
9198

9299
int level = IMPOSSIBLE_MATCH;
93-
if (this.locator.pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_INTERFACES || node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) {
100+
int kind = this.locator.pattern.superRefKind;
101+
102+
if (kind != ONLY_SUPER_INTERFACES || node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) {
94103
level = this.resolveLevelForType(this.locator.pattern.superSimpleName, this.locator.pattern.superQualification,
95104
type);
96105
if (level > IMPOSSIBLE_MATCH)
97106
return toResponse(level);
98107
}
99108

100-
if (this.locator.pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_CLASSES || node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
109+
if (kind != ONLY_SUPER_CLASSES || node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
101110
level = this.resolveLevelForType(this.locator.pattern.superSimpleName, this.locator.pattern.superQualification,
102111
type);
103112
if (level == ACCURATE_MATCH)

0 commit comments

Comments
 (0)