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

Improve performannce of error prone PreferAssertj check #875

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
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 @@ -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