View Javadoc

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