View Javadoc

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