Merge branch 'bytecodeGenerics' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into bytecodeGenerics

This commit is contained in:
JanUlrich 2020-11-06 15:26:25 +01:00
commit b91aadf24a
4 changed files with 144 additions and 47 deletions

View File

@ -3,7 +3,7 @@ package de.dhbwstuttgart.bytecode.gGenericsAli;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
public class ClassConstraint extends TPHConstraint { public class ClassConstraint extends TPHConstraint {
private TPHConstraint constraint; //private TPHConstraint constraint;
public ClassConstraint(String left, String right, Relation rel) { public ClassConstraint(String left, String right, Relation rel) {
super(left, right, rel); super(left, right, rel);

View File

@ -15,28 +15,38 @@ public class FamilyOfGeneratedGenerics {
public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
List<ClassConstraint> cs_cl = new ArrayList<>(); List<ClassConstraint> cs_cl = new ArrayList<>();
System.out.println("0: " + cs_cl);
List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs, cs_cl, posOfTphs); List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs, cs_cl, posOfTphs);
cs_cl.addAll(classConstraints1); for (ClassConstraint cons: classConstraints1) {
System.out.println("1: " + cs_cl); if (!checkForDuplicates(cons, cs_cl)) {
cs_cl.add(cons);
}
}
List<ClassConstraint> classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher List<ClassConstraint> classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher
cs_cl.addAll(classConstraints2); for (ClassConstraint cons: classConstraints2) {
System.out.println("2: " + cs_cl); if (!checkForDuplicates(cons, cs_cl)) {
cs_cl.add(cons);
}
}
List<ClassConstraint> classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs); List<ClassConstraint> classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs);
cs_cl.addAll(classConstraints3); for (ClassConstraint cons: classConstraints3) {
System.out.println("3: " + cs_cl); if (!checkForDuplicates(cons, cs_cl)) {
cs_cl.add(cons);
}
}
return cs_cl; return cs_cl;
} }
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> constraintsSet) { public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) {
//TODO: Regeln //TODO: Regeln
List<MethodConstraint> cs_m = new ArrayList<>(); List<MethodConstraint> cs_m = new ArrayList<>();
// for(TPHConstraint cons: constraintsSet){ List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs);
// for (MethodConstraint cons: methodConstraints1) {
// cs_m = if (!checkForDuplicates(cons, cs_m)) {
// } cs_m.add(cons);
return null; }
}
List<MethodConstraint> methodConstraints2 = firstTransitiveSubtypeForMethodTypes();
return cs_m;
} }
/** /**
@ -49,7 +59,10 @@ public class FamilyOfGeneratedGenerics {
if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) {
for(String tph: posOfTphs.keySet()) { for(String tph: posOfTphs.keySet()) {
if(tph == allCons.getLeft() && posOfTphs.get(tph) == PositionFinder.Position.FIELD) { if(tph == allCons.getLeft() && posOfTphs.get(tph) == PositionFinder.Position.FIELD) {
tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
if (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd);
}
} }
} }
} }
@ -62,13 +75,15 @@ public class FamilyOfGeneratedGenerics {
* {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs } * {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs }
*/ */
public static List<ClassConstraint> transitiveSubtypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl) { public static List<ClassConstraint> transitiveSubtypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl) {
//TODO:
List<ClassConstraint> tempCC= new ArrayList<>(); List<ClassConstraint> tempCC= new ArrayList<>();
for(ClassConstraint cCons: cs_cl) { for(ClassConstraint cCons: cs_cl) {
if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) { if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) {
for(TPHConstraint allCons: allConstraints) { for(TPHConstraint allCons: allConstraints) {
if(cCons.getRight() == allCons.getLeft() && allCons.getRight() != null && allCons.getRel()==Relation.EXTENDS){ if(cCons.getRight() == allCons.getLeft() && allCons.getRight() != null && allCons.getRel()==Relation.EXTENDS){
tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
if (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd);
}
} }
} }
} }
@ -82,18 +97,19 @@ public class FamilyOfGeneratedGenerics {
* or (\exists T~: (T~ <. T) \in cs_cl)) and (\existsnot T': T <. T') \in cs)} * or (\exists T~: (T~ <. T) \in cs_cl)) and (\existsnot T': T <. T') \in cs)}
*/ */
public static List<ClassConstraint> hasNoSupertypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<String, PositionFinder.Position> posOfTphs) { public static List<ClassConstraint> hasNoSupertypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<String, PositionFinder.Position> posOfTphs) {
//TODO:
List<ClassConstraint> tempCC= new ArrayList<>(); List<ClassConstraint> tempCC= new ArrayList<>();
for(TPHConstraint allCons: allConstraints) { for(TPHConstraint allCons: allConstraints) {
for(ClassConstraint cCons: cs_cl) { for(ClassConstraint cCons: cs_cl) {
for(String tph: posOfTphs.keySet()) { for(String tph: posOfTphs.keySet()) {
System.out.println("cCons: " + cCons);
if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) || if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) ||
(posOfTphs.containsKey(cCons.getLeft()) && cCons.getRight() != null) && (posOfTphs.containsKey(cCons.getRight()) && cCons.getLeft() != null) &&
allCons.getRight() == null && (allCons.getLeft() == tph && allCons.getRight() == null) &&
allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS) { allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS &&
System.out.println("C3: " + allCons); cCons.getRight() == tph && allCons.getLeft() == tph) {
tempCC.add(new ClassConstraint(allCons.getLeft(), "Object", Relation.EXTENDS)); ClassConstraint consToAdd = new ClassConstraint(cCons.getRight(), "Object", Relation.EXTENDS);
if (!checkForDuplicates(consToAdd, tempCC)){
tempCC.add(consToAdd);
}
} }
} }
} }
@ -107,57 +123,101 @@ public class FamilyOfGeneratedGenerics {
/** /**
* Def. FGG: erste Zeile von cs_m * 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} * {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs}
*//* */
public List<MethodConstraint> typeOfTheMethodInClSigma() { // cl_\sigma?? public static List<MethodConstraint> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, List<MethodConstraint> cs_m, HashMap<String, PositionFinder.Position> posOfTphs) { // cl_\sigma??
//TODO: //TODO:
return cs_m; List<MethodConstraint> 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.METHOD || posOfTphs.get(tph) == PositionFinder.Position.CONSTRUCTOR)) {
MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
if (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd);
}
}
}
}
}
return tempCC;
} }
*/
/** /**
* Def. FGG: zweite Zeile von 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} * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs}
*//* */
public List<MethodConstraint> firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs public static List<MethodConstraint> firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs
//TODO: //TODO:
return cs_m; return null;
} }
*/
/** /**
* Def. FGG: dritte Zeile von 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} * {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs}
*//* */
public List<MethodConstraint> secondTransitiveSubtypeForMethodTypes() { public static List<MethodConstraint> secondTransitiveSubtypeForMethodTypes() {
//TODO: //TODO:
return cs_m; return null;
} }
*/
/** /**
* Def. FGG: vierte Zeile von 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), * {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)} * (\existsnot T': T <. T') \in cs)}
*//* */
public List<MethodConstraint> hasNoSupertypeForMethodTypes() { public static List<MethodConstraint> hasNoSupertypeForMethodTypes() {
//TODO: //TODO:
return cs_m; return null;
} }
*/
/** /**
* nimm die Menge cs_cl aus cs_m raus * nimm die Menge cs_cl aus cs_m raus
*//*
public List<MethodConstraint> methodTypesWithoutClassTypes() {
//TODO:
return cs_m;
}
*/ */
public static List<MethodConstraint> methodTypesWithoutClassTypes() {
//TODO:
return null;
}
public static boolean checkForDuplicates(TPHConstraint constraint, List list) {
List<TPHConstraint> tempList = list;
for (TPHConstraint tphC: tempList) {
return (constraint.getLeft() == tphC.getLeft() &&
constraint.getRight() == tphC.getRight() &&
constraint.getRel() == tphC.getRel()); //constraint already in ArrayList if true
}
return false;
}
public static List<TPHConstraint> buildTransitiveClosure(List list) {
List<TPHConstraint> iterList = list;
List<TPHConstraint> runList = list;
List<TPHConstraint> tcList = new ArrayList<>();
boolean addedConToList = false;
for (TPHConstraint cons: iterList) {
for (TPHConstraint cons2: runList) {
if(cons.getRight() == cons2.getLeft()) {
for (TPHConstraint tcCons: tcList) {
if (!checkForDuplicates(tcCons,tcList)) {
tcList.add(new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS)); //Duplikate? dürfte nicht sein -> checken
addedConToList = true;
if (addedConToList) {
list.add(new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS));
buildTransitiveClosure(list);
}
}
//TODO: über aktualisierte Liste laufen wegen Updates -> Rekursion?
}
}
}
}
return tcList;
}

View File

@ -4,6 +4,7 @@ import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint; import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint;
import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics;
import de.dhbwstuttgart.bytecode.gGenericsAli.MethodConstraint;
import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -42,6 +43,10 @@ public class TestExample42 {
@Test @Test
public void genericTest() { public void genericTest() {
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs());
System.out.println(classConstraints); System.out.println("ClassConstraints: " + classConstraints);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),fillPosOfTphs());
System.out.println("MethodConstraints: " + methodConstraints);
List<TPHConstraint> testCons;
} }
} }

View File

@ -0,0 +1,32 @@
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.MethodConstraint;
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 TestTransitiveClosure {
public List<TPHConstraint> fillList() {
List<TPHConstraint> list = new ArrayList<>();
list.add(new TPHConstraint("A", "B", Relation.EXTENDS));
list.add(new TPHConstraint("B", "C", Relation.EXTENDS));
list.add(new TPHConstraint("C", "D", Relation.EXTENDS));
return list;
}
@Test
public void genericTest() {
List<TPHConstraint> testCons = FamilyOfGeneratedGenerics.buildTransitiveClosure(fillList());
System.out.println(testCons);
}
}