View Javadoc

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