View Javadoc

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