Separated the algorithm into two parts. Part 1 is the simplification of

the constraints and part 2 is generation of generics.
This commit is contained in:
Fayez Abu Alia 2019-07-12 19:50:46 +02:00
parent 347d86a379
commit dfddc44f29
11 changed files with 278 additions and 74 deletions

View File

@ -106,7 +106,7 @@ public class TPHExtractor extends AbstractASTWalker {
if (inMethod) if (inMethod)
methodAndTph.getPairs().add(ag); methodAndTph.getPairs().add(ag);
allPairs.add(ag); allPairs.add(ag);
TPHConstraint con = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName(), Relation.EXTENDS); TPHConstraint con = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName());
if(!containsConstraint(allCons,con)) if(!containsConstraint(allCons,con))
allCons.add(con); allCons.add(con);
// } // }

View File

@ -2,8 +2,8 @@ package de.dhbwstuttgart.bytecode.constraint;
public class EqualConstraint extends TPHConstraint { public class EqualConstraint extends TPHConstraint {
public EqualConstraint(String left, String right, Relation rel) { public EqualConstraint(String left, String right) {
super(left, right, rel); super(left, right, Relation.EQUAL);
} }
} }

View File

@ -2,8 +2,8 @@ package de.dhbwstuttgart.bytecode.constraint;
public class ExtendsConstraint extends TPHConstraint { public class ExtendsConstraint extends TPHConstraint {
public ExtendsConstraint(String left, String right, Relation rel) { public ExtendsConstraint(String left, String right) {
super(left, right, rel); super(left, right, Relation.EXTENDS);
} }
} }

View File

@ -0,0 +1,92 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.simplifyRes.ConstraintsSimplierResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
/**
* @author fayez
*
*/
public class ConstraintsSimplifier {
public static ConstraintsSimplierResult simplifyConstraints(TPHExtractor tphExtractor, List<GenericsGeneratorResult> genericsGeneratorResults, List<String> tphsClass) {
List<TPHConstraint> allCons = tphExtractor.allCons;
List<TPHConstraint> equalCons = new ArrayList<>();
List<SimpleCycle> simpleCycles = findSimpleCycles(allCons);
if(!simpleCycles.isEmpty()) {
handleSimpleCycles(allCons, simpleCycles);
equalCons.addAll(GenericsGeneratorUtility.getEqualConstraints(allCons));
}
/* Step 2 */
List<ConstraintsWithSameLeftSide> foundCons = GenericsGeneratorUtility.findConstraintsWithLeftSameLeftSide(allCons);
if(!foundCons.isEmpty()) {
List<TPHConstraint> equalCons2 = GenericsGeneratorUtility.simplifyConstraintsWithSameLeftSide(foundCons,tphExtractor,tphsClass);
equalCons.addAll(equalCons2);
}
List<LongCycle> longCycles = findLongCycles(allCons);
if(!longCycles.isEmpty()) {
handleLongCycle(genericsGeneratorResults, allCons, longCycles);
}
/* The right hand side of equal constraint is the new name in name replacement result */
List<NameReplacementResult> nameReplacementResults = GenericsGeneratorUtility.createNameReplacementResultsFromEqualConstraints(equalCons);
ConstraintsSimplierResult result = new ConstraintsSimplierResult(genericsGeneratorResults, nameReplacementResults);
return result;
}
/**
* @param genericsGeneratorResults
* @param allCons
* @param longCycles
*/
public static void handleLongCycle(List<GenericsGeneratorResult> genericsGeneratorResults,
List<TPHConstraint> allCons, List<LongCycle> longCycles) {
GenericsGeneratorUtility.modifyRelationForConstraintsinLongCycle(longCycles);
List<NameReplacementResult> nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormLongCycle(allCons, longCycles);
GenericsGeneratorUtility.createResults(genericsGeneratorResults,nameReplacementResults);
GenericsGeneratorUtility.removeEqualConstraints(allCons);
}
/**
* @param allCons
* @return
*/
public static List<LongCycle> findLongCycles(List<TPHConstraint> allCons) {
/* find all long cycles */
CyclesFinder cyclesFinder = new CyclesFinder(allCons);
List<LongCycle> longCycles = cyclesFinder.findLongCycles();
return longCycles;
}
/**
* @param allCons
* @param simpleCycles
*/
public static void handleSimpleCycles(List<TPHConstraint> allCons, List<SimpleCycle> simpleCycles) {
GenericsGeneratorUtility.modifyRelation(simpleCycles);
GenericsGeneratorUtility.removeAllReverseConstraints(allCons,simpleCycles);
GenericsGeneratorUtility.substituteTPH(allCons, simpleCycles);
}
/**
* @param allCons
* @return
*/
public static List<SimpleCycle> findSimpleCycles(List<TPHConstraint> allCons) {
CyclesFinder cyclesFinder = new CyclesFinder(allCons);
/* find all simple cycles if they are exist */
List<SimpleCycle> simpleCycles = cyclesFinder.findSimpleCycles();
return simpleCycles;
}
}

