Skip to content

Commit

Permalink
Improve performannce of error prone PreferAssertj check (#875)
Browse files Browse the repository at this point in the history
Improve performannce of error prone PreferAssertj check
  • Loading branch information
carterkozak authored and bulldozer-bot[bot] committed Sep 23, 2019
1 parent 6cd6b5c commit bfd1caf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public final class PreferAssertj extends BugChecker implements BugChecker.Method
"junit.framework.TestCase",
"junit.framework.Assert");

// Must match everything otherwise matched by this check, used to avoid
// additional checks for each method invocation.
private static final Matcher<ExpressionTree> FAST_CHECK = MethodMatchers.staticMethod()
.onClassAny(LEGACY_ASSERT_CLASSES);

private static final Matcher<ExpressionTree> ASSERT_TRUE =
MethodMatchers.staticMethod()
.onClassAny(LEGACY_ASSERT_CLASSES)
Expand Down Expand Up @@ -223,6 +228,11 @@ public final class PreferAssertj extends BugChecker implements BugChecker.Method
@Override
@SuppressWarnings({"CyclomaticComplexity", "MethodLength"})
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
// We check a lot of methods, the fast check allows us to quickly rule out most invocations
// without individually checking each of the more specific patterns.
if (!FAST_CHECK.matches(tree, state)) {
return Description.NO_MATCH;
}
if (ASSERT_TRUE.matches(tree, state)) {
return withAssertThat(tree, state, 0, (assertThat, fix) -> fix.replace(tree, assertThat + ".isTrue()"));
}
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-875.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Improve performannce of error prone PreferAssertj check
links:
- https://github.com/palantir/gradle-baseline/pull/875

0 comments on commit bfd1caf

Please sign in to comment.