From 3bf45888e52fe97ff9077aa8995152160e30c9d0 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 12 Dec 2018 13:03:53 +0100 Subject: [PATCH] Simplify gefixt --- .../descriptor/DescriptorToString.java | 2 +- .../bytecode/utilities/Simplify.java | 78 +++++++++++++++++-- test/bytecode/simplifyalgo/SameLeftSide.java | 14 ++-- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index d92efe5a0..5464aa910 100644 --- a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -109,7 +109,7 @@ public class DescriptorToString implements DescriptorVisitor{ desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } }else { -// System.out.println("Cons has NOT Gens"); +// System.out.println("Cons has NO Gens"); desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } } diff --git a/src/de/dhbwstuttgart/bytecode/utilities/Simplify.java b/src/de/dhbwstuttgart/bytecode/utilities/Simplify.java index 2ec212f7e..bcaaf2b5a 100644 --- a/src/de/dhbwstuttgart/bytecode/utilities/Simplify.java +++ b/src/de/dhbwstuttgart/bytecode/utilities/Simplify.java @@ -8,6 +8,7 @@ import java.util.LinkedList; import org.objectweb.asm.Type; import de.dhbwstuttgart.bytecode.TPHExtractor; +import de.dhbwstuttgart.bytecode.constraint.EqualConstraint; import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; @@ -42,7 +43,7 @@ public class Simplify { if(revCon != null) { revCon.setRel(Relation.EQUAL); // the reverse constraint is removed because - // otherwise there is twice the same constraint + // otherwise there is the same constraint twice // (e.g. A A=B and B=A) consToRemove.add(revCon); c.setRel(Relation.EQUAL); @@ -199,6 +200,7 @@ public class Simplify { // if yes => check if the super type in the method, if not // then ignore it. HashMap subAndSuper = new HashMap<>(); + ArrayList eqCons = new ArrayList<>(); for(TPHConstraint c : allCons) { if(subAndSuper.containsKey(c.getLeft())) { LinkedList all = new LinkedList<>(); @@ -216,9 +218,28 @@ public class Simplify { if(!containTPH(methodTphs, all.getLast())) continue; } + if(subAndSuper.containsKey(c.getLeft())) { + System.out.println(c.getLeft()); + String r = c.getRight(); + String r2 = subAndSuper.get(c.getLeft()); + EqualConstraint eq = new EqualConstraint(r2, r, Relation.EQUAL); + eqCons.add(eq); + substituteInMap(subAndSuper,eqCons,allCons,r,r2); + } subAndSuper.put(c.getLeft(), c.getRight()); } + System.out.println("SAME LEFT SIDE: "); + subAndSuper.forEach((c,hs)->{ + if(c!=null) { + System.out.print(c+ " -> " + hs); + + } + + + System.out.println(); + }); + System.out.println("----------------"); int numOfVisitedPairs = 0; for(String sub : subAndSuper.keySet()) { if(isTPHInConstraint(result,sub)) @@ -280,17 +301,23 @@ public class Simplify { } if(!containTPH(methodTphs, superTphRes)) { - result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), null); + HashSet equals = getEqualsTphsFromEqualCons(eqCons,superTphRes); + + result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), equals); } else { - result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), null); - if(!isTPHInConstraint(result, superTphRes)) - result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), null); + HashSet equals = getEqualsTphsFromEqualCons(eqCons,subTphRes); + result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), equals); + if(!isTPHInConstraint(result, superTphRes) && !isTphInEqualSet(result,superTphRes)) { + HashSet equals2 = getEqualsTphsFromEqualCons(eqCons,superTphRes); + result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), equals2); + } } } for(String tph : methodTphs) { - if(!isTPHInConstraint(result, tph)) { - result.put(new ExtendsConstraint(tph, Type.getInternalName(Object.class), Relation.EXTENDS), null); + if(!isTPHInConstraint(result, tph) && !isTphInEqualSet(result,tph)) { + HashSet equals = getEqualsTphsFromEqualCons(eqCons,tph); + result.put(new ExtendsConstraint(tph, Type.getInternalName(Object.class), Relation.EXTENDS), equals); } } @@ -314,6 +341,43 @@ public class Simplify { return result; } + private static boolean isTphInEqualSet(HashMap> result, String tph) { + for(HashSet hs: result.values()) { + if(hs.contains(tph)) + return true; + } + return false; + } + + private static HashSet getEqualsTphsFromEqualCons(ArrayList eqCons, String tph) { + HashSet ee = new HashSet<>(); + for(TPHConstraint c : eqCons) { + if(c.getLeft().equals(tph)) + ee.add(c.getRight()); + if(c.getRight().equals(tph)) + ee.add(c.getLeft()); + } + return ee; + } + + private static void substituteInMap(HashMap subAndSuper, ArrayList allCons,ArrayList eqCons, String toSubs, + String tph) { + substituteTPH(allCons, toSubs, tph); + if(subAndSuper.containsKey(toSubs) && subAndSuper.containsKey(tph)) { + toSubs = subAndSuper.remove(toSubs); + EqualConstraint eq = new EqualConstraint(subAndSuper.get(tph), toSubs, Relation.EQUAL); + eqCons.add(eq); + substituteInMap(subAndSuper, allCons,eqCons,toSubs, subAndSuper.get(tph)); + } else if(subAndSuper.containsKey(toSubs) && !subAndSuper.containsKey(tph)) { + String val = subAndSuper.remove(toSubs); + subAndSuper.put(tph, val); + } else { + for(String k : subAndSuper.keySet()) { + subAndSuper.replace(k, toSubs, tph); + } + } + } + private static TPHConstraint getConstraint(String oldRight, String right, ArrayList allCons) { for(TPHConstraint c : allCons) { if(c.getLeft().equals(oldRight) && c.getRight().equals(right)) diff --git a/test/bytecode/simplifyalgo/SameLeftSide.java b/test/bytecode/simplifyalgo/SameLeftSide.java index b5c4e875f..402fe1e16 100644 --- a/test/bytecode/simplifyalgo/SameLeftSide.java +++ b/test/bytecode/simplifyalgo/SameLeftSide.java @@ -70,9 +70,11 @@ public class SameLeftSide { TPHConstraint d = new ExtendsConstraint("D", Type.getInternalName(Object.class), Relation.EXTENDS); TPHConstraint a = new ExtendsConstraint("A", "D", Relation.EXTENDS); TPHConstraint b = new ExtendsConstraint("B", "D", Relation.EXTENDS); - result.put(d, null); - result.put(a, null); - result.put(b, null); + result.put(d, new HashSet<>()); + result.put(a, new HashSet<>()); + HashSet hs = new HashSet<>(); + + result.put(b, hs); HashMap> sim = Simplify.simplifyConstraints(methName, tphExtractor); boolean areEquals = SimpleCycle.areMapsEqual(result, sim); @@ -86,8 +88,10 @@ public class SameLeftSide { TPHConstraint e = new ExtendsConstraint("E", Type.getInternalName(Object.class), Relation.EXTENDS); TPHConstraint c = new ExtendsConstraint("C", "E", Relation.EXTENDS); - result.put(e, null); - result.put(c, null); + result.put(e, new HashSet<>()); + HashSet hs = new HashSet<>(); + hs.add("B"); + result.put(c, hs); HashMap> sim = Simplify.simplifyConstraints(methName2, tphExtractor); boolean areEquals = SimpleCycle.areMapsEqual(result, sim);