Skip to content

Commit

Permalink
Streamline search visitor by using existing utility methods and lambda
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed Oct 23, 2024
1 parent 5917214 commit c72d922
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static IJavaElement findElementForNodeViaDirectBinding(ASTNode key) {
return Optional.ofNullable(key).map(DOMASTNodeUtils::getBinding).map(IBinding::getJavaElement).orElse(null);
}

private static IBinding getBinding(ASTNode astNode) {
public static IBinding getBinding(ASTNode astNode) {
if (astNode instanceof Name name) {
return name.resolveBinding();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.jdt.internal.core.search.matching;

import java.util.function.Function;
import org.eclipse.jdt.core.dom.*;

/**
Expand All @@ -28,69 +29,49 @@ public PatternLocatorVisitor(PatternLocator patternLocator, MatchingNodeSet node
this.locator = locator;
}

@Override
public boolean visit(AnnotationTypeDeclaration node) {
private boolean defaultVisitImplementation(ASTNode node) {
return defaultVisitImplementationWithFunc(node, DOMASTNodeUtils::getBinding);
}

private boolean defaultVisitImplementationWithFunc(ASTNode node, Function<ASTNode, IBinding> func) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
level = this.patternLocator.resolveLevel(func.apply(node), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;

}


@Override
public boolean visit(AnnotationTypeDeclaration node) {
return defaultVisitImplementation(node);
}

@Override
public boolean visit(MethodDeclaration node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(MethodInvocation node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveMethodBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(ExpressionMethodReference node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveMethodBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(SuperMethodReference node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveMethodBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(SuperMethodInvocation node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveMethodBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}

private boolean visitAbstractTypeDeclaration(AbstractTypeDeclaration node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(EnumDeclaration node) {
Expand All @@ -106,21 +87,11 @@ public boolean visit(RecordDeclaration node) {
}
@Override
public boolean visit(AnonymousClassDeclaration node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}

private boolean visitType(Type node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(SimpleType type) {
Expand Down Expand Up @@ -158,30 +129,15 @@ public boolean visit(UnionType node) {
}
@Override
public boolean visit(ClassInstanceCreation node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveConstructorBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(CreationReference node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveMethodBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(SuperConstructorInvocation node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveConstructorBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(SimpleName node) {
Expand All @@ -203,21 +159,11 @@ public boolean visit(SimpleName node) {
}
@Override
public boolean visit(VariableDeclarationFragment node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(SingleVariableDeclaration node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
return defaultVisitImplementation(node);
}
@Override
public boolean visit(EnumConstantDeclaration node) {
Expand Down

0 comments on commit c72d922

Please sign in to comment.