1 |
| package net.sourceforge.pmd.rules; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.ast.ASTAllocationExpression; |
5 |
| import net.sourceforge.pmd.ast.ASTArrayDimsAndInits; |
6 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceType; |
7 |
| import net.sourceforge.pmd.ast.ASTExpression; |
8 |
| import net.sourceforge.pmd.ast.ASTName; |
9 |
| import net.sourceforge.pmd.symboltable.VariableNameDeclaration; |
10 |
| |
11 |
| import java.util.List; |
12 |
| |
13 |
| public class StringInstantiation extends AbstractRule { |
14 |
| |
15 |
9
| public Object visit(ASTAllocationExpression node, Object data) {
|
16 |
9
| if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
|
17 |
3
| return data;
|
18 |
| } |
19 |
| |
20 |
6
| ASTClassOrInterfaceType clz = (ASTClassOrInterfaceType)node.jjtGetChild(0);
|
21 |
6
| if (!clz.getImage().equals("String")) {
|
22 |
0
| return data;
|
23 |
| } |
24 |
| |
25 |
6
| List exp = node.findChildrenOfType(ASTExpression.class);
|
26 |
6
| if (exp.size() >=2 ){
|
27 |
2
| return data;
|
28 |
| } |
29 |
| |
30 |
4
| if (node.getFirstChildOfType(ASTArrayDimsAndInits.class) != null) {
|
31 |
1
| return data;
|
32 |
| } |
33 |
| |
34 |
3
| ASTName name = (ASTName)node.getFirstChildOfType(ASTName.class);
|
35 |
3
| if (name == null) {
|
36 |
2
| addViolation(data, node);
|
37 |
2
| return data;
|
38 |
| } |
39 |
| |
40 |
1
| VariableNameDeclaration nd = (VariableNameDeclaration)name.getNameDeclaration();
|
41 |
| |
42 |
1
| if (nd == null || nd.getTypeImage().equals("String")) {
|
43 |
0
| addViolation(data, node);
|
44 |
| |
45 |
| } |
46 |
1
| return data;
|
47 |
| } |
48 |
| } |