View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.rules.strings;
    5
    6   import net.sourceforge.pmd.AbstractRule;
    7   import net.sourceforge.pmd.ast.ASTBlockStatement;
    8   import net.sourceforge.pmd.ast.ASTLiteral;
    9
    10  /**
    11   * This rule finds the following:
    12   * <p/>
    13   * <pre>
    14   *         StringBuffer.append(&quot;c&quot;); // appends a
    15   *         single character
    16   * </pre>
    17   * <p/>
    18   * It is preferable to use StringBuffer.append('c'); // appends a single
    19   * character Implementation of PMD RFE 1373863
    20   */
    21  public class AppendCharacterWithChar extends AbstractRule {
    22
    23      public Object visit(ASTLiteral node, Object data) {
    24          ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    25          if (bs == null) {
    26              return data;
    27          }
    28
    29          if (node.isSingleCharacterStringLiteral()) {
    30              if (!InefficientStringBuffering.isInStringBufferOperation(node, 8, "append")) {
    31                  return data;
    32              }
    33              addViolation(data, node);
    34          }
    35          return data;
    36      }
    37  }