diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java index 13387548..7f86e935 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java @@ -3,7 +3,13 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; public class ClassConstraint extends TPHConstraint { + private TPHConstraint constraint; + public ClassConstraint(String left, String right, Relation rel) { super(left, right, rel); } + //besser? + /*public ClassConstraint(TPHConstraint constraint) { + this.constraint = constraint; + }*/ } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index ece7b32f..fa626c48 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -3,76 +3,77 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public class FamilyOfGeneratedGenerics { - public List cs; - public List cs_cl; - public List cs_m; - public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { - cs = (List) tphExtractor.allCons; - cs_cl = new ArrayList(); - cs_m = new ArrayList(); - } - -// public List getFamilyOfGeneratedGenerics(){ -// -// } - - public void addConstraintToClassConstraint() { //Inputparameter List constraintsSet weg - List classConstraints1 = typeOfANodeOfAField(cs); + public static List getClassConstraints(List cs, HashMap posOfTphs) { //Inputparameter List constraintsSet weg + List cs_cl = new ArrayList<>(); + System.out.println("0: " + cs_cl); + List classConstraints1 = typeOfANodeOfAField(cs, cs_cl, posOfTphs); cs_cl.addAll(classConstraints1); + System.out.println("1: " + cs_cl); List classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher cs_cl.addAll(classConstraints2); - List classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl); + System.out.println("2: " + cs_cl); + List classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs); cs_cl.addAll(classConstraints3); + System.out.println("3: " + cs_cl); + + return cs_cl; } - public void addConstraintToMethodConstraint(List constraintsSet) { + public static List getMethodConstraints(List constraintsSet) { //TODO: Regeln + List cs_m = new ArrayList<>(); // for(TPHConstraint cons: constraintsSet){ // // cs_m = // } - + return null; } /** * Def. FGG: erste Zeile von cs_cl * {T < .T' | T is a type variable in a type of a node of a field} */ - public List typeOfANodeOfAField(List cs) { - //TODO: - for(TPHConstraint cons: cs){ - System.out.println(cons); - System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - if(cons.getRight()!=null && cons.getRel()==Relation.EXTENDS) { - cs_cl.add(new ClassConstraint(cons.getLeft(), cons.getRight(),cons.getRel())); + public static List typeOfANodeOfAField(List allConstraints, List cs_cl, HashMap posOfTphs) { + List tempCC= new ArrayList<>(); + for(TPHConstraint allCons: allConstraints){ + if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { + for(String tph: posOfTphs.keySet()) { + if(tph == allCons.getLeft() && posOfTphs.get(tph) == PositionFinder.Position.FIELD) { + tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); + } + } } } - System.out.println(cs_cl); - System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - return cs_cl; + return tempCC; } /** * Def. FGG: zweite Zeile von cs_cl * {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs } */ - public List transitiveSubtypeForClassTypes(List allConstraints, List classConstraints) { + public static List transitiveSubtypeForClassTypes(List allConstraints, List cs_cl) { //TODO: - for(ClassConstraint cCons: classConstraints) { - for(TPHConstraint allCons: allConstraints) -// if(tphExtractor.containsConstraint(allConstraints, cCons)) { -// } - if(cCons.getRight() == allCons.getLeft()){ - + List tempCC= new ArrayList<>(); + for(ClassConstraint cCons: cs_cl) { + if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) { + for(TPHConstraint allCons: allConstraints) { + if(cCons.getRight() == allCons.getLeft() && allCons.getRight() != null && allCons.getRel()==Relation.EXTENDS){ + tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); + } + } } } - return cs_cl; + return tempCC; } /** @@ -80,56 +81,83 @@ public class FamilyOfGeneratedGenerics { * {T <. Object | ((T is a type variable in a type of a node of a field * or (\exists T~: (T~ <. T) \in cs_cl)) and (\existsnot T': T <. T') \in cs)} */ - public List hasNoSupertypeForClassTypes(List allConstraints, List classConstraints) { + public static List hasNoSupertypeForClassTypes(List allConstraints, List cs_cl, HashMap posOfTphs) { //TODO: - return cs_cl; + List tempCC= new ArrayList<>(); + for(TPHConstraint allCons: allConstraints) { + for(ClassConstraint cCons: cs_cl) { + for(String tph: posOfTphs.keySet()) { + System.out.println("cCons: " + cCons); + if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) || + (posOfTphs.containsKey(cCons.getLeft()) && cCons.getRight() != null) && + allCons.getRight() == null && + allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS) { + System.out.println("C3: " + allCons); + tempCC.add(new ClassConstraint(allCons.getLeft(), "Object", Relation.EXTENDS)); + } + } + } + } + return tempCC; } +/* - /** + */ +/** * Def. FGG: erste Zeile von cs_m * {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs} - */ + *//* + public List typeOfTheMethodInClSigma() { // cl_\sigma?? //TODO: return cs_m; } - /** + */ +/** * Def. FGG: zweite Zeile von cs_m * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs} - */ + *//* + public List firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs //TODO: return cs_m; } - /** + */ +/** * Def. FGG: dritte Zeile von cs_m * {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs} - */ + *//* + public List secondTransitiveSubtypeForMethodTypes() { //TODO: return cs_m; } - /** + */ +/** * Def. FGG: vierte Zeile von cs_m * {T <. Object | (T is a type variable in a type of a node of the method/constructor m in cl_\sigma), * (\existsnot T': T <. T') \in cs)} - */ + *//* + public List hasNoSupertypeForMethodTypes() { //TODO: return cs_m; } - /** + */ +/** * nimm die Menge cs_cl aus cs_m raus - */ + *//* + public List methodTypesWithoutClassTypes() { //TODO: return cs_m; } +*/ @@ -145,5 +173,4 @@ public class FamilyOfGeneratedGenerics { } */ - } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/PositionFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/PositionFinder.java new file mode 100644 index 00000000..ba9585f0 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/PositionFinder.java @@ -0,0 +1,76 @@ +package de.dhbwstuttgart.bytecode.gGenericsAli; + +import de.dhbwstuttgart.bytecode.TPHExtractor; +import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +import java.util.HashMap; +import java.util.Set; + +public class PositionFinder{ + static HashMap posOfTphs = new HashMap(); + + public enum Position{ + METHOD, + CONSTRUCTOR, + FIELD + } + + public static HashMap getPositionOfTPH(SourceFile sf, Set tphs) { + + new Walker().visit(sf); + for (String tph: posOfTphs.keySet()) { + System.out.println(tph + " " + posOfTphs.get(tph)); + } + + return null; + } + public static void putPositionInMethod(String tph) { + posOfTphs.put(tph, Position.METHOD); + } + + public static void putPositionInField(String tph) { + posOfTphs.put(tph, Position.FIELD); + } + + public static void putPositionInConstructor(String tph) { + posOfTphs.put(tph, Position.CONSTRUCTOR); + } + + + + static class Walker extends AbstractASTWalker{ + Boolean inMethod = false; + Boolean inConstructor = false; + + @Override + public void visit(TypePlaceholder tph) { + if (inMethod) { + if (inConstructor) { + putPositionInConstructor(tph.getName()); + } + putPositionInMethod(tph.getName()); + } else { + putPositionInField(tph.getName()); + } + } + + @Override + public void visit(Field field) { + super.visit(field); + } + + @Override + public void visit(Method method) { + inMethod = true; + super.visit(method); + } + + @Override + public void visit(Constructor cons) { + inConstructor = true; + super.visit(cons); + } + } + +} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index 8672ce8c..00b18273 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -135,8 +135,8 @@ public class GeneratedGenericsFinder implements ASTVisitor { classOrInterface.accept(tphExtractor); //PL 2020-10-16: Ab hier GGenerics implementieren durch Ali //Rueckgabe an generatedGenericsForSF - fogg = new FamilyOfGeneratedGenerics(tphExtractor); - fogg.addConstraintToClassConstraint(); +// fogg = new FamilyOfGeneratedGenerics(tphExtractor); +// fogg.getClassConstraints(); tphsClass = tphExtractor.tphsClass; simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass); @@ -152,8 +152,8 @@ public class GeneratedGenericsFinder implements ASTVisitor { if(ggResult != null) generatedGenericsForSF.addGenericGeneratorResultClass(ggResult); - - + + } } diff --git a/src/test/java/constraintSimplify/FamilyOfGenerics.java b/src/test/java/constraintSimplify/FamilyOfGenerics.java index f13efa53..83561450 100644 --- a/src/test/java/constraintSimplify/FamilyOfGenerics.java +++ b/src/test/java/constraintSimplify/FamilyOfGenerics.java @@ -1,13 +1,20 @@ package constraintSimplify; +import de.dhbwstuttgart.bytecode.TPHExtractor; +import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; +import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.result.ResultSet; import org.antlr.v4.runtime.Token; +import org.junit.Assert; import org.junit.Test; import java.io.File; @@ -24,7 +31,11 @@ public class FamilyOfGenerics { @Test public void generateBC() throws Exception { - + SourceFile sf = generateAST(); + PositionFinder.getPositionOfTPH(sf, null); + TPHExtractor tphExtractor = new TPHExtractor(); + List results = new ArrayList(); + GeneratedGenericsFinder generatedGenericsFinder = new GeneratedGenericsFinder(sf, results); } public static SourceFile generateAST(){ @@ -32,7 +43,9 @@ public class FamilyOfGenerics { ArrayList fields = new ArrayList<>(); ArrayList methods = new ArrayList<>(); - methods.add(generateMethod()); + fields.add(generateField("fld1")); + String[] paramNames = {"a"}; + methods.add(generateMethod("testMethode", paramNames)); classes.add(new ClassOrInterface(Modifier.PUBLIC, new JavaClassName("Test"), fields, Optional.empty(), methods, new ArrayList<>(), generateEmptyGenericDeclList(), @@ -42,16 +55,23 @@ public class FamilyOfGenerics { return new SourceFile("Test.jav", classes, new HashSet<>()); } - public static Method generateMethod(){ + public static Method generateMethod(String methodName, String[] paramNames){ ArrayList parameters = new ArrayList<>(); - FormalParameter fp = new FormalParameter("param1", TypePlaceholder.fresh(new NullToken()), new NullToken()); - parameters.add(fp); + for(String str: paramNames) { + FormalParameter fp = new FormalParameter(str, TypePlaceholder.fresh(new NullToken()), new NullToken()); + parameters.add(fp); + + } ParameterList parameterList = new ParameterList(parameters, new NullToken()); - return new Method(Modifier.PUBLIC, "testMethode", TypePlaceholder.fresh(new NullToken()), parameterList, + return new Method(Modifier.PUBLIC, methodName, TypePlaceholder.fresh(new NullToken()), parameterList, new Block(new ArrayList<>(), new NullToken()), generateEmptyGenericDeclList(), new NullToken()); } public static GenericDeclarationList generateEmptyGenericDeclList(){ return new GenericDeclarationList(new ArrayList<>(), new NullToken()); } + + public static Field generateField(String fieldName) { + return new Field(fieldName, TypePlaceholder.fresh(new NullToken()), Modifier.PUBLIC, new NullToken()); + } } diff --git a/src/test/java/insertGenerics/TestExample42.java b/src/test/java/insertGenerics/TestExample42.java new file mode 100644 index 00000000..aded8519 --- /dev/null +++ b/src/test/java/insertGenerics/TestExample42.java @@ -0,0 +1,47 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint; +import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TestExample42 { + public List fillConstraintsList() { + List cs = new ArrayList<>(); + cs.add(new TPHConstraint("M", "N", Relation.EXTENDS)); + cs.add(new TPHConstraint("N", "Z", Relation.EXTENDS)); + cs.add(new TPHConstraint("Q", "K", Relation.EXTENDS)); + cs.add(new TPHConstraint("K", "P", Relation.EXTENDS)); + cs.add(new TPHConstraint("W", "M", Relation.EXTENDS)); + cs.add(new TPHConstraint("Z", "V", Relation.EXTENDS)); + return cs; + } + + public HashMap fillPosOfTphs() { + HashMap posOfTphs = new HashMap<>(); + posOfTphs.put("K", PositionFinder.Position.FIELD); + posOfTphs.put("L", PositionFinder.Position.METHOD); + posOfTphs.put("M", PositionFinder.Position.METHOD); + posOfTphs.put("N", PositionFinder.Position.METHOD); + posOfTphs.put("P", PositionFinder.Position.METHOD); + posOfTphs.put("Q", PositionFinder.Position.METHOD); + posOfTphs.put("U", PositionFinder.Position.METHOD); + posOfTphs.put("V", PositionFinder.Position.METHOD); + posOfTphs.put("W", PositionFinder.Position.METHOD); + posOfTphs.put("Z", PositionFinder.Position.METHOD); + return posOfTphs; + } + + @Test + public void genericTest() { + List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); + System.out.println(classConstraints); + } +}