|
| 1 | +package tech.picnic.errorprone.bugpatterns; |
| 2 | + |
| 3 | +import static com.google.errorprone.BugPattern.LinkType.CUSTOM; |
| 4 | +import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; |
| 5 | +import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION; |
| 6 | +import static com.google.errorprone.matchers.Matchers.allOf; |
| 7 | +import static com.google.errorprone.matchers.Matchers.argumentCount; |
| 8 | +import static com.google.errorprone.matchers.Matchers.instanceMethod; |
| 9 | +import static java.util.Objects.requireNonNull; |
| 10 | +import static tech.picnic.errorprone.utils.Documentation.BUG_PATTERNS_BASE_URL; |
| 11 | + |
| 12 | +import com.google.auto.service.AutoService; |
| 13 | +import com.google.common.collect.ImmutableMap; |
| 14 | +import com.google.common.collect.Iterables; |
| 15 | +import com.google.errorprone.BugPattern; |
| 16 | +import com.google.errorprone.VisitorState; |
| 17 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 18 | +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
| 19 | +import com.google.errorprone.fixes.SuggestedFix; |
| 20 | +import com.google.errorprone.fixes.SuggestedFixes; |
| 21 | +import com.google.errorprone.matchers.Description; |
| 22 | +import com.google.errorprone.matchers.Matcher; |
| 23 | +import com.google.errorprone.util.ASTHelpers; |
| 24 | +import com.sun.source.tree.ExpressionTree; |
| 25 | +import com.sun.source.tree.MethodInvocationTree; |
| 26 | +import java.util.Optional; |
| 27 | +import tech.picnic.errorprone.utils.SourceCode; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link BugChecker} that flags AssertJ equality and identity checks on unconditionally unwrapped |
| 31 | + * {@link Optional} instances for simplification. |
| 32 | + * |
| 33 | + * <p>This bug checker cannot be replaced with a simple Refaster rule, as the Refaster approach |
| 34 | + * would require that all overloads of the mentioned methods (such as {@link |
| 35 | + * org.assertj.core.api.AbstractStringAssert#isEqualTo(String)}) are explicitly enumerated. This bug |
| 36 | + * checker generically matches all such current and future overloads. |
| 37 | + */ |
| 38 | +@AutoService(BugChecker.class) |
| 39 | +@BugPattern( |
| 40 | + summary = "Prefer `.hasValue(value)` and `.containsSame(value)` over more verbose alternatives", |
| 41 | + link = BUG_PATTERNS_BASE_URL + "AssertJOptionalAssertion", |
| 42 | + linkType = CUSTOM, |
| 43 | + severity = SUGGESTION, |
| 44 | + tags = SIMPLIFICATION) |
| 45 | +public final class AssertJOptionalAssertion extends BugChecker |
| 46 | + implements MethodInvocationTreeMatcher { |
| 47 | + private static final long serialVersionUID = 1L; |
| 48 | + private static final ImmutableMap<String, String> REPLACEMENT_METHODS = |
| 49 | + ImmutableMap.of("isEqualTo", "hasValue", "isSameAs", "containsSame"); |
| 50 | + private static final Matcher<MethodInvocationTree> ASSERTION = |
| 51 | + allOf( |
| 52 | + instanceMethod() |
| 53 | + .onDescendantOf("org.assertj.core.api.Assert") |
| 54 | + .namedAnyOf(REPLACEMENT_METHODS.keySet()), |
| 55 | + argumentCount(1)); |
| 56 | + private static final Matcher<ExpressionTree> OPTIONAL_UNWRAP = |
| 57 | + instanceMethod() |
| 58 | + .onExactClass(Optional.class.getCanonicalName()) |
| 59 | + .namedAnyOf("get", "orElseThrow"); |
| 60 | + |
| 61 | + /** Instantiates a new {@link AssertJOptionalAssertion} instance. */ |
| 62 | + public AssertJOptionalAssertion() {} |
| 63 | + |
| 64 | + @Override |
| 65 | + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
| 66 | + if (!ASSERTION.matches(tree, state)) { |
| 67 | + return Description.NO_MATCH; |
| 68 | + } |
| 69 | + |
| 70 | + return extractOptionalUnwrap(tree, state) |
| 71 | + .map(optionalUnwrap -> describeMatch(tree, suggestFix(tree, optionalUnwrap, state))) |
| 72 | + .orElse(Description.NO_MATCH); |
| 73 | + } |
| 74 | + |
| 75 | + private static Optional<MethodInvocationTree> extractOptionalUnwrap( |
| 76 | + MethodInvocationTree tree, VisitorState state) { |
| 77 | + if (!(ASTHelpers.getReceiver(tree) instanceof MethodInvocationTree receiver) |
| 78 | + || receiver.getArguments().size() != 1 |
| 79 | + || !ASTHelpers.getSymbol(receiver).isStatic()) { |
| 80 | + /* This doesn't look like the start of an assertion statement. */ |
| 81 | + return Optional.empty(); |
| 82 | + } |
| 83 | + |
| 84 | + if (!(Iterables.getOnlyElement(receiver.getArguments()) instanceof MethodInvocationTree subject) |
| 85 | + || !OPTIONAL_UNWRAP.matches(subject, state)) { |
| 86 | + /* The assertion doesn't involve the unconditional unwrapping of an `Optional`. */ |
| 87 | + return Optional.empty(); |
| 88 | + } |
| 89 | + |
| 90 | + return Optional.of(subject); |
| 91 | + } |
| 92 | + |
| 93 | + private static SuggestedFix suggestFix( |
| 94 | + MethodInvocationTree assertion, MethodInvocationTree optionalUnwrap, VisitorState state) { |
| 95 | + ExpressionTree optional = |
| 96 | + requireNonNull( |
| 97 | + ASTHelpers.getReceiver(optionalUnwrap), "Method invocation must have receiver"); |
| 98 | + return SuggestedFixes.renameMethodInvocation(assertion, getReplacementMethod(assertion), state) |
| 99 | + .toBuilder() |
| 100 | + .replace(optionalUnwrap, SourceCode.treeToString(optional, state)) |
| 101 | + .build(); |
| 102 | + } |
| 103 | + |
| 104 | + private static String getReplacementMethod(MethodInvocationTree tree) { |
| 105 | + return requireNonNull( |
| 106 | + REPLACEMENT_METHODS.get(ASTHelpers.getSymbol(tree).getSimpleName().toString()), |
| 107 | + "Unexpected method name"); |
| 108 | + } |
| 109 | +} |
0 commit comments