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.List;
12
13 public class AvoidFieldNameMatchingMethodName extends AbstractRule {
14
15 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
16 if (node.isInterface()) {
17 return data;
18 }
19 return super.visit(node, data);
20 }
21
22 public Object visit(ASTFieldDeclaration node, Object data) {
23 String varName = node.getVariableName();
24 String fieldDeclaringType = getDeclaringType(node);
25 if (varName != null) {
26 varName = varName.toLowerCase();
27 ASTClassOrInterfaceDeclaration cl = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
28 if (cl != null) {
29 List<ASTMethodDeclaration> methods = cl.findChildrenOfType(ASTMethodDeclaration.class);
30 for (ASTMethodDeclaration m: methods) {
31
32 if (fieldDeclaringType.equals(getDeclaringType(m))) {
33 String n = m.getMethodName();
34 if (varName.equals(n.toLowerCase())) {
35 addViolation(data, node);
36 }
37 }
38 }
39 }
40 }
41 return data;
42 }
43 }