Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 56 |  | Methods: | 1
| NCLOC: | 43 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
StringInstantiation.java | 78.6% | 85.7% | 100% | 83.3% |

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