KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > eval > SymbolicEvaluationStack


1 package spoon.reflect.eval;
2
3 import java.util.List JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Stack JavaDoc;
6
7 import spoon.reflect.code.CtAbstractInvocation;
8 import spoon.reflect.reference.CtExecutableReference;
9 import spoon.reflect.reference.CtVariableReference;
10
11 /**
12  * This class defines a symbolic evaluation stack for
13  * {@link spoon.reflect.eval.SymbolicEvaluator}.
14  */

15 public class SymbolicEvaluationStack {
16
17     /**
18      * Tests the equality with another stack.
19      */

20     @Override JavaDoc
21     public boolean equals(Object JavaDoc obj) {
22         SymbolicEvaluationStack c = (SymbolicEvaluationStack) obj;
23         return frameStack.equals(c.frameStack);
24     }
25
26     private Stack JavaDoc<SymbolicStackFrame> frameStack = new Stack JavaDoc<SymbolicStackFrame>();
27
28     /**
29      * Constructs and returns the calling stack for this evaluation stack.
30      */

31     public Stack JavaDoc<CtAbstractInvocation> getCallingStack() {
32         Stack JavaDoc<CtAbstractInvocation> s = new Stack JavaDoc<CtAbstractInvocation>();
33         for (SymbolicStackFrame f : frameStack) {
34             s.add(f.getInvocation());
35         }
36         return s;
37     }
38
39     /**
40      * Enters and creates a new {@link SymbolicStackFrame}, which is pushed on
41      * the top of the stack.
42      *
43      * @param caller
44      * the invocation that starts this new frame
45      * @param target
46      * the target of the invocation (to be <code>this</code>, see
47      * {@link #getThis()})
48      * @param executable
49      * the entered executable
50      * @param variables
51      * the variables accessible from the frame (invocation's
52      * parameters)
53      */

54     public void enterFrame(CtAbstractInvocation caller,
55             SymbolicInstance target, CtExecutableReference executable,
56             List JavaDoc<SymbolicInstance> arguments,
57             Map JavaDoc<CtVariableReference, SymbolicInstance> variables) {
58         frameStack.push(new SymbolicStackFrame(caller, target, executable,
59                 arguments, variables));
60     }
61
62     /**
63      * Pops the top frame in order to leave it.
64      */

65     public void exitFrame() {
66         frameStack.pop();
67     }
68
69     /**
70      * Gets the this value of the top frame.
71      */

72     public SymbolicInstance<?> getThis() {
73         return frameStack.peek().getThis();
74     }
75
76     /**
77      * Gets the symbolic value of a variable within the top frame.
78      */

79     public SymbolicInstance getVariableValue(CtVariableReference<?> vref) {
80         if (frameStack.peek().getVariables().containsKey(vref)) {
81             return frameStack.peek().getVariables().get(vref);
82         } else {
83             throw new RuntimeException JavaDoc("unknown variable '"+vref+"'");
84         }
85     }
86
87     /**
88      * Sets the symbolic value of a variable within the top frame.
89      */

90     public void setVariableValue(CtVariableReference<?> vref,
91             SymbolicInstance value) {
92         if (frameStack.peek().getVariables().containsKey(vref)) {
93             frameStack.peek().getVariables().put(vref, value);
94         } else {
95             throw new RuntimeException JavaDoc("unknown variable '"+vref+"'");
96         }
97     }
98
99     /**
100      * Creates a copy of the given stack.
101      */

102     public SymbolicEvaluationStack(SymbolicEvaluationStack stack) {
103         for (SymbolicStackFrame f : stack.frameStack) {
104             frameStack.add(new SymbolicStackFrame(f));
105         }
106     }
107
108     /**
109      * Creates an empty evaluation stack.
110      */

111     public SymbolicEvaluationStack() {
112     }
113
114     /**
115      * A string representation.
116      */

117     @Override JavaDoc
118     public String JavaDoc toString() {
119         return "" + frameStack;
120     }
121
122     /**
123      * Dumps the stack on the screen.
124      */

125     public void dump() {
126         System.out.println("Stack:");
127         int i = 1;
128         for (SymbolicStackFrame f : frameStack) {
129             System.out.print(" " + (i++) + "\t");
130             System.out.println(f.toString());
131         }
132     }
133
134     /**
135      * Gets the current result (returned value) for the top stack frame of this
136      * stack.
137      */

138     public SymbolicInstance getResult() {
139         return frameStack.peek().getResult();
140     }
141
142     /**
143      * Sets the current result (returned value) for the top stack frame of this
144      * stack.
145      */

146     public void setResult(SymbolicInstance result) {
147         frameStack.peek().setResult(result);
148     }
149
150     /**
151      * Gets the frames as a stack.
152      */

153     public Stack JavaDoc<SymbolicStackFrame> getFrameStack() {
154         return frameStack;
155     }
156
157 }
158
Popular Tags