01: package org.acm.seguin.pmd.rules;
02:
03: import org.acm.seguin.pmd.AbstractRule;
04: import org.acm.seguin.pmd.RuleContext;
05: import net.sourceforge.jrefactory.ast.ASTPrimitiveType;
06: import net.sourceforge.jrefactory.ast.ASTVariableDeclaratorId;
07: import net.sourceforge.jrefactory.ast.SimpleNode;
08: import org.acm.seguin.pmd.symboltable.Scope;
09: import org.acm.seguin.pmd.symboltable.NameOccurrence;
10: import org.acm.seguin.pmd.symboltable.VariableNameDeclaration;
11:
12: import java.util.Iterator;
13: import java.util.List;
14: import java.util.Map;
15:
16: public class StringToStringRule extends AbstractRule {
17:
18: public Object visit(ASTVariableDeclaratorId node, Object data) {
19: SimpleNode nameNode = node.getTypeNameNode();
20: if (nameNode instanceof ASTPrimitiveType
21: || !nameNode.getImage().equals("String")) {
22: return data;
23: }
24: // now we know we're at a variable declaration of type String
25: Map decls = ((Scope) node.getScope())
26: .getVariableDeclarations(true);
27: for (Iterator i = decls.keySet().iterator(); i.hasNext();) {
28: VariableNameDeclaration decl = (VariableNameDeclaration) i
29: .next();
30: if (!decl.getImage().equals(node.getImage())) {
31: continue;
32: }
33: List usages = (List) decls.get(decl);
34: for (Iterator j = usages.iterator(); j.hasNext();) {
35: NameOccurrence occ = (NameOccurrence) j.next();
36: if (occ.getNameForWhichThisIsAQualifier() != null
37: && occ.getNameForWhichThisIsAQualifier()
38: .getImage().indexOf("toString") != -1) {
39: RuleContext ctx = (RuleContext) data;
40: ctx.getReport()
41: .addRuleViolation(
42: createRuleViolation(ctx, occ
43: .getBeginLine()));
44: }
45: }
46: }
47: return data;
48: }
49: }
|