AvoidFieldNameMatchingTypeNameRule.java
01 /**
02  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03  */
04 package net.sourceforge.pmd.lang.java.rule.naming;
05 
06 import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
07 import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
08 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
09 
10 public class AvoidFieldNameMatchingTypeNameRule extends AbstractJavaRule {
11 
12     public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
13         if (node.isInterface()) {
14             return data;
15         }
16         return super.visit(node, data);
17     }
18 
19     public Object visit(ASTFieldDeclaration node, Object data) {
20         ASTClassOrInterfaceDeclaration cl = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
21         if (cl != null && node.getVariableName().equalsIgnoreCase(cl.getImage())) {
22             addViolation(data, node);
23         }
24         return data;
25     }
26 }