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.codesize;
5   
6   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
7   import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
8   import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
9   import net.sourceforge.pmd.lang.java.ast.ASTExplicitConstructorInvocation;
10  import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
11  import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
12  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
13  import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
14  import net.sourceforge.pmd.stat.DataPoint;
15  import net.sourceforge.pmd.util.NumericConstants;
16  
17  /**
18   * Non-commented source statement counter for type declarations.
19   * 
20   * @author Jason Bennett
21   */
22  public class NcssTypeCountRule extends AbstractNcssCountRule {
23  
24      /**
25       * Count type declarations. This includes classes as well as enums and
26       * annotations.
27       */
28      public NcssTypeCountRule() {
29          super(ASTTypeDeclaration.class);
30          setProperty(MINIMUM_DESCRIPTOR, 1500d);
31      }
32  
33      @Override
34      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
35  
36          if (!node.isNested()) {
37              return super.visit(node, data);
38          }
39  
40          return countNodeChildren(node, data);
41      }
42  
43      @Override
44      public Object visit(ASTConstructorDeclaration node, Object data) {
45          return countNodeChildren(node, data);
46      }
47  
48      @Override
49      public Object visit(ASTExplicitConstructorInvocation node, Object data) {
50          return NumericConstants.ONE;
51      }
52  
53      @Override
54      public Object visit(ASTEnumDeclaration node, Object data) {
55          /*
56           * If the enum is a type in and of itself, don't count its declaration
57           * twice.
58           */
59          if (node.jjtGetParent() instanceof ASTTypeDeclaration) {
60              Integer nodeCount = countNodeChildren(node, data);
61              int count = nodeCount.intValue() - 1;
62              return Integer.valueOf(count);
63          }
64          return countNodeChildren(node, data);
65      }
66  
67      @Override
68      public Object visit(ASTMethodDeclaration node, Object data) {
69          return countNodeChildren(node, data);
70      }
71  
72      @Override
73      public Object visit(ASTInitializer node, Object data) {
74          return countNodeChildren(node, data);
75      }
76  
77      @Override
78      public Object visit(ASTFieldDeclaration node, Object data) {
79          return NumericConstants.ONE;
80      }
81  
82      @Override
83      public Object[] getViolationParameters(DataPoint point) {
84          return new String[] { String.valueOf((int) point.getScore()) };
85      }
86  }