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