1 package Analysis.SSJava;
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Iterator;
9 import IR.ClassDescriptor;
11 import IR.MethodDescriptor;
13 public class LocationInfo {
15 // Map<Descriptor, String> mapDescToLocSymbol;
16 Map<String, Set<Descriptor>> mapLocSymbolToDescSet;
18 Map<Descriptor, CompositeLocation> mapDescToInferCompositeLocation;
22 public LocationInfo() {
23 mapDescToInferCompositeLocation = new HashMap<Descriptor, CompositeLocation>();
24 mapLocSymbolToDescSet = new HashMap<String, Set<Descriptor>>();
27 public LocationInfo(ClassDescriptor cd) {
32 public Map<String, Set<Descriptor>> getMapLocSymbolToDescSet() {
33 return mapLocSymbolToDescSet;
36 public Map<Descriptor, CompositeLocation> getMapDescToInferLocation() {
37 return mapDescToInferCompositeLocation;
40 public void mapDescriptorToLocation(Descriptor desc, CompositeLocation inferLoc) {
41 mapDescToInferCompositeLocation.put(desc, inferLoc);
44 // public void mapDescSymbolToLocName(String descSymbol, String locName) {
45 // mapDescSymbolToLocName.put(descSymbol, locName);
48 public CompositeLocation getInferLocation(Descriptor desc) {
49 if (!mapDescToInferCompositeLocation.containsKey(desc)) {
50 CompositeLocation newInferLoc = new CompositeLocation();
54 loc = new Location(md, desc.getSymbol());
56 loc = new Location(cd, desc.getSymbol());
58 newInferLoc.addLocation(loc);
59 mapDescToInferCompositeLocation.put(desc, newInferLoc);
60 addMapLocSymbolToDescSet(desc.getSymbol(), desc);
62 return mapDescToInferCompositeLocation.get(desc);
65 public void addMapLocSymbolToDescSet(String locSymbol, Descriptor desc) {
66 System.out.println("mapLocSymbolToDescSet=" + mapLocSymbolToDescSet);
67 if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
68 mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
70 mapLocSymbolToDescSet.get(locSymbol).add(desc);
73 public Location getFieldInferLocation(Descriptor desc) {
74 return getInferLocation(desc).get(0);
77 public Set<Descriptor> getDescSet(String locSymbol) {
78 if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
79 mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
81 return mapLocSymbolToDescSet.get(locSymbol);
84 public void mergeMapping(String oldLocSymbol, String newSharedLoc) {
85 Set<Descriptor> descSet = getDescSet(oldLocSymbol);
86 getDescSet(newSharedLoc).addAll(descSet);
87 mapLocSymbolToDescSet.remove(oldLocSymbol);
89 Set<Descriptor> keySet = mapDescToInferCompositeLocation.keySet();
90 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
91 Descriptor key = (Descriptor) iterator.next();
92 CompositeLocation inferLoc = getInferLocation(key);
94 CompositeLocation newInferLoc = new CompositeLocation();
95 if (inferLoc.getSize() > 1) {
96 // local variable has a composite location [refLoc.inferedLoc]
98 Location oldLoc = inferLoc.get(inferLoc.getSize() - 1);
99 // oldLoc corresponds to infered loc.
101 if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
102 for (int i = 0; i < inferLoc.getSize() - 1; i++) {
103 Location loc = inferLoc.get(i);
104 newInferLoc.addLocation(loc);
106 Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
107 newInferLoc.addLocation(newLoc);
108 mapDescriptorToLocation(key, newInferLoc);
114 // local var has a local location
115 Location oldLoc = inferLoc.get(0);
116 if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
117 Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
118 newInferLoc.addLocation(newLoc);
119 mapDescriptorToLocation(key, newInferLoc);