01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.strings;
04:
05: import net.sourceforge.pmd.AbstractRule;
06: import net.sourceforge.pmd.ast.ASTName;
07: import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
08: import net.sourceforge.pmd.symboltable.NameOccurrence;
09: import net.sourceforge.pmd.typeresolution.TypeHelper;
10:
11: public class StringToStringRule extends AbstractRule {
12:
13: public Object visit(ASTVariableDeclaratorId node, Object data) {
14: if (!TypeHelper.isA(node.getNameDeclaration(), String.class)) {
15: return data;
16: }
17: boolean isArray = node.isArray();
18: for (NameOccurrence occ : node.getUsages()) {
19: NameOccurrence qualifier = occ
20: .getNameForWhichThisIsAQualifier();
21: if (qualifier != null) {
22: if (!isArray
23: && qualifier.getImage().indexOf("toString") != -1) {
24: addViolation(data, occ.getLocation());
25: } else if (isArray
26: && qualifier.getLocation() != null
27: && !ASTName.class.equals(qualifier
28: .getLocation().getClass())
29: && qualifier.getImage().equals("toString")) {
30: addViolation(data, occ.getLocation());
31: }
32: }
33: }
34: return data;
35: }
36: }
|