View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.design;
5   
6   import java.util.HashSet;
7   import java.util.Set;
8   
9   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
10  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
11  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
12  
13  public class SingleMethodSingletonRule extends AbstractJavaRule {
14  
15      private Set<String> methodset = new HashSet<String>();
16  
17      @Override
18      public Object visit(ASTCompilationUnit node, Object data) {
19          methodset.clear();
20          return super.visit(node, data);
21      }
22  
23      @Override
24      public Object visit(ASTMethodDeclaration node, Object data) {
25          if (node.getResultType().isVoid()) {
26              return super.visit(node, data);
27          }
28  
29          if ("getInstance".equals(node.getMethodName())) {
30              if (!methodset.add(node.getMethodName())) {
31                  addViolation(data, node);
32              }
33          }
34  
35          return super.visit(node, data);
36      }
37  }