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