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 Iterator iterator() {
18          return list.iterator();
19      }
20  
21      public IDataFlowNode getLast() {
22          return (IDataFlowNode) list.getLast();
23      }
24  
25      public void removeLast() {
26          list.removeLast();
27      }
28  
29      public boolean isEmpty() {
30          return list.isEmpty();
31      }
32  
33      public void addLast(IDataFlowNode n) {
34          list.addLast(n);
35      }
36  
37      public boolean isDoBranchNode() {
38          return ((IDataFlowNode) list.getLast()).isType(NodeType.DO_EXPR);
39      }
40  
41      public boolean isFirstDoStatement() {
42          return isFirstDoStatement((IDataFlowNode) list.getLast());
43      }
44  
45      public IDataFlowNode getDoBranchNodeFromFirstDoStatement() {
46          IDataFlowNode inode = (IDataFlowNode) list.getLast();
47          if (!isFirstDoStatement()) return null;
48          for (int i = 0; i < inode.getParents().size(); i++) {
49              IDataFlowNode parent = (IDataFlowNode) inode.getParents().get(i);
50              if (parent.isType(NodeType.DO_EXPR)) {
51                  return parent;
52              }
53          }
54          return null;
55      }
56  
57      public boolean isEndNode() {
58          return ((IDataFlowNode) list.getLast()).getChildren().size() == 0;
59          //return inode instanceof StartOrEndDataFlowNode;
60      }
61  
62      public boolean isBranch() {
63          return ((IDataFlowNode) list.getLast()).getChildren().size() > 1;
64      }
65  
66      private boolean isFirstDoStatement(IDataFlowNode inode) {
67          int index = inode.getIndex() - 1;
68          if (index < 0) return false;
69          return ((IDataFlowNode) inode.getFlow().get(index)).isType(NodeType.DO_BEFORE_FIRST_STATEMENT);
70      }
71  }
72