Fixes null captured parameters
[jpf-core.git] / src / main / gov / nasa / jpf / vm / Path.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.vm;
19
20 import gov.nasa.jpf.util.Printable;
21
22 import java.io.PrintWriter;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25
26
27 /**
28  * Path represents the data structure in which a execution trace is recorded.
29  */
30 public class Path implements Printable, Iterable<Transition>, Cloneable {
31   String             application;  
32   private LinkedList<Transition> stack;
33   
34   private Path() {} // for cloning
35   
36   public Path (String app) {
37     application = app;
38     stack = new LinkedList<Transition>();
39   }
40   
41   @Override
42   public Path clone() {
43     Path clone = new Path();
44     clone.application = application;
45     
46     // we need to deep copy the stack to preserve CG and ThreadInfo state
47     LinkedList<Transition> clonedStack = new LinkedList<Transition>();
48     for (Transition t : stack){
49       clonedStack.add( (Transition)t.clone());
50     }
51     clone.stack = clonedStack;
52     
53     return clone;
54   }
55   
56   public String getApplication () {
57     return application;
58   }
59
60   public Transition getLast () {
61     if (stack.isEmpty()) {
62       return null;
63     } else {
64       return stack.getLast();
65     }
66   }
67
68   public void add (Transition t) {
69     stack.add(t);
70   }
71
72   public Transition get (int pos) {
73     return stack.get(pos);
74   }
75
76   public boolean isEmpty() {
77     return (stack.size() == 0);
78   }
79   
80   public int size () {
81     return stack.size();
82   }
83
84   public boolean hasOutput () {
85     for (Transition t : stack) {
86       if (t.getOutput() != null) {
87         return true;
88       }
89     }
90     
91     return false;
92   }
93   
94   public void printOutputOn (PrintWriter pw) {
95     for (Transition t : stack) {
96       String output = t.getOutput();
97       if (t != null) {
98         pw.print(output);
99       }
100     }
101   }
102   
103   @Override
104   public void printOn (PrintWriter pw) {
105 /**** <2do> this is going away
106     int    length = size;
107     Transition entry;
108
109     for (int index = 0; index < length; index++) {
110       pw.print("Transition #");
111       pw.print(index);
112       
113       if ((entry = get(index)) != null) {
114         pw.print(' ');
115
116         entry.printOn(pw);
117       }
118     }
119 ***/
120   }
121
122   public void removeLast () {
123     stack.removeLast();
124   }
125   
126   @Override
127   public Iterator<Transition> iterator () {
128     return stack.iterator();
129   }
130   
131   public Iterator<Transition> descendingIterator() {
132     return stack.descendingIterator();
133   }
134 }