View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.rules;
    5
    6   import net.sourceforge.pmd.AbstractRule;
    7   import net.sourceforge.pmd.Rule;
    8   import net.sourceforge.pmd.ast.ASTAllocationExpression;
    9   import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
    10  import net.sourceforge.pmd.ast.ASTPrimaryExpression;
    11  import net.sourceforge.pmd.ast.ASTPrimarySuffix;
    12  import net.sourceforge.pmd.ast.SimpleNode;
    13
    14  import java.util.HashSet;
    15  import java.util.Set;
    16
    17  public class UnnecessaryConversionTemporary extends AbstractRule implements Rule {
    18
    19      private boolean inPrimaryExpressionContext;
    20      private boolean usingPrimitiveWrapperAllocation;
    21      private Set primitiveWrappers = new HashSet();
    22
    23      public UnnecessaryConversionTemporary() {
    24          primitiveWrappers.add("Integer");
    25          primitiveWrappers.add("Boolean");
    26          primitiveWrappers.add("Double");
    27          primitiveWrappers.add("Long");
    28          primitiveWrappers.add("Short");
    29          primitiveWrappers.add("Byte");
    30          primitiveWrappers.add("Float");
    31      }
    32
    33      public Object visit(ASTPrimaryExpression node, Object data) {
    34          if (node.jjtGetNumChildren() == 0 || (node.jjtGetChild(0)).jjtGetNumChildren() == 0 || !(node.jjtGetChild(0).jjtGetChild(0) instanceof ASTAllocationExpression)) {
    35              return super.visit(node, data);
    36          }
    37          // TODO... hmmm... is this inPrimaryExpressionContext gibberish necessary?
    38          inPrimaryExpressionContext = true;
    39          super.visit(node, data);
    40          inPrimaryExpressionContext = false;
    41          usingPrimitiveWrapperAllocation = false;
    42          return data;
    43      }
    44
    45      public Object visit(ASTAllocationExpression node, Object data) {
    46          if (!inPrimaryExpressionContext || !(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
    47              return super.visit(node, data);
    48          }
    49          if (!primitiveWrappers.contains(((SimpleNode) node.jjtGetChild(0)).getImage())) {
    50              return super.visit(node, data);
    51          }
    52          usingPrimitiveWrapperAllocation = true;
    53          return super.visit(node, data);
    54      }
    55
    56      public Object visit(ASTPrimarySuffix node, Object data) {
    57          if (!inPrimaryExpressionContext || !usingPrimitiveWrapperAllocation) {
    58              return super.visit(node, data);
    59          }
    60          if (node.getImage() != null && node.getImage().equals("toString")) {
    61              addViolation(data, node);
    62          }
    63          return super.visit(node, data);
    64      }
    65
    66  }