1 |
| package net.sourceforge.pmd.rules.design; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.ast.ASTLiteral; |
5 |
| import net.sourceforge.pmd.ast.ASTName; |
6 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
7 |
| import net.sourceforge.pmd.ast.ASTPrimaryPrefix; |
8 |
| import net.sourceforge.pmd.ast.ASTPrimarySuffix; |
9 |
| |
10 |
| import java.util.List; |
11 |
| |
12 |
| public class PositionLiteralsFirstInComparisons extends AbstractRule { |
13 |
| |
14 |
2
| public Object visit(ASTPrimaryExpression exp, Object data) {
|
15 |
| |
16 |
2
| if (exp.jjtGetNumChildren() < 2 || !(exp.jjtGetChild(0) instanceof ASTPrimaryPrefix)) {
|
17 |
0
| return data;
|
18 |
| } |
19 |
2
| ASTPrimaryPrefix prefix = (ASTPrimaryPrefix)exp.jjtGetChild(0);
|
20 |
2
| if (prefix.jjtGetNumChildren() != 1 || !(prefix.jjtGetChild(0) instanceof ASTName)) {
|
21 |
1
| return data;
|
22 |
| } |
23 |
1
| ASTName name = (ASTName)prefix.jjtGetChild(0);
|
24 |
1
| if (name.getImage() == null || !name.getImage().endsWith(".equals")) {
|
25 |
0
| return data;
|
26 |
| } |
27 |
| |
28 |
| |
29 |
1
| if (!(exp.jjtGetChild(1) instanceof ASTPrimarySuffix)) {
|
30 |
0
| return data;
|
31 |
| } |
32 |
1
| ASTPrimarySuffix suffix = (ASTPrimarySuffix)exp.jjtGetChild(1);
|
33 |
1
| List literals = suffix.findChildrenOfType(ASTLiteral.class);
|
34 |
1
| if (literals.isEmpty()) {
|
35 |
0
| return data;
|
36 |
| } |
37 |
| |
38 |
1
| addViolation(data, exp);
|
39 |
| |
40 |
1
| return data;
|
41 |
| } |
42 |
| } |