1 |
| package net.sourceforge.pmd.rules.optimization; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.ast.ASTAssignmentOperator; |
5 |
| import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; |
6 |
| import net.sourceforge.pmd.ast.ASTName; |
7 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
8 |
| import net.sourceforge.pmd.ast.ASTStatementExpression; |
9 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
10 |
| import net.sourceforge.pmd.ast.Node; |
11 |
| import net.sourceforge.pmd.ast.SimpleNode; |
12 |
| import net.sourceforge.pmd.symboltable.NameOccurrence; |
13 |
| |
14 |
| import java.util.Iterator; |
15 |
| |
16 |
| public class UseStringBufferForStringAppends extends AbstractRule { |
17 |
| |
18 |
5
| public Object visit(ASTVariableDeclaratorId node, Object data) {
|
19 |
5
| if (node.getTypeNameNode().jjtGetNumChildren() == 0 || !"String".equals(((SimpleNode) node.getTypeNameNode().jjtGetChild(0)).getImage())) {
|
20 |
1
| return data;
|
21 |
| } |
22 |
4
| Node parent = node.jjtGetParent().jjtGetParent();
|
23 |
4
| if (!parent.getClass().equals(ASTLocalVariableDeclaration.class)) {
|
24 |
0
| return data;
|
25 |
| } |
26 |
4
| for (Iterator iter = node.getUsages().iterator(); iter.hasNext();) {
|
27 |
9
| NameOccurrence no = (NameOccurrence) iter.next();
|
28 |
9
| SimpleNode name = (SimpleNode) no.getLocation();
|
29 |
9
| ASTStatementExpression statement = (ASTStatementExpression) name.getFirstParentOfType(ASTStatementExpression.class);
|
30 |
9
| if (statement == null) {
|
31 |
0
| continue;
|
32 |
| } |
33 |
9
| if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0).getClass().equals(ASTPrimaryExpression.class)) {
|
34 |
| |
35 |
| |
36 |
| |
37 |
9
| ASTName astName = (ASTName) ((SimpleNode) statement.jjtGetChild(0)).getFirstChildOfType(ASTName.class);
|
38 |
9
| if (astName != null && astName.equals(name)) {
|
39 |
8
| ASTAssignmentOperator assignmentOperator = (ASTAssignmentOperator) statement.getFirstChildOfType(ASTAssignmentOperator.class);
|
40 |
8
| if (assignmentOperator != null && assignmentOperator.isCompound()) {
|
41 |
4
| addViolation(data, assignmentOperator);
|
42 |
| } |
43 |
| } |
44 |
| } |
45 |
| } |
46 |
| |
47 |
4
| return data;
|
48 |
| } |
49 |
| } |