View File

@ -4,10 +4,8 @@
package de.dhbwstuttgart.bytecode.genericsGenerator; package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
@ -51,18 +49,17 @@ public class CyclesFinder {
public List<LongCycle> findLongCycles() { public List<LongCycle> findLongCycles() {
List<TPHConstraint> vistedConstraints = new ArrayList<>(allCons.size()); List<TPHConstraint> vistedConstraints = new ArrayList<>(allCons.size());
List<LongCycle> longCycles = new ArrayList<>(); List<LongCycle> longCycles = new ArrayList<>();
Map<String, String> tableOfConstraints = createConstraintsTable();
for (TPHConstraint constraint : allCons) { for (TPHConstraint constraint : allCons) {
if (!vistedConstraints.contains(constraint)) { if (!vistedConstraints.contains(constraint)) {
vistedConstraints.add(constraint); vistedConstraints.add(constraint);
checkConstraint(constraint, vistedConstraints, longCycles, tableOfConstraints); checkConstraint(constraint, vistedConstraints, longCycles);
} }
} }
return longCycles; return longCycles;
} }
private void checkConstraint(TPHConstraint constraint, List<TPHConstraint> vistedConstraints, private void checkConstraint(TPHConstraint constraint, List<TPHConstraint> vistedConstraints,
List<LongCycle> longCycles, Map<String, String> tableOfConstraints) { List<LongCycle> longCycles) {
List<String> tphsInRelation = new LinkedList<>(); List<String> tphsInRelation = new LinkedList<>();
List<TPHConstraint> maybeCycle = new ArrayList<>(); List<TPHConstraint> maybeCycle = new ArrayList<>();
maybeCycle.add(constraint); maybeCycle.add(constraint);
@ -119,14 +116,6 @@ public class CyclesFinder {
return null; return null;
} }
private Map<String, String> createConstraintsTable() {
Map<String, String> table = new HashMap<>();
for (TPHConstraint constraint : allCons) {
table.put(constraint.getLeft(), constraint.getRight());
}
return table;
}
private boolean isCycle(List<String> tphsInRelation) { private boolean isCycle(List<String> tphsInRelation) {
return tphsInRelation.get(0).equals(tphsInRelation.get(tphsInRelation.size() - 1)); return tphsInRelation.get(0).equals(tphsInRelation.get(tphsInRelation.size() - 1));
} }

View File

@ -5,19 +5,12 @@ package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.EqualConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.simplifyRes.ConstraintsSimplierResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.utilities.ConstraintsFinder;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.MethodUtility; import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
import de.dhbwstuttgart.bytecode.utilities.NameReplacer;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
/** /**
@ -29,33 +22,13 @@ public class GenericsGenerator {
public static List<GenericsGeneratorResult> generateConstraintsForMethod(Method method, TPHExtractor tphExtractor, public static List<GenericsGeneratorResult> generateConstraintsForMethod(Method method, TPHExtractor tphExtractor,
List<String> tphsClass) { List<String> tphsClass) {
List<GenericsGeneratorResult> genericsGeneratorResults = new ArrayList<>(); List<GenericsGeneratorResult> genericsGeneratorResults = new ArrayList<>();
ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor, genericsGeneratorResults,tphsClass);
List<TPHConstraint> allCons = tphExtractor.allCons; List<TPHConstraint> allCons = tphExtractor.allCons;
String methodID = MethodUtility.createID(tphExtractor.getResolver(), method); String methodID = MethodUtility.createID(tphExtractor.getResolver(), method);
List<String> methodTPHS = GenericsGeneratorUtility.getMethodTPHS(methodID ,tphExtractor.ListOfMethodsAndTph); List<String> methodTPHS = GenericsGeneratorUtility.getMethodTPHS(methodID ,tphExtractor.ListOfMethodsAndTph);
CyclesFinder cyclesFinder = new CyclesFinder(allCons);
/* find all simple cycles if they are exist */
List<SimpleCycle> simpleCycles = cyclesFinder.findSimpleCycles();
if(!simpleCycles.isEmpty()) {
GenericsGeneratorUtility.modifyRelation(simpleCycles);
GenericsGeneratorUtility.removeAllReverseConstraints(allCons,simpleCycles);
GenericsGeneratorUtility.substituteTPH(allCons, simpleCycles);
GenericsGeneratorUtility.removeEqualConstraints(allCons);
}
/* Step 2 */
List<ConstraintsWithSameLeftSide> foundCons = GenericsGeneratorUtility.findConstraintsWithLeftSameLeftSide(allCons);
GenericsGeneratorUtility.simplifyConstraintsWithSameLeftSide(foundCons,allCons,methodTPHS);
/* find all long cycles */
List<LongCycle> longCycles = cyclesFinder.findLongCycles();
if(!longCycles.isEmpty()) {
GenericsGeneratorUtility.modifyRelationForConstraintsinLongCycle(longCycles);
List<NameReplacementResult> nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormLongCycle(allCons, longCycles);
GenericsGeneratorUtility.createResults(genericsGeneratorResults,nameReplacementResults);
}
return null; return null;
} }

