01: package net.sourceforge.pmd.rules.codesize;
02:
03: import java.util.Set;
04:
05: import net.sourceforge.pmd.RuleContext;
06: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
07: import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
08: import net.sourceforge.pmd.ast.ASTEnumDeclaration;
09: 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,
46: 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 Integer.valueOf(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<DataPoint> p) {
76: for (DataPoint point : p) {
77: addViolation(ctx, point.getNode(), String
78: .valueOf((int) point.getScore()));
79: }
80: }
81:
82: }
|