View Javadoc
1 package net.sourceforge.pmd.rules; 2 3 import net.sourceforge.pmd.AbstractRule; 4 import net.sourceforge.pmd.RuleContext; 5 import net.sourceforge.pmd.ast.ASTPrimitiveType; 6 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; 7 import net.sourceforge.pmd.ast.SimpleNode; 8 import net.sourceforge.pmd.symboltable.NameOccurrence; 9 import net.sourceforge.pmd.symboltable.VariableNameDeclaration; 10 11 import java.util.Iterator; 12 import java.util.List; 13 import java.util.Map; 14 15 public class StringToStringRule extends AbstractRule { 16 17 public Object visit(ASTVariableDeclaratorId node, Object data) { 18 SimpleNode nameNode = node.getTypeNameNode(); 19 if (nameNode instanceof ASTPrimitiveType || !nameNode.getImage().equals("String")) { 20 return data; 21 } 22 // now we know we're at a variable declaration of type String 23 Map decls = node.getScope().getVariableDeclarations(true); 24 for (Iterator i = decls.keySet().iterator(); i.hasNext();) { 25 VariableNameDeclaration decl = (VariableNameDeclaration) i.next(); 26 if (!decl.getImage().equals(node.getImage())) { 27 continue; 28 } 29 List usages = (List) decls.get(decl); 30 for (Iterator j = usages.iterator(); j.hasNext();) { 31 NameOccurrence occ = (NameOccurrence) j.next(); 32 if (occ.getNameForWhichThisIsAQualifier() != null && occ.getNameForWhichThisIsAQualifier().getImage().indexOf("toString") != -1) { 33 RuleContext ctx = (RuleContext) data; 34 ctx.getReport().addRuleViolation(createRuleViolation(ctx, occ.getBeginLine())); 35 } 36 } 37 } 38 return data; 39 } 40 }

This page was automatically generated by Maven