Compare commits
45 Commits
bigRefacto
...
bytecodeGe
Author | SHA1 | Date | |
---|---|---|---|
88175f8b48 | |||
2f37bb7313 | |||
a04316f629 | |||
3b062de612 | |||
c32ef1e31f | |||
5ed6a4617a | |||
7139a1709e | |||
e9bcea4b9e | |||
d671e74fcf | |||
c2f0368d2e | |||
d06c6abb5a | |||
f7101da621 | |||
c0c24eed3b | |||
af36b43786 | |||
997d3b9bea | |||
ed7cc55139 | |||
29a7f2e7fb | |||
4ac67120a4 | |||
f0db6cb901 | |||
2e5581ab6e | |||
e8ac8e3c5a | |||
a4bc49f5c4 | |||
9cf7cec37b | |||
531b1ccd22 | |||
7ced6338cb | |||
0550f73ef7 | |||
b91aadf24a | |||
70b6c35731 | |||
e617a0911a | |||
7cf46b02b7 | |||
8cf57ba35b | |||
486d7d9011 | |||
8402d18f83 | |||
8d25920a87 | |||
ab9e9e16bd | |||
b45964ed5f | |||
0ecd3916f8 | |||
c86fe95da5 | |||
d677d053d6 | |||
5060cca6db | |||
7900449897 | |||
f6154b8f2c | |||
1d7c894f39 | |||
61f35f8f8b | |||
1e7c25805e |
src
main
java
de
dhbwstuttgart
bytecode
genericsGenerator
insertGenerics
parser
scope
syntaxtree
typeinference
unify
model
test
java
constraintSimplify
insertGenerics
resources
insertGenericsJav
@ -9,6 +9,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.TPHExtractor;
|
||||
import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics;
|
||||
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
|
||||
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass;
|
||||
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.MethodAndConstraints;
|
||||
@ -74,6 +75,7 @@ public class GeneratedGenericsFinder implements ASTVisitor {
|
||||
private GenericGenratorResultForSourceFile generatedGenericsForSF;//Ergebnis des GGenerics
|
||||
private ResultSet resultSet;
|
||||
private final List<String> methodNameAndParamsT = new ArrayList<>();
|
||||
private FamilyOfGeneratedGenerics fogg;
|
||||
|
||||
private String pkgName;
|
||||
private JavaClassName className;
|
||||
@ -131,6 +133,10 @@ public class GeneratedGenericsFinder implements ASTVisitor {
|
||||
tphExtractor.setResultSet(resultSet);
|
||||
resolver = new Resolver(resultSet);
|
||||
classOrInterface.accept(tphExtractor);
|
||||
//PL 2020-10-16: Ab hier GGenerics implementieren durch Ali
|
||||
//Rueckgabe an generatedGenericsForSF
|
||||
fogg = new FamilyOfGeneratedGenerics(tphExtractor);
|
||||
|
||||
tphsClass = tphExtractor.tphsClass;
|
||||
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
|
||||
if(!isVisited) {
|
||||
@ -145,8 +151,8 @@ public class GeneratedGenericsFinder implements ASTVisitor {
|
||||
|
||||
if(ggResult != null)
|
||||
generatedGenericsForSF.addGenericGeneratorResultClass(ggResult);
|
||||
|
||||
|
||||
System.out.println("ddd");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
package de.dhbwstuttgart.bytecode.insertGenerics;
|
||||
|
||||
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;
|
||||
}*/
|
||||
}
|
414
src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java
Normal file
414
src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java
Normal file
@ -0,0 +1,414 @@
|
||||
package de.dhbwstuttgart.bytecode.insertGenerics;
|
||||
|
||||
import com.ibm.icu.text.CurrencyMetaInfo;
|
||||
import de.dhbwstuttgart.bytecode.TPHExtractor;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class FamilyOfGeneratedGenerics {
|
||||
public List<TPHConstraint> allConstraints = new ArrayList<>();
|
||||
// HashMap speichert ob TPH in einer Methode oder in der Klasse ist; und wenn es in der Methode ist, in welcher Methode
|
||||
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>();
|
||||
public List<ClassConstraint> classConstraints = new ArrayList<>();
|
||||
public List<MethodConstraint> methodConstraints = new ArrayList<>();
|
||||
public HashMap<MethodConstraint, String> methodsWithPosition = new HashMap<>();
|
||||
|
||||
|
||||
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) {
|
||||
this.allConstraints = tphExtractor.allCons;
|
||||
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
|
||||
this.classConstraints = getClassConstraints(allConstraints,posOfTPHs);
|
||||
// this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs);
|
||||
|
||||
}
|
||||
|
||||
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> classConstraints1 = typeOfANodeOfAField(cs, posOfTphs);
|
||||
for (ClassConstraint cons: classConstraints1) {
|
||||
if (!checkForDuplicates(cons, cs_cl)) {
|
||||
cs_cl.add(cons);
|
||||
}
|
||||
}
|
||||
List<ClassConstraint> classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher
|
||||
for (ClassConstraint cons: classConstraints2) {
|
||||
if (!checkForDuplicates(cons, cs_cl)) {
|
||||
cs_cl.add(cons);
|
||||
}
|
||||
}
|
||||
List<ClassConstraint> classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs);
|
||||
for (ClassConstraint cons: classConstraints3) {
|
||||
if (!checkForDuplicates(cons, cs_cl)) {
|
||||
cs_cl.add(cons);
|
||||
}
|
||||
}
|
||||
return cs_cl;
|
||||
}
|
||||
|
||||
// public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
// //TODO: Regeln
|
||||
// List<MethodConstraint> cs_m = new ArrayList<>();
|
||||
// List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
|
||||
// for (MethodConstraint cons: methodConstraints1) {
|
||||
// if (!checkForDuplicates(cons, cs_m)) {
|
||||
// cs_m.add(cons);
|
||||
// }
|
||||
// }
|
||||
// List<MethodConstraint> methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m);
|
||||
// for (MethodConstraint cons: methodConstraints2) {
|
||||
// if (!checkForDuplicates(cons, cs_m)) {
|
||||
// cs_m.add(cons);
|
||||
// }
|
||||
// }
|
||||
// List<MethodConstraint> methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m);
|
||||
// for (MethodConstraint cons: methodConstraints3) {
|
||||
// if (!checkForDuplicates(cons, cs_m)) {
|
||||
// cs_m.add(cons);
|
||||
// }
|
||||
// }
|
||||
// List<MethodConstraint> methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs);
|
||||
// for (MethodConstraint cons: methodConstraints4) {
|
||||
// if (!checkForDuplicates(cons, cs_m)) {
|
||||
// cs_m.add(cons);
|
||||
// }
|
||||
// }
|
||||
// List<MethodConstraint> methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m);
|
||||
// cs_m = methodConstraints5;
|
||||
// return cs_m;
|
||||
// }
|
||||
|
||||
public static HashMap<MethodConstraint, String> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
//TODO: Regeln
|
||||
HashMap<MethodConstraint, String> cs_m = new HashMap<>();
|
||||
HashMap<MethodConstraint, String> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
|
||||
for (MethodConstraint cons: methodConstraints1.keySet()) {
|
||||
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
|
||||
cs_m.put(cons,);
|
||||
}
|
||||
}
|
||||
HashMap<MethodConstraint, String> methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m);
|
||||
for (MethodConstraint cons: methodConstraints2.keySet()) {
|
||||
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
|
||||
cs_m.add(cons);
|
||||
}
|
||||
}
|
||||
HashMap<MethodConstraint, String> methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m);
|
||||
for (MethodConstraint cons: methodConstraints3.keySet()) {
|
||||
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
|
||||
cs_m.add(cons);
|
||||
}
|
||||
}
|
||||
HashMap<MethodConstraint, String> methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs);
|
||||
for (MethodConstraint cons: methodConstraints4.keySet()) {
|
||||
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
|
||||
cs_m.add(cons);
|
||||
}
|
||||
}
|
||||
HashMap<MethodConstraint, String> methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m);
|
||||
cs_m = methodConstraints5;
|
||||
return cs_m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Def. FGG: erste Zeile von cs_cl
|
||||
* {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, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
//RuntimeException re = new RuntimeException("enthält EQUALS-Relation");
|
||||
List<ClassConstraint> 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).fst == PositionFinder.Position.FIELD) {
|
||||
ClassConstraint consToAdd = new ClassConstraint(tph, allCons.getRight(), allCons.getRel());
|
||||
if (!checkForDuplicates(consToAdd, tempCC)) {
|
||||
tempCC.add(consToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/*else if (allCons.getRel() != Relation.EXTENDS) {
|
||||
throw re;
|
||||
}*/
|
||||
}
|
||||
return tempCC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Def. FGG: zweite Zeile von cs_cl
|
||||
* {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) {
|
||||
List<ClassConstraint> 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){
|
||||
ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
|
||||
if (!checkForDuplicates(consToAdd, tempCC)) {
|
||||
tempCC.add(consToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tempCC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Def. FGG: dritte Zeile von cs_cl
|
||||
* {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 static List<ClassConstraint> hasNoSupertypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
List<ClassConstraint> tempCC= new ArrayList<>();
|
||||
for(TPHConstraint allCons: allConstraints) {
|
||||
for(ClassConstraint cCons: cs_cl) {
|
||||
for(String tph: posOfTphs.keySet()) {
|
||||
boolean tvInField = posOfTphs.get(tph).fst == PositionFinder.Position.FIELD;
|
||||
boolean hasSmallerTVInClCons = (posOfTphs.containsKey(cCons.getRight()) && cCons.getRight() == tph && cCons.getLeft() != null);
|
||||
if( ((tvInField || hasSmallerTVInClCons) && cCons.getRel()==Relation.EXTENDS) &&
|
||||
checkUpperBound(allConstraints, tph) && allCons.getRel()==Relation.EXTENDS) {
|
||||
ClassConstraint consToAdd = new ClassConstraint(tph, "Object", Relation.EXTENDS);
|
||||
if (!checkForDuplicates(consToAdd, tempCC)){
|
||||
tempCC.add(consToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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 static HashMap<MethodConstraint, String> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // cl_\sigma??
|
||||
//TODO:
|
||||
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
|
||||
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).fst == PositionFinder.Position.METHOD || posOfTphs.get(tph).fst == PositionFinder.Position.CONSTRUCTOR)) {
|
||||
MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
|
||||
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
|
||||
tempMC.put(consToAdd, posOfTphs.get(tph).snd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tempMC;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 static HashMap<MethodConstraint, String> firstTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<MethodConstraint, String> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //transitive closure of cs
|
||||
//TODO:
|
||||
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
|
||||
List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints);
|
||||
for(MethodConstraint mC1 : cs_m.keySet()) { //(R <. R')
|
||||
for(MethodConstraint mC2 : cs_m.keySet()) { //(S <. S')
|
||||
String lSide = mC1.getRight(); //R'
|
||||
String rSide = mC2.getLeft(); //S
|
||||
for(TPHConstraint tphC : tcOfCs) {
|
||||
if(tphC.getLeft().equals(lSide)&&tphC.getRight().equals(rSide)) { //is it (R',S)
|
||||
MethodConstraint consToAdd = new MethodConstraint(lSide, rSide, tphC.getRel()); //create (R'<.S)
|
||||
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
|
||||
tempMC.put(consToAdd, posOfTphs.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tempMC;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 static HashMap<MethodConstraint, String> secondTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<MethodConstraint, String> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
//TODO:
|
||||
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
|
||||
List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints);
|
||||
for(ClassConstraint cC : cs_cl) {
|
||||
for(MethodConstraint mC : cs_m.keySet()) {
|
||||
String leftSide = mC.getRight();
|
||||
String rightSide = cC.getLeft();
|
||||
for(TPHConstraint tphC : tcOfCs) {
|
||||
if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) {
|
||||
MethodConstraint consToAdd = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel());
|
||||
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
|
||||
tempMC.put(consToAdd, posOfTphs.get());
|
||||
// System.out.println(consToAdd);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tempMC;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 static HashMap<MethodConstraint, String> hasNoSupertypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
//TODO:
|
||||
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
|
||||
for(String tph: posOfTphs.keySet()) {
|
||||
for(TPHConstraint allCons: allConstraints) {
|
||||
if((posOfTphs.get(tph).fst.equals(PositionFinder.Position.METHOD) || posOfTphs.get(tph).fst.equals(PositionFinder.Position.CONSTRUCTOR)) && checkUpperBound(allConstraints,tph)) {
|
||||
MethodConstraint consToAdd = new MethodConstraint(tph, "Object", Relation.EXTENDS);
|
||||
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
|
||||
tempMC.put(consToAdd, posOfTphs.get(tph).snd);
|
||||
// System.out.println(consToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tempMC;
|
||||
}
|
||||
|
||||
/**
|
||||
* nimm die Menge cs_cl aus cs_m raus
|
||||
*/
|
||||
public static HashMap<MethodConstraint, String> methodTypesWithoutClassTypes(List<ClassConstraint> cs_cl, HashMap<MethodConstraint, String> cs_m) {
|
||||
//TODO:
|
||||
List<TPHConstraint> tempCC = new ArrayList<>();
|
||||
for(ClassConstraint cc: cs_cl) {
|
||||
TPHConstraint tphC = new TPHConstraint(cc.getLeft(), cc.getRight(), cc.getRel());
|
||||
tempCC.add(tphC);
|
||||
}
|
||||
List<TPHConstraint> tempMC = new ArrayList<>();
|
||||
for(MethodConstraint mc: cs_m.keySet()) {
|
||||
TPHConstraint tphC = new TPHConstraint(mc.getLeft(), mc.getRight(), mc.getRel());
|
||||
tempMC.add(tphC);
|
||||
}
|
||||
List<TPHConstraint> tempMC2 = new ArrayList<>();
|
||||
tempMC2.addAll(tempMC);
|
||||
List<MethodConstraint> tempMCToReturn = new ArrayList<>();
|
||||
|
||||
for(TPHConstraint cc: tempCC) {
|
||||
for(TPHConstraint mc: tempMC) {
|
||||
if(cc.getLeft().equals(mc.getLeft()) && cc.getRight().equals(mc.getRight())) {
|
||||
tempMC2.remove(mc);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(TPHConstraint tphC: tempMC2) {
|
||||
MethodConstraint mCons = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel());
|
||||
tempMCToReturn.add(mCons);
|
||||
}
|
||||
return tempMCToReturn;
|
||||
}
|
||||
|
||||
public static boolean checkForDuplicates(TPHConstraint constraint, List list) {
|
||||
List<TPHConstraint> tempList = list;
|
||||
boolean hasSame = false;
|
||||
for (TPHConstraint tphC: tempList) {
|
||||
hasSame = constraint.getLeft() == tphC.getLeft() &&
|
||||
constraint.getRight() == tphC.getRight() &&
|
||||
constraint.getRel() == tphC.getRel(); //constraint already in ArrayList if true
|
||||
if (hasSame)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean checkForDuplicatesInMethods(TPHConstraint constraint, HashMap<MethodConstraint, String> hashMap, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
|
||||
HashMap<MethodConstraint, String> tempMap = hashMap;
|
||||
boolean hasSame = false;
|
||||
for (TPHConstraint tphC: tempMap.keySet()) {
|
||||
hasSame = constraint.getLeft() == tphC.getLeft() &&
|
||||
constraint.getRight() == tphC.getRight() &&
|
||||
constraint.getRel() == tphC.getRel() &&
|
||||
posOfTphs.get(); //constraint already in ArrayList if true
|
||||
if (hasSame)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<TPHConstraint> buildTransitiveClosure(List list) {
|
||||
List<TPHConstraint> iterList = new ArrayList<>(list);
|
||||
List<TPHConstraint> runList = new ArrayList<>(list);
|
||||
List<TPHConstraint> tcList = new ArrayList<>(list);
|
||||
boolean addedConToList = false;
|
||||
for (TPHConstraint cons: iterList) {
|
||||
for (TPHConstraint cons2: runList) {
|
||||
if(cons.getRight() == cons2.getLeft()) {
|
||||
TPHConstraint consToAdd = new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS);
|
||||
if (!checkForDuplicates(consToAdd,tcList)) {
|
||||
tcList.add(consToAdd);
|
||||
addedConToList = true;
|
||||
if (addedConToList) {
|
||||
return buildTransitiveClosure(tcList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tcList;
|
||||
}
|
||||
|
||||
public static boolean checkUpperBound(List<TPHConstraint> cs, String tph) {
|
||||
for(int i=0; i<cs.size(); i++) {
|
||||
if(cs.get(i).getLeft() == tph) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static HashMap<String, PairTphMethod<PositionFinder.Position, String>> positionConverter(HashMap<String, Boolean> allTphs, List<MethodAndTPH> listOfMethodsAndTphs) {
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> convertedPositions = new HashMap<>();
|
||||
for(String tph: allTphs.keySet()) {
|
||||
if(allTphs.get(tph)) { //if true, then tph is a method-TPH
|
||||
for(MethodAndTPH methTph: listOfMethodsAndTphs) {
|
||||
if (methTph.getTphs().contains(tph)) {
|
||||
convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.METHOD, methTph.getId()));
|
||||
}
|
||||
}
|
||||
} else { // else it is in the class-TPH
|
||||
convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.FIELD, null));
|
||||
}
|
||||
}
|
||||
return convertedPositions;
|
||||
}
|
||||
|
||||
/* public PositionFinder.Position positionConverter(TPHExtractor tphExtractor) {
|
||||
if(tphExtractor.allTPHS.keySet())
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* GeneratedGenericsFinder genGenFinder;
|
||||
ConstraintsSimplierResult simplifiedConstraints = null;
|
||||
GenericsGeneratorResultForClass ggResult = null;
|
||||
Method m;
|
||||
|
||||
public void addMethodConstraints(List<MethodConstraint> cs_m) {
|
||||
genGenFinder.addMethodConstraints(simplifiedConstraints, ggResult, m);
|
||||
cs_m.add();
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
package de.dhbwstuttgart.bytecode.gGenericsAli;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
*/
|
||||
/**
|
||||
* gets set of typed variable constraints with substitutions and the set of typed classes
|
||||
* and returns the set of families of generated generics and the set of families of type variable mappings
|
||||
*//*
|
||||
|
||||
public class GGenerics implements preGGenerics {
|
||||
private TVarConstraints tVarCons;
|
||||
private Substitutions subst;
|
||||
private TClass typedClass;
|
||||
|
||||
|
||||
public GGenerics(TVarConstraints tVarCons, Substitutions subst, TClass typedClass) throws IOException, ClassNotFoundException {
|
||||
this.tVarCons = tVarCons;
|
||||
this.subst = subst;
|
||||
this.typedClass = typedClass;
|
||||
}
|
||||
|
||||
List<File> input = new ArrayList<>();
|
||||
List<File> classpath = new ArrayList<>();
|
||||
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(input, classpath);
|
||||
compiler.typeInference();
|
||||
|
||||
public List<ResultSet> getResultOfTypeInference() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
*/
|
@ -0,0 +1,9 @@
|
||||
package de.dhbwstuttgart.bytecode.insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
|
||||
public class MethodConstraint extends TPHConstraint {
|
||||
public MethodConstraint(String left, String right, Relation rel) {
|
||||
super(left, right, rel);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package de.dhbwstuttgart.bytecode.insertGenerics;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** A generic class for pairs.
|
||||
*
|
||||
* <p><b>This is NOT part of any supported API.
|
||||
* If you write code that depends on this, you do so at your own risk.
|
||||
* This code and its internal interfaces are subject to change or
|
||||
* deletion without notice.</b>
|
||||
*/
|
||||
|
||||
|
||||
public class PairTphMethod<A, B> {
|
||||
public final A fst;
|
||||
public final B snd;
|
||||
|
||||
public PairTphMethod(A fst, B snd) {
|
||||
this.fst = fst;
|
||||
this.snd = snd;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "PairTphMethod[" + fst + "," + snd + "]";
|
||||
}
|
||||
|
||||
public boolean equals(Object other) {
|
||||
return
|
||||
other instanceof PairTphMethod<?,?> &&
|
||||
Objects.equals(fst, ((PairTphMethod<?,?>)other).fst) &&
|
||||
Objects.equals(snd, ((PairTphMethod<?,?>)other).snd);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
if (fst == null) return (snd == null) ? 0 : snd.hashCode() + 1;
|
||||
else if (snd == null) return fst.hashCode() + 2;
|
||||
else return fst.hashCode() * 17 + snd.hashCode();
|
||||
}
|
||||
|
||||
public static <A,B> PairTphMethod<A,B> of(A a, B b) {
|
||||
return new PairTphMethod<>(a,b);
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package de.dhbwstuttgart.bytecode.insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.*;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
public class PositionFinder{
|
||||
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)
|
||||
|
||||
public enum Position{
|
||||
METHOD,
|
||||
CONSTRUCTOR,
|
||||
FIELD
|
||||
}
|
||||
|
||||
public static HashMap<String, PairTphMethod<Position, String>> getPositionOfTPH(SourceFile sf, Set<String> 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, String methodId) {
|
||||
posOfTphs.put(tph, new PairTphMethod<>(Position.METHOD, methodId));
|
||||
}
|
||||
|
||||
public static void putPositionInField(String tph) {
|
||||
posOfTphs.put(tph, new PairTphMethod<>(Position.FIELD, null));
|
||||
}
|
||||
|
||||
public static void putPositionInConstructor(String tph, String id) {
|
||||
posOfTphs.put(tph, new PairTphMethod<>(Position.CONSTRUCTOR, id));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static class Walker extends AbstractASTWalker{
|
||||
Boolean inMethod = false;
|
||||
Boolean inConstructor = false;
|
||||
|
||||
@Override
|
||||
public void visit(TypePlaceholder tph) {
|
||||
if (inMethod) {
|
||||
if (inConstructor) {
|
||||
// System.out.println(tph);
|
||||
// putPositionInConstructor(tph.getName(),);
|
||||
}
|
||||
// System.out.println(tph);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package de.dhbwstuttgart.bytecode.insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface preGGenerics {
|
||||
public List<ResultSet> getResultOfTypeInference();
|
||||
}
|
@ -43,7 +43,7 @@ public class GatherNames {
|
||||
}
|
||||
else{
|
||||
if(typeDecl.classDeclaration().normalClassDeclaration() != null){
|
||||
if(pkgName != ""){
|
||||
if(!pkgName.isEmpty()){
|
||||
nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
|
||||
}
|
||||
else{
|
||||
|
@ -64,6 +64,10 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.getName().hashCode();
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
|
@ -114,6 +114,7 @@ public abstract class UnifyType {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj == null)return false;
|
||||
return this.toString().equals(obj.toString());
|
||||
}
|
||||
}
|
68
src/test/java/constraintSimplify/FamilyOfGenerics.java
Normal file
68
src/test/java/constraintSimplify/FamilyOfGenerics.java
Normal file
@ -0,0 +1,68 @@
|
||||
package constraintSimplify;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.TPHExtractor;
|
||||
import de.dhbwstuttgart.bytecode.insertGenerics.PositionFinder;
|
||||
import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder;
|
||||
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.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FamilyOfGenerics {
|
||||
|
||||
@Test
|
||||
public void generateBC() throws Exception {
|
||||
SourceFile sf = generateAST();
|
||||
PositionFinder.getPositionOfTPH(sf, null);
|
||||
TPHExtractor tphExtractor = new TPHExtractor();
|
||||
List<ResultSet> results = new ArrayList<ResultSet>();
|
||||
GeneratedGenericsFinder generatedGenericsFinder = new GeneratedGenericsFinder(sf, results);
|
||||
}
|
||||
|
||||
public static SourceFile generateAST(){
|
||||
ArrayList<ClassOrInterface> classes = new ArrayList<>();
|
||||
ArrayList<Field> fields = new ArrayList<>();
|
||||
ArrayList<Method> methods = new ArrayList<>();
|
||||
|
||||
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(),
|
||||
new RefType(new JavaClassName("java.lang.Object"), new NullToken()),
|
||||
false, new ArrayList<>(), new NullToken()));
|
||||
|
||||
return new SourceFile("Test.jav", classes, new HashSet<>());
|
||||
}
|
||||
|
||||
public static Method generateMethod(String methodName, String[] paramNames){
|
||||
ArrayList<FormalParameter> parameters = new ArrayList<>();
|
||||
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, 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());
|
||||
}
|
||||
}
|
214
src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java
Normal file
214
src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java
Normal file
@ -0,0 +1,214 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.insertGenerics.*;
|
||||
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class FamilyOfGeneratedGenericsTest extends TestCase {
|
||||
|
||||
public void testIdentityMethod(){
|
||||
/*
|
||||
Example method:
|
||||
A id(B i) return i;
|
||||
gives constraint: B <. A and A <. Object, which are method constraints
|
||||
*/
|
||||
|
||||
List<TPHConstraint> inputConstraints = new ArrayList<>();
|
||||
inputConstraints.add(new TPHConstraint("B", "A", TPHConstraint.Relation.EXTENDS));
|
||||
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
|
||||
PairTphMethod<PositionFinder.Position, String> meth1 = new PairTphMethod<PositionFinder.Position, String>(PositionFinder.Position.METHOD, "m1");
|
||||
tphPositions.put("A", meth1);
|
||||
tphPositions.put("B", meth1);
|
||||
|
||||
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
|
||||
assertTrue(classConstraints.isEmpty());
|
||||
|
||||
/*
|
||||
MethodConstraints should be the same as the input constraint
|
||||
*/
|
||||
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, new ArrayList<ClassConstraint>(), tphPositions);
|
||||
assertTrue(methodConstraints.size() == 2);
|
||||
assertTrue(methodConstraints.get(0).getLeft().equals("B"));
|
||||
assertTrue(methodConstraints.get(0).getRight().equals("A"));
|
||||
}
|
||||
|
||||
public void testClassField(){
|
||||
/*
|
||||
class Example{
|
||||
A f;
|
||||
B fReturn(){
|
||||
return f;
|
||||
}
|
||||
}
|
||||
gives constraint: A <. B and B <. Object which are class constraints
|
||||
*/
|
||||
|
||||
List<TPHConstraint> inputConstraints = new ArrayList<>();
|
||||
inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS));
|
||||
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
|
||||
PairTphMethod<PositionFinder.Position, String> posOfA = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
tphPositions.put("A", posOfA);
|
||||
PairTphMethod<PositionFinder.Position, String> posOfB = new PairTphMethod<>(PositionFinder.Position.METHOD, "fReturn");
|
||||
tphPositions.put("B", posOfB);
|
||||
|
||||
/*
|
||||
ClassConstraints should not be the same as the input constraint
|
||||
*/
|
||||
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
|
||||
System.out.println(classConstraints);
|
||||
assertTrue(classConstraints.size() == 2);
|
||||
//assertTrue(classConstraints.get(0).getLeft().equals("A"));
|
||||
//assertTrue(classConstraints.get(0).getRight().equals("B"));
|
||||
}
|
||||
|
||||
public void testSecondLineOfClassConstraints() {
|
||||
/*
|
||||
class Example() {
|
||||
A a;
|
||||
B b = a;
|
||||
C anyMethod() {
|
||||
F f;
|
||||
return f;
|
||||
}
|
||||
D otherMethod(E e) {
|
||||
this.b = e;
|
||||
e = this.a;
|
||||
return e;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
List<TPHConstraint> inputConstraints = new ArrayList<>();
|
||||
inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("F", "C", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("E", "B", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("A", "E", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("E", "D", TPHConstraint.Relation.EXTENDS));
|
||||
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
|
||||
PairTphMethod<PositionFinder.Position, String> posOfA = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
PairTphMethod<PositionFinder.Position, String> posOfB = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
PairTphMethod<PositionFinder.Position, String> posOfC = new PairTphMethod<>(PositionFinder.Position.METHOD, "anyMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfD = new PairTphMethod<>(PositionFinder.Position.METHOD, "otherMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfE = new PairTphMethod<>(PositionFinder.Position.METHOD, "otherMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfF = new PairTphMethod<>(PositionFinder.Position.METHOD, "anyMethod");
|
||||
|
||||
tphPositions.put("A", posOfA);
|
||||
tphPositions.put("B", posOfB);
|
||||
tphPositions.put("C", posOfC);
|
||||
tphPositions.put("F", posOfF);
|
||||
tphPositions.put("D", posOfD);
|
||||
tphPositions.put("E", posOfE);
|
||||
|
||||
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
|
||||
System.out.println(classConstraints);
|
||||
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, classConstraints, tphPositions);
|
||||
System.out.println(methodConstraints);
|
||||
assertFalse(classConstraints.isEmpty());
|
||||
assertTrue(classConstraints.size() == 6);
|
||||
assertFalse(methodConstraints.isEmpty());
|
||||
assertTrue(methodConstraints.size() == 2);
|
||||
|
||||
}
|
||||
|
||||
public void testTPHsAndGenerics() {
|
||||
/*
|
||||
class TPHsAndGenerics {
|
||||
Fun1<A,B> id = x -> x;
|
||||
C id2 (D x) {
|
||||
return id.apply(x);
|
||||
}
|
||||
E m(F a, G b){
|
||||
var c = m2(a,b);
|
||||
return a;
|
||||
}
|
||||
H m2(I a, J b){
|
||||
return b;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
List<TPHConstraint> inputConstraints = new ArrayList<>();
|
||||
inputConstraints.add(new TPHConstraint("A","B", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("B","C", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("D","A", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("F","E", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("F","I", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("G","J", TPHConstraint.Relation.EXTENDS));
|
||||
inputConstraints.add(new TPHConstraint("J","H", TPHConstraint.Relation.EXTENDS));
|
||||
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> tphPositions = new HashMap<>();
|
||||
PairTphMethod<PositionFinder.Position, String> posOfA = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
PairTphMethod<PositionFinder.Position, String> posOfB = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
PairTphMethod<PositionFinder.Position, String> posOfC = new PairTphMethod<>(PositionFinder.Position.METHOD, "id2");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfD = new PairTphMethod<>(PositionFinder.Position.METHOD, "id2");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfE = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfF = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfG = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfH = new PairTphMethod<>(PositionFinder.Position.METHOD, "m2");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfI = new PairTphMethod<>(PositionFinder.Position.METHOD, "m2");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfJ = new PairTphMethod<>(PositionFinder.Position.METHOD, "m2");
|
||||
|
||||
tphPositions.put("A", posOfA);
|
||||
tphPositions.put("B", posOfB);
|
||||
tphPositions.put("C", posOfC);
|
||||
tphPositions.put("D", posOfD);
|
||||
tphPositions.put("E", posOfE);
|
||||
tphPositions.put("F", posOfF);
|
||||
tphPositions.put("G", posOfG);
|
||||
tphPositions.put("H", posOfH);
|
||||
tphPositions.put("I", posOfI);
|
||||
tphPositions.put("J", posOfJ);
|
||||
|
||||
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
|
||||
System.out.println(classConstraints);
|
||||
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, classConstraints, tphPositions);
|
||||
System.out.println(methodConstraints);
|
||||
|
||||
assertFalse(classConstraints.isEmpty());
|
||||
assertTrue(classConstraints.size() == 3);
|
||||
assertFalse(methodConstraints.isEmpty());
|
||||
assertTrue(methodConstraints.size()==9);
|
||||
}
|
||||
|
||||
|
||||
public void testPositionConverter() {
|
||||
HashMap<String, Boolean> allTphsOld = new HashMap<>();
|
||||
List<MethodAndTPH> listOfMethodsAndTphs = new ArrayList<>();
|
||||
allTphsOld.put("A", true);
|
||||
allTphsOld.put("B", false);
|
||||
MethodAndTPH m1 = new MethodAndTPH("m1");
|
||||
m1.getTphs().add("A");
|
||||
MethodAndTPH bla = new MethodAndTPH("bla");
|
||||
MethodAndTPH blubb = new MethodAndTPH("blubb");
|
||||
// blubb.getTphs().add("A");
|
||||
listOfMethodsAndTphs.add(bla);
|
||||
listOfMethodsAndTphs.add(blubb);
|
||||
listOfMethodsAndTphs.add(m1);
|
||||
|
||||
|
||||
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld, listOfMethodsAndTphs);
|
||||
System.out.println(allTphsNew);
|
||||
//was tun wenn zwei (oder mehr) Methoden gleiches TPH enthalten?
|
||||
//ist dies möglich oder werden die TPHs immer verschieden initialisiert und dann erst am Ende gemappt?
|
||||
//überarbeiten oder lassen?
|
||||
|
||||
|
||||
assertTrue(allTphsNew.get("A").fst.equals(PositionFinder.Position.METHOD));
|
||||
assertTrue(allTphsNew.get("B").fst.equals(PositionFinder.Position.FIELD));
|
||||
}
|
||||
|
||||
|
||||
public void testFirstTransitiveSubtypeForMethodTypes(){
|
||||
|
||||
}
|
||||
|
||||
}
|
30
src/test/java/insertGenerics/MethodsTest.java
Normal file
30
src/test/java/insertGenerics/MethodsTest.java
Normal file
@ -0,0 +1,30 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import junit.framework.TestResult;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodsTest {
|
||||
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/";
|
||||
public static final String fileDirectory = rootDirectory + "TestGGFinder.jav";
|
||||
|
||||
@Test
|
||||
public void testVisit(ClassOrInterface coi) throws IOException, ClassNotFoundException {
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(new File(fileDirectory));
|
||||
SourceFile sf = compiler.sourceFiles.get(compiler.sourceFiles.keySet());
|
||||
List<ResultSet> results = compiler.typeInference();
|
||||
|
||||
GeneratedGenericsFinder ggf = new GeneratedGenericsFinder(sf, results);
|
||||
ClassOrInterface coiHere = coi;
|
||||
ggf.visit(coiHere);
|
||||
//assert results.size()>0;
|
||||
}
|
||||
}
|
65
src/test/java/insertGenerics/TestExample42.java
Normal file
65
src/test/java/insertGenerics/TestExample42.java
Normal file
@ -0,0 +1,65 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.insertGenerics.*;
|
||||
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<TPHConstraint> fillConstraintsList() {
|
||||
List<TPHConstraint> 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<String, PairTphMethod<PositionFinder.Position, String>> fillPosOfTphs() {
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs = new HashMap<>();
|
||||
|
||||
// TPHs "U" und "L" auskommentiert, da nach Vorgaben L zu Z umbenannt und U als void interpretiert wird
|
||||
PairTphMethod<PositionFinder.Position, String> posOfK = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
// PairTphMethod<PositionFinder.Position, String> posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "id");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfM = new PairTphMethod<>(PositionFinder.Position.METHOD, "id");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfN = new PairTphMethod<>(PositionFinder.Position.METHOD, "id");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfP = new PairTphMethod<>(PositionFinder.Position.METHOD, "setA");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfQ = new PairTphMethod<>(PositionFinder.Position.METHOD, "setA");
|
||||
// PairTphMethod<PositionFinder.Position, String> posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfV = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfW = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfZ = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
|
||||
|
||||
posOfTphs.put("K", posOfK);
|
||||
// posOfTphs.put("L", posOfL);
|
||||
posOfTphs.put("M", posOfM);
|
||||
posOfTphs.put("N", posOfN);
|
||||
posOfTphs.put("P", posOfP);
|
||||
posOfTphs.put("Q", posOfQ);
|
||||
// posOfTphs.put("U", posOfU);
|
||||
posOfTphs.put("V", posOfV);
|
||||
posOfTphs.put("W", posOfW);
|
||||
posOfTphs.put("Z", posOfZ);
|
||||
return posOfTphs;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void genericTest() {
|
||||
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs());
|
||||
System.out.println("ClassConstraints: " + classConstraints);
|
||||
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs());
|
||||
System.out.println("MethodConstraints: " + methodConstraints);
|
||||
for(MethodConstraint mC: methodConstraints) {
|
||||
System.out.println(mC);
|
||||
}
|
||||
|
||||
List<TPHConstraint> testCons;
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.insertGenerics.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class TestExample42_allInOneMethod {
|
||||
public List<TPHConstraint> fillConstraintsList() {
|
||||
List<TPHConstraint> 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<String, PairTphMethod<PositionFinder.Position, String>> fillPosOfTphs() {
|
||||
HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs = new HashMap<>();
|
||||
|
||||
// TPHs "U" und "L" auskommentiert, da nach Vorgaben L zu Z umbenannt und U als void interpretiert wird
|
||||
PairTphMethod<PositionFinder.Position, String> posOfK = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
|
||||
// PairTphMethod<PositionFinder.Position, String> posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfM = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfN = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfP = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfQ = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
// PairTphMethod<PositionFinder.Position, String> posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfV = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfW = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
PairTphMethod<PositionFinder.Position, String> posOfZ = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
|
||||
|
||||
posOfTphs.put("K", posOfK);
|
||||
// posOfTphs.put("L", posOfL);
|
||||
posOfTphs.put("M", posOfM);
|
||||
posOfTphs.put("N", posOfN);
|
||||
posOfTphs.put("P", posOfP);
|
||||
posOfTphs.put("Q", posOfQ);
|
||||
// posOfTphs.put("U", posOfU);
|
||||
posOfTphs.put("V", posOfV);
|
||||
posOfTphs.put("W", posOfW);
|
||||
posOfTphs.put("Z", posOfZ);
|
||||
return posOfTphs;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void genericTest() {
|
||||
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs());
|
||||
System.out.println("ClassConstraints: " + classConstraints);
|
||||
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs());
|
||||
System.out.println("MethodConstraints: " + methodConstraints);
|
||||
|
||||
List<TPHConstraint> testCons;
|
||||
}
|
||||
}
|
28
src/test/java/insertGenerics/TestTransitiveClosure.java
Normal file
28
src/test/java/insertGenerics/TestTransitiveClosure.java
Normal file
@ -0,0 +1,28 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
92
src/test/java/insertGenerics/TryTest.java
Normal file
92
src/test/java/insertGenerics/TryTest.java
Normal file
@ -0,0 +1,92 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class TryTest {
|
||||
|
||||
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/";
|
||||
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException {
|
||||
execute(new File(rootDirectory+"TestGGFinder.jav"));
|
||||
}
|
||||
|
||||
private static class TestResultSet{
|
||||
|
||||
}
|
||||
|
||||
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException {
|
||||
//filesToTest.add(new File(rootDirectory+"fc.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Vector.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
|
||||
//filesToTest.add(new File(rootDirectory+"Import.jav"));
|
||||
// //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
|
||||
// //filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
|
||||
// //filesToTest.add(new File(rootDirectory+"test.jav"));
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
|
||||
for(File f : compiler.sourceFiles.keySet()){
|
||||
SourceFile sf = compiler.sourceFiles.get(f);
|
||||
// System.out.println(ASTTypePrinter.print(sf));
|
||||
// System.out.println("---------------------------1");
|
||||
// System.out.println(ASTPrinter.print(sf));
|
||||
// System.out.println("---------------------------2");
|
||||
}
|
||||
List<ResultSet> results = compiler.typeInference();
|
||||
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
|
||||
//compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles);
|
||||
for(File f : compiler.sourceFiles.keySet()){
|
||||
SourceFile sf = compiler.sourceFiles.get(f);
|
||||
System.out.println(ASTTypePrinter.print(sf));
|
||||
// System.out.println("---------------------------3");
|
||||
System.out.println(ASTPrinter.print(sf));
|
||||
// System.out.println("---------------------------4");
|
||||
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
|
||||
assert results.size()>0;
|
||||
Set<String> insertedTypes = new HashSet<>();
|
||||
for(ResultSet resultSet : results){
|
||||
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles);
|
||||
assert result.size()>0;
|
||||
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
||||
for(TypeInsert tip : result){
|
||||
insertedTypes.add(tip.insert(content));
|
||||
}
|
||||
}
|
||||
for(String s : insertedTypes){
|
||||
System.out.println("---------------------------51");
|
||||
System.out.println(s);
|
||||
System.out.println("---------------------------52");
|
||||
}
|
||||
}
|
||||
return new TestResultSet();
|
||||
}
|
||||
|
||||
static String readFile(String path, Charset encoding)
|
||||
throws IOException
|
||||
{
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
}
|
17
src/test/resources/insertGenericsJav/TestGGFinder.jav
Normal file
17
src/test/resources/insertGenericsJav/TestGGFinder.jav
Normal file
@ -0,0 +1,17 @@
|
||||
class Generics{
|
||||
a;
|
||||
|
||||
id(b) {
|
||||
var c = b;
|
||||
return c;
|
||||
}
|
||||
|
||||
setA(x) {
|
||||
a = x;
|
||||
return a;
|
||||
}
|
||||
|
||||
m(x,y) {
|
||||
x = id(y);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user