1
2
3
4 package net.sourceforge.pmd.lang.plsql.rule.codesize;
5
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import net.sourceforge.pmd.lang.ast.Node;
11 import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
12 import net.sourceforge.pmd.lang.plsql.ast.ASTPackageSpecification;
13 import net.sourceforge.pmd.lang.plsql.ast.ASTTypeSpecification;
14 import net.sourceforge.pmd.lang.plsql.ast.ASTVariableOrConstantDeclaration;
15 import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
16 import net.sourceforge.pmd.lang.plsql.rule.AbstractPLSQLRule;
17 import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
18 import net.sourceforge.pmd.util.NumericConstants;
19
20
21 public class TooManyFieldsRule extends AbstractPLSQLRule {
22
23 private static final int DEFAULT_MAXFIELDS = 15;
24
25 private Map<String, Integer> stats;
26 private Map<String, PLSQLNode> nodes;
27
28 private static final IntegerProperty MAX_FIELDS_DESCRIPTOR = new IntegerProperty(
29 "maxfields", "Max allowable fields",
30 1, 300, DEFAULT_MAXFIELDS, 1.0f
31 );
32
33 public TooManyFieldsRule() {
34 definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR);
35 }
36
37 @Override
38 public Object visit(ASTInput node, Object data) {
39
40
41 stats = new HashMap<String, Integer>(5);
42 nodes = new HashMap<String, PLSQLNode>(5);
43
44 return super.visit(node, data);
45 }
46
47 @Override
48 public Object visit(ASTPackageSpecification node, Object data) {
49
50 int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
51
52 List<ASTVariableOrConstantDeclaration> l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class);
53
54 for (ASTVariableOrConstantDeclaration fd: l) {
55 bumpCounterFor(node);
56 }
57 for (String k : stats.keySet()) {
58 int val = stats.get(k);
59 Node n = nodes.get(k);
60 if (val > maxFields) {
61 addViolation(data, n);
62 }
63 }
64 return data;
65 }
66
67 @Override
68 public Object visit(ASTTypeSpecification node, Object data) {
69
70 int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
71
72 List<ASTVariableOrConstantDeclaration> l = node.findDescendantsOfType(ASTVariableOrConstantDeclaration.class);
73
74 for (ASTVariableOrConstantDeclaration fd: l) {
75 bumpCounterFor(node);
76 }
77 for (String k : stats.keySet()) {
78 int val = stats.get(k);
79 Node n = nodes.get(k);
80 if (val > maxFields) {
81 addViolation(data, n);
82 }
83 }
84 return data;
85 }
86
87 private void bumpCounterFor(PLSQLNode clazz) {
88 String key = clazz.getImage();
89 if (!stats.containsKey(key)) {
90 stats.put(key, NumericConstants.ZERO);
91 nodes.put(key, clazz);
92 }
93 Integer i = Integer.valueOf(stats.get(key) + 1);
94 stats.put(key, i);
95 }
96 }