View Javadoc

    1   package net.sourceforge.pmd.rules.strings;
    2
    3   import net.sourceforge.pmd.AbstractRule;
    4   import net.sourceforge.pmd.ast.ASTName;
    5   import net.sourceforge.pmd.ast.ASTPrimaryExpression;
    6   import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
    7   import net.sourceforge.pmd.ast.ASTPrimarySuffix;
    8   import net.sourceforge.pmd.ast.Node;
    9
    10  public class UnnecessaryCaseChange extends AbstractRule {
    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              }
    61              return i;
    62          }
    63          return -1;
    64      }
    65
    66      private String getBadSuffixOrNull(ASTPrimaryExpression exp, int equalsPosition) {
    67          // verify PrimarySuffix[@Image='equals']
    68          if (!(exp.jjtGetChild(equalsPosition) instanceof ASTPrimarySuffix)) {
    69              return null;
    70          }
    71
    72          ASTPrimarySuffix suffix = (ASTPrimarySuffix) exp.jjtGetChild(equalsPosition);
    73          if (suffix.getImage() == null || !(suffix.hasImageEqualTo("equals") || suffix.hasImageEqualTo("equalsIgnoreCase"))) {
    74              return null;
    75          }
    76          return suffix.getImage();
    77      }
    78
    79  }