From 17c7b396d20390a70a8c5a771db65c8363cb896a Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 27 Nov 2023 11:40:16 +0100 Subject: [PATCH] Simplify `AbstractMatcherTestChecker` (#853) By using `TreePathScanner` rather than directly using `TreeScanner`. --- .../matchers/AbstractMatcherTestChecker.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/AbstractMatcherTestChecker.java b/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/AbstractMatcherTestChecker.java index 48fac2d4..2f1f7e73 100644 --- a/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/AbstractMatcherTestChecker.java +++ b/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/AbstractMatcherTestChecker.java @@ -13,7 +13,7 @@ import com.sun.source.tree.MethodTree; 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; /** @@ -34,15 +34,11 @@ 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))) { @@ -50,11 +46,11 @@ abstract class AbstractMatcherTestChecker extends BugChecker implements Compilat } } - 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 * can be `ExpressionTree`s, they will never be matched by Refaster. @@ -63,24 +59,24 @@ abstract class AbstractMatcherTestChecker extends BugChecker implements Compilat } @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; }