Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 77 |  | Methods: | 3
| NCLOC: | 69 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
UnnecessaryCast.java | 0% | 0% | 0% | 0% |

1 |  | package net.sourceforge.pmd.rules.migration;
2 |  |
3 |  | import net.sourceforge.pmd.AbstractRule;
4 |  | import net.sourceforge.pmd.ast.ASTCastExpression;
5 |  | import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
6 |  | import net.sourceforge.pmd.ast.ASTFieldDeclaration;
7 |  | import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
8 |  | import net.sourceforge.pmd.ast.ASTName;
9 |  | import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
10 |  | import net.sourceforge.pmd.ast.SimpleNode;
11 |  | import net.sourceforge.pmd.symboltable.NameOccurrence;
12 |  |
13 |  | import java.util.HashSet;
14 |  | import java.util.Iterator;
15 |  | import java.util.List;
16 |  | import java.util.Set;
17 |  |
18 |  | public class UnnecessaryCast extends AbstractRule {
19 |  |
20 |  | private static Set implClassNames = new HashSet();
21 |  |
22 |  | static {
23 | 0 | implClassNames.add("List");
24 | 0 | implClassNames.add("Set");
25 | 0 | implClassNames.add("Map");
26 | 0 | implClassNames.add("java.util.List");
27 | 0 | implClassNames.add("java.util.Set");
28 | 0 | implClassNames.add("java.util.Map");
29 | 0 | implClassNames.add("ArrayList");
30 | 0 | implClassNames.add("HashSet");
31 | 0 | implClassNames.add("HashMap");
32 | 0 | implClassNames.add("LinkedHashMap");
33 | 0 | implClassNames.add("LinkedHashSet");
34 | 0 | implClassNames.add("TreeSet");
35 | 0 | implClassNames.add("TreeMap");
36 | 0 | implClassNames.add("Vector");
37 | 0 | implClassNames.add("java.util.ArrayList");
38 | 0 | implClassNames.add("java.util.HashSet");
39 | 0 | implClassNames.add("java.util.HashMap");
40 | 0 | implClassNames.add("java.util.LinkedHashMap");
41 | 0 | implClassNames.add("java.util.LinkedHashSet");
42 | 0 | implClassNames.add("java.util.TreeSet");
43 | 0 | implClassNames.add("java.util.TreeMap");
44 | 0 | implClassNames.add("java.util.Vector");
45 |  | }
46 |  |
47 | 0 | public Object visit(ASTLocalVariableDeclaration node, Object data) {
48 | 0 | return process(node, data);
49 |  | }
50 |  |
51 | 0 | public Object visit(ASTFieldDeclaration node, Object data) {
52 | 0 | return process(node, data);
53 |  | }
54 |  |
55 | 0 | private Object process(SimpleNode node, Object data) {
56 | 0 | ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.getFirstChildOfType(ASTClassOrInterfaceType.class);
57 | 0 | if (cit == null || !implClassNames.contains(cit.getImage())) {
58 | 0 | return data;
59 |  | }
60 | 0 | cit = (ASTClassOrInterfaceType) cit.getFirstChildOfType(ASTClassOrInterfaceType.class);
61 | 0 | if (cit == null) {
62 | 0 | return data;
63 |  | }
64 | 0 | ASTVariableDeclaratorId decl = (ASTVariableDeclaratorId) node
65 |  | .getFirstChildOfType(ASTVariableDeclaratorId.class);
66 | 0 | List usages = decl.getUsages();
67 | 0 | for (Iterator iter = usages.iterator(); iter.hasNext();) {
68 | 0 | NameOccurrence no = (NameOccurrence) iter.next();
69 | 0 | ASTName name = (ASTName) no.getLocation();
70 | 0 | SimpleNode n = (SimpleNode) name.jjtGetParent().jjtGetParent().jjtGetParent();
71 | 0 | if (ASTCastExpression.class.equals(n.getClass())) {
72 | 0 | addViolation(data, n);
73 |  | }
74 |  | }
75 | 0 | return null;
76 |  | }
77 |  | }