View Javadoc

    1   package net.sourceforge.pmd.lang.java.rule;
    2
    3   import java.util.HashMap;
    4   import java.util.Iterator;
    5   import java.util.Map;
    6
    7   import net.sourceforge.pmd.RuleContext;
    8   import net.sourceforge.pmd.lang.dfa.DataFlowNode;
    9   import net.sourceforge.pmd.lang.dfa.VariableAccess;
    10  import net.sourceforge.pmd.lang.dfa.pathfinder.CurrentPath;
    11  import net.sourceforge.pmd.lang.dfa.pathfinder.DAAPathFinder;
    12  import net.sourceforge.pmd.lang.dfa.pathfinder.Executable;
    13  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
    14
    15  //FUTURE This is not referenced by any RuleSet?
    16  public class UselessAssignment extends AbstractJavaRule implements Executable {
    17
    18      private RuleContext rc;
    19
    20      public Object visit(ASTMethodDeclaration node, Object data) {
    21          this.rc = (RuleContext) data;
    22
    23  /*
    24          IDataFlowNode n1 = node.getDataFlowNode();
    25          List f = n1.getFlow();
    26          for (Iterator i = f.iterator(); i.hasNext();) {
    27              DataFlowNode dfan = (DataFlowNode)i.next();
    28              System.out.println(dfan);
    29              List va = dfan.getVariableAccess();
    30              for (Iterator j = va.iterator(); j.hasNext();) {
    31                  VariableAccess o = (VariableAccess)j.next();
    32                  System.out.println(o);
    33              }
    34          }
    35  */
    36
    37          DAAPathFinder a = new DAAPathFinder(node.getDataFlowNode().getFlow().get(0), this);
    38          a.run();
    39
    40          return data;
    41      }
    42
    43      private static class Usage {
    44          public int accessType;
    45          public DataFlowNode node;
    46
    47          public Usage(int accessType, DataFlowNode node) {
    48              this.accessType = accessType;
    49              this.node = node;
    50          }
    51
    52          public String toString() {
    53              return "accessType = " + accessType + ", line = " + node.getLine();
    54          }
    55      }
    56
    57      public void execute(CurrentPath path) {
    58          Map<String, Usage> hash = new HashMap<String, Usage>();
    59          //System.out.println("path size is " + path.size());
    60          for (Iterator<DataFlowNode> i = path.iterator(); i.hasNext();) {
    61              //System.out.println("i = " + i);
    62              DataFlowNode inode = i.next();
    63              if (inode.getVariableAccess() == null) {
    64                  continue;
    65              }
    66              for (int j = 0; j < inode.getVariableAccess().size(); j++) {
    67                  VariableAccess va = inode.getVariableAccess().get(j);
    68                  //System.out.println("inode = " + inode + ", va = " + va);
    69                  Usage u = hash.get(va.getVariableName());
    70                  if (u != null) {
    71                      // At some point investigate and possibly reintroduce this line2 thing
    72                      //int line2 = ((Integer) array.get(1)).intValue();
    73
    74                      // DD - definition followed by another definition
    75                      // FIXME need to check for assignment as well!
    76                      if (va.isDefinition() && va.accessTypeMatches(u.accessType)) {
    77                          //System.out.println(va.getVariableName() + ":" + u);
    78                          addViolation(rc, u.node.getNode(), va.getVariableName());
    79                      }
    80  /*                        // UR - ??
    81                    else if (last == VariableAccess.UNDEFINITION && va.isReference()) {
    82                          //this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getNode(), va.getVariableName(), "UR"));
    83                      }
    84                      // DU - variable is defined and then goes out of scope
    85                      // i.e., unused parameter
    86                      else if (last == VariableAccess.DEFINITION && va.isUndefinition()) {
    87                          if (inode.getNode() != null) {
    88                              this.rc.getReport().addRuleViolation(createRuleViolation(rc, tmp, va.getVariableName(), "DU"));
    89                          }
    90                      }
    91  */
    92                  }
    93                  u = new Usage(va.getAccessType(), inode);
    94                  hash.put(va.getVariableName(), u);
    95              }
    96          }
    97      }
    98  }