Clover coverage report - PMD - 3.7
Coverage timestamp: Wed May 31 2006 09:25:59 EDT

file stats: | LOC: | 44 |  | Methods: | 1
| NCLOC: | 39 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

UseIndexOfChar.java

78.6%

93.8%

100%

87.1%

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.ASTLiteral;
6 |  | import net.sourceforge.pmd.ast.ASTPrimaryExpression;
7 |  | import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
8 |  | import net.sourceforge.pmd.ast.SimpleNode;
9 |  | import net.sourceforge.pmd.symboltable.NameOccurrence;
10 |  |
11 |  | import java.util.Iterator;
12 |  | import java.util.List;
13 |  |
14 |  | public class UseIndexOfChar extends AbstractRule {
15 | 6 | public Object visit(ASTVariableDeclaratorId node, Object data) {
16 | 6 | if (!node.getNameDeclaration().getTypeImage().equals("String")) {
17 | 0 | return data;
18 |  | }
19 | 6 | for (Iterator i = node.getUsages().iterator(); i.hasNext();) {
20 | 6 | NameOccurrence occ = (NameOccurrence) i.next();
21 | 6 | if (occ.getNameForWhichThisIsAQualifier() != null &&
22 |  | (occ.getNameForWhichThisIsAQualifier().getImage().indexOf("indexOf") != -1 ||
23 |  | occ.getNameForWhichThisIsAQualifier().getImage().indexOf("lastIndexOf") != -1)) {
24 | 6 | SimpleNode parent = (SimpleNode)occ.getLocation().jjtGetParent().jjtGetParent();
25 | 6 | if (parent instanceof ASTPrimaryExpression) {
26 |  | // bail out if it's something like indexOf("a" + "b")
27 | 6 | List additives = parent.findChildrenOfType(ASTAdditiveExpression.class);
28 | 6 | if (!additives.isEmpty()) {
29 | 1 | return data;
30 |  | }
31 | 5 | List literals = parent.findChildrenOfType(ASTLiteral.class);
32 | 5 | for (Iterator j = literals.iterator(); j.hasNext();) {
33 | 7 | ASTLiteral literal = (ASTLiteral)j.next();
34 | 7 | if (literal.getImage().length() == 3 && literal.getImage().charAt(0) == '\"') {
35 | 3 | addViolation(data, occ.getLocation());
36 |  | }
37 |  | }
38 |  | }
39 |  | }
40 |  | }
41 | 5 | return data;
42 |  | }
43 |  | }
44 |  |