Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 47 |  | Methods: | 2
| NCLOC: | 38 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
AvoidFieldNameMatchingMethodName.java | 78.6% | 100% | 100% | 91.2% |

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 | if (!methods.isEmpty()) {
32 | 3 | for (Iterator it = methods.iterator(); it.hasNext();) {
33 | 3 | ASTMethodDeclaration m = (ASTMethodDeclaration) it.next();
34 |  | //Make sure we are comparing fields and methods inside same type
35 | 3 | if (fieldDeclaringType.equals(getDeclaringType(m))) {
36 | 2 | String n = m.getMethodName();
37 | 2 | if (varName.equals(n.toLowerCase())) {
38 | 2 | addViolation(data, node);
39 |  | }
40 |  | }
41 |  | }
42 |  | }
43 |  | }
44 |  | }
45 | 4 | return data;
46 |  | }
47 |  | }