From 87d0a46ba53c260bbc09d6d52fa69f574e711b8f Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Jul 2019 10:24:58 +0200 Subject: [PATCH] Generic generator algorithm v0.1 --- .../genericsGenerator/GenericsGenerator.java | 291 +++++++++--------- 1 file changed, 148 insertions(+), 143 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java index 1e8f9f6e..28c317ed 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java @@ -1,5 +1,5 @@ /** - * + * */ package de.dhbwstuttgart.bytecode.genericsGenerator; @@ -29,186 +29,191 @@ import de.dhbwstuttgart.syntaxtree.Method; * */ public class GenericsGenerator { - - public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor, - final List tphsClass, final ConstraintsSimplierResult simplifiedConstraints) { - List classConstraints = generateConstraintsForClass(tphExtractor, - simplifiedConstraints, tphsClass); + public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor, + final List tphsClass, final ConstraintsSimplierResult simplifiedConstraints) { + + List classConstraints = generateConstraintsForClass(tphExtractor, + simplifiedConstraints, tphsClass); // GenericGeneratorResultsForAllMethods methodsAndConstraints = generateConstraintsForAllMethods(tphExtractor, // simplifiedConstraints, tphsClass); - GenericGeneratorResultsForAllMethods methodsAndConstraints = new GenericGeneratorResultsForAllMethods(new ArrayList<>()); - - return new GenericsGeneratorResultForClass(className, classConstraints, methodsAndConstraints); - } + GenericGeneratorResultsForAllMethods methodsAndConstraints = new GenericGeneratorResultsForAllMethods(new ArrayList<>()); + + return new GenericsGeneratorResultForClass(className, classConstraints, methodsAndConstraints); + } - /** - * @param tphExtractor - * @param tphsClass - * @return - */ - public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor, - final List tphsClass) { - final List genericsGeneratorResults = new ArrayList<>(); + /** + * @param tphExtractor + * @param tphsClass + * @return + */ + public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor, + final List tphsClass) { + final List genericsGeneratorResults = new ArrayList<>(); - ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor, - genericsGeneratorResults, tphsClass); - return simplifiedConstraints; - } - + ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor, + genericsGeneratorResults, tphsClass); + return simplifiedConstraints; + } - private static List generateConstraintsForClass(final TPHExtractor tphExtractor, - final ConstraintsSimplierResult simplifiedConstraints, final List tphsClass) { - final List constraints = new ArrayList<>(); - - if(tphsClass.isEmpty()) - return constraints; - - final List allCons = tphExtractor.allCons; - final Set visitedTPHs = new HashSet<>(); + private static List generateConstraintsForClass(final TPHExtractor tphExtractor, + final ConstraintsSimplierResult simplifiedConstraints, final List tphsClass) { + final List constraints = new ArrayList<>(); - final List methodTPHs = tphExtractor.ListOfMethodsAndTph.stream().map(m -> m.getLocalTphs()) - .flatMap(l -> l.stream()).collect(Collectors.toList()); - for (String tph : tphsClass) { + if (tphsClass.isEmpty()) + return constraints; - if (visitedTPHs.contains(tph)) - continue; + final List allCons = tphExtractor.allCons; - final LinkedList tphsInRel = GenericsGeneratorUtility - .createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph); - if(!tphsInRel.isEmpty()) { - GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass); - constraints.add(constraint); - } - } + final Set visitedTPHs = new HashSet<>(); - GenericsGeneratorUtility.addTPHsToClassTPHs(constraints, tphsClass); - GenericsGeneratorUtility.removeClassTPHsFromMethodTPHs(tphsClass, tphExtractor); + final List methodTPHs = tphExtractor.ListOfMethodsAndTph.stream().map(m -> m.getLocalTphs()) + .flatMap(l -> l.stream()).collect(Collectors.toList()); - generateGGConstraintsForTPH(tphsClass, constraints, simplifiedConstraints); + createConstraintsForClassTphs(simplifiedConstraints, tphsClass, constraints, allCons, visitedTPHs, methodTPHs); - return constraints; - } + GenericsGeneratorUtility.addTPHsToClassTPHs(constraints, tphsClass); + GenericsGeneratorUtility.removeClassTPHsFromMethodTPHs(tphsClass, tphExtractor); - private static GenericsGeneratorResult generateGGResultForClass(LinkedList tphsInRel, - ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { - String subType = tphsInRel.getFirst(); + generateGGConstraintsForTPH(tphsClass, constraints, simplifiedConstraints); - String superType = GenericsGeneratorUtility.getNextClassTph(tphsClass, tphsInRel); + return constraints; + } - TPHConstraint constraint = new ExtendsConstraint(subType, superType); + private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List tphsClass, List constraints, List allCons, Set visitedTPHs, List methodTPHs) { + for (String tph : tphsClass) { - Set equalSet = GenericsGeneratorUtility - .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); + if (visitedTPHs.contains(tph)) + continue; - return new GenericsGeneratorResult(constraint, equalSet); - } + final LinkedList tphsInRel = GenericsGeneratorUtility + .createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph); + if (!tphsInRel.isEmpty()) { + GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass); + constraints.add(constraint); + } + } + } - private static GenericGeneratorResultsForAllMethods generateConstraintsForAllMethods( - final TPHExtractor tphExtractor, ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { + private static GenericsGeneratorResult generateGGResultForClass(LinkedList tphsInRel, + ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { + String subType = tphsInRel.getFirst(); - final List methodsAndConstraints = new ArrayList<>(); - final GenericGeneratorResultsForAllMethods results = new GenericGeneratorResultsForAllMethods( - methodsAndConstraints); - tphExtractor.ListOfMethodsAndTph.forEach(m -> { - MethodAndConstraints methAndCons = generateConstraintsForMethod(tphExtractor.allCons, m, - simplifiedConstraints, tphsClass); - methodsAndConstraints.add(methAndCons); - }); + String superType = GenericsGeneratorUtility.getNextClassTph(tphsClass, tphsInRel); - return results; - } + TPHConstraint constraint = new ExtendsConstraint(subType, superType); - public static MethodAndConstraints generateConstraintsForMethod(final List allCons, - final MethodAndTPH m, final ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { - final List localTphs = m.getLocalTphs(); - - List constraints = new ArrayList<>(); - MethodAndConstraints result = new MethodAndConstraints(m.getId(), constraints); - - if(localTphs.isEmpty()) - return result; - - if(localTphs.size() == 1 && tphsClass.isEmpty()) { - generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints); - return result; - } - - final Set visitedTPHs = new HashSet<>(); - - for (String tph : localTphs) { + Set equalSet = GenericsGeneratorUtility + .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); - if (visitedTPHs.contains(tph)) - continue; + return new GenericsGeneratorResult(constraint, equalSet); + } - final LinkedList tphsInRel = GenericsGeneratorUtility.createLinkedListForTPHsInRelation(allCons, - 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); - } - } - } + private static GenericGeneratorResultsForAllMethods generateConstraintsForAllMethods( + final TPHExtractor tphExtractor, ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { - generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints); - return result; - } + final List methodsAndConstraints = new ArrayList<>(); + final GenericGeneratorResultsForAllMethods results = new GenericGeneratorResultsForAllMethods( + methodsAndConstraints); + tphExtractor.ListOfMethodsAndTph.forEach(m -> { + MethodAndConstraints methAndCons = generateConstraintsForMethod(tphExtractor.allCons, m, + simplifiedConstraints, tphsClass); + methodsAndConstraints.add(methAndCons); + }); - private static void generateGGConstraintsForTPH(List localTphs, - final List constraints, final ConstraintsSimplierResult simplifiedConstraints) { + return results; + } - localTphs.forEach(tph -> { - boolean isInSimplifiedConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph); - if(isInSimplifiedConstraints) { - GenericsGeneratorResult ggResult = GenericsGeneratorUtility.getGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),tph).get(); + public static MethodAndConstraints generateConstraintsForMethod(final List allCons, + final MethodAndTPH m, final ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { + final List localTphs = m.getLocalTphs(); + + List constraints = new ArrayList<>(); + MethodAndConstraints result = new MethodAndConstraints(m.getId(), constraints); + + if (localTphs.isEmpty()) + return result; + + if (localTphs.size() == 1 && tphsClass.isEmpty()) { + generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints); + return result; + } + + final Set visitedTPHs = new HashSet<>(); + + for (String tph : localTphs) { + + if (visitedTPHs.contains(tph)) + continue; + + final LinkedList tphsInRel = GenericsGeneratorUtility.createLinkedListForTPHsInRelation(allCons, + 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); + return result; + } + + private static void generateGGConstraintsForTPH(List localTphs, + final List 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); - constraints.add(ggResult); - } else { - boolean isInConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(constraints, tph); - - if (!isInConstraints) { - GenericsGeneratorResult ggResult = generateGGResult(tph, simplifiedConstraints.getNameReplacementResults()); - constraints.add(ggResult); - } - } - }); + constraints.add(ggResult); + } else { + boolean isInConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(constraints, tph); - } + if (!isInConstraints) { + GenericsGeneratorResult ggResult = generateGGResult(tph, simplifiedConstraints.getNameReplacementResults()); + constraints.add(ggResult); + } + } + }); - private static GenericsGeneratorResult generateGGResult(String tph, - List nameReplacementResults) { - String superType = Type.getInternalName(Object.class); - TPHConstraint constraint = new ExtendsConstraint(tph, superType); - Set equalSet = GenericsGeneratorUtility.createEqualSet(nameReplacementResults, tph); + } - return new GenericsGeneratorResult(constraint, equalSet); - } + private static GenericsGeneratorResult generateGGResult(String tph, + List nameReplacementResults) { + String superType = Type.getInternalName(Object.class); + TPHConstraint constraint = new ExtendsConstraint(tph, superType); + Set equalSet = GenericsGeneratorUtility.createEqualSet(nameReplacementResults, tph); - private static GenericsGeneratorResult generateGGResult(final LinkedList tphsInRel, - final ConstraintsSimplierResult simplifiedConstraints) { + return new GenericsGeneratorResult(constraint, equalSet); + } - String subType = tphsInRel.getFirst(); + private static GenericsGeneratorResult generateGGResult(final LinkedList tphsInRel, + final ConstraintsSimplierResult simplifiedConstraints) { - if (tphsInRel.size() == 1) { - Optional constraintFromSimplifiedCon = simplifiedConstraints - .getGenericsGenResults().stream().filter(g -> g.getConstraint().getLeft().equals(subType)) - .findFirst(); - if (constraintFromSimplifiedCon.isPresent()) - return constraintFromSimplifiedCon.get(); - } + String subType = tphsInRel.getFirst(); - String superType = tphsInRel.size() > 1 ? tphsInRel.getLast() : Type.getInternalName(Object.class); + if (tphsInRel.size() == 1) { + Optional constraintFromSimplifiedCon = simplifiedConstraints + .getGenericsGenResults().stream().filter(g -> g.getConstraint().getLeft().equals(subType)) + .findFirst(); + if (constraintFromSimplifiedCon.isPresent()) + return constraintFromSimplifiedCon.get(); + } - TPHConstraint constraint = new ExtendsConstraint(subType, superType); + String superType = tphsInRel.size() > 1 ? tphsInRel.getLast() : Type.getInternalName(Object.class); - Set equalSet = GenericsGeneratorUtility - .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); + TPHConstraint constraint = new ExtendsConstraint(subType, superType); - return new GenericsGeneratorResult(constraint, equalSet); - } + Set equalSet = GenericsGeneratorUtility + .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); + + return new GenericsGeneratorResult(constraint, equalSet); + } }