diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java index cd0b9a6ca9d..52889c8e00a 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java @@ -150,6 +150,9 @@ public static IBinding getBinding(ASTNode astNode) { if (astNode instanceof ClassInstanceCreation ref) { return ref.resolveConstructorBinding(); } + if (astNode instanceof TypeParameter ref) { + return ref.resolveBinding(); + } // TODO more... return null; } diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocator.java index b8d64078d25..90a263fab7a 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/MatchLocator.java @@ -1515,6 +1515,10 @@ private SearchMatch toMatch(org.eclipse.jdt.core.dom.ASTNode node, int accuracy, } return new TypeReferenceMatch(element, accuracy, node.getStartPosition(), node.getLength(), DOMASTNodeUtils.insideDocComment(node), getParticipant(), resource); } + if (node instanceof org.eclipse.jdt.core.dom.TypeParameter) { + IJavaElement element = DOMASTNodeUtils.getEnclosingJavaElement(node); + return new TypeParameterReferenceMatch(element, accuracy, node.getStartPosition(), node.getLength(), DOMASTNodeUtils.insideDocComment(node), getParticipant(), resource); + } if (node instanceof Name name) { IBinding b = name.resolveBinding(); IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node); diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/PatternLocatorVisitor.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/PatternLocatorVisitor.java index bd1bc372875..7661cc24ca9 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/PatternLocatorVisitor.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/PatternLocatorVisitor.java @@ -51,7 +51,10 @@ private boolean defaultVisitImplementationWithFunc( public boolean visit(AnnotationTypeDeclaration node) { return defaultVisitImplementation(node, x -> this.patternLocator.match(node, this.nodeSet, this.locator)); } - + @Override + public boolean visit(TypeParameter node) { + return defaultVisitImplementation(node, x -> this.patternLocator.match(node, this.nodeSet, this.locator)); + } @Override public boolean visit(MethodDeclaration node) { return defaultVisitImplementation(node, x -> this.patternLocator.match(node, this.nodeSet, this.locator)); diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeParameterLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeParameterLocator.java index e54ee72f13a..8c225520838 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeParameterLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeParameterLocator.java @@ -68,6 +68,17 @@ public int match(Type node, MatchingNodeSet nodeSet, MatchLocator locator) { return IMPOSSIBLE_MATCH; } + @Override + public int match(org.eclipse.jdt.core.dom.TypeParameter node, MatchingNodeSet nodeSet, MatchLocator locator) { + if (this.pattern.findReferences) { + if (matchesName(this.pattern.name, node.getName().toString().toCharArray())) { + int level = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; + return nodeSet.addMatch(node, level); + } + } + return IMPOSSIBLE_MATCH; + } + /* * Verify whether a type parameter matches name pattern. diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeReferenceLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeReferenceLocator.java index 603064785d8..7dbfc7b1fd4 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeReferenceLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/TypeReferenceLocator.java @@ -1015,6 +1015,13 @@ public String toString() { return "Locator for " + this.pattern.toString(); //$NON-NLS-1$ } +private boolean hasPackageDeclarationAncestor(org.eclipse.jdt.core.dom.ASTNode node) { + if( node instanceof PackageDeclaration) { + return true; + } + return node == null ? false : hasPackageDeclarationAncestor(node.getParent()); +} + @Override public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) { if (binding == null) { @@ -1023,13 +1030,16 @@ public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, if (binding instanceof ITypeBinding typeBinding) { return resolveLevelForTypeBinding(node, typeBinding, locator); } - if( binding instanceof IPackageBinding packageBinding && node instanceof SimpleName sn) { + if( binding instanceof IPackageBinding && node instanceof SimpleName sn) { // var x = (B36479.C)val; // might interpret the B36479 to be a package and C a type, // rather than B36479 to be a type and C to be an inner-type if( this.isDeclarationOfReferencedTypesPattern) { return IMPOSSIBLE_MATCH; } + if( hasPackageDeclarationAncestor(node)) { + return IMPOSSIBLE_MATCH; + } String identifier = sn.getIdentifier(); if( matchesName(this.pattern.simpleName, identifier.toCharArray())) { return INACCURATE_MATCH;