Repository navigation
Expand file tree
/
Copy pathTooManyFieldsRule.java
More file actions
99 lines (78 loc) · 3.06 KB
/
Copy pathTooManyFieldsRule.java
File metadata and controls
99 lines (78 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql.rule.design;
import static net.sourceforge.pmd.properties.NumericConstraints.positive;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
import net.sourceforge.pmd.lang.plsql.ast.ASTPackageSpecification;
import net.sourceforge.pmd.lang.plsql.ast.ASTTypeSpecification;
import net.sourceforge.pmd.lang.plsql.ast.ASTVariableOrConstantDeclaration;
import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
import net.sourceforge.pmd.lang.plsql.rule.AbstractPLSQLRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
public class TooManyFieldsRule extends AbstractPLSQLRule {
private static final int DEFAULT_MAXFIELDS = 15;
private Map<String, Integer> stats;
private Map<String, PLSQLNode> nodes;
private static final PropertyDescriptor<Integer> MAX_FIELDS_DESCRIPTOR
= PropertyFactory.intProperty("maxfields")
.desc("Max allowable fields")
.defaultValue(DEFAULT_MAXFIELDS)
.require(positive())
.build();
public TooManyFieldsRule() {
definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR);
}
@Override
public Object visit(ASTInput node, Object data) {
stats = new HashMap<>(5);
nodes = new HashMap<>(5);
return super.visit(node, data);
}
@Override
public Object visit(ASTPackageSpecification node, Object data) {
int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
List<ASTVariableOrConstantDeclaration> l = node.descendants(ASTVariableOrConstantDeclaration.class).toList();
for (ASTVariableOrConstantDeclaration fd : l) {
bumpCounterFor(fd);
}
for (String k : stats.keySet()) {
int val = stats.get(k);
Node n = nodes.get(k);
if (val > maxFields) {
asCtx(data).addViolation(n);
}
}
return data;
}
@Override
public Object visit(ASTTypeSpecification node, Object data) {
int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
List<ASTVariableOrConstantDeclaration> l = node.descendants(ASTVariableOrConstantDeclaration.class).toList();
for (ASTVariableOrConstantDeclaration fd : l) {
bumpCounterFor(fd);
}
for (String k : stats.keySet()) {
int val = stats.get(k);
Node n = nodes.get(k);
if (val > maxFields) {
asCtx(data).addViolation(n);
}
}
return data;
}
private void bumpCounterFor(PLSQLNode clazz) {
String key = clazz.getImage();
if (!stats.containsKey(key)) {
stats.put(key, 0);
nodes.put(key, clazz);
}
Integer i = stats.get(key) + 1;
stats.put(key, i);
}
}