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

This commit is contained in:
JanUlrich 2020-12-18 09:12:30 +01:00
commit 1c9bc90201
6 changed files with 264 additions and 42 deletions

View File

@ -135,8 +135,7 @@ 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.getClassConstraints();
fogg = new FamilyOfGeneratedGenerics(tphExtractor);
tphsClass = tphExtractor.tphsClass;
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);

View File

@ -1,17 +1,28 @@
package de.dhbwstuttgart.bytecode.insertGenerics;
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<>();
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>();
public List<ClassConstraint> classConstraints = new ArrayList<>();
public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) {
this.allConstraints = tphExtractor.allCons;
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
this.classConstraints = getClassConstraints(allConstraints,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, cs_cl, posOfTphs);
List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs, posOfTphs);
for (ClassConstraint cons: classConstraints1) {
if (!checkForDuplicates(cons, cs_cl)) {
cs_cl.add(cons);
@ -32,7 +43,7 @@ public class FamilyOfGeneratedGenerics {
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
List<MethodConstraint> cs_m = new ArrayList<>();
List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs);
@ -49,12 +60,13 @@ public class FamilyOfGeneratedGenerics {
* 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, List<ClassConstraint> cs_cl, 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");
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) == PositionFinder.Position.FIELD) {
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);
@ -62,6 +74,9 @@ public class FamilyOfGeneratedGenerics {
}
}
}
/*else if (allCons.getRel() != Relation.EXTENDS) {
throw re;
}*/
}
return tempCC;
}
@ -92,17 +107,16 @@ 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 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<>();
for(TPHConstraint allCons: allConstraints) {
for(ClassConstraint cCons: cs_cl) {
for(String tph: posOfTphs.keySet()) {
if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) ||
(posOfTphs.containsKey(cCons.getRight()) && cCons.getLeft() != null) &&
(allCons.getLeft() == tph && allCons.getRight() == null) &&
allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS &&
cCons.getRight() == tph && allCons.getLeft() == tph) {
ClassConstraint consToAdd = new ClassConstraint(cCons.getRight(), "Object", Relation.EXTENDS);
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);
}
@ -112,22 +126,28 @@ public class FamilyOfGeneratedGenerics {
}
return tempCC;
}
/*
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;
}
*/
/**
* 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 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:
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)) {
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 (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd);
@ -208,7 +228,6 @@ public class FamilyOfGeneratedGenerics {
return buildTransitiveClosure(tcList);
}
}
//TODO: über aktualisierte Liste laufen wegen Updates -> Rekursion?
}
}
}
@ -216,6 +235,27 @@ public class FamilyOfGeneratedGenerics {
}
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())
}*/

View File

@ -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);
}
}

View File

@ -7,7 +7,9 @@ import java.util.HashMap;
import java.util.Set;
public class PositionFinder{
static HashMap<String, Position> posOfTphs = new HashMap<String, Position>();
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,
@ -15,7 +17,7 @@ public class PositionFinder{
FIELD
}
public static HashMap<String, Position> getPositionOfTPH(SourceFile sf, Set<String> tphs) {
public static HashMap<String, PairTphMethod<Position, String>> getPositionOfTPH(SourceFile sf, Set<String> tphs) {
new Walker().visit(sf);
for (String tph: posOfTphs.keySet()) {
@ -24,16 +26,16 @@ public class PositionFinder{
return null;
}
public static void putPositionInMethod(String tph) {
posOfTphs.put(tph, Position.METHOD);
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, Position.FIELD);
posOfTphs.put(tph, new PairTphMethod<>(Position.FIELD, null));
}
public static void putPositionInConstructor(String tph) {
posOfTphs.put(tph, Position.CONSTRUCTOR);
public static void putPositionInConstructor(String tph, String id) {
posOfTphs.put(tph, new PairTphMethod<>(Position.CONSTRUCTOR, id));
}
@ -46,9 +48,11 @@ public class PositionFinder{
public void visit(TypePlaceholder tph) {
if (inMethod) {
if (inConstructor) {
putPositionInConstructor(tph.getName());
// System.out.println(tph);
// putPositionInConstructor(tph.getName(),);
}
putPositionInMethod(tph.getName());
// System.out.println(tph);
// putPositionInMethod(tph.getName(),);
} else {
putPositionInField(tph.getName());
}

View File

@ -1,10 +1,8 @@
package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics;
import de.dhbwstuttgart.bytecode.insertGenerics.MethodConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.PositionFinder;
import de.dhbwstuttgart.bytecode.insertGenerics.*;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import junit.framework.TestCase;
import java.util.ArrayList;
@ -23,9 +21,10 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
List<TPHConstraint> inputConstraints = new ArrayList<>();
inputConstraints.add(new TPHConstraint("B", "A", TPHConstraint.Relation.EXTENDS));
HashMap<String, PositionFinder.Position> tphPositions = new HashMap<>();
tphPositions.put("A", PositionFinder.Position.METHOD);
tphPositions.put("B", PositionFinder.Position.METHOD);
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());
@ -53,16 +52,151 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
List<TPHConstraint> inputConstraints = new ArrayList<>();
inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS));
HashMap<String, PositionFinder.Position> tphPositions = new HashMap<>();
tphPositions.put("A", PositionFinder.Position.FIELD);
tphPositions.put("B", PositionFinder.Position.METHOD);
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 be the same as the input constraint
ClassConstraints should not be the same as the input constraint
*/
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
assertTrue(classConstraints.size() == 1);
assertTrue(classConstraints.get(0).getLeft().equals("A"));
assertTrue(classConstraints.get(0).getRight().equals("B"));
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);
assertFalse(classConstraints.isEmpty());
assertTrue(classConstraints.size() == 6);
}
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);
assertFalse(classConstraints.isEmpty());
assertTrue(classConstraints.size() == 3);
}
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));
}
}

View File

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