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 11 import java.util.HashSet ; 12 import java.util.List ; 13 import java.util.Set ; 14 15 16 32 public class UseStringBufferLength extends AbstractRule { 33 34 44 private Set alreadySeen = new HashSet (); 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 (!vnd.getTypeImage().equals("StringBuffer") || alreadySeen.contains(vnd)) { 61 return data; 62 } 63 alreadySeen.add(vnd); 64 65 SimpleNode parent = (SimpleNode) decl.jjtGetParent().jjtGetParent(); 66 for (int jx = 0; jx < parent.jjtGetNumChildren(); jx++) { 67 SimpleNode achild = (SimpleNode) parent.jjtGetChild(jx); 68 if (isViolation(parent, achild)) { 69 addViolation(data, decl); 70 } 71 } 72 73 return data; 74 } 75 76 79 private boolean isViolation(SimpleNode parent, SimpleNode achild) { 80 if ("equals".equals(achild.getImage())) { 81 List literals = parent.findChildrenOfType(ASTLiteral.class); 82 return (!literals.isEmpty() && "\"\"".equals(((SimpleNode) literals.get(0)).getImage())); 83 } else if ("length".equals(achild.getImage())) { 84 return true; 85 } 86 return false; 87 } 88 89 90 } 91 | Popular Tags |