View Javadoc

1   package net.sourceforge.pmd.lang.java.rule.strings;
2   
3   import net.sourceforge.pmd.lang.java.ast.ASTName;
4   import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
5   import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
6   import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
7   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
8   import net.sourceforge.pmd.lang.ast.Node;
9   
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 + 1) instanceof 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() != 1 || !(child.jjtGetChild(0) instanceof ASTName)) {
47                      continue;
48                  }
49          
50                  ASTName name = (ASTName) child.jjtGetChild(0);
51                  image = name.getImage();
52              } else if (child instanceof ASTPrimarySuffix) {
53                  image = ((ASTPrimarySuffix) child).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(equalsPosition) instanceof ASTPrimarySuffix)) {
70              return null;
71          }
72  
73          ASTPrimarySuffix suffix = (ASTPrimarySuffix) exp.jjtGetChild(equalsPosition);
74          if (suffix.getImage() == null || !(suffix.hasImageEqualTo("equals") || suffix.hasImageEqualTo("equalsIgnoreCase"))) {
75              return null;
76          }
77          return suffix.getImage();
78      }
79  
80  }