+ return ComparisonResult.LESS;
+
+ }
+
+ public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet, String errMsg) {
+
+ // System.out.println("Calculating GLB=" + inputSet);
+ CompositeLocation glbCompLoc = new CompositeLocation();
+
+ // calculate GLB of the first(priority) element
+ Set<String> priorityLocIdentifierSet = new HashSet<String>();
+ Descriptor priorityDescriptor = null;
+
+ Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
+ new Hashtable<String, Set<CompositeLocation>>();
+ // mapping from the priority loc ID to its full representation by the
+ // composite location
+
+ int maxTupleSize = 0;
+ CompositeLocation maxCompLoc = null;
+
+ Location prevPriorityLoc = null;
+ for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
+ CompositeLocation compLoc = (CompositeLocation) iterator.next();
+ if (compLoc.getSize() > maxTupleSize) {
+ maxTupleSize = compLoc.getSize();
+ maxCompLoc = compLoc;
+ }
+ Location priorityLoc = compLoc.get(0);
+ String priorityLocId = priorityLoc.getLocIdentifier();
+ priorityLocIdentifierSet.add(priorityLocId);
+
+ if (locId2CompLocSet.containsKey(priorityLocId)) {
+ locId2CompLocSet.get(priorityLocId).add(compLoc);
+ } else {
+ Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
+ newSet.add(compLoc);
+ locId2CompLocSet.put(priorityLocId, newSet);
+ }
+
+ // check if priority location are coming from the same lattice
+ if (priorityDescriptor == null) {
+ priorityDescriptor = priorityLoc.getDescriptor();
+ } else {
+ priorityDescriptor = getCommonParentDescriptor(priorityLoc, prevPriorityLoc, errMsg);
+ }
+ prevPriorityLoc = priorityLoc;
+ // else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
+ // throw new Error("Failed to calculate GLB of " + inputSet
+ // + " because they are from different lattices.");
+ // }
+ }
+
+ SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
+ String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
+
+ glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
+ Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
+
+ if (compSet == null) {
+ // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
+ // mean that the result is already lower than <x1,y1> and <x2,y2>
+ // assign TOP to the rest of the location elements
+
+ // in this case, do not take care about delta
+ // CompositeLocation inputComp = inputSet.iterator().next();
+ for (int i = 1; i < maxTupleSize; i++) {
+ glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor()));
+ }
+ } else {
+
+ // here find out composite location that has a maximum length tuple
+ // if we have three input set: [A], [A,B], [A,B,C]
+ // maximum length tuple will be [A,B,C]
+ int max = 0;
+ CompositeLocation maxFromCompSet = null;
+ for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
+ CompositeLocation c = (CompositeLocation) iterator.next();
+ if (c.getSize() > max) {
+ max = c.getSize();
+ maxFromCompSet = c;
+ }
+ }
+
+ if (compSet.size() == 1) {
+ // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
+ CompositeLocation comp = compSet.iterator().next();
+ for (int i = 1; i < comp.getSize(); i++) {
+ glbCompLoc.addLocation(comp.get(i));
+ }
+
+ // if input location corresponding to glb is a delta, need to apply
+ // delta to glb result
+ if (comp instanceof DeltaLocation) {
+ glbCompLoc = new DeltaLocation(glbCompLoc, 1);
+ }
+
+ } else {
+ // when GLB(x1,x2)==x1 and x2 : GLB case 1
+ // if more than one location shares the same priority GLB
+ // need to calculate the rest of GLB loc
+
+ // setup input set starting from the second tuple item
+ Set<CompositeLocation> innerGLBInput = new HashSet<CompositeLocation>();
+ for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
+ CompositeLocation compLoc = (CompositeLocation) iterator.next();
+ CompositeLocation innerCompLoc = new CompositeLocation();
+ for (int idx = 1; idx < compLoc.getSize(); idx++) {
+ innerCompLoc.addLocation(compLoc.get(idx));
+ }
+ if (innerCompLoc.getSize() > 0) {
+ innerGLBInput.add(innerCompLoc);
+ }
+ }
+
+ if (innerGLBInput.size() > 0) {
+ CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
+ for (int idx = 0; idx < innerGLB.getSize(); idx++) {
+ glbCompLoc.addLocation(innerGLB.get(idx));
+ }
+ }
+
+ // if input location corresponding to glb is a delta, need to apply
+ // delta to glb result
+
+ for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
+ CompositeLocation compLoc = (CompositeLocation) iterator.next();
+ if (compLoc instanceof DeltaLocation) {
+ if (glbCompLoc.equals(compLoc)) {
+ glbCompLoc = new DeltaLocation(glbCompLoc, 1);
+ break;
+ }
+ }
+ }
+
+ }
+ }
+
+ // System.out.println("GLB=" + glbCompLoc);
+ return glbCompLoc;
+
+ }
+
+ static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
+
+ SSJavaLattice<String> lattice = null;
+
+ if (d instanceof ClassDescriptor) {
+ lattice = ssjava.getCd2lattice().get(d);
+ } else if (d instanceof MethodDescriptor) {
+ if (ssjava.getMd2lattice().containsKey(d)) {
+ lattice = ssjava.getMd2lattice().get(d);
+ } else {
+ // use default lattice for the method
+ lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
+ }
+ }
+
+ return lattice;
+ }
+
+ static Descriptor getCommonParentDescriptor(Location loc1, Location loc2, String msg) {
+
+ Descriptor d1 = loc1.getDescriptor();
+ Descriptor d2 = loc2.getDescriptor();
+
+ Descriptor descriptor;
+
+ if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
+
+ if (d1.equals(d2)) {
+ descriptor = d1;
+ } else {
+ // identifying which one is parent class
+ Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
+ Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
+
+ if (d1 == null && d2 == null) {
+ throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+ + " because they are not comparable at " + msg);
+ } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
+ descriptor = d1;
+ } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
+ descriptor = d2;
+ } else {
+ throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+ + " because they are not comparable at " + msg);
+ }
+ }
+
+ } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
+
+ if (d1.equals(d2)) {
+ descriptor = d1;
+ } else {
+
+ // identifying which one is parent class
+ MethodDescriptor md1 = (MethodDescriptor) d1;
+ MethodDescriptor md2 = (MethodDescriptor) d2;
+
+ if (!md1.matches(md2)) {
+ throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+ + " because they are not comparable at " + msg);
+ }
+
+ Set<Descriptor> d1SubClassesSet =
+ ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
+ Set<Descriptor> d2SubClassesSet =
+ ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
+
+ if (d1 == null && d2 == null) {
+ throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+ + " because they are not comparable at " + msg);
+ } else if (d1 != null && d1SubClassesSet.contains(d2)) {
+ descriptor = d1;
+ } else if (d2 != null && d2SubClassesSet.contains(d1)) {
+ descriptor = d2;
+ } else {
+ throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+ + " because they are not comparable at " + msg);
+ }
+ }
+
+ } else {
+ throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+ + " because they are not comparable at " + msg);
+ }
+
+ return descriptor;
+