1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.ast.ASTPrimitiveType; |
8 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
9 |
| import net.sourceforge.pmd.ast.SimpleNode; |
10 |
| import net.sourceforge.pmd.symboltable.NameOccurrence; |
11 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
12 |
| |
13 |
| import java.util.Iterator; |
14 |
| import java.util.List; |
15 |
| import java.util.Map; |
16 |
| |
17 |
| public class StringToStringRule extends AbstractRule { |
18 |
| |
19 |
7
| public Object visit(ASTVariableDeclaratorId node, Object data) {
|
20 |
7
| SimpleNode nameNode = node.getTypeNameNode();
|
21 |
7
| if (nameNode instanceof ASTPrimitiveType) {
|
22 |
1
| return data;
|
23 |
| } |
24 |
| |
25 |
6
| if (!((SimpleNode)(nameNode.jjtGetChild(0))).getImage().equals("String")) {
|
26 |
1
| return data;
|
27 |
| } |
28 |
| |
29 |
| |
30 |
5
| Map decls = node.getScope().getVariableDeclarations();
|
31 |
5
| for (Iterator i = decls.keySet().iterator(); i.hasNext();) {
|
32 |
6
| VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
|
33 |
6
| if (!decl.getImage().equals(node.getImage())) {
|
34 |
1
| continue;
|
35 |
| } |
36 |
5
| List usages = (List) decls.get(decl);
|
37 |
5
| for (Iterator j = usages.iterator(); j.hasNext();) {
|
38 |
4
| NameOccurrence occ = (NameOccurrence) j.next();
|
39 |
4
| if (occ.getNameForWhichThisIsAQualifier() != null && occ.getNameForWhichThisIsAQualifier().getImage().indexOf("toString") != -1) {
|
40 |
4
| addViolation(data, occ.getLocation());
|
41 |
| } |
42 |
| } |
43 |
| } |
44 |
5
| return data;
|
45 |
| } |
46 |
| } |