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 |
4
| public Object visit(ASTMethodDeclaration node, Object data) {
|
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
4
| ASTResultType type = (ASTResultType) node.getFirstChildOfType(ASTResultType.class);
|
24 |
4
| ASTMethodDeclarator decl = (ASTMethodDeclarator) node.getFirstChildOfType(ASTMethodDeclarator.class);
|
25 |
4
| String name = decl.getImage();
|
26 |
4
| if (name.equalsIgnoreCase("hashcode") && !name.equals("hashCode")
|
27 |
| && decl.jjtGetChild(0).jjtGetNumChildren() == 0 |
28 |
| && type.jjtGetNumChildren() != 0) { |
29 |
3
| SimpleJavaNode t = (SimpleJavaNode) type.jjtGetChild(0).jjtGetChild(0);
|
30 |
3
| if (t instanceof ASTPrimitiveType && "int".equals(t.getImage())) {
|
31 |
3
| addViolation(data, node);
|
32 |
3
| return data;
|
33 |
| } |
34 |
| } |
35 |
1
| return super.visit(node, data);
|
36 |
| } |
37 |
| |
38 |
| } |