Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 45 |  | Methods: | 2
| NCLOC: | 36 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
AvoidFieldNameMatchingMethodName.java | 75% | 100% | 100% | 90.3% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.rules;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
8 |  | import net.sourceforge.pmd.ast.ASTFieldDeclaration;
9 |  | import net.sourceforge.pmd.ast.ASTMethodDeclaration;
10 |  |
11 |  | import java.util.Iterator;
12 |  | import java.util.List;
13 |  |
14 |  | public class AvoidFieldNameMatchingMethodName extends AbstractRule {
15 |  |
16 | 8 | public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
17 | 8 | if (node.isInterface()) {
18 | 1 | return data;
19 |  | }
20 | 7 | return super.visit(node, data);
21 |  | }
22 |  |
23 | 4 | public Object visit(ASTFieldDeclaration node, Object data) {
24 | 4 | String varName = node.getVariableName();
25 | 4 | String fieldDeclaringType = getDeclaringType(node);
26 | 4 | if (varName != null) {
27 | 4 | varName = varName.toLowerCase();
28 | 4 | ASTClassOrInterfaceDeclaration cl = (ASTClassOrInterfaceDeclaration) node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
29 | 4 | if (cl != null) {
30 | 4 | List methods = cl.findChildrenOfType(ASTMethodDeclaration.class);
31 | 4 | for (Iterator it = methods.iterator(); it.hasNext();) {
32 | 3 | ASTMethodDeclaration m = (ASTMethodDeclaration) it.next();
33 |  | //Make sure we are comparing fields and methods inside same type
34 | 3 | if (fieldDeclaringType.equals(getDeclaringType(m))) {
35 | 2 | String n = m.getMethodName();
36 | 2 | if (varName.equals(n.toLowerCase())) {
37 | 2 | addViolation(data, node);
38 |  | }
39 |  | }
40 |  | }
41 |  | }
42 |  | }
43 | 4 | return data;
44 |  | }
45 |  | }