Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 55 |  | Methods: | 1
| NCLOC: | 43 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
StringInstantiation.java | 92.9% | 95.2% | 100% | 94.4% |

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 | 12 | public Object visit(ASTAllocationExpression node, Object data) {
18 | 12 | if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
19 | 3 | return data;
20 |  | }
21 |  |
22 | 9 | ASTClassOrInterfaceType clz = (ASTClassOrInterfaceType) node.jjtGetChild(0);
23 | 9 | if (!clz.hasImageEqualTo("String")) {
24 | 1 | return data;
25 |  | }
26 |  |
27 | 8 | List exp = node.findChildrenOfType(ASTExpression.class);
28 | 8 | if (exp.size() >= 2) {
29 | 2 | return data;
30 |  | }
31 |  |
32 | 6 | if (node.getFirstChildOfType(ASTArrayDimsAndInits.class) != null || node.getFirstChildOfType(ASTAdditiveExpression.class) != null) {
33 | 1 | return data;
34 |  | }
35 |  |
36 | 5 | ASTName name = (ASTName) node.getFirstChildOfType(ASTName.class);
37 |  | // Literal, i.e., new String("foo")
38 | 5 | if (name == null) {
39 | 3 | addViolation(data, node);
40 | 3 | return data;
41 |  | }
42 |  |
43 | 2 | NameDeclaration nd = name.getNameDeclaration();
44 | 2 | if (!(nd instanceof VariableNameDeclaration)) {
45 | 0 | return data;
46 |  | }
47 |  |
48 | 2 | VariableNameDeclaration vnd = (VariableNameDeclaration) nd;
49 |  | // nd == null in cases like: return new String(str);
50 | 2 | if (vnd == null || vnd.getTypeImage().equals("String")) {
51 | 1 | addViolation(data, node);
52 |  | }
53 | 2 | return data;
54 |  | }
55 |  | }