diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index 71d71f99..508bd520 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -3,13 +3,9 @@ package de.dhbwstuttgart.bytecode; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; import java.util.Optional; -import java.util.Set; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; @@ -18,22 +14,18 @@ import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError; -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString; import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; import de.dhbwstuttgart.bytecode.signature.Signature; import de.dhbwstuttgart.bytecode.signature.TypeToSignature; import de.dhbwstuttgart.bytecode.signature.TypeToString; -import de.dhbwstuttgart.bytecode.simplifyRes.SimplifyResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass; -import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; import de.dhbwstuttgart.bytecode.utilities.MethodUtility; import de.dhbwstuttgart.bytecode.utilities.NormalConstructor; import de.dhbwstuttgart.bytecode.utilities.NormalMethod; import de.dhbwstuttgart.bytecode.utilities.Resolver; -import de.dhbwstuttgart.bytecode.utilities.Simplify; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.ASTVisitor; @@ -80,7 +72,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typeinference.result.GenericInsertPair; import de.dhbwstuttgart.typeinference.result.ResultSet; public class BytecodeGen implements ASTVisitor { @@ -204,7 +195,7 @@ public class BytecodeGen implements ASTVisitor { List consClass = generatedGenerics.getClassConstraints(); // Signature signature = new Signature(classOrInterface, genericsAndBounds, consClass); - sig = signature.toString(); + sig = signature.createSignatureForClassOrInterface(); System.out.println("Signature: => " + sig); } @@ -219,12 +210,7 @@ public class BytecodeGen implements ASTVisitor { } for (Constructor c : classOrInterface.getConstructors()) { -// if(!isConsWithNoParamsVisited) { c.accept(this); -// } - -// if(!c.getParameterList().iterator().hasNext()) -// isConsWithNoParamsVisited = true; } for (Method m : classOrInterface.getMethods()) { @@ -237,12 +223,6 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(Constructor field) { - System.out.println("ResultSet: "); - resultSet.results.forEach(a -> { - System.out.println(a.getLeft().toString() + " = " + a.getRight().toString()); - }); - System.out.println("---------------"); - // stores generics and their bounds of method HashMap genericsAndBoundsMethod = new HashMap<>(); @@ -270,15 +250,10 @@ public class BytecodeGen implements ASTVisitor { String sig = null; if (hasGen) { List constraints = generatedGenerics.getClassConstraints(); -// Map> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes); -//// ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons); Signature signature = new Signature(genericsAndBounds, methodParamsAndTypes, resultSet, constraints); sig = signature.createSignatureForConstructor(field); -// Map> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes); -// Signature signature = new Signature(field, genericsAndBounds, methodParamsAndTypes, resultSet, constraints); -// sig = signature.toString(); } if (field.getParameterList().iterator().hasNext()) System.out.println(field.getParameterList().iterator().next().getType().acceptTV(new TypeToDescriptor())); @@ -363,8 +338,6 @@ public class BytecodeGen implements ASTVisitor { List constraints = generatedGenerics.getMethodConstraintsByID(id); List classConstraints = generatedGenerics.getClassConstraints(); -// Map> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes); -//// ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons); Signature signature = new Signature(genericsAndBoundsMethod, genericsAndBounds, methodParamsAndTypes, resultSet, constraints,classConstraints); sig = signature.createSignatureForMethod(method); @@ -373,7 +346,6 @@ public class BytecodeGen implements ASTVisitor { NormalMethod meth = new NormalMethod(method, genericsAndBounds, genericsAndBoundsMethod, hasGen); methDesc = meth.accept(new DescriptorToString(resultSet)); -// System.out.println(methDesc); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + acc, method.getName(), methDesc, sig, null); mv.visitCode(); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/ConstraintsSimplierResult.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplierResult.java similarity index 86% rename from src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/ConstraintsSimplierResult.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplierResult.java index f99cdf2a..5ed0bd5e 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/ConstraintsSimplierResult.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplierResult.java @@ -1,12 +1,12 @@ /** * */ -package de.dhbwstuttgart.bytecode.simplifyRes; +package de.dhbwstuttgart.bytecode.genericsGenerator; import java.util.List; -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.NameReplacementResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; /** * This class represents the result of the constraints simplifier. diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java index 525bab3c..b510c71b 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java @@ -7,8 +7,7 @@ import java.util.*; import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.simplifyRes.ConstraintsSimplierResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*; /** * @author fayez @@ -19,8 +18,7 @@ public class ConstraintsSimplifier { List allCons = tphExtractor.allCons; List equalCons = new ArrayList<>(); - List simpleCycles = findSimpleCycles(allCons); - + /* Step 1 */ List foundCons = GenericsGeneratorUtility.findConstraintsWithLeftSameLeftSide(allCons); if(!foundCons.isEmpty()) { @@ -34,10 +32,10 @@ public class ConstraintsSimplifier { // equalCons.addAll(GenericsGeneratorUtility.getEqualConstraints(allCons)); // } // - List longCycles = findLongCycles(allCons); + List cycles = findCycles(allCons); - if(!longCycles.isEmpty()) { - handleLongCycle(genericsGeneratorResults, allCons, longCycles,tphExtractor); + if(!cycles.isEmpty()) { + handleCycle(genericsGeneratorResults, allCons, cycles,tphExtractor); } /* The right hand side of equal constraint is the new name in name replacement result */ List nameReplacementResults = GenericsGeneratorUtility.createNameReplacementResultsFromEqualConstraints(equalCons); @@ -114,13 +112,13 @@ public class ConstraintsSimplifier { /** * @param genericsGeneratorResults * @param allCons - * @param longCycles + * @param cycles * @param tphExtractor */ - public static void handleLongCycle(List genericsGeneratorResults, - List allCons, List longCycles, TPHExtractor tphExtractor) { - GenericsGeneratorUtility.modifyRelationForConstraintsinLongCycle(longCycles); - List nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormLongCycle(allCons, longCycles,tphExtractor); + public static void handleCycle(List genericsGeneratorResults, + List allCons, List cycles, TPHExtractor tphExtractor) { + GenericsGeneratorUtility.modifyRelationForConstraintsInCycle(cycles); + List nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormCycle(allCons, cycles,tphExtractor); GenericsGeneratorUtility.createResults(genericsGeneratorResults,nameReplacementResults); GenericsGeneratorUtility.removeEqualConstraints(allCons); } @@ -129,33 +127,11 @@ public class ConstraintsSimplifier { * @param allCons * @return */ - public static List findLongCycles(List allCons) { - /* find all long cycles */ + public static List findCycles(List allCons) { + /* find all cycles */ CyclesFinder cyclesFinder = new CyclesFinder(allCons); - List longCycles = cyclesFinder.findLongCycles(); - return longCycles; + List cycles = cyclesFinder.findCycles(); + return cycles; } - /** - * @param allCons - * @param simpleCycles - * @param tphExtractor - */ - public static void handleSimpleCycles(List allCons, List simpleCycles, TPHExtractor tphExtractor) { - GenericsGeneratorUtility.modifyRelation(simpleCycles); - GenericsGeneratorUtility.removeAllReverseConstraints(allCons,simpleCycles); - GenericsGeneratorUtility.substituteTPH(allCons, simpleCycles); - GenericsGeneratorUtility.replaceOldNames(simpleCycles,tphExtractor); - } - - /** - * @param allCons - * @return - */ - public static List findSimpleCycles(List allCons) { - CyclesFinder cyclesFinder = new CyclesFinder(allCons); - /* find all simple cycles if they are exist */ - List simpleCycles = cyclesFinder.findSimpleCycles(); - return simpleCycles; - } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java index cf8c0885..9b0867f5 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java @@ -10,6 +10,7 @@ import java.util.Optional; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.Cycle; /** * @author fayez @@ -22,46 +23,22 @@ public class CyclesFinder { this.allCons = allCons; } - public List findSimpleCycles() { - List simpleCycles = new ArrayList<>(); - allCons.forEach(c -> { - String left = c.getLeft(); - String right = c.getRight(); - if (c.getRel() == Relation.EXTENDS && !containsInCycle(c, simpleCycles)) { - Optional revCon = GenericsGeneratorUtility.getReverseConstraint(allCons, left, right); - if (revCon.isPresent()) { - TPHConstraint reverseConstraint = revCon.get(); - SimpleCycle simpleCycle = new SimpleCycle(c, reverseConstraint); - simpleCycles.add(simpleCycle); - } - } - }); - - return simpleCycles; - } - - private boolean containsInCycle(TPHConstraint c, List simpleCycles) { - return simpleCycles.stream().filter(sc -> { - return sc.contains(c); - }).count() > 0; - } - - public List findLongCycles() { + public List findCycles() { List vistedConstraints = new ArrayList<>(allCons.size()); - List longCycles = new ArrayList<>(); + List cycles = new ArrayList<>(); for (TPHConstraint constraint : allCons) { if(constraint.getRel()==Relation.EQUAL) continue; if (!vistedConstraints.contains(constraint)) { vistedConstraints.add(constraint); - checkConstraint(constraint, vistedConstraints, longCycles); + checkConstraint(constraint, vistedConstraints, cycles); } } - return longCycles; + return cycles; } private void checkConstraint(TPHConstraint constraint, List vistedConstraints, - List longCycles) { + List cycles) { List tphsInRelation = new LinkedList<>(); List maybeCycle = new ArrayList<>(); maybeCycle.add(constraint); @@ -69,13 +46,13 @@ public class CyclesFinder { tphsInRelation.add(constraint.getRight()); Optional nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle); while (nextConstraint.isPresent()) { - if(containsInLongCycle(nextConstraint.get(), longCycles)) { + if(containsInLongCycle(nextConstraint.get(), cycles)) { break; } if (isCycle(tphsInRelation)) { addAllToVisitedConstraints(vistedConstraints, maybeCycle); - LongCycle cycle = new LongCycle(maybeCycle); - longCycles.add(cycle); + Cycle cycle = new Cycle(maybeCycle); + cycles.add(cycle); return; } nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle); @@ -84,8 +61,8 @@ public class CyclesFinder { // addAllToVisitedConstraints(vistedConstraints, maybeCycle); } - private boolean containsInLongCycle(TPHConstraint c, List longCycles) { - for(LongCycle cycle : longCycles) { + private boolean containsInLongCycle(TPHConstraint c, List cycles) { + for(Cycle cycle : cycles) { if(cycle.containConstraint(c)) return true; } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java similarity index 94% rename from src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index 29e1aad8..5e473ebe 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -1,32 +1,20 @@ /** * */ -package de.dhbwstuttgart.bytecode.simplifyRes; +package de.dhbwstuttgart.bytecode.genericsGenerator; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; import java.util.Optional; -import java.util.Set; -import de.dhbwstuttgart.bytecode.BytecodeGen; import de.dhbwstuttgart.bytecode.TPHExtractor; -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; -import de.dhbwstuttgart.bytecode.genericsGenerator.GenericsGenerator; -import de.dhbwstuttgart.bytecode.genericsGenerator.GenericsGeneratorUtility; -import de.dhbwstuttgart.bytecode.signature.Signature; -import de.dhbwstuttgart.bytecode.signature.TypeToSignature; -import de.dhbwstuttgart.bytecode.signature.TypeToString; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.MethodAndConstraints; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import de.dhbwstuttgart.bytecode.utilities.MethodUtility; import de.dhbwstuttgart.bytecode.utilities.Resolver; -import de.dhbwstuttgart.bytecode.utilities.Simplify; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; @@ -69,7 +57,6 @@ import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -148,14 +135,8 @@ public class GeneratedGenericsFinder implements ASTVisitor { if(!isVisited) { ggResult = GenericsGenerator.generateConstraints(className, tphExtractor, tphsClass,simplifiedConstraints); isVisited = true; - } else { - } -// for(Constructor m : classOrInterface.getConstructors()) { -// addMethodConstraints(simplifiedConstraints, ggResult, m); -// } - for(Method m : classOrInterface.getMethods()) { addMethodConstraints(simplifiedConstraints, ggResult, m); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java index 28c317ed..4b964d5c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java @@ -11,24 +11,24 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*; import org.objectweb.asm.Type; import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericGeneratorResultsForAllMethods; -import de.dhbwstuttgart.bytecode.simplifyRes.ConstraintsSimplierResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass; -import de.dhbwstuttgart.bytecode.simplifyRes.MethodAndConstraints; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; -import de.dhbwstuttgart.syntaxtree.Method; /** * @author fayez * */ public class GenericsGenerator { + /*TODO: When generating generics for a class if we have the following: + tphClass < tphMeth1 < tphMeth2 + then we have to convert tphMeth1, tphMeth2 to tph class and generate the following + class constraints: tphClass < tphMeth1, tphMeth1 < tphMeth2, tphMeth2 < Object + */ public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor, final List tphsClass, final ConstraintsSimplierResult simplifiedConstraints) { @@ -83,6 +83,9 @@ public class GenericsGenerator { } private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List tphsClass, List constraints, List allCons, Set visitedTPHs, List methodTPHs) { + List classAndMethodTphs = new ArrayList<>(tphsClass); + classAndMethodTphs.addAll(methodTPHs); + for (String tph : tphsClass) { if (visitedTPHs.contains(tph)) @@ -90,13 +93,46 @@ public class GenericsGenerator { final LinkedList tphsInRel = GenericsGeneratorUtility .createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph); - if (!tphsInRel.isEmpty()) { - GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass); - constraints.add(constraint); - } + + generateConstraintsForClassFromList(tphsInRel,simplifiedConstraints,classAndMethodTphs,constraints); } } + private static void generateConstraintsForClassFromList(LinkedList tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List classAndMethodTphs, List constraints) { + + if(tphsInRel.isEmpty()) + return; + + List resultConstraints = new ArrayList<>(); + String subType = tphsInRel.getFirst(); + String superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel); + + int idxOfSuper = tphsInRel.indexOf(superType); + int size = tphsInRel.size(); + while (size > 2 && idxOfSuper < size-1){ + GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType); + constraints.add(genericsGeneratorResult); + + tphsInRel = new LinkedList<>(tphsInRel.subList(idxOfSuper, size)); + subType = tphsInRel.getFirst(); + superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel); + + idxOfSuper = tphsInRel.indexOf(superType); + size = tphsInRel.size(); + } + GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType); + constraints.add(genericsGeneratorResult); + } + + private static GenericsGeneratorResult getGenericsGeneratorResultForClass(ConstraintsSimplierResult simplifiedConstraints, String subType, String superType) { + TPHConstraint constraint = new ExtendsConstraint(subType, superType); + + Set equalSet = GenericsGeneratorUtility + .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); + + return new GenericsGeneratorResult(constraint, equalSet); + } + /* TODO Remove this method*/ private static GenericsGeneratorResult generateGGResultForClass(LinkedList tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { String subType = tphsInRel.getFirst(); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java index 9db65374..f909734f 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java @@ -12,6 +12,7 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*; import org.objectweb.asm.Type; import de.dhbwstuttgart.bytecode.TPHExtractor; @@ -19,7 +20,6 @@ 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; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; import de.dhbwstuttgart.bytecode.utilities.ConstraintsFinder; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import de.dhbwstuttgart.bytecode.utilities.NameReplacer; @@ -74,21 +74,6 @@ public class GenericsGeneratorUtility { } - public static void modifyRelation(List simpleCycles) { - simpleCycles.forEach(sc->{ - sc.getConstraint().setRel(Relation.EQUAL); - sc.getReverseConstraint().setRel(Relation.EQUAL); - }); - } - - public static void removeAllReverseConstraints(List allCons, List simpleCycles) { - simpleCycles.forEach(sc->allCons.remove(sc.getReverseConstraint())); - } - - public static void substituteTPH(List allCons, List simpleCycles) { - simpleCycles.forEach(sc->substituteTPH(allCons, sc.getConstraint().getLeft(), sc.getConstraint().getRight())); - } - public static List findConstraintsWithLeftSameLeftSide(List allCons) { // finder looks for constraints that have the same left hand side // and put them in a list @@ -279,13 +264,13 @@ public class GenericsGeneratorUtility { } - public static void modifyRelationForConstraintsinLongCycle(List longCycles) { - longCycles.stream().map(lc->lc.getConstraints()).flatMap(e->e.stream()).forEach(c->c.setRel(Relation.EQUAL)); + public static void modifyRelationForConstraintsInCycle(List cycles) { + cycles.stream().map(lc->lc.getConstraints()).flatMap(e->e.stream()).forEach(c->c.setRel(Relation.EQUAL)); } - public static List substituteTPHSFormLongCycle(List allCons, List longCycles, TPHExtractor tphExtractor) { + public static List substituteTPHSFormCycle(List allCons, List cycles, TPHExtractor tphExtractor) { List results = new ArrayList<>(); - longCycles.forEach(lc->{ + cycles.forEach(lc->{ Set names = getNamesFromCycle(lc); String newName = names.stream().findFirst().get(); @@ -328,7 +313,7 @@ public class GenericsGeneratorUtility { }); } - public static Set getNamesFromCycle(LongCycle lc) { + public static Set getNamesFromCycle(Cycle lc) { Set names = new HashSet<>(); lc.getConstraints().forEach(c->names.add(c.getLeft())); return names; @@ -388,23 +373,6 @@ public class GenericsGeneratorUtility { return allCons.stream().filter(c->c.getRight().equals(tph)).findFirst(); } - public static void replaceOldNames(List simpleCycles, TPHExtractor tphExtractor) { - simpleCycles.forEach(sc -> { - Stream> tphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getTphs()); - Stream> localTphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getLocalTphs()); - - String newName = sc.getConstraint().getRight(); - String oldName = sc.getConstraint().getLeft(); - - List names = new ArrayList<>(); - names.add(oldName); - names.add(newName); - - replaceOldNames(newName, names, tphsOfMethods); - replaceOldNames(newName, names, localTphsOfMethods); - }); - } - public static boolean isTPHInGenericGeneratorResult(final List constraints, final String tph) { return constraints.stream().filter(g->g.getConstraint().getLeft().equals(tph)).findFirst().isPresent(); } @@ -576,6 +544,7 @@ public class GenericsGeneratorUtility { public static LinkedList createLinkedListForTPHsInRelationClass(List allCons, List tphsClass, List methodTPHs, Set visitedTPHs, String tph) { final LinkedList tphsInRel = new LinkedList<>(); + boolean isNextSuperTypeFound = findNextSuperTyp(allCons,tphsClass ,visitedTPHs, tph, tphsInRel); if(!tphsInRel.isEmpty() && !isNextSuperTypeFound && !methodTPHs.contains(tphsInRel.getLast())) findNextSubType(allCons, tphsClass,visitedTPHs, tph, tphsInRel); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/SimpleCycle.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/SimpleCycle.java deleted file mode 100644 index 6218e4a0..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/SimpleCycle.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGenerator; - -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; - -/** - * @author fayez - * - */ -public class SimpleCycle { - private TPHConstraint constraint; - private TPHConstraint reverseConstraint; - - /** - * @param constraint - * @param reverseConstraint - */ - public SimpleCycle(TPHConstraint constraint, TPHConstraint reverseConstraint) { - this.constraint = constraint; - this.reverseConstraint = reverseConstraint; - } - - /** - * @return the constraint - */ - public TPHConstraint getConstraint() { - return constraint; - } - - /** - * @param constraint the constraint to set - */ - public void setConstraint(TPHConstraint constraint) { - this.constraint = constraint; - } - - /** - * @return the reverseConstraint - */ - public TPHConstraint getReverseConstraint() { - return reverseConstraint; - } - - /** - * @param reverseConstraint the reverseConstraint to set - */ - public void setReverseConstraint(TPHConstraint reverseConstraint) { - this.reverseConstraint = reverseConstraint; - } - - public boolean contains(TPHConstraint c) { - if (constraint.getLeft().equals(c.getLeft()) && constraint.getRight().equals(c.getRight()) - && constraint.getRel().equals(c.getRel())) - return true; - if (reverseConstraint.getLeft().equals(c.getLeft()) && reverseConstraint.getRight().equals(c.getRight()) - && reverseConstraint.getRel().equals(c.getRel())) - return true; - - return false; - } - - @Override - public String toString() { - return constraint.toString() + " -> " + reverseConstraint.toString(); - } - -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/ConstraintsWithSameLeftSide.java similarity index 92% rename from src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/ConstraintsWithSameLeftSide.java index 93636cb7..1b184048 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/ConstraintsWithSameLeftSide.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.bytecode.genericsGenerator; +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.List; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/LongCycle.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/Cycle.java similarity index 85% rename from src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/LongCycle.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/Cycle.java index 717fea0c..ff6521d8 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/LongCycle.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/Cycle.java @@ -1,7 +1,7 @@ /** * */ -package de.dhbwstuttgart.bytecode.genericsGenerator; +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.List; @@ -11,13 +11,13 @@ import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; * @author fayez * */ -public class LongCycle { +public class Cycle { private final List constraints; /** * @param constraints */ - public LongCycle(List constraints) { + public Cycle(List constraints) { this.constraints = constraints; } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGeneratorResultsForAllMethods.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGeneratorResultsForAllMethods.java similarity index 87% rename from src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGeneratorResultsForAllMethods.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGeneratorResultsForAllMethods.java index 9798ad7f..40f7692a 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGeneratorResultsForAllMethods.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGeneratorResultsForAllMethods.java @@ -1,13 +1,11 @@ /** * */ -package de.dhbwstuttgart.bytecode.simplifyRes; +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.Collections; import java.util.List; -import com.google.common.collect.Collections2; - /** * @author fayez * diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java new file mode 100644 index 00000000..85d0bcbf --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java @@ -0,0 +1,51 @@ +/** + * + */ +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +/** + * The simplify results of a source file (package) + * + * @author fayez + * + */ +public class GenericGenratorResultForSourceFile { + private String pkgName; + private final List genericGeneratorResultForAllClasses = new ArrayList<>(); + + /** + * @param pkgName + */ + public GenericGenratorResultForSourceFile(String pkgName) { + this.pkgName = pkgName; + } + + public List getGenericGeneratorResultForAllClasses() { + return genericGeneratorResultForAllClasses; + } + + /** + * Appends the simplify results of a class to simplifyResForSF + * + * @param sResClass simplify results of a class to added + */ + public void addGenericGeneratorResultClass(GenericsGeneratorResultForClass sResClass) { + genericGeneratorResultForAllClasses.add(sResClass); + } + + public GenericsGeneratorResultForClass getSimplifyResultsByName(String pkgName, String name) { + + if (this.pkgName.equals(pkgName)) { + return genericGeneratorResultForAllClasses.stream() + .filter(sr -> sr.getClassName().equals(name)) + .findAny() + .orElseThrow(() -> new NoSuchElementException( + "Simplify results for the class " + pkgName + "." + name + " are not found")); + } + throw new NoSuchElementException("Simplify results for the class " + pkgName + "." + name + " are not found"); + } +} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResult.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResult.java similarity index 94% rename from src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResult.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResult.java index 7b7f8b2d..c0d1251a 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResult.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResult.java @@ -1,7 +1,7 @@ /** * */ -package de.dhbwstuttgart.bytecode.simplifyRes; +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.Set; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java new file mode 100644 index 00000000..d6532ecb --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java @@ -0,0 +1,67 @@ +/** + * + */ +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; + +import java.util.Collections; +import java.util.List; + +/** + * @author fayez + * + */ +public class GenericsGeneratorResultForClass { + private final String className; + private final List classConstraints; + private final GenericGeneratorResultsForAllMethods methodsAndTheirConstraints; + + public GenericsGeneratorResultForClass(String className) { + this(className, Collections.emptyList(), new GenericGeneratorResultsForAllMethods()); + } + /** + * @param className + * @param classConstraints + * @param methodsAndTheirConstraints + */ + public GenericsGeneratorResultForClass(String className, List classConstraints, + GenericGeneratorResultsForAllMethods methodsAndTheirConstraints) { + this.className = className; + this.classConstraints = classConstraints; + this.methodsAndTheirConstraints = methodsAndTheirConstraints; + } + + /** + * @return the className + */ + public String getClassName() { + return className; + } + + /** + * @return the classConstraints + */ + public List getClassConstraints() { + return classConstraints; + } + + /** + * @return the methodsAndTheirConstraints + */ + public GenericGeneratorResultsForAllMethods getMethodsAndTheirConstraints() { + return methodsAndTheirConstraints; + } + + public boolean contains(String id) { + return methodsAndTheirConstraints.getMethodsAndConstraints().stream().map(mc -> mc.getMethodID()) + .anyMatch(i -> i.equals(id)); + } + + + + + public List getMethodConstraintsByID(String id) { + return methodsAndTheirConstraints.getMethodsAndConstraints().stream().filter(mc -> mc.getMethodID().equals(id)) + .findFirst().get().getConstraints(); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/MethodAndConstraints.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/MethodAndConstraints.java similarity index 91% rename from src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/MethodAndConstraints.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/MethodAndConstraints.java index 1c647285..3f034f6d 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/MethodAndConstraints.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/MethodAndConstraints.java @@ -1,7 +1,7 @@ /** * */ -package de.dhbwstuttgart.bytecode.simplifyRes; +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.List; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/NameReplacementResult.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/NameReplacementResult.java similarity index 92% rename from src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/NameReplacementResult.java rename to src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/NameReplacementResult.java index f3c2e454..4a91655e 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/NameReplacementResult.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/NameReplacementResult.java @@ -1,7 +1,7 @@ /** * */ -package de.dhbwstuttgart.bytecode.genericsGenerator; +package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.List; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java index b92dc2c3..90c4ed20 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -6,12 +6,9 @@ import org.objectweb.asm.Type; import org.objectweb.asm.signature.SignatureVisitor; import org.objectweb.asm.signature.SignatureWriter; -import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; -import de.dhbwstuttgart.bytecode.utilities.Simplify; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; @@ -22,8 +19,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.syntaxtree.type.WildcardType; -import de.dhbwstuttgart.typeinference.result.ResolvedType; import de.dhbwstuttgart.typeinference.result.ResultSet; public class Signature { @@ -75,21 +70,18 @@ public class Signature { this.classOrInterface = classOrInterface; this.genericsAndBounds = genericsAndBounds; this.consClass = consClass; - createSignatureForClassOrInterface(); } public Signature(HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, HashMap methodParamsAndTypes, ResultSet resultSet, List constraints,List consClass) { - //this.method = method; this.genericsAndBoundsMethod = genericsAndBoundsMethod; this.genericsAndBounds = genericsAndBounds; this.methodParamsAndTypes = methodParamsAndTypes; this.resultSet = resultSet; this.constraints = constraints; this.consClass = consClass; - //createSignatureForMethod(this.method); } public String createSignatureForConstructor(Constructor constructor) { @@ -145,33 +137,19 @@ public class Signature { } private void createSignatureForFunN(int numberOfParams, String to, String[] paramTypes) { - for (int i = 0; i < numberOfParams; i++) { - int j = i + 1; - sw.visitFormalTypeParameter("T" + j); - // getBounds von Params - sw.visitClassBound().visitClassType(paramTypes[i]); - sw.visitClassBound().visitEnd(); - } + defineTypeVariablesForParametersOfFunN(numberOfParams); sw.visitFormalTypeParameter("R"); visitClassBound(to); // TODO: prüfe ob Return-Type = void, sw.visitSuperclass().visitClassType(Type.getInternalName(Object.class)); - ; sw.visitEnd(); } private void createSignatureForFunN(int numberOfParams) { -// sw.visitClassBound().visitEnd(); - for (int i = 0; i < numberOfParams; i++) { - int j = i + 1; - sw.visitFormalTypeParameter("T" + j); - // getBounds von Params - sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); - sw.visitClassBound().visitEnd(); - } + defineTypeVariablesForParametersOfFunN(numberOfParams); sw.visitFormalTypeParameter("R"); // getBounds vom Return-Type @@ -179,171 +157,17 @@ public class Signature { sw.visitClassBound().visitEnd(); // TODO: prüfe ob Return-Type = void, sw.visitSuperclass().visitClassType(Type.getInternalName(Object.class)); - ; sw.visitEnd(); } - /** - * Creates signature for a method or constructor with @see - * {@link SignatureWriter} Signature looks like: (params L.. OR T.. Or basistape)ReturnType - * - * @param method method or constructor - * @param isConstructor true if constructor - */ - private void createSignatureForConsOrMethod(Method method, boolean isConstructor) { - Iterator itr = method.getGenerics().iterator(); - // visits all formal type parameter and visits their bounds - while (itr.hasNext()) { - System.out.println("HAS GENERICS!!"); - GenericTypeVar g = itr.next(); - visitTypeVarsAndTheirBounds(g, genericsAndBoundsMethod); + private void defineTypeVariablesForParametersOfFunN(int numberOfParams) { + for (int i = 0; i < numberOfParams; i++) { + int j = i + 1; + sw.visitFormalTypeParameter("T" + j); + // getBounds von Params + sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); + sw.visitClassBound().visitEnd(); } - - // Wenn die RückgabeType eine TPH ist, wird als generic behandelt - // z.B: Type = TPH K => wird eine Formal Type Parameter K$ erzeugt und Bound = - // Object - if (!isConstructor) { - String ret = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()); - ArrayList allConsBeforeSimplify = new ArrayList<>(); - - Map> allConstraints = new HashMap<>(); - - if (ret.contains("<")) { - allConsBeforeSimplify = getAllConstraints( - (RefType) resultSet.resolveType(method.getReturnType()).resolvedType); - } - - for (String paramName : methodParamsAndTypes.keySet()) { - RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName); - String pT = t.acceptTV(new TypeToSignature()); - - if (pT.contains("<")) - allConsBeforeSimplify = getAllConstraints((RefType) t); - } - boolean doSimplify = false; - if (!allConsBeforeSimplify.isEmpty()) { - addConstraintsToMap(allConstraints, allConsBeforeSimplify); - doSimplify = true; - } - createTypeVars(allConstraints, doSimplify); - - if (!ret.equals("V")) { - if (ret.contains(SPECIAL_CHAR) && !ret.contains(SPECIAL_CHAR_FOR_FUN) && !ret.contains("<")) { - if (genericsAndBounds.containsKey(ret)) { - genericsAndBoundsMethod.put(ret.substring(1), genericsAndBounds.get(ret.substring(1))); - } - } - - } - } - - visitParams(); - if (isConstructor || resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()) - .equals("V")) { - sw.visitReturnType().visitBaseType('V'); - } else { - RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); - // return type deswegen ist false -// doVisitParamsOrReturn(returnType, false); - } -// sw.visitEnd(); - } - - private void addConstraintsToMap(Map> allConstraints, - ArrayList allConsBeforeSimplify) { - for (TPHConstraint tphCons : allConsBeforeSimplify) { - allConstraints.put(tphCons, null); - } - } - - private String getEqualTPH(Map> methodConstraints2, String tph) { - for (TPHConstraint cons : methodConstraints2.keySet()) { - if (methodConstraints2.get(cons) != null) { - if (methodConstraints2.get(cons).contains(tph)) { - return cons.getLeft(); - } - } - } - return tph; - } - - private void createTypeVars(Map> allConstraints, boolean doSimplify) { - allConstraints.putAll(methodConstraints); - - Map> simplifiedConstraints; - if (doSimplify) { - simplifiedConstraints = Simplify.simplifyContraints(allConstraints); - } else { - simplifiedConstraints = allConstraints; - } - - for (TPHConstraint cons : simplifiedConstraints.keySet()) { - // need subst. - String sub = cons.getLeft() + SPECIAL_CHAR; - if (!genericsAndBoundsMethod.containsKey(sub) && !genericsAndBounds.containsKey(sub)) { - sw.visitFormalTypeParameter(sub); - String bound = cons.getRight(); - if (bound.equals(Type.getInternalName(Object.class))) { - sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); - sw.visitClassBound().visitEnd(); - genericsAndBoundsMethod.put(sub, bound); - } else { - String boundName = bound + SPECIAL_CHAR; - sw.visitClassBound().visitTypeVariable(boundName); - genericsAndBoundsMethod.put(sub, boundName); - } - } - - } - } - - private ArrayList getAllConstraints(RefType ref) { - final ArrayList res = new ArrayList<>(); - for (RefTypeOrTPHOrWildcardOrGeneric p : ref.getParaList()) { - ResolvedType resType; - if (p instanceof WildcardType) { - resType = resultSet.resolveType(((WildcardType) p).getInnerType()); - } else { - resType = resultSet.resolveType(p); - } - - RefTypeOrTPHOrWildcardOrGeneric resolved = resType.resolvedType; - if (resolved instanceof TypePlaceholder) { - // resType.getAdditionalGenerics().forEach(ag ->{ - resultSet.genIns.forEach(ag -> { - TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName()); - if (!contains(res, constr)) { - res.add(constr); - } - }); - } - if (resolved instanceof WildcardType) { - WildcardType resWC = (WildcardType) resolved; - ResolvedType resType2 = resultSet.resolveType(resWC.getInnerType()); - if (resType2.resolvedType instanceof TypePlaceholder) { - // resType2.getAdditionalGenerics().forEach(ag ->{ - resultSet.genIns.forEach(ag -> { - TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName()); - if (!contains(res, constr)) { - res.add(constr); - } - }); - } - } - - } - System.out.println("RES GIP === " + res.size()); - return res; - } - - private boolean contains(ArrayList constraints, TPHConstraint constr) { - for (int i = 0; i < constraints.size(); ++i) { - TPHConstraint p = constraints.get(i); - if (constr.getLeft().equals(p.getLeft()) && constr.getRight().equals(p.getRight())) - return true; - } - return false; } /** @@ -363,8 +187,7 @@ public class Signature { break; case "GRT": GenericRefType g = (GenericRefType) t; -// sv.visitTypeVariable(g.acceptTV(new TypeToSignature()).substring(1)); - sv.visitTypeVariable(g.acceptTV(new TypeToSignature())); + sv.visitTypeVariable(g.acceptTV(new TypeToSignature(constraints))); break; case "TPH": RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType; @@ -375,13 +198,12 @@ public class Signature { // das braucht man nicht es reicht: sv.visitTypeVariable(r.acceptTV(new // TypeToSignature()) // - String sig2 = r.acceptTV(new TypeToSignature()); + String sig2 = r.acceptTV(new TypeToSignature(constraints)); if (r instanceof GenericRefType) { sv.visitTypeVariable(sig2); } else if (!(r instanceof TypePlaceholder)) { if (sig2.contains(SPECIAL_CHAR_FOR_FUN)) { - System.out.println(" Signature FUN$$: " + r); - sv.visitInterface().visitClassType(sig2.substring(1, sig2.length())); + sv.visitInterface().visitClassType(sig2.substring(1)); } else { // Kann zwischen GenericRefType und RefType nicht unterscheiden // Deswegen wird immer geprüft, ob der Name in Generic Maps liegt @@ -405,7 +227,6 @@ public class Signature { } else { toVisit = getEqualTPH(constraints, realName) + SPECIAL_CHAR; } - System.out.println(r.getClass() + " Signature TPH: " + r.acceptTV(new TypeToSignature())); } sv.visitTypeVariable(toVisit); } @@ -413,41 +234,18 @@ public class Signature { break; case "SWC": - System.out.println("SWC---Signature"); SuperWildcardType swc = (SuperWildcardType) t; String sigInner = swc.getInnerType().acceptTV(new TypeToSignature(constraints)); - if (swc.getInnerType() instanceof TypePlaceholder) { - sv.visitTypeArgument(SUPER_CHAR).visitTypeVariable(sigInner.substring(1, sigInner.length())); - } else if (swc.getInnerType() instanceof RefType) { - if (sigInner.contains(SPECIAL_CHAR_FOR_FUN)) { - sv.visitTypeArgument(SUPER_CHAR).visitInterface().visitClassType(sigInner.substring(1, sigInner.length())); - } else { - sv.visitTypeArgument(SUPER_CHAR).visitClassType(sigInner.substring(1, sigInner.length())); - } - } else { - sv.visitTypeArgument(SUPER_CHAR).visitTypeVariable(sigInner.substring(1)); - } + int length = sigInner.length(); + visitWildCard(sv, sigInner, length, swc.getInnerType(), SUPER_CHAR); break; case "EWC": - System.out.println("EWC---Signature"); ExtendsWildcardType ewc = (ExtendsWildcardType) t; String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature(constraints)); - System.out.println(esigInner); - if (ewc.getInnerType() instanceof TypePlaceholder) { - sv.visitTypeArgument(EXTENDS_CHAR).visitTypeVariable(esigInner.substring(1, esigInner.length())); - } else if (ewc.getInnerType() instanceof RefType) { - if (esigInner.contains(SPECIAL_CHAR_FOR_FUN)) { - sv.visitTypeArgument(EXTENDS_CHAR).visitInterface() - .visitClassType(esigInner.substring(1, esigInner.length())); - } else { -// sv.visitClassType(esigInner.substring(1,esigInner.length())); - sv.visitTypeArgument(EXTENDS_CHAR).visitClassType(esigInner.substring(1, esigInner.length())); - } - } else { - sv.visitTypeArgument(EXTENDS_CHAR).visitTypeVariable(esigInner.substring(1)); - } + int lengthEWCSig = esigInner.length(); + visitWildCard(sv, esigInner, lengthEWCSig, ewc.getInnerType(), EXTENDS_CHAR); break; default: @@ -457,6 +255,24 @@ public class Signature { } } + private void visitWildCard(SignatureVisitor sv, String sigInner, int length, RefTypeOrTPHOrWildcardOrGeneric innerType, char superOrExtendsChar) { + if (innerType instanceof TypePlaceholder) { + sv.visitTypeArgument(superOrExtendsChar).visitTypeVariable(sigInner.substring(1, length)); + } else if (innerType instanceof RefType) { + checkInnerSignatureOfWildCard(sv, sigInner, length, superOrExtendsChar); + } else { + sv.visitTypeArgument(superOrExtendsChar).visitTypeVariable(sigInner.substring(1)); + } + } + + private void checkInnerSignatureOfWildCard(SignatureVisitor sv, String sigInner, int length, char superOrExtendsChar) { + if (sigInner.contains(SPECIAL_CHAR_FOR_FUN)) { + sv.visitTypeArgument(superOrExtendsChar).visitInterface().visitClassType(sigInner.substring(1, length)); + } else { + sv.visitTypeArgument(superOrExtendsChar).visitClassType(sigInner.substring(1, length)); + } + } + private Optional getEqualTPHFromClassConstraints(List consClass, String tph) { return consClass.stream() .filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph)) @@ -469,38 +285,29 @@ public class Signature { .findFirst().get().getConstraint().getLeft(); } - /** - * @param isParameterType - * @return - */ - private SignatureVisitor getSignatureVisitor(boolean isParameterType) { - SignatureVisitor sv; - if (isParameterType) { - sv = sw.visitParameterType(); - } else { - sv = sw.visitReturnType(); - } - return sv; - } - /** * Creates signature for class or interface with {@link SignatureWriter} * Signature looks like: superclass */ - private void createSignatureForClassOrInterface() { - Iterator itr = classOrInterface.getGenerics().iterator(); - - while (itr.hasNext()) { - GenericTypeVar g = itr.next(); - visitTypeVarsAndTheirBounds(g, genericsAndBounds); - } + public String createSignatureForClassOrInterface() { + defineTypeVariablesForClassOrInterface(); defineGenericsFromConstraints(consClass,genericsAndBounds); String sClass = classOrInterface.getSuperClass().acceptTV(new TypeToSignature()); sw.visitSuperclass().visitClassType(sClass.substring(1, sClass.length() - 1)); sw.visitEnd(); + return sw.toString(); + } + + private void defineTypeVariablesForClassOrInterface() { + Iterator itr = classOrInterface.getGenerics().iterator(); + + while (itr.hasNext()) { + GenericTypeVar g = itr.next(); + visitTypeVarsAndTheirBounds(g, genericsAndBounds); + } } /** @@ -553,7 +360,6 @@ public class Signature { while (bItr.hasNext()) { RefTypeOrTPHOrWildcardOrGeneric b = bItr.next(); String boundDesc = b.acceptTV(new TypeToDescriptor()); -// System.out.println("GetBounds: " + boundDesc); // Ensure that <...> extends java.lang.Object OR ... if (b instanceof GenericRefType) { sw.visitClassBound().visitTypeVariable(boundDesc); @@ -562,11 +368,6 @@ public class Signature { } genAndBounds.put(g.getName(), boundDesc); } -// sw.visitClassBound().visitEnd(); - } - - public String toString() { - return sw.toString(); } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index e35573b0..c8306f02 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -5,8 +5,7 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; -import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResult.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResult.java deleted file mode 100644 index 04544d0b..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResult.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.simplifyRes; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; - -/** - * The class SimplifyResult represents the results of the simplify algorithm. - * This class contains all constraints and all type placeholders of a class. The type placeholders - * are represented by their names. - * The simplify results of each method of a class are also contained in this class. - * - * @author fayez - * - */ -public class SimplifyResult { - private List classConstraints; - private List tphsClass; - private final List simplifyResultForMethod = new ArrayList<>(); - - public SimplifyResult(List classConstraints, List tphsClass) { - this.classConstraints = classConstraints; - this.tphsClass = tphsClass; - } - - public SimplifyResult() { - } - - public List getClassConstraints() { - return classConstraints; - } - - public List getSimplifyResultForMethod() { - return simplifyResultForMethod; - } - - public List getTphsClass() { - return tphsClass==null?new ArrayList<>():tphsClass; - } - - /** - * @param classConstraints the classConstraints to set - */ - public void setClassConstraints(List classConstraints) { - this.classConstraints = classConstraints; - } - - /** - * @param tphsClass the tphsClass to set - */ - public void setTphsClass(List tphsClass) { - this.tphsClass = tphsClass; - } - - public Map> getMethodConstraintsByID(String id){ - return simplifyResultForMethod.stream().filter(sr->sr.getMethodID().equals(id)).findAny().get().getConastraintsAndEqualTPHs(); - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResultClass.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResultClass.java deleted file mode 100644 index ef0aa85f..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResultClass.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.simplifyRes; - - -/** - * The simplify results of a class - * - * @author fayez - * - */ -public class SimplifyResultClass { - private final String className; - private SimplifyResult simplifyResults; - - /** - * @param className - */ - public SimplifyResultClass(String className) { - this.className = className; - } - - /** - * @param className - * @param simplifyResults - */ - public SimplifyResultClass(String className, SimplifyResult simplifyResults) { - this.className = className; - this.simplifyResults = simplifyResults; - } - - public String getClassName() { - return className; - } - - public SimplifyResult getSimplifyResults() { - return simplifyResults; - } - - /** - * @param simplifyResults the simplifyResults to set - */ - public void setSimplifyResults(SimplifyResult simplifyResults) { - this.simplifyResults = simplifyResults; - } - -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResultMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResultMethod.java deleted file mode 100644 index 94f505fa..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/SimplifyResultMethod.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.dhbwstuttgart.bytecode.simplifyRes; - -import java.util.Map; -import java.util.Set; - -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; - -/** - * The simplify results of a methed - * - * @author fayez - * - */ -public class SimplifyResultMethod { - - private final String methodID; - private final Map> conastraintsAndEqualTPHs; - - - public SimplifyResultMethod(String methodName, Map> conastraintsAndEqualTPHs) { - this.methodID = methodName; - this.conastraintsAndEqualTPHs = conastraintsAndEqualTPHs; - } - - - /** - * @return the methodName - */ - public String getMethodID() { - return methodID; - } - - - /** - * @return the conastraintsAndEqualTPHs - */ - public Map> getConastraintsAndEqualTPHs() { - return conastraintsAndEqualTPHs; - } - - -} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/TypeVariableFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/TypeVariableFinder.java deleted file mode 100644 index 2b46e609..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/TypeVariableFinder.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.simplifyRes; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.objectweb.asm.Type; - -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.type.GenericRefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; - -/** - * @author fayez - * - */ -public class TypeVariableFinder { - private ClassOrInterface classOrInterface; - private Map genericsAndBounds; - private List consClass; - - public TypeVariableFinder(ClassOrInterface classOrInterface, Map genericsAndBounds, - List consClass) { - this.classOrInterface = classOrInterface; - this.genericsAndBounds = genericsAndBounds; - this.consClass = consClass; - } - - public void findTV() { - Iterator itr = classOrInterface.getGenerics().iterator(); - - while(itr.hasNext()) { - GenericTypeVar g = itr.next(); - getBoundsOfTypeVar(g,genericsAndBounds); - } - - if(!consClass.isEmpty()) { - ArrayList types = new ArrayList<>(); - ArrayList superTypes = new ArrayList<>(); - - for(TPHConstraint cons : consClass) { - types.add(cons.getLeft()); - superTypes.add(cons.getRight()); - } - - for(TPHConstraint cons : consClass) { - String t = cons.getLeft()+"$"; - String bound = cons.getRight(); - if(!bound.equals(Type.getInternalName(Object.class))) - bound += "$"; - genericsAndBounds.put(t, bound); - } - - } - } - - /** - * Get bounds of type variable - * @param g type variable - * @param genAndBounds - */ - private void getBoundsOfTypeVar(GenericTypeVar g, Map genAndBounds) { - - Iterator bItr = g.getBounds().iterator(); - while(bItr.hasNext()) { - RefTypeOrTPHOrWildcardOrGeneric b =bItr.next(); - String boundDesc = b.acceptTV(new TypeToDescriptor()); - // Ensure that <...> extends java.lang.Object OR ... - genAndBounds.put(g.getName(), boundDesc); - } - } - -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/ConstraintsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/ConstraintsFinder.java index 88294f63..8597abfb 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/ConstraintsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/ConstraintsFinder.java @@ -5,7 +5,7 @@ import java.util.List; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; -import de.dhbwstuttgart.bytecode.genericsGenerator.ConstraintsWithSameLeftSide; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.ConstraintsWithSameLeftSide; public class ConstraintsFinder { private List allConstaints; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/NameReplacer.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/NameReplacer.java index 777955e6..89654239 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/NameReplacer.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/NameReplacer.java @@ -7,7 +7,7 @@ import java.util.Map; import java.util.stream.Stream; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.NameReplacementResult; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; public class NameReplacer { diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java deleted file mode 100644 index e0f9aa20..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java +++ /dev/null @@ -1,1044 +0,0 @@ -package de.dhbwstuttgart.bytecode.utilities; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -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; -import de.dhbwstuttgart.bytecode.genericsGenerator.ConstraintsWithSameLeftSide; -import de.dhbwstuttgart.bytecode.genericsGenerator.GenericsGeneratorUtility; -import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult; -import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; - -public class Simplify { - - public static Map> simplifyConstraints(Method method, TPHExtractor tphExtractor, - List tphsClass) { - // 1. check if there are any simple cycles like L set L=R and: - // * remove both constraints - // * substitute L with R in all constraint - // b)no => go to next step - // 2. check the result of step 1 if there are any equal-constraints like L=R, - // M=R .. - // a) yes => put all such TPhs in a map and define "key-Cons" - // -- key-Cons = TPH < Object -- - // put this Constraint and the - // b) no - // 3. is - - // all constraints that will be simplified -// ArrayList allCons = tphExtractor.allCons; - ArrayList allCons = new ArrayList<>(); - - for(TPHConstraint c : tphExtractor.allCons) { - if(!containsConstraint(allCons,c)) { - TPHConstraint nc = new TPHConstraint(c.getLeft(), c.getRight(), c.getRel()); - allCons.add(nc); - } - } - ArrayList consToRemove = new ArrayList<>(); - - String id = MethodUtility.createID(tphExtractor.getResolver(), method); - // get all tph of the method - ArrayList methodTphs = new ArrayList<>(); - ArrayList localTphs = new ArrayList<>(); - for (MethodAndTPH m : tphExtractor.ListOfMethodsAndTph) { - if (m.getId().equals(id)) { - methodTphs = m.getTphs(); - localTphs = m.getLocalTphs(); - break; - } - } - - // step 1: - for (TPHConstraint c : allCons) { - - String left = c.getLeft(); - String right = c.getRight(); - if (c.getRel() == Relation.EXTENDS) { - TPHConstraint revCon = getReverseConstraint(allCons, left, right); - if (revCon != null) { - revCon.setRel(Relation.EQUAL); - // the reverse constraint is removed because - // otherwise there is the same constraint twice - // (e.g. A A=B and B=A) - consToRemove.add(revCon); - c.setRel(Relation.EQUAL); - substituteTPH(allCons, left, right); - } - } - } - - System.out.println(); - System.out.println("NEW ALL CONST: " + allCons.size()); - allCons.forEach(c -> System.out.println(c.toString())); - System.out.println("----------------"); - allCons.removeAll(consToRemove); - consToRemove = new ArrayList<>(); - - int size = allCons.size(); - - System.out.println("AFTER DELETE ALL CONST: " + allCons.size()); - allCons.forEach(c -> System.out.println(c.toString())); - System.out.println("----------------"); - Map> result = new HashMap<>(); - - // check if there is any long cycle (e.g. A allTypes = new LinkedList<>(); - // we will put all constraints which are in the cycle, in this ArrayList. - // Later these contraints will be converted to equal-constraints - ArrayList constraints = new ArrayList<>(size); - int visited = 0; - - // contains all constraints - HashMap ss1 = new HashMap<>(); - - for (TPHConstraint constr : allCons) { - ss1.put(constr.getLeft(), constr.getRight()); - } - - for (TPHConstraint c : allCons) { - - if (visited >= size) - break; - // Only extends-constraints will be checked - if (c.getRel() == Relation.EXTENDS) { - ++visited; - - String left = c.getLeft(); - String right = c.getRight(); - // put the types in linked list - allTypes.add(left); - allTypes.add(right); - - constraints.add(c); - - boolean isCycle = false; - - // iterate through the map to check if there is a cycle - while (ss1.containsKey(right)) { - ++visited; - String oldRight = right; - right = ss1.get(right); - - TPHConstraint toAdd = getConstraint(oldRight, right, allCons); - - if (toAdd != null) - constraints.add(toAdd); - - if (left.equals(right)) { - isCycle = true; - break; - } - - allTypes.add(right); - } - - if (isCycle) { - // convert all constraints to equal constraints - setAllEqual(constraints); - // these constraints will be removed from allCons - consToRemove.addAll(constraints); - // all equal types will be substitute with one type - substituteAllTPH(allCons, constraints, left); - - HashSet eq = new HashSet<>(); - // put all equal types in a set - for (String t : allTypes) { - eq.add(t); - } - // generate a new constraint (left < Object) - TPHConstraint constraint = new ExtendsConstraint(left, Type.getInternalName(Object.class)); - // put the generated constraint and its equal set into result set - result.put(constraint, eq); - constraints.clear(); - } - allTypes.clear(); - } - } - - for (TPHConstraint ec : allCons) { - - if(ec.getRel() == Relation.EQUAL) { - if(!localTphs.contains(ec.getRight())) { - localTphs.add(ec.getRight()); - } - if(!methodTphs.contains(ec.getRight())) { - methodTphs.add(ec.getRight()); - } - - } - } - // build an equal set that contains all types - // which are equal and for each equal constraint put left side and right side - // in this set Then generate a constraint type < Object and put it - // with the equal set into the result. -// for(TPHConstraint c : allCons) { -// if(c.getRel()==Relation.EQUAL) { -// if(!isTPHInResEqual(result, c.getLeft())) { -// HashSet equalTPHs = getEqualsTPHs(result, c); -// TPHConstraint constraint = getKeyConstraint(result,c); -// equalTPHs.add(c.getLeft()); -// equalTPHs.add(c.getRight()); -// result.put(constraint, equalTPHs); -// } -// consToRemove.add(c); -// size--; -// } -// } - - System.out.println("Step 2 Result: "); - result.forEach((c, hs) -> { - System.out.print(c.toString() + " -> "); - hs.forEach(s -> { - System.out.print(s + ", "); - }); - System.out.println(); - }); - System.out.println("----------------"); - - if (!allCons.isEmpty() && allCons.size() < 2) { - TPHConstraint cons = allCons.get(0); - if (!result.containsKey(cons)) { - result.put(cons, null); - if (!cons.getRight().equals(Type.getInternalName(Object.class))) - result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class)), null); - } - - return result; - } - - size += result.keySet().size(); - // all constraints which have Object on the right side will - // be ignored, because they are simplified and can not be changed. - for (TPHConstraint c : allCons) { - if (c.getRight().equals(Type.getInternalName(Object.class))) - size--; - } - - // finder looks for constraints that have the same left hand side - // and put them in a list - ConstraintsFinder finder = new ConstraintsFinder(allCons); - List foundCons = finder.findConstraints(); - - ArrayList eqCons = new ArrayList<>(); - - for (ConstraintsWithSameLeftSide list : foundCons) { - // 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, methodTphs, localTphs); - // new name -> [all old names] - NameReplacementResult replRes = replacer.replaceNames(); - // create an equal constraint for each value in repres - String key = replRes.getName(); - for (String val : replRes.getOldNames()) { - EqualConstraint ec = new EqualConstraint(val, key); - eqCons.add(ec); - } - for (TPHConstraint c : allCons) { - if (c.getRel() == Relation.EQUAL && key.equals(c.getRight())) { - eqCons.add(c); - } - } - GenericsGeneratorUtility.updateEqualCons(replRes, eqCons); - - TPHConstraint c = list.getConstraints().get(0); - allCons.removeAll(list.getConstraints()); - allCons.add(c); - } - - // check if there are multiple constraint with the same left side. - // 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<>(); -// all.add(c.getLeft()); -// String sup =c.getRight(); -// all.add(sup); -// HashMap ss = new HashMap<>(); -// for(TPHConstraint constr : allCons) { -// ss.put(constr.getLeft(), constr.getRight()); -// } -// while(ss.containsKey(sup)) { -// sup = ss.get(sup); -// all.add(sup); -// } -// 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("----------------"); - HashMap subAndSuper = new HashMap<>(); - - for (TPHConstraint c : allCons) { - if (c.getRel() == Relation.EXTENDS) - subAndSuper.put(c.getLeft(), c.getRight()); - } - - for (TPHConstraint ec : allCons) { - if(ec.getRel() == Relation.EQUAL) { - methodTphs.remove(ec.getLeft()); - localTphs.remove(ec.getLeft()); - if(!localTphs.contains(ec.getRight())) { - localTphs.add(ec.getRight()); - } - if(!methodTphs.contains(ec.getRight())) { - methodTphs.add(ec.getRight()); - } - } - - } - - int numOfVisitedPairs = 0; - for (String sub : subAndSuper.keySet()) { - if (isTPHInConstraint(result, sub)) - continue; - - if (!containTPH(methodTphs, sub)) - continue; - - if (numOfVisitedPairs >= size) - break; - LinkedList tphInRel = new LinkedList<>(); - tphInRel.add(sub); - String superT = subAndSuper.get(sub); - tphInRel.add(superT); - - numOfVisitedPairs++; - while (!localTphs.contains(superT) && subAndSuper.containsKey(superT)) { - superT = subAndSuper.get(superT); - if (tphInRel.contains(superT)) { - break; - } - tphInRel.add(superT); - numOfVisitedPairs++; - } - - // Subtype - String subTphRes = tphInRel.getFirst(); - // Die größte Supertype - String superTphRes = tphInRel.getLast(); - - // if there is any constraint X < subTph, then - // add X at the beginning of the list. - while (!localTphs.contains(subTphRes) && subAndSuper.containsValue(subTphRes)) { - for (String tph : subAndSuper.keySet()) { - if (containTPH(methodTphs, tph) && subAndSuper.get(tph).equals(subTphRes)) { - subTphRes = tph; - break; - } - } - if (subTphRes.equals(tphInRel.getFirst())) { - break; - } - - if (isTPHInConstraint(result, subTphRes)) - break; - - tphInRel.addFirst(subTphRes); - numOfVisitedPairs++; - } - - subTphRes = tphInRel.getFirst(); - // if the last type in the list not a type in method-types - // then find the last type in front of this type, which is - // a type in method-types - int i = 2; - while (!containTPH(methodTphs, superTphRes) && (tphInRel.size() - i) > 0) { - superTphRes = tphInRel.get(tphInRel.size() - i); - i++; - } - - if (!containTPH(methodTphs, superTphRes)) { - HashSet equals = getEqualsTphsFromEqualCons(eqCons, superTphRes); - if (tphsClass.contains(superTphRes)/*classTPHSContainsTPH(tphsClass, superTphRes)*/) { - result.put(new ExtendsConstraint(subTphRes, superTphRes), equals); - } else { - result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class)), - equals); - } - - } else { - HashSet equals = getEqualsTphsFromEqualCons(eqCons, subTphRes); - result.put(new ExtendsConstraint(subTphRes, superTphRes), equals); - if (!isTPHInConstraint(result, superTphRes) && !isTphInEqualSet(result, superTphRes)) { - HashSet equals2 = getEqualsTphsFromEqualCons(eqCons, superTphRes); - result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class)), - equals2); - } - } - } - -// for(String tph : methodTphs) { - for (String tph : localTphs) { - if (!isTPHInConstraint(result, tph) && !isTphInEqualSet(result, tph)) { - HashSet equals = getEqualsTphsFromEqualCons(eqCons, tph); - result.put(new ExtendsConstraint(tph, Type.getInternalName(Object.class)), equals); - } - } - - // build an equal set that contains all types - // which are equal and for each equal constraint put left side and right side - // in this set Then generate a constraint type < Object and put it - // with the equal set into the result. - for (TPHConstraint c : allCons) { - if (c.getRel() == Relation.EQUAL) { - if (!isTPHInResEqual(result, c.getLeft())) { - Set equalTPHs = getEqualsTPHs(result, c); - TPHConstraint constraint = getKeyConstraint(result, c); - equalTPHs.add(c.getLeft()); - equalTPHs.add(c.getRight()); - result.put(constraint, equalTPHs); - } - consToRemove.add(c); - size--; - } - } - - // remove all equal-constraints - allCons.removeAll(consToRemove); - // add all generated constraints to allCons - allCons.addAll(result.keySet()); - - System.out.println("EndResult: "); - result.forEach((c, hs) -> { - if (c != null) { - System.out.print(c.toString() + " -> "); - if (hs == null) { - System.out.print(" [] "); - } else { - hs.forEach(s -> { - System.out.print(s + ", "); - }); - } - } - - System.out.println(); - }); - System.out.println("----------------"); - return result; - } - - private static boolean isTPHInResEqual(Map> result, String left) { - // TODO Auto-generated method stub - return false; - } - - private static boolean containsConstraint(ArrayList allCons, TPHConstraint c) { - for(TPHConstraint con:allCons) { - if(c.getLeft().equals(con.getLeft()) && c.getRight().equals(c.getRight())) { - return true; - } - } - return false; - } - - private static void updateEqualCons(Map> replRes, ArrayList eqCons) { - List oldNames = replRes.values().iterator().next(); - String newName = replRes.keySet().iterator().next(); - for (TPHConstraint c : eqCons) { -// if(oldNames.contains(c.getLeft())) -// c.setLeft(newName); - if (oldNames.contains(c.getRight())) - c.setRight(newName); - } - - } - - public static Map> simplifyConstraintsClass(TPHExtractor tphExtractor, - List tphsClass) { - // all constraints that will be simplified - ArrayList allCons = tphExtractor.allCons; - ArrayList consToRemove = new ArrayList<>(); - - // step 1: - for (TPHConstraint c : allCons) { - - String left = c.getLeft(); - String right = c.getRight(); - if (c.getRel() == Relation.EXTENDS) { - TPHConstraint revCon = getReverseConstraint(allCons, left, right); - if (revCon != null) { - revCon.setRel(Relation.EQUAL); - // the reverse constraint is removed because - // otherwise there is the same constraint twice - // (e.g. A A=B and B=A) - consToRemove.add(revCon); - c.setRel(Relation.EQUAL); - substituteTPH(allCons, left, right); - } - } - } - - allCons.removeAll(consToRemove); - consToRemove = new ArrayList<>(); - - int size = allCons.size(); - - HashMap> result = new HashMap<>(); - - // check if there is any long cycle (e.g. A allTypes = new LinkedList<>(); - // we will put all constraints which are in the cycle, in this ArrayList. - // Later these contraints will be converted to equal-constraints - ArrayList constraints = new ArrayList<>(size); - int visited = 0; - - // contains all constraints - HashMap ss1 = new HashMap<>(); - - for (TPHConstraint constr : allCons) { - ss1.put(constr.getLeft(), constr.getRight()); - } - - for (TPHConstraint c : allCons) { - - if (visited >= size) - break; - // Only extends-constraints will be checked - if (c.getRel() == Relation.EXTENDS) { - ++visited; - - String left = c.getLeft(); - String right = c.getRight(); - // put the types in linked list - allTypes.add(left); - allTypes.add(right); - - constraints.add(c); - - boolean isCycle = false; - - // iterate through the map to check if there is a cycle - while (ss1.containsKey(right)) { - ++visited; - String oldRight = right; - right = ss1.get(right); - - TPHConstraint toAdd = getConstraint(oldRight, right, allCons); - - if (toAdd != null) - constraints.add(toAdd); - - if (left.equals(right)) { - isCycle = true; - break; - } - - allTypes.add(right); - } - - if (isCycle) { - // convert all constraints to equal constraints - setAllEqual(constraints); - // these constraints will be removed from allCons - consToRemove.addAll(constraints); - // all equal types will be substitute with one type - substituteAllTPH(allCons, constraints, left); - - HashSet eq = new HashSet<>(); - // put all equal types in a set - for (String t : allTypes) { - eq.add(t); - } - // generate a new constraint (left < Object) - TPHConstraint constraint = new ExtendsConstraint(left, Type.getInternalName(Object.class)); - // put the generated constraint and its equal set into result set - result.put(constraint, eq); - constraints.clear(); - } - allTypes.clear(); - } - } - // build an equal set that contains all types - // which are equal and for each equal constraint put left side and right side - // in this set Then generate a constraint type < Object and put it - // with the equal set into the result. - for (TPHConstraint c : allCons) { - if (c.getRel() == Relation.EQUAL) { - if (!isTPHInResEqual(result, c.getLeft())) { - Set equalTPHs = getEqualsTPHs(result, c); - TPHConstraint constraint = getKeyConstraint(result, c); - equalTPHs.add(c.getLeft()); - equalTPHs.add(c.getRight()); - result.put(constraint, equalTPHs); - } - consToRemove.add(c); - size--; - } - } - - // remove all equal-constraints - allCons.removeAll(consToRemove); - // add all generated constraints to allCons - allCons.addAll(result.keySet()); - - if (!allCons.isEmpty() && allCons.size() < 2) { - TPHConstraint cons = allCons.get(0); - if (!result.containsKey(cons)) { - result.put(cons, null); - result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class)), - null); - } - - return result; - } - - size += result.keySet().size(); - // all constraints which have Object on the right side will - // be ignored, because they are simplified and can not be changed. - for (TPHConstraint c : allCons) { - if (c.getRight().equals(Type.getInternalName(Object.class))) - size--; - } - - // check if there are multiple constraint with the same left side. - // 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) { - - subAndSuper.put(c.getLeft(), c.getRight()); - } - - int numOfVisitedPairs = 0; - for (String sub : subAndSuper.keySet()) { - if (isTPHInConstraint(result, sub)) - continue; - -// if (!classTPHSContainsTPH(tphsClass, sub)) -// continue; - if (!tphsClass.contains(sub)) - continue; - - if (numOfVisitedPairs >= size) - break; - LinkedList tphInRel = new LinkedList<>(); - tphInRel.add(sub); - String superT = subAndSuper.get(sub); - tphInRel.add(superT); - - numOfVisitedPairs++; - while (subAndSuper.containsKey(superT)) { - superT = subAndSuper.get(superT); - if (tphInRel.contains(superT)) { - break; - } - tphInRel.add(superT); - numOfVisitedPairs++; - } - - // Subtype - String subTphRes = tphInRel.getFirst(); - // Die größte Supertype - String superTphRes = tphInRel.getLast(); - - // if there is any constraint X < subTph, then - // add X at the beginning of the list. - while (subAndSuper.containsValue(subTphRes)) { - for (String tph : subAndSuper.keySet()) { - if (/*classTPHSContainsTPH(tphsClass, tph)*/tphsClass.contains(tph) && subAndSuper.get(tph).equals(subTphRes)) { - subTphRes = tph; - break; - } - } - if (subTphRes.equals(tphInRel.getFirst())) { - break; - } - - if (isTPHInConstraint(result, subTphRes)) - break; - - tphInRel.addFirst(subTphRes); - numOfVisitedPairs++; - } - - subTphRes = tphInRel.getFirst(); - - HashSet equals = getEqualsTphsFromEqualCons(eqCons, superTphRes); - result.put(new ExtendsConstraint(subTphRes, superTphRes), equals); - - } - - System.out.println("EndResult: "); - result.forEach((c, hs) -> { - if (c != null) { - System.out.print(c.toString() + " -> "); - if (hs == null) { - System.out.print(" [] "); - } else { - hs.forEach(s -> { - System.out.print(s + ", "); - }); - } - } - - System.out.println(); - }); - System.out.println("----------------"); - return result; - } - - private static boolean classTPHSContainsTPH(ArrayList tphsClass, String superTphRes) { - for (TypePlaceholder tph : tphsClass) { - if (tph.getName().equals(superTphRes)) - return true; - } - return false; - } - - private static boolean isTphInEqualSet(Map> result, String tph) { - for (Set 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); - 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)) - return c; - } - return null; - } - - private static boolean isTPHInResEqual(HashMap> result, String left) { - for (Set eq : result.values()) { - if (eq.contains(left)) { - return true; - } - } - return false; - } - - private static void substituteAllTPH(ArrayList allCons, ArrayList constraints, - String first) { - for (TPHConstraint c : allCons) { - for (TPHConstraint c2 : constraints) { - if (c.getRel() == Relation.EXTENDS) { - if (c2.getLeft().equals(c.getLeft()) || c2.getRight().equals(c.getLeft())) - c.setLeft(first); - if (c2.getLeft().equals(c.getRight()) || c2.getRight().equals(c.getRight())) - c.setRight(first); - } - - } - } - } - - private static void setAllEqual(ArrayList constraints) { - for (TPHConstraint c : constraints) { - c.setRel(Relation.EQUAL); - } - } - - public static Map> simplifyContraints( - Map> allConstraints) { - // 1. check if there are any cycles like L set L=R and: - // * remove both constraints - // * substitute L with R in all constraint - // b)no => go to next step - // 2. check the result of step 1 if there are any equal-constraints like L=R, - // M=R .. - // a) yes => put all such TPhs in a map and define "key-Cons" - // -- key-Cons = TPH < Object -- - // put this Constraint and the - // b) no - // 3. is - ArrayList allCons = new ArrayList<>(); - for (TPHConstraint tphCons : allConstraints.keySet()) { - allCons.add(tphCons); - } - ArrayList consToRemove = new ArrayList<>(); - // step 1: - for (TPHConstraint c : allCons) { - - String left = c.getLeft(); - String right = c.getRight(); - if (c.getRel() == Relation.EXTENDS) { - TPHConstraint revCon = getReverseConstraint(allCons, left, right); - if (revCon != null) { - revCon.setRel(Relation.EQUAL); - consToRemove.add(revCon); - c.setRel(Relation.EQUAL); - substituteTPH(allCons, left, right); - } - } - } - System.out.println(); - System.out.println("NEW ALL CONST: " + allCons.size()); - allCons.forEach(c -> System.out.println(c.toString())); - System.out.println("----------------"); - allCons.removeAll(consToRemove); - consToRemove = new ArrayList<>(); - -// int size = allCons.size(); - - System.out.println("AFTER DELETE ALL CONST: " + allCons.size()); - allCons.forEach(c -> System.out.println(c.toString())); - System.out.println("----------------"); - Map> result = new HashMap<>(); - - for (TPHConstraint c : allCons) { - if (c.getRel() == Relation.EQUAL) { - Set equalTPHs = getEqualsTPHs(result, c); - TPHConstraint constraint = getKeyConstraint(result, c); - equalTPHs.add(c.getLeft()); - equalTPHs.add(c.getRight()); - result.put(constraint, equalTPHs); - consToRemove.add(c); -// size--; - } - } - System.out.println("Step 2 Result: "); - result.forEach((c, hs) -> { - System.out.print(c.toString() + " -> "); - hs.forEach(s -> { - System.out.print(s + ", "); - }); - System.out.println(); - }); - System.out.println("----------------"); - allCons.removeAll(consToRemove); - allCons.addAll(result.keySet()); - - if (!allCons.isEmpty() && allCons.size() < 2) { - - if (!result.containsKey(allCons.get(0))) - result.put(allCons.get(0), null); - - return result; - } - -// size += result.keySet().size(); - -// for(TPHConstraint c : allCons) { -// if(c.getRight().equals(Type.getInternalName(Object.class))) -// size--; -// } - - HashMap subAndSuper = new HashMap<>(); - - for (TPHConstraint c : allCons) { - subAndSuper.put(c.getLeft(), c.getRight()); - } - -// int numOfVisitedPairs = 0; - for (String sub : subAndSuper.keySet()) { - if (isTPHInConstraint(result, sub)) - continue; - -// if(numOfVisitedPairs>=size) -// break; - - LinkedList tphInRel = new LinkedList<>(); - tphInRel.add(sub); - String superT = subAndSuper.get(sub); - tphInRel.add(superT); - -// numOfVisitedPairs++; - while (subAndSuper.containsKey(superT)) { - superT = subAndSuper.get(superT); - if (tphInRel.contains(superT)) { - break; - } - tphInRel.add(superT); -// numOfVisitedPairs++; - } - - // Subtype - String subTphRes = tphInRel.getFirst(); - // Die größte Supertype - String superTphRes = tphInRel.getLast(); - - while (subAndSuper.containsValue(subTphRes)) { - for (String tph : subAndSuper.keySet()) { - if (subAndSuper.get(tph).equals(subTphRes)) { - subTphRes = tph; - break; - } - } - if (subTphRes.equals(tphInRel.getFirst())) { - break; - } - tphInRel.addFirst(subTphRes); -// numOfVisitedPairs++; - } - - subTphRes = tphInRel.getFirst(); - - int i = 2; - while (superTphRes.equals(Type.getInternalName(Object.class)) && (tphInRel.size() - i) > 0) { - superTphRes = tphInRel.get(tphInRel.size() - i); - i++; - } - if (superTphRes.equals(Type.getInternalName(Object.class))) { - result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class)), - null); - } else { - result.put(new ExtendsConstraint(subTphRes, superTphRes), null); - result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class)), - null); - } - - } - System.out.println("EndResult: "); - result.forEach((c, hs) -> { - if (c != null) { - System.out.print(c.toString() + " -> "); - if (hs == null) { - System.out.print(" [] "); - } else { - hs.forEach(s -> { - System.out.print(s + ", "); - }); - } - } - - System.out.println(); - }); - System.out.println("----------------"); - return result; - } - - private static void substituteTPH(ArrayList allCons, String left, String right) { - allCons.forEach(c -> { - if (c.getRel() == Relation.EXTENDS) { - if (c.getLeft().equals(left)) - c.setLeft(right); - if (c.getRight().equals(left)) - c.setRight(right); - } - }); - } - - private static TPHConstraint getReverseConstraint(ArrayList allCons, String left, String right) { - for (TPHConstraint c : allCons) { - if (c.getLeft().equals(right) && c.getRight().equals(left)) { - return c; - } - } - return null; - } - - private static boolean isTPHInConstraint(Map> result, String sub) { - for (TPHConstraint c : result.keySet()) { - if (c.getLeft().equals(sub)) - return true; - } - return false; - } - - private static boolean containTPH(ArrayList methodTphs, String sub) { - for (String tph : methodTphs) { - if (tph.equals(sub)) - return true; - } - return false; - } - - private static TPHConstraint getKeyConstraint(HashMap> result, TPHConstraint toFind, - ArrayList allCons) { - - for (TPHConstraint c : result.keySet()) { - if (c.containTPH(toFind.getLeft())) - return c; - } - for (TPHConstraint c : allCons) { - if (toFind.getRight().equals(c.getLeft())) - return c; - } - - return new ExtendsConstraint(toFind.getRight(), Type.getInternalName(Object.class)); - } - - private static TPHConstraint getKeyConstraint(Map> result, - TPHConstraint toFind) { - - for (TPHConstraint c : result.keySet()) { - if (c.containTPH(toFind.getLeft()) || c.containTPH(toFind.getRight())) - return c; - } - - return new ExtendsConstraint(toFind.getRight(), Type.getInternalName(Object.class)); - } - - private static Set getEqualsTPHs(Map> result, TPHConstraint toFind) { - for (TPHConstraint c : result.keySet()) { - if (c.containTPH(toFind.getLeft()) || c.containTPH(toFind.getRight())) - return result.get(c); - } - return new HashSet<>(); - } -} diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index c4d0407a..e3dce48a 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -3,23 +3,18 @@ package de.dhbwstuttgart.core; import de.dhbwstuttgart.bytecode.BytecodeGen; import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError; -import de.dhbwstuttgart.bytecode.simplifyRes.SimplifyResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GeneratedGenericsFinder; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.environment.CompilationEnvironment; import de.dhbwstuttgart.parser.JavaTXParser; -import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typeinference.constraints.Constraint; @@ -31,7 +26,6 @@ import de.dhbwstuttgart.typeinference.unify.RuleSet; import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.distributeVariance; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; @@ -46,15 +40,10 @@ import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.Writer; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.antlr.v4.parse.ANTLRParser.throwsSpec_return; -import org.apache.commons.io.output.NullOutputStream; //import org.apache.commons.io.output.NullOutputStream; public class JavaTXCompiler { @@ -66,13 +55,6 @@ public class JavaTXCompiler { Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll? public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); - /** - * Äußerste Liste der Source-Files. - * Danach Liste der Klassen in Source File. - * Danach Map Klassenname - */ - private List>> simplifyResultsSF = new ArrayList<>(); - public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { this(Arrays.asList(sourceFile)); INSTANCE = this; diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 188bb2f1..44bfd2ff 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -1,37 +1,20 @@ package de.dhbwstuttgart.typedeployment; -import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError; -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; import de.dhbwstuttgart.bytecode.utilities.MethodUtility; import de.dhbwstuttgart.bytecode.utilities.Resolver; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.NewArray; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.result.*; -import org.antlr.v4.parse.ANTLRParser.action_return; -import org.antlr.v4.parse.ANTLRParser.block_return; -import org.antlr.v4.parse.ANTLRParser.labeledAlt_return; -import org.antlr.v4.parse.ANTLRParser.parserRule_return; import org.antlr.v4.runtime.Token; import org.objectweb.asm.Type; -import org.stringtemplate.v4.compiler.STParser.ifstat_return; -import java.io.File; -import java.io.IOException; -import java.io.Serializable; import java.util.*; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -import javax.print.DocFlavor.STRING; -import javax.security.auth.kerberos.KerberosKey; /** * TODO: diff --git a/src/test/java/bytecode/FieldTphConsMethTest.java b/src/test/java/bytecode/FieldTphConsMethTest.java index 9f01a3d3..18bb7f2b 100644 --- a/src/test/java/bytecode/FieldTphConsMethTest.java +++ b/src/test/java/bytecode/FieldTphConsMethTest.java @@ -40,7 +40,7 @@ public class FieldTphConsMethTest { Field a = classToTest.getDeclaredField("a"); a.setAccessible(true); - Method m = classToTest.getDeclaredMethod("m", Object.class); + Method m = classToTest.getDeclaredMethod("id", Object.class); Object result = m.invoke(instanceOfClass, 42); assertEquals(42,result); diff --git a/src/test/java/bytecode/OLTest.java b/src/test/java/bytecode/OLTest.java index 2b9ea40b..d9de9f25 100644 --- a/src/test/java/bytecode/OLTest.java +++ b/src/test/java/bytecode/OLTest.java @@ -12,7 +12,7 @@ import java.util.List; import org.junit.BeforeClass; import org.junit.Test; -import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.typeinference.result.ResultSet; diff --git a/src/test/java/bytecode/mathStrucTest.java b/src/test/java/bytecode/mathStrucTest.java new file mode 100644 index 00000000..801de468 --- /dev/null +++ b/src/test/java/bytecode/mathStrucTest.java @@ -0,0 +1,38 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Field; +import java.net.URL; +import java.net.URLClassLoader; + +import org.junit.BeforeClass; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class mathStrucTest { + + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static String pathToClassFile; + private static Object instanceOfClass; + + + @Test + public void test() throws Exception { + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/mathStruc.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"); + pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("mathStruc"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } + +} diff --git a/src/test/resources/bytecode/javFiles/Faculty.jav b/src/test/resources/bytecode/javFiles/Faculty.jav index 6576b3da..9d1e6124 100644 --- a/src/test/resources/bytecode/javFiles/Faculty.jav +++ b/src/test/resources/bytecode/javFiles/Faculty.jav @@ -3,9 +3,9 @@ import java.lang.Integer; //import java.lang.Short; public class Faculty { - public fact; - Faculty() { - fact = (x) -> { + //public fact; + //Faculty() { + public fact = (x) -> { if (x == 1) { return 1; } @@ -13,7 +13,7 @@ public class Faculty { return x * (fact.apply(x-1)); } }; - } + public getFact(x) { @@ -48,4 +48,4 @@ public class Faculty { // return x * m(x-1); // } // } -} +//} diff --git a/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav b/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav index e749bb4f..5a550337 100644 --- a/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav +++ b/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav @@ -1,11 +1,26 @@ public class FieldTphConsMeth { a; - public FieldTphConsMeth(c) { - a = m(c); - } + /*public FieldTphConsMeth(c) { + a = id(c); + }*/ - m(b) { + id(b) { return b; } + + setA(x) { + a = x; + return a; + } + + m(x,y) { + x = id(y); + } + + m2(x,y) { + x = setA(y); + return x; + } + } \ No newline at end of file diff --git a/src/test/resources/bytecode/javFiles/Tph2.jav b/src/test/resources/bytecode/javFiles/Tph2.jav index c957eae6..760e4fa7 100644 --- a/src/test/resources/bytecode/javFiles/Tph2.jav +++ b/src/test/resources/bytecode/javFiles/Tph2.jav @@ -1,6 +1,13 @@ public class Tph2 { + id = x->x; + id3 (x) { + return id.apply(x); + } + + m(a,b){ var c = m2(a,b); + //m2(a,b); return a; } diff --git a/src/test/resources/bytecode/javFiles/Tph3.jav b/src/test/resources/bytecode/javFiles/Tph3.jav index f2bcd2ef..553e2745 100644 --- a/src/test/resources/bytecode/javFiles/Tph3.jav +++ b/src/test/resources/bytecode/javFiles/Tph3.jav @@ -7,7 +7,8 @@ public class Tph3 { // m2(a,b){ // return m(a,b); // } - m1(x, y) { m2(x); x = y; } + m1(x, y) { m2(x); x = y; + } m2(y) { m1(y, y); } } diff --git a/src/test/resources/bytecode/javFiles/Tph4.jav b/src/test/resources/bytecode/javFiles/Tph4.jav index 1eb529de..58fe1d16 100644 --- a/src/test/resources/bytecode/javFiles/Tph4.jav +++ b/src/test/resources/bytecode/javFiles/Tph4.jav @@ -2,11 +2,11 @@ public class Tph4{ m(a,b){ var c = m2(b); var d = m2(c); - return a; + return d; } m2(b){ - return b + return b; } } \ No newline at end of file diff --git a/src/test/resources/bytecode/javFiles/mathStruc.jav b/src/test/resources/bytecode/javFiles/mathStruc.jav new file mode 100644 index 00000000..2a5f9816 --- /dev/null +++ b/src/test/resources/bytecode/javFiles/mathStruc.jav @@ -0,0 +1,11 @@ +class mathStruc { + model; + + //Fun1*, Fun1*,MathStruc >> + innerOp = (o) -> (ms) -> new mathStruc<>(o.apply(model,ms.model)); + + mathStruc(m) { + model =m; + //innerOp = (o) -> (ms) -> new mathStruc<>(o.apply(this.model,ms.model)); + } +} \ No newline at end of file