1 package de.example.custom.java.checks;
5 import org.sonar.api.utils.log.Logger;
6 import org.sonar.api.utils.log.Loggers;
7 import org.sonar.check.Rule;
8 import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
9 import org.sonar.plugins.java.api.JavaFileScannerContext;
10 import org.sonar.plugins.java.api.semantic.Symbol;
11 import org.sonar.plugins.java.api.tree.ClassTree;
12 import org.sonar.plugins.java.api.tree.Tree;
13 import org.sonar.plugins.java.api.tree.Tree.Kind;
14 import org.sonar.plugins.java.api.tree.VariableTree;
16 import com.google.common.collect.ImmutableList;
19 public class SpringServiceInstanceFieldCheck extends IssuableSubscriptionVisitor {
20 private static final Logger LOG = Loggers.get(SpringServiceInstanceFieldCheck.class);
22 private JavaFileScannerContext context;
26 public List<Kind> nodesToVisit() {
27 return ImmutableList.of(Kind.CLASS, Kind.VARIABLE);
31 public void visitNode(Tree tree) {
32 if (tree.is(Kind.CLASS) && isSpringService((ClassTree) tree)) {
39 private static boolean isOwnedByASpringService(VariableTree variable) {
40 Symbol owner = variable.symbol().owner();
41 return owner.isTypeSymbol() && (owner.type().isSubtypeOf("javax.servlet.http.HttpServlet") || owner.type().isSubtypeOf("org.apache.struts.action.Action"));
44 private static boolean isSpringService(ClassTree tree) {
45 tree.symbol().metadata().isAnnotatedWith("javax.inject.Inject");