modified: src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java

modified:   src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java
	modified:   src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PositionFinder.java
	modified:   src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java
	modified:   src/test/java/insertGenerics/TestExample42.java
This commit is contained in:
AluAli 2020-12-04 14:25:37 +01:00
parent f7101da621
commit d06c6abb5a
5 changed files with 48 additions and 25 deletions

View File

@ -136,7 +136,6 @@ public class GeneratedGenericsFinder implements ASTVisitor {
//PL 2020-10-16: Ab hier GGenerics implementieren durch Ali //PL 2020-10-16: Ab hier GGenerics implementieren durch Ali
//Rueckgabe an generatedGenericsForSF //Rueckgabe an generatedGenericsForSF
fogg = new FamilyOfGeneratedGenerics(tphExtractor); fogg = new FamilyOfGeneratedGenerics(tphExtractor);
// fogg.getClassConstraints();
tphsClass = tphExtractor.tphsClass; tphsClass = tphExtractor.tphsClass;
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass); simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);

View File

@ -12,13 +12,15 @@ import java.util.List;
public class FamilyOfGeneratedGenerics { public class FamilyOfGeneratedGenerics {
public List<TPHConstraint> allConstraints = new ArrayList<>(); public List<TPHConstraint> allConstraints = new ArrayList<>();
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>(); public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>();
public List<ClassConstraint> classConstraints = new ArrayList<>();
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) {
this.allConstraints = tphExtractor.allCons; this.allConstraints = tphExtractor.allCons;
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
this.classConstraints = getClassConstraints(allConstraints,posOfTPHs);
} }
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, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
List<ClassConstraint> cs_cl = new ArrayList<>(); List<ClassConstraint> cs_cl = new ArrayList<>();
List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs, posOfTphs); List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs, posOfTphs);
for (ClassConstraint cons: classConstraints1) { for (ClassConstraint cons: classConstraints1) {
@ -41,7 +43,7 @@ public class FamilyOfGeneratedGenerics {
return cs_cl; return cs_cl;
} }
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//TODO: Regeln //TODO: Regeln
List<MethodConstraint> cs_m = new ArrayList<>(); List<MethodConstraint> cs_m = new ArrayList<>();
List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs); List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs);
@ -58,13 +60,13 @@ public class FamilyOfGeneratedGenerics {
* Def. FGG: erste Zeile von cs_cl * Def. FGG: erste Zeile von cs_cl
* {T < .T' | T is a type variable in a type of a node of a field} * {T < .T' | T is a type variable in a type of a node of a field}
*/ */
public static List<ClassConstraint> typeOfANodeOfAField(List<TPHConstraint> allConstraints, HashMap<String, PositionFinder.Position> posOfTphs) { public static List<ClassConstraint> typeOfANodeOfAField(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//RuntimeException re = new RuntimeException("enthält EQUALS-Relation"); //RuntimeException re = new RuntimeException("enthält EQUALS-Relation");
List<ClassConstraint> tempCC= new ArrayList<>(); List<ClassConstraint> tempCC= new ArrayList<>();
for(TPHConstraint allCons: allConstraints){ for(TPHConstraint allCons: allConstraints){
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).fst == PositionFinder.Position.FIELD) {
ClassConstraint consToAdd = new ClassConstraint(tph, allCons.getRight(), allCons.getRel()); ClassConstraint consToAdd = new ClassConstraint(tph, allCons.getRight(), allCons.getRel());
if (!checkForDuplicates(consToAdd, tempCC)) { if (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd); tempCC.add(consToAdd);
@ -105,12 +107,12 @@ public class FamilyOfGeneratedGenerics {
* {T <. Object | ((T is a type variable in a type of a node of a field * {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)} * 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, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
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()) {
boolean tvInField = posOfTphs.get(tph) == PositionFinder.Position.FIELD; boolean tvInField = posOfTphs.get(tph).fst == PositionFinder.Position.FIELD;
boolean hasSmallerTVInClCons = (posOfTphs.containsKey(cCons.getRight()) && cCons.getRight() == tph && cCons.getLeft() != null); boolean hasSmallerTVInClCons = (posOfTphs.containsKey(cCons.getRight()) && cCons.getRight() == tph && cCons.getLeft() != null);
if( ((tvInField || hasSmallerTVInClCons) && cCons.getRel()==Relation.EXTENDS) && if( ((tvInField || hasSmallerTVInClCons) && cCons.getRel()==Relation.EXTENDS) &&
checkUpperBound(allConstraints, tph) && allCons.getRel()==Relation.EXTENDS) { checkUpperBound(allConstraints, tph) && allCons.getRel()==Relation.EXTENDS) {
@ -139,13 +141,13 @@ public class FamilyOfGeneratedGenerics {
* {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 static List<MethodConstraint> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, List<MethodConstraint> cs_m, HashMap<String, PositionFinder.Position> posOfTphs) { // cl_\sigma?? public static List<MethodConstraint> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, List<MethodConstraint> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // cl_\sigma??
//TODO: //TODO:
List<MethodConstraint> tempCC= new ArrayList<>(); List<MethodConstraint> tempCC= new ArrayList<>();
for(TPHConstraint allCons: allConstraints){ for(TPHConstraint allCons: allConstraints){
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.METHOD || posOfTphs.get(tph) == PositionFinder.Position.CONSTRUCTOR)) { if(tph == allCons.getLeft() && (posOfTphs.get(tph).fst == PositionFinder.Position.METHOD || posOfTphs.get(tph).fst == PositionFinder.Position.CONSTRUCTOR)) {
MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
if (!checkForDuplicates(consToAdd, tempCC)) { if (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd); tempCC.add(consToAdd);

View File

@ -10,6 +10,7 @@ public class PositionFinder{
static HashMap<String, PairTphMethod<Position, String>> posOfTphs = new HashMap<String, PairTphMethod<Position, String>>(); static HashMap<String, PairTphMethod<Position, String>> posOfTphs = new HashMap<String, PairTphMethod<Position, String>>();
static PairTphMethod<Position, String> whichMethod; // gibt an, in welcher Methode sich TPH befindet (Position.METHOD, id_of_method) static PairTphMethod<Position, String> whichMethod; // gibt an, in welcher Methode sich TPH befindet (Position.METHOD, id_of_method)
public enum Position{ public enum Position{
METHOD, METHOD,
CONSTRUCTOR, CONSTRUCTOR,

View File

@ -3,6 +3,7 @@ package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.*; import de.dhbwstuttgart.bytecode.insertGenerics.*;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.ArrayList; import java.util.ArrayList;
@ -11,26 +12,31 @@ import java.util.List;
public class FamilyOfGeneratedGenericsTest extends TestCase { public class FamilyOfGeneratedGenericsTest extends TestCase {
/*
public void testIdentityMethod(){ public void testIdentityMethod(){
/* */
/*
Example method: Example method:
A id(B i) return i; A id(B i) return i;
gives constraint: B <. A, which is a method constraint gives constraint: B <. A, which is a method constraint
*/ *//*
List<TPHConstraint> inputConstraints = new ArrayList<>(); List<TPHConstraint> inputConstraints = new ArrayList<>();
inputConstraints.add(new TPHConstraint("B", "A", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("B", "A", TPHConstraint.Relation.EXTENDS));
HashMap<String, PositionFinder.Position> tphPositions = new HashMap<>(); HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
tphPositions.put("A", PositionFinder.Position.METHOD); tphPositions.put("A", PositionFinder.Position.METHOD);
tphPositions.put("B", PositionFinder.Position.METHOD); tphPositions.put("B", PositionFinder.Position.METHOD);
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions); List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
assertTrue(classConstraints.isEmpty()); assertTrue(classConstraints.isEmpty());
/*
MethodConstraints should be the same as the input constraint
*/ */
/*
MethodConstraints should be the same as the input constraint
*//*
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, tphPositions); List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, tphPositions);
assertTrue(methodConstraints.size() == 1); assertTrue(methodConstraints.size() == 1);
assertTrue(methodConstraints.get(0).getLeft().equals("B")); assertTrue(methodConstraints.get(0).getLeft().equals("B"));
@ -38,7 +44,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
} }
public void testClassField(){ public void testClassField(){
/* */
/*
class Example{ class Example{
A f; A f;
B fReturn(){ B fReturn(){
@ -46,18 +53,21 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
} }
} }
gives constraint: A <. B, which is a class constraint gives constraint: A <. B, which is a class constraint
*/ *//*
List<TPHConstraint> inputConstraints = new ArrayList<>(); List<TPHConstraint> inputConstraints = new ArrayList<>();
inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS));
HashMap<String, PositionFinder.Position> tphPositions = new HashMap<>(); HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
tphPositions.put("A", PositionFinder.Position.FIELD); tphPositions.put("A", PositionFinder.Position.FIELD);
tphPositions.put("B", PositionFinder.Position.METHOD); tphPositions.put("B", PositionFinder.Position.METHOD);
/*
ClassConstraints should not be the same as the input constraint
*/ */
/*
ClassConstraints should not be the same as the input constraint
*//*
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions); List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
System.out.println(classConstraints); System.out.println(classConstraints);
assertTrue(classConstraints.size() == 2); assertTrue(classConstraints.size() == 2);
@ -66,7 +76,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
} }
public void testSecondLineOfClassConstraints() { public void testSecondLineOfClassConstraints() {
/* */
/*
class Example() { class Example() {
A a; A a;
B b = a; B b = a;
@ -80,7 +91,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
return e; return e;
} }
} }
*/ *//*
List<TPHConstraint> inputConstraints = new ArrayList<>(); List<TPHConstraint> inputConstraints = new ArrayList<>();
inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS));
@ -89,7 +101,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
inputConstraints.add(new TPHConstraint("A", "E", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("A", "E", TPHConstraint.Relation.EXTENDS));
inputConstraints.add(new TPHConstraint("E", "D", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("E", "D", TPHConstraint.Relation.EXTENDS));
HashMap<String, PositionFinder.Position> tphPositions = new HashMap<>(); HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
tphPositions.put("A", PositionFinder.Position.FIELD); tphPositions.put("A", PositionFinder.Position.FIELD);
tphPositions.put("B", PositionFinder.Position.FIELD); tphPositions.put("B", PositionFinder.Position.FIELD);
tphPositions.put("C", PositionFinder.Position.METHOD); tphPositions.put("C", PositionFinder.Position.METHOD);
@ -105,7 +117,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
} }
public void testTPHsAndGenerics() { public void testTPHsAndGenerics() {
/* */
/*
class TPHsAndGenerics { class TPHsAndGenerics {
Fun1<A,B> id = x -> x; Fun1<A,B> id = x -> x;
C id2 (D x) { C id2 (D x) {
@ -119,7 +132,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
return b; return b;
} }
} }
*/ *//*
List<TPHConstraint> inputConstraints = new ArrayList<>(); List<TPHConstraint> inputConstraints = new ArrayList<>();
inputConstraints.add(new TPHConstraint("A","B", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("A","B", TPHConstraint.Relation.EXTENDS));
@ -130,7 +144,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
inputConstraints.add(new TPHConstraint("G","J", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("G","J", TPHConstraint.Relation.EXTENDS));
inputConstraints.add(new TPHConstraint("J","H", TPHConstraint.Relation.EXTENDS)); inputConstraints.add(new TPHConstraint("J","H", TPHConstraint.Relation.EXTENDS));
HashMap<String, PositionFinder.Position> tphPositions = new HashMap<>(); HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
tphPositions.put("A", PositionFinder.Position.FIELD); tphPositions.put("A", PositionFinder.Position.FIELD);
tphPositions.put("B", PositionFinder.Position.FIELD); tphPositions.put("B", PositionFinder.Position.FIELD);
tphPositions.put("C", PositionFinder.Position.METHOD); tphPositions.put("C", PositionFinder.Position.METHOD);
@ -148,6 +162,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
assertFalse(classConstraints.isEmpty()); assertFalse(classConstraints.isEmpty());
assertTrue(classConstraints.size() == 3); assertTrue(classConstraints.size() == 3);
} }
*/
public void testPositionConverter() { public void testPositionConverter() {
@ -155,8 +170,12 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
List<MethodAndTPH> listOfMethodsAndTphs = new ArrayList<>(); List<MethodAndTPH> listOfMethodsAndTphs = new ArrayList<>();
allTphsOld.put("A", true); allTphsOld.put("A", true);
allTphsOld.put("B", false); allTphsOld.put("B", false);
MethodAndTPH m1 = new MethodAndTPH("m1");
listOfMethodsAndTphs.add(new MethodAndTPH("bla")); listOfMethodsAndTphs.add(new MethodAndTPH("bla"));
listOfMethodsAndTphs.add(new MethodAndTPH("blubb")); listOfMethodsAndTphs.add(new MethodAndTPH("blubb"));
listOfMethodsAndTphs.add(m1);

View File

@ -1,3 +1,4 @@
/*
package insertGenerics; package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
@ -50,3 +51,4 @@ public class TestExample42 {
List<TPHConstraint> testCons; List<TPHConstraint> testCons;
} }
} }
*/