914d34513ee38fe282fefa30e102fc138aa2efa7
[JavaForFun] /
1 package de.example.custom.java.checks;
2
3 import java.util.List;
4
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.semantic.Symbol.MethodSymbol;
10 import org.sonar.plugins.java.api.semantic.Type;
11 import org.sonar.plugins.java.api.tree.MethodTree;
12 import org.sonar.plugins.java.api.tree.Tree;
13 import org.sonar.plugins.java.api.tree.Tree.Kind;
14
15 import com.google.common.collect.ImmutableList;
16
17 @Rule(key = "GUJ0001")
18 public class ParameterCheck extends IssuableSubscriptionVisitor {
19         private static final Logger LOG = Loggers.get(ParameterCheck.class);
20
21
22         @Override
23         public List<Kind> nodesToVisit() {
24                 return ImmutableList.of(Kind.METHOD);
25         }
26
27         @Override
28         public void visitNode(Tree tree) {
29                 LOG.info("Visiting Node");
30
31                 MethodTree method = (MethodTree) tree;
32                 
33                 if (method.parameters().size() == 1) {
34                         MethodSymbol symbol = method.symbol();
35                         Type firstParameterType = symbol.parameterTypes().get(0);
36                         Type returnType = symbol.returnType().type();
37                         if(returnType.is(firstParameterType.fullyQualifiedName())) {
38                                 reportIssue(method.simpleName(), "Never do that!");
39                         }
40                 }
41                 
42         }
43 }