View Javadoc

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