KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > migration > UnnecessaryCast


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