View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.naming;
5   
6   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
7   import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
8   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
9   
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  }