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.ASTClassOrInterfaceDeclaration;
    7   import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
    8   import net.sourceforge.pmd.ast.ASTEnumDeclaration;
    9   import net.sourceforge.pmd.ast.ASTExplicitConstructorInvocation;
    10  import net.sourceforge.pmd.ast.ASTFieldDeclaration;
    11  import net.sourceforge.pmd.ast.ASTInitializer;
    12  import net.sourceforge.pmd.ast.ASTMethodDeclaration;
    13  import net.sourceforge.pmd.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 NcssTypeCount extends AbstractNcssCount {
    23
    24    /**
    25     * Count type declarations. This includes classes as well as enums and
    26     * annotations.
    27     */
    28    public NcssTypeCount() {
    29      super( ASTTypeDeclaration.class );
    30    }
    31
    32    public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    33
    34      if ( !node.isNested() ) {
    35        return super.visit( node, data );
    36      }
    37
    38      return countNodeChildren( node, data );
    39    }
    40
    41    public Object visit(ASTConstructorDeclaration node, Object data) {
    42      return countNodeChildren( node, data );
    43    }
    44
    45    public Object visit(ASTExplicitConstructorInvocation node, Object data) {
    46      return NumericConstants.ONE;
    47    }
    48
    49    public Object visit(ASTEnumDeclaration node, Object data) {
    50      /*
    51       * If the enum is a type in and of itself, don't count its declaration
    52       * twice.
    53       */
    54      if ( node.jjtGetParent() instanceof ASTTypeDeclaration ) {
    55        Integer nodeCount = countNodeChildren( node, data );
    56        int count = nodeCount.intValue() - 1;
    57        return Integer.valueOf( count );
    58      }
    59      return countNodeChildren( node, data );
    60    }
    61
    62    public Object visit(ASTMethodDeclaration node, Object data) {
    63      return countNodeChildren( node, data );
    64    }
    65
    66    public Object visit(ASTInitializer node, Object data) {
    67      return countNodeChildren( node, data );
    68    }
    69
    70    public Object visit(ASTFieldDeclaration node, Object data) {
    71      return NumericConstants.ONE;
    72    }
    73
    74    protected void makeViolations(RuleContext ctx, Set<DataPoint> p) {
    75      for ( DataPoint point: p ) {
    76        addViolation( ctx, point.getNode(),
    77            String.valueOf( (int) point.getScore() ) );
    78      }
    79    }
    80
    81  }