Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT 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 | 29 | public Object visit(ASTLocalVariableDeclaration decl, Object data) {
18 | 29 | for (int i = 0; i < decl.jjtGetNumChildren(); i++) {
19 | 59 | if (!(decl.jjtGetChild(i) instanceof ASTVariableDeclarator)) {
20 | 29 | continue;
21 |  | }
22 | 30 | 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 | 30 | if (!node.getNameDeclaration().isArray() && !actuallyUsed(node.getUsages())) {
27 | 16 | addViolation(data, node, node.getNameDeclaration().getImage());
28 |  | }
29 |  | }
30 | 29 | return data;
31 |  | }
32 |  |
33 | 29 | private boolean actuallyUsed(List usages) {
34 | 29 | 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 | 16 | return false;
43 |  | }
44 |  |
45 |  | }