mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Simplify AutowiredConstructorCheck
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package tech.picnic.errorprone.bugpatterns;
|
||||
|
||||
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
|
||||
import static com.google.errorprone.matchers.Matchers.annotations;
|
||||
import static com.google.errorprone.matchers.Matchers.isType;
|
||||
|
||||
import com.google.auto.service.AutoService;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.errorprone.BugPattern;
|
||||
import com.google.errorprone.BugPattern.LinkType;
|
||||
import com.google.errorprone.BugPattern.ProvidesFix;
|
||||
@@ -8,16 +13,16 @@ import com.google.errorprone.BugPattern.SeverityLevel;
|
||||
import com.google.errorprone.BugPattern.StandardTags;
|
||||
import com.google.errorprone.VisitorState;
|
||||
import com.google.errorprone.bugpatterns.BugChecker;
|
||||
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher;
|
||||
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
|
||||
import com.google.errorprone.fixes.SuggestedFix;
|
||||
import com.google.errorprone.matchers.AnnotationType;
|
||||
import com.google.errorprone.matchers.Description;
|
||||
import com.google.errorprone.matchers.Matcher;
|
||||
import com.google.errorprone.matchers.MultiMatcher;
|
||||
import com.google.errorprone.util.ASTHelpers;
|
||||
import com.sun.source.tree.AnnotationTree;
|
||||
import com.sun.source.tree.ClassTree;
|
||||
import com.sun.source.tree.MethodTree;
|
||||
import com.sun.tools.javac.code.Symbol.MethodSymbol;
|
||||
import com.sun.source.tree.Tree;
|
||||
import java.util.List;
|
||||
|
||||
/** A {@link BugChecker} which flags redundant {@code @Autowired} constructor annotations. */
|
||||
@AutoService(BugChecker.class)
|
||||
@@ -28,23 +33,23 @@ import com.sun.tools.javac.code.Symbol.MethodSymbol;
|
||||
severity = SeverityLevel.SUGGESTION,
|
||||
tags = StandardTags.SIMPLIFICATION,
|
||||
providesFix = ProvidesFix.REQUIRES_HUMAN_ATTENTION)
|
||||
public final class AutowiredConstructorCheck extends BugChecker implements AnnotationTreeMatcher {
|
||||
public final class AutowiredConstructorCheck extends BugChecker implements ClassTreeMatcher {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Matcher<AnnotationTree> AUTOWIRED_ANNOTATION =
|
||||
new AnnotationType("org.springframework.beans.factory.annotation.Autowired");
|
||||
private static final MultiMatcher<Tree, AnnotationTree> AUTOWIRED_ANNOTATION =
|
||||
annotations(AT_LEAST_ONE, isType("org.springframework.beans.factory.annotation.Autowired"));
|
||||
|
||||
@Override
|
||||
public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
|
||||
if (!AUTOWIRED_ANNOTATION.matches(tree, state)) {
|
||||
public Description matchClass(ClassTree tree, VisitorState state) {
|
||||
List<MethodTree> constructors = ASTHelpers.getConstructors(tree);
|
||||
if (constructors.size() != 1) {
|
||||
return Description.NO_MATCH;
|
||||
}
|
||||
|
||||
if (!isWithinConstructor(state)) {
|
||||
return Description.NO_MATCH;
|
||||
}
|
||||
|
||||
ClassTree clazz = state.findEnclosing(ClassTree.class);
|
||||
if (clazz == null || ASTHelpers.getConstructors(clazz).size() != 1) {
|
||||
List<AnnotationTree> annotations =
|
||||
AUTOWIRED_ANNOTATION
|
||||
.multiMatchResult(Iterables.getOnlyElement(constructors), state)
|
||||
.matchingNodes();
|
||||
if (annotations.size() != 1) {
|
||||
return Description.NO_MATCH;
|
||||
}
|
||||
|
||||
@@ -53,16 +58,7 @@ public final class AutowiredConstructorCheck extends BugChecker implements Annot
|
||||
* means that the associated import can be removed as well. Rather than adding code for this case we
|
||||
* leave flagging the unused import to Error Prone's `RemoveUnusedImports` check.
|
||||
*/
|
||||
return describeMatch(tree, SuggestedFix.delete(tree));
|
||||
}
|
||||
|
||||
private static boolean isWithinConstructor(VisitorState state) {
|
||||
MethodTree method = state.findEnclosing(MethodTree.class);
|
||||
if (method == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
MethodSymbol sym = ASTHelpers.getSymbol(method);
|
||||
return sym != null && sym.isConstructor();
|
||||
AnnotationTree annotation = Iterables.getOnlyElement(annotations);
|
||||
return describeMatch(annotation, SuggestedFix.delete(annotation));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public final class AutowiredConstructorCheckTest {
|
||||
"",
|
||||
"interface Container {",
|
||||
" class A {",
|
||||
" @Autowired A() {}",
|
||||
" @Autowired @Deprecated A() {}",
|
||||
" }",
|
||||
"",
|
||||
" class B {",
|
||||
@@ -83,7 +83,7 @@ public final class AutowiredConstructorCheckTest {
|
||||
"",
|
||||
"interface Container {",
|
||||
" class A {",
|
||||
" A() {}",
|
||||
" @Deprecated A() {}",
|
||||
" }",
|
||||
"",
|
||||
" class B {",
|
||||
|
||||
Reference in New Issue
Block a user