1 package de.example.custom.java.checks;
5 import javax.annotation.Nullable;
7 import org.sonar.api.utils.log.Logger;
8 import org.sonar.api.utils.log.Loggers;
9 import org.sonar.check.Rule;
10 import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
11 import org.sonar.plugins.java.api.JavaFileScannerContext;
12 import org.sonar.plugins.java.api.semantic.Symbol;
13 import org.sonar.plugins.java.api.tree.AnnotationTree;
14 import org.sonar.plugins.java.api.tree.ClassTree;
15 import org.sonar.plugins.java.api.tree.Tree;
16 import org.sonar.plugins.java.api.tree.Tree.Kind;
17 import org.sonar.plugins.java.api.tree.VariableTree;
19 import com.google.common.collect.ImmutableList;
21 @Rule(key = "GUJ0002")
22 public class SpringServiceInstanceFieldCheck extends IssuableSubscriptionVisitor {
23 private static final Logger LOG = Loggers.get(SpringServiceInstanceFieldCheck.class);
25 private JavaFileScannerContext context;
29 public List<Kind> nodesToVisit() {
30 return ImmutableList.of(Kind.CLASS, Kind.VARIABLE);
34 public void visitNode(Tree tree) {
36 if (tree.is(Kind.CLASS) && isSpringService((ClassTree) tree)) {
43 public void visitAnnotation(AnnotationTree annotationTree) {
44 scan(annotationTree.annotationType());
45 scan(annotationTree.arguments());
49 private static boolean isOwnedByASpringService(VariableTree variable) {
50 Symbol owner = variable.symbol().owner();
51 return owner.isTypeSymbol() && (owner.type().isSubtypeOf("javax.servlet.http.HttpServlet") || owner.type().isSubtypeOf("org.apache.struts.action.Action"));
54 private static boolean isSpringService(ClassTree tree) {
55 tree.symbol().metadata().isAnnotatedWith("javax.inject.Inject");
56 tree.symbol().metadata().isAnnotatedWith("javax.inject.Inject");
61 protected void scan(@Nullable Tree tree) {