|
16 | 16 |
|
17 | 17 | package com.google.errorprone.bugpatterns; |
18 | 18 |
|
19 | | -import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
20 | 20 | import static com.google.errorprone.util.ASTHelpers.findSuperMethods; |
21 | 21 | import static com.google.errorprone.util.ASTHelpers.getSymbol; |
| 22 | +import static com.sun.source.tree.Tree.Kind.NULL_LITERAL; |
22 | 23 |
|
23 | 24 | import com.google.common.util.concurrent.Futures; |
24 | 25 | import com.google.errorprone.VisitorState; |
25 | | -import com.google.errorprone.fixes.Fix; |
| 26 | +import com.google.errorprone.bugpatterns.BugChecker.ReturnTreeMatcher; |
26 | 27 | import com.google.errorprone.fixes.SuggestedFix; |
27 | | -import com.google.errorprone.matchers.Matcher; |
| 28 | +import com.google.errorprone.matchers.Description; |
| 29 | +import com.sun.source.tree.ExpressionTree; |
28 | 30 | import com.sun.source.tree.MethodTree; |
29 | 31 | import com.sun.source.tree.ReturnTree; |
30 | | -import com.sun.tools.javac.code.Symbol.MethodSymbol; |
31 | | -import java.util.Optional; |
| 32 | +import com.sun.source.tree.StatementTree; |
| 33 | +import com.sun.source.util.TreePath; |
32 | 34 |
|
33 | 35 | /** |
34 | 36 | * Superclass for checks that {@code AsyncCallable} and {@code AsyncFunction} implementations do not |
35 | 37 | * directly {@code return null}. |
36 | 38 | */ |
37 | | -abstract class AbstractAsyncTypeReturnsNull extends AbstractMethodReturnsNull { |
| 39 | +abstract class AbstractAsyncTypeReturnsNull extends BugChecker implements ReturnTreeMatcher { |
| 40 | + private final Class<?> asyncClass; |
38 | 41 |
|
39 | 42 | AbstractAsyncTypeReturnsNull(Class<?> asyncClass) { |
40 | | - super(overridesMethodOfClass(asyncClass)); |
| 43 | + this.asyncClass = asyncClass; |
41 | 44 | } |
42 | 45 |
|
43 | 46 | @Override |
44 | | - protected Optional<Fix> provideFix(ReturnTree tree) { |
45 | | - return Optional.of( |
46 | | - SuggestedFix.builder() |
47 | | - .replace(tree.getExpression(), "immediateFuture(null)") |
48 | | - .addStaticImport(Futures.class.getName() + ".immediateFuture") |
49 | | - .build()); |
| 47 | + public final Description matchReturn(ReturnTree tree, VisitorState state) { |
| 48 | + if (tree.getExpression() == null || tree.getExpression().getKind() != NULL_LITERAL) { |
| 49 | + return NO_MATCH; |
| 50 | + } |
| 51 | + TreePath path = state.getPath(); |
| 52 | + while (path != null && path.getLeaf() instanceof StatementTree) { |
| 53 | + path = path.getParentPath(); |
| 54 | + } |
| 55 | + if (path == null || !(path.getLeaf() instanceof MethodTree methodTree)) { |
| 56 | + return NO_MATCH; |
| 57 | + } |
| 58 | + if (findSuperMethods(getSymbol(methodTree), state.getTypes()).stream() |
| 59 | + .noneMatch( |
| 60 | + superMethod -> |
| 61 | + superMethod.owner != null |
| 62 | + && superMethod.owner.getQualifiedName().contentEquals(asyncClass.getName()))) { |
| 63 | + return NO_MATCH; |
| 64 | + } |
| 65 | + return describeMatch(tree, provideFix(tree.getExpression())); |
50 | 66 | } |
51 | 67 |
|
52 | | - private static Matcher<MethodTree> overridesMethodOfClass(Class<?> clazz) { |
53 | | - checkNotNull(clazz); |
54 | | - return new Matcher<MethodTree>() { |
55 | | - @Override |
56 | | - public boolean matches(MethodTree tree, VisitorState state) { |
57 | | - MethodSymbol symbol = getSymbol(tree); |
58 | | - for (MethodSymbol superMethod : findSuperMethods(symbol, state.getTypes())) { |
59 | | - if (superMethod.owner != null |
60 | | - && superMethod.owner.getQualifiedName().contentEquals(clazz.getName())) { |
61 | | - return true; |
62 | | - } |
63 | | - } |
64 | | - return false; |
65 | | - } |
66 | | - }; |
| 68 | + protected SuggestedFix provideFix(ExpressionTree tree) { |
| 69 | + return SuggestedFix.builder() |
| 70 | + .replace(tree, "immediateFuture(null)") |
| 71 | + .addStaticImport(Futures.class.getName() + ".immediateFuture") |
| 72 | + .build(); |
67 | 73 | } |
68 | 74 | } |
0 commit comments