View Javadoc

    1   package net.sourceforge.pmd.dfa.pathfinder;
    2
    3   import net.sourceforge.pmd.dfa.IDataFlowNode;
    4   import net.sourceforge.pmd.dfa.NodeType;
    5
    6   import java.util.Iterator;
    7   import java.util.LinkedList;
    8
    9   public class CurrentPath {
    10
    11      private LinkedList list;
    12
    13      public CurrentPath() {
    14          list = new LinkedList();
    15      }
    16
    17      public int getLength() {
    18          return list.size();
    19      }
    20
    21      public Iterator iterator() {
    22          return list.iterator();
    23      }
    24
    25      public IDataFlowNode getLast() {
    26          return (IDataFlowNode) list.getLast();
    27      }
    28
    29      public void removeLast() {
    30          list.removeLast();
    31      }
    32
    33      public boolean isEmpty() {
    34          return list.isEmpty();
    35      }
    36
    37      public void addLast(IDataFlowNode n) {
    38          list.addLast(n);
    39          //System.out.println("adding: " + n);
    40      }
    41
    42      public boolean isDoBranchNode() {
    43          return ((IDataFlowNode) list.getLast()).isType(NodeType.DO_EXPR);
    44      }
    45
    46      public boolean isFirstDoStatement() {
    47          return isFirstDoStatement((IDataFlowNode) list.getLast());
    48      }
    49
    50      public IDataFlowNode getDoBranchNodeFromFirstDoStatement() {
    51          IDataFlowNode inode = (IDataFlowNode) list.getLast();
    52          if (!isFirstDoStatement()) return null;
    53          for (int i = 0; i < inode.getParents().size(); i++) {
    54              IDataFlowNode parent = (IDataFlowNode) inode.getParents().get(i);
    55              if (parent.isType(NodeType.DO_EXPR)) {
    56                  return parent;
    57              }
    58          }
    59          return null;
    60      }
    61
    62      public boolean isEndNode() {
    63          return ((IDataFlowNode) list.getLast()).getChildren().size() == 0;
    64          //return inode instanceof StartOrEndDataFlowNode;
    65      }
    66
    67      public boolean isBranch() {
    68          return ((IDataFlowNode) list.getLast()).getChildren().size() > 1;
    69      }
    70
    71      private boolean isFirstDoStatement(IDataFlowNode inode) {
    72          int index = inode.getIndex() - 1;
    73          if (index < 0) return false;
    74          return ((IDataFlowNode) inode.getFlow().get(index)).isType(NodeType.DO_BEFORE_FIRST_STATEMENT);
    75      }
    76  }
    77