|
16 | 16 |
|
17 | 17 | package com.google.errorprone.bugpatterns;
|
18 | 18 |
|
19 |
| -import static com.google.common.collect.ImmutableList.toImmutableList; |
20 | 19 | import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
|
| 20 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 21 | +import static com.google.errorprone.util.ASTHelpers.getType; |
| 22 | +import static com.google.errorprone.util.ASTHelpers.isSubtype; |
21 | 23 |
|
22 |
| -import com.google.common.collect.ImmutableList; |
23 | 24 | import com.google.errorprone.BugPattern;
|
24 | 25 | import com.google.errorprone.VisitorState;
|
25 |
| -import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; |
| 26 | +import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; |
26 | 27 | import com.google.errorprone.fixes.SuggestedFixes;
|
27 | 28 | import com.google.errorprone.matchers.Description;
|
28 | 29 | import com.google.errorprone.matchers.Matchers;
|
|
40 | 41 | "To return a custom message with a Throwable class, one should "
|
41 | 42 | + "override getMessage() instead of toString().",
|
42 | 43 | severity = WARNING)
|
43 |
| -public final class OverrideThrowableToString extends BugChecker implements ClassTreeMatcher { |
| 44 | +public final class OverrideThrowableToString extends BugChecker implements MethodTreeMatcher { |
44 | 45 |
|
45 | 46 | @Override
|
46 |
| - public Description matchClass(ClassTree classTree, VisitorState state) { |
47 |
| - if (!ASTHelpers.isSubtype( |
48 |
| - ASTHelpers.getType(classTree), state.getSymtab().throwableType, state)) { |
49 |
| - return Description.NO_MATCH; |
| 47 | + public Description matchMethod(MethodTree methodTree, VisitorState state) { |
| 48 | + if (!Matchers.toStringMethodDeclaration().matches(methodTree, state)) { |
| 49 | + return NO_MATCH; |
50 | 50 | }
|
51 |
| - ImmutableList<MethodTree> methods = |
52 |
| - classTree.getMembers().stream() |
53 |
| - .filter(m -> m instanceof MethodTree) |
54 |
| - .map(m -> (MethodTree) m) |
55 |
| - .collect(toImmutableList()); |
56 |
| - if (methods.stream().anyMatch(m -> m.getName().contentEquals("getMessage"))) { |
57 |
| - return Description.NO_MATCH; |
| 51 | + ClassTree classTree = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class); |
| 52 | + if (!isSubtype(getType(classTree), state.getSymtab().throwableType, state)) { |
| 53 | + return NO_MATCH; |
58 | 54 | }
|
59 |
| - return methods.stream() |
60 |
| - .filter(m -> Matchers.toStringMethodDeclaration().matches(m, state)) |
61 |
| - .findFirst() |
62 |
| - .map(m -> describeMatch(m, SuggestedFixes.renameMethod(m, "getMessage", state))) |
63 |
| - .orElse(Description.NO_MATCH); |
| 55 | + if (classTree.getMembers().stream() |
| 56 | + .filter(m -> m instanceof MethodTree) |
| 57 | + .map(m -> (MethodTree) m) |
| 58 | + .anyMatch(m -> m.getName().contentEquals("getMessage"))) { |
| 59 | + return NO_MATCH; |
| 60 | + } |
| 61 | + return describeMatch(methodTree, SuggestedFixes.renameMethod(methodTree, "getMessage", state)); |
64 | 62 | }
|
65 | 63 | }
|
0 commit comments