Generic generator algorithm v0.1

This commit is contained in:
Fayez Abu Alia 2019-07-31 10:24:58 +02:00
parent 7a4bc32974
commit 87d0a46ba5

View File

@ -30,185 +30,190 @@ import de.dhbwstuttgart.syntaxtree.Method;
*/ */
public class GenericsGenerator { public class GenericsGenerator {
public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor, public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor,
final List<String> tphsClass, final ConstraintsSimplierResult simplifiedConstraints) { final List<String> tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
List<GenericsGeneratorResult> classConstraints = generateConstraintsForClass(tphExtractor, List<GenericsGeneratorResult> classConstraints = generateConstraintsForClass(tphExtractor,
simplifiedConstraints, tphsClass); simplifiedConstraints, tphsClass);
// GenericGeneratorResultsForAllMethods methodsAndConstraints = generateConstraintsForAllMethods(tphExtractor, // GenericGeneratorResultsForAllMethods methodsAndConstraints = generateConstraintsForAllMethods(tphExtractor,
// simplifiedConstraints, tphsClass); // simplifiedConstraints, tphsClass);
GenericGeneratorResultsForAllMethods methodsAndConstraints = new GenericGeneratorResultsForAllMethods(new ArrayList<>()); GenericGeneratorResultsForAllMethods methodsAndConstraints = new GenericGeneratorResultsForAllMethods(new ArrayList<>());
return new GenericsGeneratorResultForClass(className, classConstraints, methodsAndConstraints); return new GenericsGeneratorResultForClass(className, classConstraints, methodsAndConstraints);
} }
/** /**
* @param tphExtractor * @param tphExtractor
* @param tphsClass * @param tphsClass
* @return * @return
*/ */
public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor, public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor,
final List<String> tphsClass) { final List<String> tphsClass) {
final List<GenericsGeneratorResult> genericsGeneratorResults = new ArrayList<>(); final List<GenericsGeneratorResult> genericsGeneratorResults = new ArrayList<>();
ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor, ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor,
genericsGeneratorResults, tphsClass); genericsGeneratorResults, tphsClass);
return simplifiedConstraints; return simplifiedConstraints;
} }
private static List<GenericsGeneratorResult> generateConstraintsForClass(final TPHExtractor tphExtractor, private static List<GenericsGeneratorResult> generateConstraintsForClass(final TPHExtractor tphExtractor,
final ConstraintsSimplierResult simplifiedConstraints, final List<String> tphsClass) { final ConstraintsSimplierResult simplifiedConstraints, final List<String> tphsClass) {
final List<GenericsGeneratorResult> constraints = new ArrayList<>(); final List<GenericsGeneratorResult> constraints = new ArrayList<>();
if(tphsClass.isEmpty()) if (tphsClass.isEmpty())
return constraints; return constraints;
final List<TPHConstraint> allCons = tphExtractor.allCons; final List<TPHConstraint> allCons = tphExtractor.allCons;
final Set<String> visitedTPHs = new HashSet<>(); final Set<String> visitedTPHs = new HashSet<>();
final List<String> methodTPHs = tphExtractor.ListOfMethodsAndTph.stream().map(m -> m.getLocalTphs()) final List<String> methodTPHs = tphExtractor.ListOfMethodsAndTph.stream().map(m -> m.getLocalTphs())
.flatMap(l -> l.stream()).collect(Collectors.toList()); .flatMap(l -> l.stream()).collect(Collectors.toList());
for (String tph : tphsClass) {
if (visitedTPHs.contains(tph)) createConstraintsForClassTphs(simplifiedConstraints, tphsClass, constraints, allCons, visitedTPHs, methodTPHs);
continue;
final LinkedList<String> tphsInRel = GenericsGeneratorUtility GenericsGeneratorUtility.addTPHsToClassTPHs(constraints, tphsClass);
.createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph); GenericsGeneratorUtility.removeClassTPHsFromMethodTPHs(tphsClass, tphExtractor);
if(!tphsInRel.isEmpty()) {
GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass);
constraints.add(constraint);
}
}
GenericsGeneratorUtility.addTPHsToClassTPHs(constraints, tphsClass); generateGGConstraintsForTPH(tphsClass, constraints, simplifiedConstraints);
GenericsGeneratorUtility.removeClassTPHsFromMethodTPHs(tphsClass, tphExtractor);
generateGGConstraintsForTPH(tphsClass, constraints, simplifiedConstraints); return constraints;
}
return constraints; private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass, List<GenericsGeneratorResult> constraints, List<TPHConstraint> allCons, Set<String> visitedTPHs, List<String> methodTPHs) {
} for (String tph : tphsClass) {
private static GenericsGeneratorResult generateGGResultForClass(LinkedList<String> tphsInRel, if (visitedTPHs.contains(tph))
ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) { continue;
String subType = tphsInRel.getFirst();
String superType = GenericsGeneratorUtility.getNextClassTph(tphsClass, tphsInRel); final LinkedList<String> tphsInRel = GenericsGeneratorUtility
.createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph);
if (!tphsInRel.isEmpty()) {
GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass);
constraints.add(constraint);
}
}
}
TPHConstraint constraint = new ExtendsConstraint(subType, superType); private static GenericsGeneratorResult generateGGResultForClass(LinkedList<String> tphsInRel,
ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
String subType = tphsInRel.getFirst();
Set<String> equalSet = GenericsGeneratorUtility String superType = GenericsGeneratorUtility.getNextClassTph(tphsClass, tphsInRel);
.createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
return new GenericsGeneratorResult(constraint, equalSet); TPHConstraint constraint = new ExtendsConstraint(subType, superType);
}
private static GenericGeneratorResultsForAllMethods generateConstraintsForAllMethods( Set<String> equalSet = GenericsGeneratorUtility
final TPHExtractor tphExtractor, ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) { .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
final List<MethodAndConstraints> methodsAndConstraints = new ArrayList<>(); return new GenericsGeneratorResult(constraint, equalSet);
final GenericGeneratorResultsForAllMethods results = new GenericGeneratorResultsForAllMethods( }
methodsAndConstraints);
tphExtractor.ListOfMethodsAndTph.forEach(m -> {
MethodAndConstraints methAndCons = generateConstraintsForMethod(tphExtractor.allCons, m,
simplifiedConstraints, tphsClass);
methodsAndConstraints.add(methAndCons);
});
return results; private static GenericGeneratorResultsForAllMethods generateConstraintsForAllMethods(
} final TPHExtractor tphExtractor, ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
public static MethodAndConstraints generateConstraintsForMethod(final List<TPHConstraint> allCons, final List<MethodAndConstraints> methodsAndConstraints = new ArrayList<>();
final MethodAndTPH m, final ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) { final GenericGeneratorResultsForAllMethods results = new GenericGeneratorResultsForAllMethods(
final List<String> localTphs = m.getLocalTphs(); methodsAndConstraints);
tphExtractor.ListOfMethodsAndTph.forEach(m -> {
MethodAndConstraints methAndCons = generateConstraintsForMethod(tphExtractor.allCons, m,
simplifiedConstraints, tphsClass);
methodsAndConstraints.add(methAndCons);
});
List<GenericsGeneratorResult> constraints = new ArrayList<>(); return results;
MethodAndConstraints result = new MethodAndConstraints(m.getId(), constraints); }
if(localTphs.isEmpty()) public static MethodAndConstraints generateConstraintsForMethod(final List<TPHConstraint> allCons,
return result; final MethodAndTPH m, final ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
final List<String> localTphs = m.getLocalTphs();
if(localTphs.size() == 1 && tphsClass.isEmpty()) { List<GenericsGeneratorResult> constraints = new ArrayList<>();
generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints); MethodAndConstraints result = new MethodAndConstraints(m.getId(), constraints);
return result;
}
final Set<String> visitedTPHs = new HashSet<>(); if (localTphs.isEmpty())
return result;
for (String tph : localTphs) { if (localTphs.size() == 1 && tphsClass.isEmpty()) {
generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints);
return result;
}
if (visitedTPHs.contains(tph)) final Set<String> visitedTPHs = new HashSet<>();
continue;
final LinkedList<String> tphsInRel = GenericsGeneratorUtility.createLinkedListForTPHsInRelation(allCons, for (String tph : localTphs) {
localTphs, tphsClass, visitedTPHs, tph);
if(!tphsInRel.isEmpty()) {
GenericsGeneratorUtility.removeNotLocalTphs(tphsInRel, localTphs, tphsClass);
if(!GenericsGeneratorUtility.isInResult(tphsInRel.getFirst(),constraints)) {
GenericsGeneratorResult constraint = generateGGResult(tphsInRel, simplifiedConstraints);
constraints.add(constraint);
}
}
}
generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints); if (visitedTPHs.contains(tph))
return result; continue;
}
private static void generateGGConstraintsForTPH(List<String> localTphs, final LinkedList<String> tphsInRel = GenericsGeneratorUtility.createLinkedListForTPHsInRelation(allCons,
final List<GenericsGeneratorResult> constraints, final ConstraintsSimplierResult simplifiedConstraints) { localTphs, tphsClass, visitedTPHs, tph);
if (!tphsInRel.isEmpty()) {
GenericsGeneratorUtility.removeNotLocalTphs(tphsInRel, localTphs, tphsClass);
if (!GenericsGeneratorUtility.isInResult(tphsInRel.getFirst(), constraints)) {
GenericsGeneratorResult constraint = generateGGResult(tphsInRel, simplifiedConstraints);
constraints.add(constraint);
}
}
}
localTphs.forEach(tph -> { generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints);
boolean isInSimplifiedConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph); return result;
if(isInSimplifiedConstraints) { }
GenericsGeneratorResult ggResult = GenericsGeneratorUtility.getGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),tph).get();
private static void generateGGConstraintsForTPH(List<String> localTphs,
final List<GenericsGeneratorResult> constraints, final ConstraintsSimplierResult simplifiedConstraints) {
localTphs.forEach(tph -> {
boolean isInSimplifiedConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph);
if (isInSimplifiedConstraints) {
GenericsGeneratorResult ggResult = GenericsGeneratorUtility.getGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph).get();
// GenericsGeneratorUtility.removeGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),ggResult); // GenericsGeneratorUtility.removeGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),ggResult);
constraints.add(ggResult); constraints.add(ggResult);
} else { } else {
boolean isInConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(constraints, tph); boolean isInConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(constraints, tph);
if (!isInConstraints) { if (!isInConstraints) {
GenericsGeneratorResult ggResult = generateGGResult(tph, simplifiedConstraints.getNameReplacementResults()); GenericsGeneratorResult ggResult = generateGGResult(tph, simplifiedConstraints.getNameReplacementResults());
constraints.add(ggResult); constraints.add(ggResult);
} }
} }
}); });
} }
private static GenericsGeneratorResult generateGGResult(String tph, private static GenericsGeneratorResult generateGGResult(String tph,
List<NameReplacementResult> nameReplacementResults) { List<NameReplacementResult> nameReplacementResults) {
String superType = Type.getInternalName(Object.class); String superType = Type.getInternalName(Object.class);
TPHConstraint constraint = new ExtendsConstraint(tph, superType); TPHConstraint constraint = new ExtendsConstraint(tph, superType);
Set<String> equalSet = GenericsGeneratorUtility.createEqualSet(nameReplacementResults, tph); Set<String> equalSet = GenericsGeneratorUtility.createEqualSet(nameReplacementResults, tph);
return new GenericsGeneratorResult(constraint, equalSet); return new GenericsGeneratorResult(constraint, equalSet);
} }
private static GenericsGeneratorResult generateGGResult(final LinkedList<String> tphsInRel, private static GenericsGeneratorResult generateGGResult(final LinkedList<String> tphsInRel,
final ConstraintsSimplierResult simplifiedConstraints) { final ConstraintsSimplierResult simplifiedConstraints) {
String subType = tphsInRel.getFirst(); String subType = tphsInRel.getFirst();
if (tphsInRel.size() == 1) { if (tphsInRel.size() == 1) {
Optional<GenericsGeneratorResult> constraintFromSimplifiedCon = simplifiedConstraints Optional<GenericsGeneratorResult> constraintFromSimplifiedCon = simplifiedConstraints
.getGenericsGenResults().stream().filter(g -> g.getConstraint().getLeft().equals(subType)) .getGenericsGenResults().stream().filter(g -> g.getConstraint().getLeft().equals(subType))
.findFirst(); .findFirst();
if (constraintFromSimplifiedCon.isPresent()) if (constraintFromSimplifiedCon.isPresent())
return constraintFromSimplifiedCon.get(); return constraintFromSimplifiedCon.get();
} }
String superType = tphsInRel.size() > 1 ? tphsInRel.getLast() : Type.getInternalName(Object.class); String superType = tphsInRel.size() > 1 ? tphsInRel.getLast() : Type.getInternalName(Object.class);
TPHConstraint constraint = new ExtendsConstraint(subType, superType); TPHConstraint constraint = new ExtendsConstraint(subType, superType);
Set<String> equalSet = GenericsGeneratorUtility Set<String> equalSet = GenericsGeneratorUtility
.createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
return new GenericsGeneratorResult(constraint, equalSet); return new GenericsGeneratorResult(constraint, equalSet);
} }
} }