View File

@ -13,7 +13,9 @@ import java.util.stream.Collectors;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.EqualConstraint; import de.dhbwstuttgart.bytecode.constraint.EqualConstraint;
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
@ -60,7 +62,9 @@ public class GenericsGeneratorUtility {
* @param newName The new name * @param newName The new name
*/ */
public static void substituteTPH(List<TPHConstraint> allCons, String oldName, String newName) { public static void substituteTPH(List<TPHConstraint> allCons, String oldName, String newName) {
allCons.forEach(c->{ allCons.stream()
.filter(c->c.getRel()==Relation.EXTENDS)
.forEach(c->{
if(c.getLeft().equals(oldName)) if(c.getLeft().equals(oldName))
c.setLeft(newName); c.setLeft(newName);
if(c.getRight().equals(oldName)) if(c.getRight().equals(oldName))
@ -113,11 +117,35 @@ public class GenericsGeneratorUtility {
} }
} }
public static List<TPHConstraint> simplifyConstraintsWithSameLeftSide(List<ConstraintsWithSameLeftSide> consWithSameLeftSide,
TPHExtractor tphExtractor, List<String> tphsClass) {
List<TPHConstraint> eqCons = new ArrayList<>();
List<TPHConstraint> allCons = tphExtractor.allCons;
for (ConstraintsWithSameLeftSide list : consWithSameLeftSide) {
NameReplacementResult replRes = modifyNames(allCons, list, tphExtractor.ListOfMethodsAndTph,tphsClass);
createEqualConstraintsForNames(replRes.getName(), replRes.getOldNames(),eqCons);
for (TPHConstraint c : allCons) {
if (c.getRel() == Relation.EQUAL && replRes.getName().equals(c.getRight())) {
eqCons.add(c);
}
}
updateEqualCons(replRes, eqCons);
TPHConstraint c = list.getConstraints().get(0);
allCons.removeAll(list.getConstraints());
allCons.add(c);
}
return eqCons;
}
private static void createEqualConstraintsForNames(String name, List<String> equalNames, List<TPHConstraint> eqCons) { private static void createEqualConstraintsForNames(String name, List<String> equalNames, List<TPHConstraint> eqCons) {
// create an equal constraint for each value in repres // create an equal constraint for each value in repres
for (String eName : equalNames) { for (String eName : equalNames) {
EqualConstraint ec = new EqualConstraint(eName, name, Relation.EQUAL); EqualConstraint ec = new EqualConstraint(eName, name);
eqCons.add(ec); eqCons.add(ec);
} }
} }
@ -138,6 +166,15 @@ public class GenericsGeneratorUtility {
return replRes; return replRes;
} }
public static NameReplacementResult modifyNames(List<TPHConstraint> allCons, ConstraintsWithSameLeftSide list, List<MethodAndTPH> methodAndTPHs, List<String> tphsClass) {
// generate a new name and replace the right hand side for each constraint
// in list with the new name
NameReplacer replacer = new NameReplacer(list.getConstraints(), allCons, methodAndTPHs, tphsClass);
// new name -> [all old names]
NameReplacementResult replRes = replacer.replaceNames();
return replRes;
}
public static void updateEqualCons(NameReplacementResult nameReplacementResult, List<TPHConstraint> eqCons) { public static void updateEqualCons(NameReplacementResult nameReplacementResult, List<TPHConstraint> eqCons) {
List<String> oldNames = nameReplacementResult.getOldNames(); List<String> oldNames = nameReplacementResult.getOldNames();
String newName = nameReplacementResult.getName(); String newName = nameReplacementResult.getName();
@ -190,7 +227,7 @@ public class GenericsGeneratorUtility {
List<NameReplacementResult> nameReplacementResults) { List<NameReplacementResult> nameReplacementResults) {
nameReplacementResults.forEach(n->{ nameReplacementResults.forEach(n->{
Set<String> equals = new HashSet<>(n.getOldNames()); Set<String> equals = new HashSet<>(n.getOldNames());
TPHConstraint cons = new EqualConstraint(n.getName(), Type.getInternalName(Object.class), Relation.EXTENDS); TPHConstraint cons = new ExtendsConstraint(n.getName(), Type.getInternalName(Object.class));
GenericsGeneratorResult ggRes = new GenericsGeneratorResult(cons, equals); GenericsGeneratorResult ggRes = new GenericsGeneratorResult(cons, equals);
genericsGeneratorResults.add(ggRes); genericsGeneratorResults.add(ggRes);
}); });
@ -204,4 +241,31 @@ public class GenericsGeneratorUtility {
public static List<TPHConstraint> getEqualConstraints(List<TPHConstraint> allCons) { public static List<TPHConstraint> getEqualConstraints(List<TPHConstraint> allCons) {
return allCons.stream().filter(c->c.getRel()==Relation.EQUAL).collect(Collectors.toList()); return allCons.stream().filter(c->c.getRel()==Relation.EQUAL).collect(Collectors.toList());
} }
public static List<NameReplacementResult> createNameReplacementResultsFromEqualConstraints(
List<TPHConstraint> equalCons) {
List<NameReplacementResult> nameReplacementResults = new ArrayList<>();
equalCons.forEach(c->{
if(isInNameReplacementResults(nameReplacementResults,c.getRight())) {
getNameReplacementByName(nameReplacementResults,c.getRight()).getOldNames().add(c.getLeft());
} else {
List<String> oldNames = new ArrayList<>();
oldNames.add(c.getLeft());
NameReplacementResult nrRes = new NameReplacementResult(c.getRight(), oldNames);
nameReplacementResults.add(nrRes);
}
});
return nameReplacementResults;
}
private static NameReplacementResult getNameReplacementByName(List<NameReplacementResult> nameReplacementResults,
String name) {
return nameReplacementResults.stream().filter(n->n.getName().equals(name)).findFirst().get();
}
public static boolean isInNameReplacementResults(List<NameReplacementResult> nameReplacementResults,String name){
if(nameReplacementResults.isEmpty())
return false;
return nameReplacementResults.stream().map(r->r.getName()).filter(n->name.equals(n)).findFirst().isPresent();
}
} }

View File

@ -258,7 +258,7 @@ public class Signature {
if(resolved instanceof TypePlaceholder) { if(resolved instanceof TypePlaceholder) {
//resType.getAdditionalGenerics().forEach(ag ->{ //resType.getAdditionalGenerics().forEach(ag ->{
resultSet.genIns.forEach(ag ->{ resultSet.genIns.forEach(ag ->{
TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName(), Relation.EXTENDS); TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName());
if(!contains(res,constr)) { if(!contains(res,constr)) {
res.add(constr); res.add(constr);
} }
@ -270,7 +270,7 @@ public class Signature {
if(resType2.resolvedType instanceof TypePlaceholder) { if(resType2.resolvedType instanceof TypePlaceholder) {
//resType2.getAdditionalGenerics().forEach(ag ->{ //resType2.getAdditionalGenerics().forEach(ag ->{
resultSet.genIns.forEach(ag ->{ resultSet.genIns.forEach(ag ->{
TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName(), Relation.EXTENDS); TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName());
if(!contains(res,constr)) { if(!contains(res,constr)) {
res.add(constr); res.add(constr);
} }

View File

@ -0,0 +1,53 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
import java.util.List;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult;
/**
* This class represents the result of the constraints simplifier.
*
* @author fayez
*
*/
public class ConstraintsSimplierResult {
private List<GenericsGeneratorResult> genericsGenResults;
private List<NameReplacementResult> nameReplacementResults;
/**
* @param genericsGenResults
*/
public ConstraintsSimplierResult(List<GenericsGeneratorResult> genericsGenResults,
List<NameReplacementResult> nameReplacementResults) {
this.genericsGenResults = genericsGenResults;
this.setNameReplacementResults(nameReplacementResults);
}
/**
* @return the genericsGenResults
*/
public List<GenericsGeneratorResult> getGenericsGenResults() {
return genericsGenResults;
}
/**
* @param genericsGenResults the genericsGenResults to set
*/
public void setGenericsGenResults(List<GenericsGeneratorResult> genericsGenResults) {
this.genericsGenResults = genericsGenResults;
}
/**
* @return the nameReplacementResults
*/
public List<NameReplacementResult> getNameReplacementResults() {
return nameReplacementResults;
}
/**
* @param nameReplacementResults the nameReplacementResults to set
*/
public void setNameReplacementResults(List<NameReplacementResult> nameReplacementResults) {
this.nameReplacementResults = nameReplacementResults;
}
}

View File

@ -10,8 +10,11 @@ import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
public class NameReplacer { public class NameReplacer {
//TODO rename
private List<TPHConstraint> constraints; private List<TPHConstraint> constraints;
private List<TPHConstraint> allConstraints; private List<TPHConstraint> allConstraints;
private List<MethodAndTPH> methodAndTPHs;
// TODO rename into tphClass
private List<String> tphs; private List<String> tphs;
private List<String> localTphs; private List<String> localTphs;
@ -30,11 +33,44 @@ public class NameReplacer {
this.tphs = tphs; this.tphs = tphs;
} }
public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints) {
this.constraints = constraints;
this.allConstraints = allConstraints;
}
public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints, List<MethodAndTPH> methodAndTPHs,
List<String> tphsClass) {
this.constraints = constraints;
this.allConstraints = allConstraints;
this.methodAndTPHs = methodAndTPHs;
this.tphs = tphsClass;
}
public NameReplacementResult replaceNames() { public NameReplacementResult replaceNames() {
String newName = NameGenerator.makeNewName(); String newName = NameGenerator.makeNewName();
List<String> names = new ArrayList<>(); List<String> names = new ArrayList<>();
substituteRightSidesWithNewName(newName, names); substituteRightSidesWithNewName(newName, names);
substituteNamesInAllConstraints(newName, names);
methodAndTPHs.stream().map(m->m.getTphs()).forEach(tphsMethod->{
if(tphsMethod.removeAll(names))
tphsMethod.add(newName);
});
if(tphs.removeAll(names))
tphs.add(newName);
NameReplacementResult res = new NameReplacementResult(newName, names);
return res;
}
public NameReplacementResult replaceNamesLocal() {
String newName = NameGenerator.makeNewName();
List<String> names = new ArrayList<>();
substituteRightSidesWithNewName(newName, names);
substituteNamesInAllConstraints(newName, names); substituteNamesInAllConstraints(newName, names);
tphs.removeAll(names); tphs.removeAll(names);

View File

@ -162,8 +162,7 @@ public class Simplify {
eq.add(t); eq.add(t);
} }
// generate a new constraint (left < Object) // generate a new constraint (left < Object)
TPHConstraint constraint = new ExtendsConstraint(left, Type.getInternalName(Object.class), TPHConstraint constraint = new ExtendsConstraint(left, Type.getInternalName(Object.class));
Relation.EXTENDS);
// put the generated constraint and its equal set into result set // put the generated constraint and its equal set into result set
result.put(constraint, eq); result.put(constraint, eq);
constraints.clear(); constraints.clear();
@ -217,8 +216,7 @@ public class Simplify {
if (!result.containsKey(cons)) { if (!result.containsKey(cons)) {
result.put(cons, null); result.put(cons, null);
if (!cons.getRight().equals(Type.getInternalName(Object.class))) if (!cons.getRight().equals(Type.getInternalName(Object.class)))
result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class), result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class)), null);
Relation.EXTENDS), null);
} }
return result; return result;
@ -248,7 +246,7 @@ public class Simplify {
// create an equal constraint for each value in repres // create an equal constraint for each value in repres
String key = replRes.getName(); String key = replRes.getName();
for (String val : replRes.getOldNames()) { for (String val : replRes.getOldNames()) {
EqualConstraint ec = new EqualConstraint(val, key, Relation.EQUAL); EqualConstraint ec = new EqualConstraint(val, key);
eqCons.add(ec); eqCons.add(ec);
} }
for (TPHConstraint c : allCons) { for (TPHConstraint c : allCons) {
@ -391,18 +389,18 @@ public class Simplify {
if (!containTPH(methodTphs, superTphRes)) { if (!containTPH(methodTphs, superTphRes)) {
HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, superTphRes); HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, superTphRes);
if (tphsClass.contains(superTphRes)/*classTPHSContainsTPH(tphsClass, superTphRes)*/) { if (tphsClass.contains(superTphRes)/*classTPHSContainsTPH(tphsClass, superTphRes)*/) {
result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), equals); result.put(new ExtendsConstraint(subTphRes, superTphRes), equals);
} else { } else {
result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class)),
equals); equals);
} }
} else { } else {
HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, subTphRes); HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, subTphRes);
result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), equals); result.put(new ExtendsConstraint(subTphRes, superTphRes), equals);
if (!isTPHInConstraint(result, superTphRes) && !isTphInEqualSet(result, superTphRes)) { if (!isTPHInConstraint(result, superTphRes) && !isTphInEqualSet(result, superTphRes)) {
HashSet<String> equals2 = getEqualsTphsFromEqualCons(eqCons, superTphRes); HashSet<String> equals2 = getEqualsTphsFromEqualCons(eqCons, superTphRes);
result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class)),
equals2); equals2);
} }
} }
@ -412,7 +410,7 @@ public class Simplify {
for (String tph : localTphs) { for (String tph : localTphs) {
if (!isTPHInConstraint(result, tph) && !isTphInEqualSet(result, tph)) { if (!isTPHInConstraint(result, tph) && !isTphInEqualSet(result, tph)) {
HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, tph); HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, tph);
result.put(new ExtendsConstraint(tph, Type.getInternalName(Object.class), Relation.EXTENDS), equals); result.put(new ExtendsConstraint(tph, Type.getInternalName(Object.class)), equals);
} }
} }
@ -583,8 +581,7 @@ public class Simplify {
eq.add(t); eq.add(t);
} }
// generate a new constraint (left < Object) // generate a new constraint (left < Object)
TPHConstraint constraint = new ExtendsConstraint(left, Type.getInternalName(Object.class), TPHConstraint constraint = new ExtendsConstraint(left, Type.getInternalName(Object.class));
Relation.EXTENDS);
// put the generated constraint and its equal set into result set // put the generated constraint and its equal set into result set
result.put(constraint, eq); result.put(constraint, eq);
constraints.clear(); constraints.clear();
@ -619,7 +616,7 @@ public class Simplify {
TPHConstraint cons = allCons.get(0); TPHConstraint cons = allCons.get(0);
if (!result.containsKey(cons)) { if (!result.containsKey(cons)) {
result.put(cons, null); result.put(cons, null);
result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS), result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class)),
null); null);
} }
@ -699,7 +696,7 @@ public class Simplify {
subTphRes = tphInRel.getFirst(); subTphRes = tphInRel.getFirst();
HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, superTphRes); HashSet<String> equals = getEqualsTphsFromEqualCons(eqCons, superTphRes);
result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), equals); result.put(new ExtendsConstraint(subTphRes, superTphRes), equals);
} }
@ -754,7 +751,7 @@ public class Simplify {
substituteTPH(allCons, toSubs, tph); substituteTPH(allCons, toSubs, tph);
if (subAndSuper.containsKey(toSubs) && subAndSuper.containsKey(tph)) { if (subAndSuper.containsKey(toSubs) && subAndSuper.containsKey(tph)) {
toSubs = subAndSuper.remove(toSubs); toSubs = subAndSuper.remove(toSubs);
EqualConstraint eq = new EqualConstraint(subAndSuper.get(tph), toSubs, Relation.EQUAL); EqualConstraint eq = new EqualConstraint(subAndSuper.get(tph), toSubs);
eqCons.add(eq); eqCons.add(eq);
substituteInMap(subAndSuper, allCons, eqCons, toSubs, subAndSuper.get(tph)); substituteInMap(subAndSuper, allCons, eqCons, toSubs, subAndSuper.get(tph));
} else if (subAndSuper.containsKey(toSubs) && !subAndSuper.containsKey(tph)) { } else if (subAndSuper.containsKey(toSubs) && !subAndSuper.containsKey(tph)) {
@ -947,11 +944,11 @@ public class Simplify {
i++; i++;
} }
if (superTphRes.equals(Type.getInternalName(Object.class))) { if (superTphRes.equals(Type.getInternalName(Object.class))) {
result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class)),
null); null);
} else { } else {
result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), null); result.put(new ExtendsConstraint(subTphRes, superTphRes), null);
result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class)),
null); null);
} }
@ -1023,7 +1020,7 @@ public class Simplify {
return c; return c;
} }
return new ExtendsConstraint(toFind.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS); return new ExtendsConstraint(toFind.getRight(), Type.getInternalName(Object.class));
} }
private static TPHConstraint getKeyConstraint(Map<TPHConstraint, Set<String>> result, private static TPHConstraint getKeyConstraint(Map<TPHConstraint, Set<String>> result,
@ -1034,7 +1031,7 @@ public class Simplify {
return c; return c;
} }
return new ExtendsConstraint(toFind.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS); return new ExtendsConstraint(toFind.getRight(), Type.getInternalName(Object.class));
} }
private static Set<String> getEqualsTPHs(Map<TPHConstraint, Set<String>> result, TPHConstraint toFind) { private static Set<String> getEqualsTPHs(Map<TPHConstraint, Set<String>> result, TPHConstraint toFind) {