Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 63 |  | Methods: | 2
| NCLOC: | 47 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
DuplicateImportsRule.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.imports;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.ast.ASTCompilationUnit;
8 |  | import net.sourceforge.pmd.ast.ASTImportDeclaration;
9 |  | import net.sourceforge.pmd.rules.ImportWrapper;
10 |  |
11 |  | import java.util.HashSet;
12 |  | import java.util.Iterator;
13 |  | import java.util.Set;
14 |  |
15 |  | public class DuplicateImportsRule extends AbstractRule {
16 |  |
17 |  | private Set singleTypeImports;
18 |  | private Set importOnDemandImports;
19 |  |
20 | 4 | public Object visit(ASTCompilationUnit node, Object data) {
21 | 4 | singleTypeImports = new HashSet();
22 | 4 | importOnDemandImports = new HashSet();
23 | 4 | super.visit(node, data);
24 |  |
25 |  | // this checks for things like:
26 |  | // import java.io.*;
27 |  | // import java.io.File;
28 | 4 | for (Iterator i = importOnDemandImports.iterator(); i.hasNext();) {
29 | 7 | ImportWrapper thisImportOnDemand = (ImportWrapper) i.next();
30 | 7 | for (Iterator j = singleTypeImports.iterator(); j.hasNext();) {
31 | 4 | ImportWrapper thisSingleTypeImport = (ImportWrapper) j.next();
32 | 4 | String singleTypePkg = thisSingleTypeImport.getName().substring(0, thisSingleTypeImport.getName().lastIndexOf("."));
33 | 4 | if (thisImportOnDemand.getName().equals(singleTypePkg)) {
34 | 1 | addViolation(data, thisSingleTypeImport.getNode(), thisSingleTypeImport.getName());
35 |  | }
36 |  | }
37 |  | }
38 | 4 | singleTypeImports.clear();
39 | 4 | importOnDemandImports.clear();
40 | 4 | return data;
41 |  | }
42 |  |
43 | 11 | public Object visit(ASTImportDeclaration node, Object data) {
44 | 11 | ImportWrapper wrapper = new ImportWrapper(node.getImportedName(), node.getImportedName(), node.getImportedNameNode());
45 |  |
46 |  | // blahhhh... this really wants to be ASTImportDeclaration to be polymorphic...
47 | 11 | if (node.isImportOnDemand()) {
48 | 8 | if (importOnDemandImports.contains(wrapper)) {
49 | 1 | addViolation(data, node.getImportedNameNode(), node.getImportedNameNode().getImage());
50 |  | } else {
51 | 7 | importOnDemandImports.add(wrapper);
52 |  | }
53 |  | } else {
54 | 3 | if (singleTypeImports.contains(wrapper)) {
55 | 1 | addViolation(data, node.getImportedNameNode(), node.getImportedNameNode().getImage());
56 |  | } else {
57 | 2 | singleTypeImports.add(wrapper);
58 |  | }
59 |  | }
60 | 11 | return data;
61 |  | }
62 |  |
63 |  | }