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 if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
67 mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
69 mapLocSymbolToDescSet.get(locSymbol).add(desc);
72 public Location getFieldInferLocation(Descriptor desc) {
73 return getInferLocation(desc).get(0);
76 public Set<Descriptor> getDescSet(String locSymbol) {
77 if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
78 mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
80 return mapLocSymbolToDescSet.get(locSymbol);
83 public void mergeMapping(String oldLocSymbol, String newSharedLoc) {
84 Set<Descriptor> descSet = getDescSet(oldLocSymbol);
85 getDescSet(newSharedLoc).addAll(descSet);
86 mapLocSymbolToDescSet.remove(oldLocSymbol);
88 Set<Descriptor> keySet = mapDescToInferCompositeLocation.keySet();
89 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
90 Descriptor key = (Descriptor) iterator.next();
91 CompositeLocation inferLoc = getInferLocation(key);
93 CompositeLocation newInferLoc = new CompositeLocation();
94 if (inferLoc.getSize() > 1) {
95 // local variable has a composite location [refLoc.inferedLoc]
97 Location oldLoc = inferLoc.get(inferLoc.getSize() - 1);
98 // oldLoc corresponds to infered loc.
100 if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
101 for (int i = 0; i < inferLoc.getSize() - 1; i++) {
102 Location loc = inferLoc.get(i);
103 newInferLoc.addLocation(loc);
105 Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
106 newInferLoc.addLocation(newLoc);
107 mapDescriptorToLocation(key, newInferLoc);
113 // local var has a local location
114 Location oldLoc = inferLoc.get(0);
115 if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
116 Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
117 newInferLoc.addLocation(newLoc);
118 mapDescriptorToLocation(key, newInferLoc);