Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 45 |  | Methods: | 2
| NCLOC: | 33 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
UnusedLocalVariableRule.java | 100% | 100% | 100% | 100% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.rules;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
8 |  | import net.sourceforge.pmd.ast.ASTVariableDeclarator;
9 |  | import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
10 |  | import net.sourceforge.pmd.symboltable.NameOccurrence;
11 |  |
12 |  | import java.util.Iterator;
13 |  | import java.util.List;
14 |  |
15 |  | public class UnusedLocalVariableRule extends AbstractRule {
16 |  |
17 | 30 | public Object visit(ASTLocalVariableDeclaration decl, Object data) {
18 | 30 | for (int i = 0; i < decl.jjtGetNumChildren(); i++) {
19 | 61 | if (!(decl.jjtGetChild(i) instanceof ASTVariableDeclarator)) {
20 | 30 | continue;
21 |  | }
22 | 31 | ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) decl.jjtGetChild(i).jjtGetChild(0);
23 |  | // TODO this isArray() check misses some cases
24 |  | // need to add DFAish code to determine if an array
25 |  | // is initialized locally or gotten from somewhere else
26 | 31 | if (!node.getNameDeclaration().isArray() && !actuallyUsed(node.getUsages())) {
27 | 17 | addViolation(data, node, node.getNameDeclaration().getImage());
28 |  | }
29 |  | }
30 | 30 | return data;
31 |  | }
32 |  |
33 | 30 | private boolean actuallyUsed(List usages) {
34 | 30 | for (Iterator j = usages.iterator(); j.hasNext();) {
35 | 15 | NameOccurrence occ = (NameOccurrence) j.next();
36 | 15 | if (occ.isOnLeftHandSide()) {
37 | 2 | continue;
38 |  | } else {
39 | 13 | return true;
40 |  | }
41 |  | }
42 | 17 | return false;
43 |  | }
44 |  |
45 |  | }