KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.reflect.eval;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Map.Entry;
6
7 import spoon.reflect.reference.CtTypeReference;
8
9 /**
10  * This class defines the heap for {@link spoon.reflect.eval.SymbolicEvaluator}.
11  */

12 public class SymbolicHeap {
13
14     /**
15      * Creates an empty heap.
16      */

17     public SymbolicHeap() {
18     }
19
20     /**
21      * Copies the given heap.
22      */

23     @SuppressWarnings JavaDoc("unchecked")
24     public SymbolicHeap(SymbolicHeap heap) {
25         statelessAbstractInstances.putAll(heap.statelessAbstractInstances);
26         for (Entry<String JavaDoc, SymbolicInstance> e : heap.statefullAbstractInstances
27                 .entrySet()) {
28             statefullAbstractInstances.put(e.getKey(), new SymbolicInstance(e
29                     .getValue()));
30         }
31     }
32
33     /**
34      * A string representation.
35      */

36     @Override JavaDoc
37     public String JavaDoc toString() {
38         return "stateful=" + statefullAbstractInstances + " stateless="
39                 + statelessAbstractInstances;
40     }
41
42     /**
43      * Dumps the heap on the screen.
44      */

45     public void dump() {
46         System.out.println("\tHeap:");
47         System.out.println("\t - stateful: " + statefullAbstractInstances);
48         System.out.println("\t - stateless: " + statelessAbstractInstances);
49     }
50
51     private Map JavaDoc<String JavaDoc, SymbolicInstance> statefullAbstractInstances = new HashMap JavaDoc<String JavaDoc, SymbolicInstance>();
52
53     private Map JavaDoc<String JavaDoc, SymbolicInstance> statelessAbstractInstances = new HashMap JavaDoc<String JavaDoc, SymbolicInstance>();
54
55     /**
56      * Gets/creates a symbolic value of a given type (automatically stored in
57      * the heap).
58      *
59      * @param <T>
60      * the actual type if known
61      * @param concreteType
62      * the type reference
63      * @return the symbolic value for the type
64      */

65     public <T> SymbolicInstance<T> getType(SymbolicEvaluator evaluator,
66             CtTypeReference<T> concreteType) {
67         SymbolicInstance<T> type = get(SymbolicInstance.getSymbolId(
68                 concreteType, "type"));
69         if (type == null) {
70             type = new SymbolicInstance<T>(evaluator, concreteType, true);
71             store(type);
72         }
73         return type;
74     }
75
76     /**
77      * Stores the given symbolic instance in the heap.
78      */

79     public void store(SymbolicInstance instance) {
80         if (instance.isStateful()) {
81             statefullAbstractInstances.put(instance.getId(), instance);
82         } else {
83             statelessAbstractInstances.put(instance.getId(), instance);
84         }
85     }
86
87     /**
88      * Gets an existing symbolic instance from its id.
89      */

90     @SuppressWarnings JavaDoc("unchecked")
91     public <T> SymbolicInstance<T> get(String JavaDoc id) {
92         SymbolicInstance<T> i = (SymbolicInstance<T>) statelessAbstractInstances
93                 .get(id);
94         if (i == null) {
95             i = (SymbolicInstance<T>) statefullAbstractInstances.get(id);
96         }
97         return i;
98     }
99
100     /**
101      * Clears this heap (only the stateful instances).
102      */

103     public void clear() {
104         statefullAbstractInstances.clear();
105     }
106
107 }
108
Popular Tags