View Javadoc

    1   package net.sourceforge.pmd.lang.java.rule.migrating;
    2
    3   import java.util.HashSet;
    4   import java.util.List;
    5   import java.util.Set;
    6
    7   import net.sourceforge.pmd.lang.ast.Node;
    8   import net.sourceforge.pmd.lang.java.ast.ASTCastExpression;
    9   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
    10  import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
    11  import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
    12  import net.sourceforge.pmd.lang.java.ast.ASTName;
    13  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
    14  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    15  import net.sourceforge.pmd.lang.java.symboltable.NameOccurrence;
    16
    17  //FUTURE This is not referenced by any RuleSet?
    18  public class UnnecessaryCastRule extends AbstractJavaRule {
    19
    20      private static Set<String> implClassNames = new HashSet<String>();
    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      @Override
    48      public Object visit(ASTLocalVariableDeclaration node, Object data) {
    49          return process(node, data);
    50      }
    51
    52      @Override
    53      public Object visit(ASTFieldDeclaration node, Object data) {
    54          return process(node, data);
    55      }
    56
    57      private Object process(Node node, Object data) {
    58          ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    59          if (cit == null || !implClassNames.contains(cit.getImage())) {
    60              return data;
    61          }
    62          cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    63          if (cit == null) {
    64              return data;
    65          }
    66          ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    67          List<NameOccurrence> usages = decl.getUsages();
    68          for (NameOccurrence no: usages) {
    69              ASTName name = (ASTName) no.getLocation();
    70              Node n = name.jjtGetParent().jjtGetParent().jjtGetParent();
    71              if (n instanceof ASTCastExpression) {
    72                  addViolation(data, n);
    73              }
    74          }
    75          return null;
    76      }
    77  }