01: package net.sourceforge.pmd.rules.naming;
02:
03: import net.sourceforge.pmd.AbstractRule;
04: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
05: import net.sourceforge.pmd.ast.ASTMethodDeclarator;
06: import net.sourceforge.pmd.ast.ASTPrimitiveType;
07: import net.sourceforge.pmd.ast.ASTResultType;
08: import net.sourceforge.pmd.ast.SimpleJavaNode;
09:
10: public class SuspiciousHashcodeMethodName extends AbstractRule {
11:
12: public Object visit(ASTMethodDeclaration node, Object 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 = node
24: .getFirstChildOfType(ASTResultType.class);
25: ASTMethodDeclarator decl = node
26: .getFirstChildOfType(ASTMethodDeclarator.class);
27: String name = decl.getImage();
28: if (name.equalsIgnoreCase("hashcode")
29: && !name.equals("hashCode")
30: && decl.jjtGetChild(0).jjtGetNumChildren() == 0
31: && type.jjtGetNumChildren() != 0) {
32: SimpleJavaNode t = (SimpleJavaNode) type.jjtGetChild(0)
33: .jjtGetChild(0);
34: if (t instanceof ASTPrimitiveType
35: && "int".equals(t.getImage())) {
36: addViolation(data, node);
37: return data;
38: }
39: }
40: return super.visit(node, data);
41: }
42:
43: }
|