1
2
3
4 package net.sourceforge.pmd.dfa;
5
6 import net.sourceforge.pmd.ast.SimpleNode;
7
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.Stack;
11
12
13 /***
14 * @author raik
15 * <p/>
16 * Structure contains only raw data. A set of nodes wich represent a data flow
17 * and 2 stacks to link the nodes to each other.
18 */
19 public class Structure {
20
21 private LinkedList dataFlow = new LinkedList();
22 private Stack braceStack = new Stack();
23 private Stack continueBreakReturnStack = new Stack();
24
25 /***
26 * This class encapsulates the access to the DataFlowNode class. Is this worthwhile?
27 * TODO I think it's too confusing to have the DataFlowNode constructor
28 * add the created instance to the LinkedList. I think it'd be clearer if we did
29 * that more "procedurally", i.e., create the object, then add it to the list.
30 *
31 */
32 public IDataFlowNode createNewNode(SimpleNode node) {
33 return new DataFlowNode(node, this.dataFlow);
34 }
35
36 public IDataFlowNode createStartNode(int line) {
37 return new StartOrEndDataFlowNode(this.dataFlow, line, true);
38 }
39
40 public IDataFlowNode createEndNode(int line) {
41 return new StartOrEndDataFlowNode(this.dataFlow, line, false);
42 }
43
44 public IDataFlowNode getLast() {
45 return (IDataFlowNode) this.dataFlow.getLast();
46 }
47
48 public IDataFlowNode getFirst() {
49 return (IDataFlowNode) this.dataFlow.getFirst();
50 }
51
52
53
54
55 /***
56 * The braceStack contains all nodes which are important to link the data
57 * flow nodes. The cbrStack contains continue, break, and return nodes.
58 * There are 2 Stacks because the have to process differently.
59 */
60 protected void pushOnStack(int type, IDataFlowNode node) {
61 StackObject obj = new StackObject(type, node);
62 if (type == NodeType.RETURN_STATEMENT || type == NodeType.BREAK_STATEMENT || type == NodeType.CONTINUE_STATEMENT) {
63
64 continueBreakReturnStack.push(obj);
65 } else {
66 braceStack.push(obj);
67 }
68 ((DataFlowNode) node).setType(type);
69 }
70
71 public List getBraceStack() {
72 return braceStack;
73 }
74
75 public List getContinueBreakReturnStack() {
76 return continueBreakReturnStack;
77 }
78
79 }