Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 65 |  | Methods: | 2
| NCLOC: | 50 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
TooManyFields.java | 85.7% | 100% | 100% | 95% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.rules.design;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
8 |  | import net.sourceforge.pmd.ast.ASTCompilationUnit;
9 |  | import net.sourceforge.pmd.ast.ASTFieldDeclaration;
10 |  | import net.sourceforge.pmd.ast.SimpleNode;
11 |  |
12 |  | import java.util.HashMap;
13 |  | import java.util.Iterator;
14 |  | import java.util.List;
15 |  | import java.util.Map;
16 |  |
17 |  |
18 |  | public class TooManyFields extends AbstractRule {
19 |  |
20 |  | private static final int DEFAULT_MAXFIELDS = 15;
21 |  |
22 |  | private Map stats;
23 |  | private Map nodes;
24 |  | private int maxFields;
25 |  |
26 | 9 | public Object visit(ASTCompilationUnit node, Object data) {
27 | 9 | maxFields = hasProperty("maxfields") ? getIntProperty("maxfields") : DEFAULT_MAXFIELDS;
28 |  |
29 | 9 | stats = new HashMap(5);
30 | 9 | nodes = new HashMap(5);
31 |  |
32 | 9 | List l = node.findChildrenOfType(ASTFieldDeclaration.class);
33 |  |
34 | 9 | for (Iterator it = l.iterator(); it.hasNext();) {
35 | 105 | ASTFieldDeclaration fd = (ASTFieldDeclaration) it.next();
36 | 105 | if (fd.isFinal() && fd.isStatic()) {
37 | 32 | continue;
38 |  | }
39 | 73 | ASTClassOrInterfaceDeclaration clazz = (ASTClassOrInterfaceDeclaration) fd.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
40 | 73 | if (clazz != null && !clazz.isInterface()) {
41 | 73 | bumpCounterFor(clazz);
42 |  | }
43 |  | }
44 | 9 | for (Iterator it = stats.keySet().iterator(); it.hasNext();) {
45 | 9 | String k = (String) it.next();
46 | 9 | int val = ((Integer) stats.get(k)).intValue();
47 | 9 | SimpleNode n = (SimpleNode) nodes.get(k);
48 | 9 | if (val > maxFields) {
49 | 4 | addViolation(data, n);
50 |  | }
51 |  | }
52 | 9 | return data;
53 |  | }
54 |  |
55 | 73 | private void bumpCounterFor(ASTClassOrInterfaceDeclaration clazz) {
56 | 73 | String key = clazz.getImage();
57 | 73 | if (!stats.containsKey(key)) {
58 | 9 | stats.put(key, new Integer(0));
59 | 9 | nodes.put(key, clazz);
60 |  | }
61 | 73 | Integer i = new Integer(((Integer) stats.get(key)).intValue() + 1);
62 | 73 | stats.put(key, i);
63 |  | }
64 |  |
65 |  | }