Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 71 |  | Methods: | 5
| NCLOC: | 58 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
UnusedImportsRule.java | 80% | 96.2% | 100% | 92.7% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.rules.imports;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
8 |  | import net.sourceforge.pmd.ast.ASTCompilationUnit;
9 |  | import net.sourceforge.pmd.ast.ASTImportDeclaration;
10 |  | import net.sourceforge.pmd.ast.ASTName;
11 |  | import net.sourceforge.pmd.ast.SimpleJavaNode;
12 |  | import net.sourceforge.pmd.ast.SimpleNode;
13 |  | import net.sourceforge.pmd.rules.ImportWrapper;
14 |  |
15 |  | import java.util.HashSet;
16 |  | import java.util.Iterator;
17 |  | import java.util.Set;
18 |  |
19 |  | public class UnusedImportsRule extends AbstractRule {
20 |  |
21 |  | private Set imports = new HashSet();
22 |  |
23 | 9 | public Object visit(ASTCompilationUnit node, Object data) {
24 | 9 | imports.clear();
25 | 9 | super.visit(node, data);
26 | 9 | for (Iterator i = imports.iterator(); i.hasNext();) {
27 | 3 | ImportWrapper wrapper = (ImportWrapper) i.next();
28 | 3 | addViolation(data, wrapper.getNode(), wrapper.getFullName());
29 |  | }
30 | 9 | return data;
31 |  | }
32 |  |
33 | 12 | public Object visit(ASTImportDeclaration node, Object data) {
34 | 12 | if (!node.isImportOnDemand()) {
35 | 12 | ASTName importedType = (ASTName) node.jjtGetChild(0);
36 | 12 | String className;
37 | 12 | if (importedType.getImage().indexOf('.') != -1) {
38 | 12 | int lastDot = importedType.getImage().lastIndexOf('.') + 1;
39 | 12 | className = importedType.getImage().substring(lastDot);
40 |  | } else {
41 | 0 | className = importedType.getImage();
42 |  | }
43 | 12 | imports.add(new ImportWrapper(importedType.getImage(), className, node));
44 |  | }
45 |  |
46 | 12 | return data;
47 |  | }
48 |  |
49 | 7 | public Object visit(ASTClassOrInterfaceType node, Object data) {
50 | 7 | check(node);
51 | 7 | return super.visit(node, data);
52 |  | }
53 |  |
54 | 7 | public Object visit(ASTName node, Object data) {
55 | 7 | check(node);
56 | 7 | return data;
57 |  | }
58 |  |
59 | 14 | private void check(SimpleNode node) {
60 | 14 | String name;
61 | 14 | if (node.getImage().indexOf('.') == -1) {
62 | 12 | name = node.getImage();
63 |  | } else {
64 | 2 | name = node.getImage().substring(0, node.getImage().indexOf('.'));
65 |  | }
66 | 14 | ImportWrapper candidate = new ImportWrapper(node.getImage(), name, new SimpleJavaNode(-1));
67 | 14 | if (imports.contains(candidate)) {
68 | 9 | imports.remove(candidate);
69 |  | }
70 |  | }
71 |  | }