Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify AbstractMatcherTestChecker #853

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeCastTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.TreePathScanner;
import org.jspecify.annotations.Nullable;

/**
Expand All @@ -34,27 +34,23 @@ abstract class AbstractMatcherTestChecker extends BugChecker implements Compilat

@Override
public Description matchCompilationUnit(CompilationUnitTree compilationUnit, VisitorState state) {
new TreeScanner<@Nullable Void, TreePath>() {
new TreePathScanner<@Nullable Void, @Nullable Void>() {
@Override
public @Nullable Void scan(@Nullable Tree tree, TreePath treePath) {
if (tree == null) {
return null;
}

TreePath path = new TreePath(treePath, tree);
public @Nullable Void scan(@Nullable Tree tree, @Nullable Void unused) {
if (tree instanceof ExpressionTree) {
TreePath path = new TreePath(getCurrentPath(), tree);
ExpressionTree expressionTree = (ExpressionTree) tree;
if (!isMethodSelect(expressionTree, path)
&& delegate.matches(expressionTree, state.withPath(path))) {
state.reportMatch(describeMatch(tree));
}
}

return super.scan(tree, path);
return super.scan(tree, null);
}

@Override
public @Nullable Void visitImport(ImportTree tree, TreePath path) {
public @Nullable Void visitImport(ImportTree tree, @Nullable Void unused) {
/*
* We're not interested in matching import statements. While components of these
mohamedsamehsalah marked this conversation as resolved.
Show resolved Hide resolved
* can be `ExpressionTree`s, they will never be matched by Refaster.
Expand All @@ -63,24 +59,24 @@ public Description matchCompilationUnit(CompilationUnitTree compilationUnit, Vis
}

@Override
public @Nullable Void visitMethod(MethodTree tree, TreePath path) {
public @Nullable Void visitMethod(MethodTree tree, @Nullable Void unused) {
/*
* We're not interested in matching e.g. parameter and return type declarations. While these
* can be `ExpressionTree`s, they will never be matched by Refaster.
*/
return scan(tree.getBody(), new TreePath(path, tree));
return scan(tree.getBody(), null);
}

@Override
public @Nullable Void visitTypeCast(TypeCastTree tree, TreePath path) {
public @Nullable Void visitTypeCast(TypeCastTree tree, @Nullable Void unused) {
/*
* We're not interested in matching the parenthesized type subtree that is part of a type
* cast expression. While such trees can be `ExpressionTree`s, they will never be matched by
* Refaster.
*/
return scan(tree.getExpression(), new TreePath(path, tree));
return scan(tree.getExpression(), null);
}
}.scan(compilationUnit, state.getPath());
}.scan(compilationUnit, null);

return Description.NO_MATCH;
}
Expand Down