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 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class UseStringBufferLength extends AbstractRule { |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| private Set alreadySeen = new HashSet(); |
45 |
| |
46 |
5
| public Object visit(ASTCompilationUnit acu, Object data) {
|
47 |
5
| alreadySeen.clear();
|
48 |
5
| return super.visit(acu, data);
|
49 |
| } |
50 |
| |
51 |
14
| public Object visit(ASTName decl, Object data) {
|
52 |
14
| if (!decl.getImage().endsWith("toString")) {
|
53 |
10
| return data;
|
54 |
| } |
55 |
4
| NameDeclaration nd = decl.getNameDeclaration();
|
56 |
4
| if (!(nd instanceof VariableNameDeclaration)) {
|
57 |
0
| return data;
|
58 |
| } |
59 |
4
| VariableNameDeclaration vnd = (VariableNameDeclaration) nd;
|
60 |
4
| if (!vnd.getTypeImage().equals("StringBuffer") || alreadySeen.contains(vnd)) {
|
61 |
0
| return data;
|
62 |
| } |
63 |
4
| alreadySeen.add(vnd);
|
64 |
| |
65 |
4
| SimpleNode parent = (SimpleNode) decl.jjtGetParent().jjtGetParent();
|
66 |
4
| for (int jx = 0; jx < parent.jjtGetNumChildren(); jx++) {
|
67 |
16
| SimpleNode achild = (SimpleNode) parent.jjtGetChild(jx);
|
68 |
16
| if (isViolation(parent, achild)) {
|
69 |
2
| addViolation(data, decl);
|
70 |
| } |
71 |
| } |
72 |
| |
73 |
4
| return data;
|
74 |
| } |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
16
| private boolean isViolation(SimpleNode parent, SimpleNode achild) {
|
80 |
16
| if ("equals".equals(achild.getImage())) {
|
81 |
3
| List literals = parent.findChildrenOfType(ASTLiteral.class);
|
82 |
3
| return (!literals.isEmpty() && "\"\"".equals(((SimpleNode) literals.get(0)).getImage()));
|
83 |
13
| } else if ("length".equals(achild.getImage())) {
|
84 |
1
| return true;
|
85 |
| } |
86 |
12
| return false;
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| } |