001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.reflect.eval;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Map.Entry;
023:
024: import spoon.reflect.reference.CtTypeReference;
025:
026: /**
027: * This class defines the heap for {@link spoon.reflect.eval.SymbolicEvaluator}.
028: */
029: public class SymbolicHeap {
030:
031: /**
032: * Creates an empty heap.
033: */
034: public SymbolicHeap() {
035: }
036:
037: /**
038: * Copies the given heap.
039: */
040: @SuppressWarnings("unchecked")
041: public SymbolicHeap(SymbolicHeap heap) {
042: statelessAbstractInstances
043: .putAll(heap.statelessAbstractInstances);
044: for (Entry<String, SymbolicInstance<?>> e : heap.statefullAbstractInstances
045: .entrySet()) {
046: statefullAbstractInstances.put(e.getKey(),
047: new SymbolicInstance(e.getValue()));
048: }
049: }
050:
051: /**
052: * A string representation.
053: */
054: @Override
055: public String toString() {
056: return "stateful=" + statefullAbstractInstances + " stateless="
057: + statelessAbstractInstances;
058: }
059:
060: /**
061: * Dumps the heap on the screen.
062: */
063: public void dump() {
064: System.out.println("\tHeap:");
065: System.out.println("\t - stateful: "
066: + statefullAbstractInstances);
067: System.out.println("\t - stateless: "
068: + statelessAbstractInstances);
069: }
070:
071: private Map<String, SymbolicInstance<?>> statefullAbstractInstances = new HashMap<String, SymbolicInstance<?>>();
072:
073: private Map<String, SymbolicInstance<?>> statelessAbstractInstances = new HashMap<String, SymbolicInstance<?>>();
074:
075: /**
076: * Gets/creates a symbolic value of a given type (automatically stored in
077: * the heap).
078: *
079: * @param <T>
080: * the actual type if known
081: * @param concreteType
082: * the type reference
083: * @return the symbolic value for the type
084: */
085: public <T> SymbolicInstance<T> getType(SymbolicEvaluator evaluator,
086: CtTypeReference<T> concreteType) {
087: SymbolicInstance<T> type = get(SymbolicInstance.getSymbolId(
088: concreteType, "type"));
089: if (type == null) {
090: type = new SymbolicInstance<T>(evaluator, concreteType,
091: true);
092: store(type);
093: }
094: return type;
095: }
096:
097: /**
098: * Stores the given symbolic instance in the heap.
099: */
100: public void store(SymbolicInstance<?> instance) {
101: if (instance.isStateful()) {
102: statefullAbstractInstances.put(instance.getId(), instance);
103: } else {
104: statelessAbstractInstances.put(instance.getId(), instance);
105: }
106: }
107:
108: /**
109: * Gets an existing symbolic instance from its id.
110: */
111: @SuppressWarnings("unchecked")
112: public <T> SymbolicInstance<T> get(String id) {
113: SymbolicInstance<T> i = (SymbolicInstance<T>) statelessAbstractInstances
114: .get(id);
115: if (i == null) {
116: i = (SymbolicInstance<T>) statefullAbstractInstances
117: .get(id);
118: }
119: return i;
120: }
121:
122: /**
123: * Clears this heap (only the stateful instances).
124: */
125: public void clear() {
126: statefullAbstractInstances.clear();
127: }
128:
129: }
|