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("some string");
23 * if (sb.toString().equals("")) {
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
36
37
38
39
40
41
42
43
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 }