View Javadoc

1   package net.sourceforge.pmd.rules.strings;
2   
3   import net.sourceforge.pmd.AbstractRule;
4   import net.sourceforge.pmd.ast.ASTCompilationUnit;
5   import net.sourceforge.pmd.ast.ASTLiteral;
6   import net.sourceforge.pmd.ast.ASTName;
7   import net.sourceforge.pmd.ast.SimpleNode;
8   import net.sourceforge.pmd.symboltable.NameDeclaration;
9   import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
10  import net.sourceforge.pmd.typeresolution.TypeHelper;
11  
12  import java.util.HashSet;
13  import java.util.List;
14  import java.util.Set;
15  
16  
17  /**
18   * This rule finds places where StringBuffer.toString() is called just to see if
19   * the string is 0 length by either using .equals("") or toString().length()
20   * <p/>
21   * <pre>
22   * StringBuffer sb = new StringBuffer(&quot;some string&quot;);
23   * if (sb.toString().equals(&quot;&quot;)) {
24   *     // this is wrong
25   * }
26   * if (sb.length() == 0) {
27   *     // this is right
28   * }
29   * </pre>
30   *
31   * @author acaplan
32   */
33  public class UseStringBufferLength extends AbstractRule {
34  
35      // FIXME  Need to remove this somehow.
36      /*
37      Specifically, we need a symbol tree that can be traversed downwards, so that instead
38      of visiting each name and then visiting the declaration for that name, we should visit all
39      the declarations and check their usages.
40      With that in place, this rule would be reduced to:
41      - find all StringBuffer declarations
42      - check each usage
43      - flag those that involve variable.toString()
44      */
45      private Set<VariableNameDeclaration> alreadySeen = new HashSet<VariableNameDeclaration>();
46  
47      public Object visit(ASTCompilationUnit acu, Object data) {
48          alreadySeen.clear();
49          return super.visit(acu, data);
50      }
51  
52      public Object visit(ASTName decl, Object data) {
53          if (!decl.getImage().endsWith("toString")) {
54              return data;
55          }
56          NameDeclaration nd = decl.getNameDeclaration();
57          if (!(nd instanceof VariableNameDeclaration)) {
58              return data;
59          }
60          VariableNameDeclaration vnd = (VariableNameDeclaration) nd;
61          if (alreadySeen.contains(vnd) || !TypeHelper.isA(vnd, StringBuffer.class)) {
62              return data;
63          }
64          alreadySeen.add(vnd);
65  
66          SimpleNode parent = (SimpleNode) decl.jjtGetParent().jjtGetParent();
67          for (int jx = 0; jx < parent.jjtGetNumChildren(); jx++) {
68              SimpleNode achild = (SimpleNode) parent.jjtGetChild(jx);
69              if (isViolation(parent, achild)) {
70                  addViolation(data, decl);
71              }
72          }
73  
74          return data;
75      }
76  
77      /**
78       * Check the given node if it calls either .equals or .length we need to check the target
79       */
80      private boolean isViolation(SimpleNode parent, SimpleNode achild) {
81          if ("equals".equals(achild.getImage())) {
82              List literals = parent.findChildrenOfType(ASTLiteral.class);
83              return (!literals.isEmpty() && "\"\"".equals(((SimpleNode) literals.get(0)).getImage()));
84          } else if ("length".equals(achild.getImage())) {
85              return true;
86          }
87          return false;
88      }
89  
90  
91  }