KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > naming > SuspiciousHashcodeMethodName


1 package net.sourceforge.pmd.rules.naming;
2
3 import net.sourceforge.pmd.AbstractRule;
4 import net.sourceforge.pmd.ast.ASTMethodDeclaration;
5 import net.sourceforge.pmd.ast.ASTMethodDeclarator;
6 import net.sourceforge.pmd.ast.ASTPrimitiveType;
7 import net.sourceforge.pmd.ast.ASTResultType;
8 import net.sourceforge.pmd.ast.SimpleJavaNode;
9
10 public class SuspiciousHashcodeMethodName extends AbstractRule {
11
12     public Object JavaDoc visit(ASTMethodDeclaration node, Object JavaDoc data) {
13         /* original XPath rule was
14          //MethodDeclaration
15         [ResultType
16         //PrimitiveType
17         [@Image='int']
18         [//MethodDeclarator
19         [@Image='hashcode' or @Image='HashCode' or @Image='Hashcode']
20         [not(FormalParameters/*)]]]
21          */

22
23         ASTResultType type = (ASTResultType) node.getFirstChildOfType(ASTResultType.class);
24         ASTMethodDeclarator decl = (ASTMethodDeclarator) node.getFirstChildOfType(ASTMethodDeclarator.class);
25         String JavaDoc name = decl.getImage();
26         if (name.equalsIgnoreCase("hashcode") && !name.equals("hashCode")
27                 && decl.jjtGetChild(0).jjtGetNumChildren() == 0
28                 && type.jjtGetNumChildren() != 0) {
29             SimpleJavaNode t = (SimpleJavaNode) type.jjtGetChild(0).jjtGetChild(0);
30             if (t instanceof ASTPrimitiveType && "int".equals(t.getImage())) {
31                 addViolation(data, node);
32                 return data;
33             }
34         }
35         return super.visit(node, data);
36     }
37
38 }
39
Popular Tags