Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Sep 12, 2022
1 parent 83629ce commit 6f199a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@
import com.sun.source.util.TreeScanner;
import javax.annotation.Nullable;

abstract class AbstractTestChecker extends BugChecker implements CompilationUnitTreeMatcher {
/**
* An abstract {@link BugChecker} that reports a match for each expression matched by the given
* {@link Matcher}.
*
* <p>Only {@link ExpressionTree}s that represent proper Java expressions (i.e. {@link
* ExpressionTree}s that may be matched by Refaster) are considered.
*/
abstract class AbstractMatcherTestChecker extends BugChecker implements CompilationUnitTreeMatcher {
private static final long serialVersionUID = 1L;

private final Matcher<ExpressionTree> delegate;

AbstractTestChecker(Matcher<ExpressionTree> delegate) {
AbstractMatcherTestChecker(Matcher<ExpressionTree> delegate) {
this.delegate = delegate;
}

Expand All @@ -27,34 +34,34 @@ public Description matchCompilationUnit(CompilationUnitTree compilationUnit, Vis
new TreeScanner<Void, Void>() {
@Nullable
@Override
public Void scan(Tree tree, @Nullable Void p) {
public Void scan(Tree tree, @Nullable Void unused) {
if (tree instanceof ExpressionTree && delegate.matches((ExpressionTree) tree, state)) {
state.reportMatch(
Description.builder(tree, canonicalName(), null, defaultSeverity(), message())
.build());
}

return super.scan(tree, p);
return super.scan(tree, unused);
}

@Nullable
@Override
public Void visitImport(ImportTree node, @Nullable Void unused) {
/*
* We're not interested in matching import statements. While components of these
* can be `ExpressionTree`s.
* can be `ExpressionTree`s, they will never be matched by Refaster.
*/
return null;
}

@Nullable
@Override
public Void visitMethod(MethodTree node, @Nullable Void p) {
public Void visitMethod(MethodTree node, @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(node.getBody(), p);
return scan(node.getBody(), unused);
}
}.scan(compilationUnit, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class IsArrayTest {
@Test
void matches() {
CompilationTestHelper.newInstance(TestChecker.class, getClass())
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass())
.addSourceLines(
"A.java",
"class A {",
Expand Down Expand Up @@ -50,13 +50,13 @@ void matches() {

/** A {@link BugChecker} which simply delegates to {@link IsArray}. */
@BugPattern(summary = "Flags expressions matched by `IsArray`", severity = ERROR)
public static final class TestChecker extends AbstractTestChecker {
public static final class MatcherTestChecker extends AbstractMatcherTestChecker {
private static final long serialVersionUID = 1L;

// XXX: This is a false positive reported by Checkstyle. See
// https://github.com/checkstyle/checkstyle/issues/10161#issuecomment-1242732120.
@SuppressWarnings("RedundantModifier")
public TestChecker() {
public MatcherTestChecker() {
super(new IsArray());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class ThrowsCheckedExceptionTest {
@Test
void matches() {
CompilationTestHelper.newInstance(TestChecker.class, getClass())
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass())
.addSourceLines(
"A.java",
"import java.util.concurrent.Callable;",
Expand Down Expand Up @@ -81,13 +81,13 @@ void matches() {

/** A {@link BugChecker} which simply delegates to {@link ThrowsCheckedException}. */
@BugPattern(summary = "Flags expressions matched by `ThrowsCheckedException`", severity = ERROR)
public static final class TestChecker extends AbstractTestChecker {
public static final class MatcherTestChecker extends AbstractMatcherTestChecker {
private static final long serialVersionUID = 1L;

// XXX: This is a false positive reported by Checkstyle. See
// https://github.com/checkstyle/checkstyle/issues/10161#issuecomment-1242732120.
@SuppressWarnings("RedundantModifier")
public TestChecker() {
public MatcherTestChecker() {
super(new ThrowsCheckedException());
}
}
Expand Down

0 comments on commit 6f199a3

Please sign in to comment.