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.ASTConstructorDeclaration;
8   import net.sourceforge.pmd.ast.ASTExplicitConstructorInvocation;
9   import net.sourceforge.pmd.stat.DataPoint;
10  import net.sourceforge.pmd.util.NumericConstants;
11  
12  /***
13   * Non-commented source statement counter for constructors.
14   * 
15   * @author Jason Bennett
16   */
17  public class NcssConstructorCount extends AbstractNcssCount {
18  
19    /***
20     * Count constructor declarations. This includes any explicit super() calls.
21     */
22    public NcssConstructorCount() {
23      super( ASTConstructorDeclaration.class );
24    }
25  
26    public Object visit(ASTExplicitConstructorInvocation node, Object data) {
27      return NumericConstants.ONE;
28    }
29  
30    protected void makeViolations(RuleContext ctx, Set p) {
31      Iterator points = p.iterator();
32      while ( points.hasNext() ) {
33        DataPoint point = (DataPoint) points.next();
34        // TODO need to put class name or constructor ID in string
35        addViolation(
36            ctx,
37            point.getNode(),
38            new String[] {
39                String.valueOf( ( (ASTConstructorDeclaration) point.getNode() ).getParameterCount() ),
40                String.valueOf( (int) point.getScore() ) } );
41      }
42    }
43  }