UnnecessaryCaseChangeRule.java
01 package net.sourceforge.pmd.lang.java.rule.strings;
02 
03 import net.sourceforge.pmd.lang.java.ast.ASTName;
04 import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
05 import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
06 import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
07 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
08 import net.sourceforge.pmd.lang.ast.Node;
09 
10 public class UnnecessaryCaseChangeRule extends AbstractJavaRule {
11 
12     public Object visit(ASTPrimaryExpression exp, Object data) {
13         int n = exp.jjtGetNumChildren();
14         if (n < 4) {
15             return data;
16         }
17 
18         int first = getBadPrefixOrNull(exp, n);
19         if (first == -1) {
20             return data;
21         }
22 
23         String second = getBadSuffixOrNull(exp, first + 2);
24         if (second == null) {
25             return data;
26         }
27 
28         if (!(exp.jjtGetChild(first + 1instanceof ASTPrimarySuffix)) {
29             return data;
30         }
31         ASTPrimarySuffix methodCall = (ASTPrimarySuffix)exp.jjtGetChild(first + 1);
32         if (!methodCall.isArguments() || methodCall.getArgumentCount() 0) {
33             return data;
34         }
35 
36         addViolation(data, exp);
37         return data;
38     }
39 
40     private int getBadPrefixOrNull(ASTPrimaryExpression exp, int childrenCount) {
41         // verify PrimaryPrefix/Name[ends-with(@Image, 'toUpperCase']
42         for(int i = 0; i < childrenCount - 3; i++) {
43             Node child = exp.jjtGetChild(i);
44             String image;
45             if (child instanceof ASTPrimaryPrefix) {
46                 if (child.jjtGetNumChildren() != || !(child.jjtGetChild(0instanceof ASTName)) {
47                     continue;
48                 }
49         
50                 ASTName name = (ASTNamechild.jjtGetChild(0);
51                 image = name.getImage();
52             else if (child instanceof ASTPrimarySuffix) {
53                 image = ((ASTPrimarySuffixchild).getImage();
54             else {
55                 continue;
56             }
57 
58             if (image == null || !(image.endsWith("toUpperCase"|| image.endsWith("toLowerCase"))) {
59                 continue;
60             else {
61                 return i;
62             }
63         }
64         return -1;
65     }
66 
67     private String getBadSuffixOrNull(ASTPrimaryExpression exp, int equalsPosition) {
68         // verify PrimarySuffix[@Image='equals']
69         if (!(exp.jjtGetChild(equalsPositioninstanceof ASTPrimarySuffix)) {
70             return null;
71         }
72 
73         ASTPrimarySuffix suffix = (ASTPrimarySuffixexp.jjtGetChild(equalsPosition);
74         if (suffix.getImage() == null || !(suffix.hasImageEqualTo("equals"|| suffix.hasImageEqualTo("equalsIgnoreCase"))) {
75             return null;
76         }
77         return suffix.getImage();
78     }
79 
80 }