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

This commit is contained in:
JanUlrich 2020-11-06 10:45:50 +01:00
commit 8cf57ba35b
6 changed files with 235 additions and 59 deletions

View File

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

View File

@ -3,76 +3,77 @@ package de.dhbwstuttgart.bytecode.gGenericsAli;
import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class FamilyOfGeneratedGenerics { public class FamilyOfGeneratedGenerics {
public List<TPHConstraint> cs;
public List<ClassConstraint> cs_cl;
public List<MethodConstraint> cs_m;
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
cs = (List<TPHConstraint>) tphExtractor.allCons; List<ClassConstraint> cs_cl = new ArrayList<>();
cs_cl = new ArrayList<ClassConstraint>(); System.out.println("0: " + cs_cl);
cs_m = new ArrayList<MethodConstraint>(); List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs, cs_cl, posOfTphs);
}
// public List<ClassConstraint,MethodConstraint> getFamilyOfGeneratedGenerics(){
//
// }
public void addConstraintToClassConstraint() { //Inputparameter List<TPHConstraint> constraintsSet weg
List<ClassConstraint> classConstraints1 = typeOfANodeOfAField(cs);
cs_cl.addAll(classConstraints1); cs_cl.addAll(classConstraints1);
System.out.println("1: " + cs_cl);
List<ClassConstraint> classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher List<ClassConstraint> classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher
cs_cl.addAll(classConstraints2); cs_cl.addAll(classConstraints2);
List<ClassConstraint> classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl); System.out.println("2: " + cs_cl);
List<ClassConstraint> classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs);
cs_cl.addAll(classConstraints3); cs_cl.addAll(classConstraints3);
System.out.println("3: " + cs_cl);
return cs_cl;
} }
public void addConstraintToMethodConstraint(List<TPHConstraint> constraintsSet) { public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> constraintsSet) {
//TODO: Regeln //TODO: Regeln
List<MethodConstraint> cs_m = new ArrayList<>();
// for(TPHConstraint cons: constraintsSet){ // for(TPHConstraint cons: constraintsSet){
// //
// cs_m = // cs_m =
// } // }
return null;
} }
/** /**
* 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 List<ClassConstraint> typeOfANodeOfAField(List<TPHConstraint> cs) { public static List<ClassConstraint> typeOfANodeOfAField(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<String, PositionFinder.Position> posOfTphs) {
//TODO: List<ClassConstraint> tempCC= new ArrayList<>();
for(TPHConstraint cons: cs){ for(TPHConstraint allCons: allConstraints){
System.out.println(cons); if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) {
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); for(String tph: posOfTphs.keySet()) {
if(cons.getRight()!=null && cons.getRel()==Relation.EXTENDS) { if(tph == allCons.getLeft() && posOfTphs.get(tph) == PositionFinder.Position.FIELD) {
cs_cl.add(new ClassConstraint(cons.getLeft(), cons.getRight(),cons.getRel())); tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()));
}
}
} }
} }
System.out.println(cs_cl); return tempCC;
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
return cs_cl;
} }
/** /**
* Def. FGG: zweite Zeile von cs_cl * Def. FGG: zweite Zeile von cs_cl
* {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs } * {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs }
*/ */
public List<ClassConstraint> transitiveSubtypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> classConstraints) { public static List<ClassConstraint> transitiveSubtypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl) {
//TODO: //TODO:
for(ClassConstraint cCons: classConstraints) { List<ClassConstraint> tempCC= new ArrayList<>();
for(TPHConstraint allCons: allConstraints) for(ClassConstraint cCons: cs_cl) {
// if(tphExtractor.containsConstraint(allConstraints, cCons)) { if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) {
// } for(TPHConstraint allCons: allConstraints) {
if(cCons.getRight() == allCons.getLeft()){ if(cCons.getRight() == allCons.getLeft() && allCons.getRight() != null && allCons.getRel()==Relation.EXTENDS){
tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()));
}
}
} }
} }
return cs_cl; return tempCC;
} }
/** /**
@ -80,56 +81,83 @@ public class FamilyOfGeneratedGenerics {
* {T <. Object | ((T is a type variable in a type of a node of a field * {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 List<ClassConstraint> hasNoSupertypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> classConstraints) { public static List<ClassConstraint> hasNoSupertypeForClassTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<String, PositionFinder.Position> posOfTphs) {
//TODO: //TODO:
return cs_cl; List<ClassConstraint> tempCC= new ArrayList<>();
for(TPHConstraint allCons: allConstraints) {
for(ClassConstraint cCons: cs_cl) {
for(String tph: posOfTphs.keySet()) {
System.out.println("cCons: " + cCons);
if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) ||
(posOfTphs.containsKey(cCons.getLeft()) && cCons.getRight() != null) &&
allCons.getRight() == null &&
allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS) {
System.out.println("C3: " + allCons);
tempCC.add(new ClassConstraint(allCons.getLeft(), "Object", Relation.EXTENDS));
}
}
}
}
return tempCC;
} }
/*
/** */
/**
* Def. FGG: erste Zeile von cs_m * Def. FGG: erste Zeile von cs_m
* {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs} * {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs}
*/ *//*
public List<MethodConstraint> typeOfTheMethodInClSigma() { // cl_\sigma?? public List<MethodConstraint> typeOfTheMethodInClSigma() { // cl_\sigma??
//TODO: //TODO:
return cs_m; return cs_m;
} }
/** */
/**
* Def. FGG: zweite Zeile von cs_m * Def. FGG: zweite Zeile von cs_m
* {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs} * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs}
*/ *//*
public List<MethodConstraint> firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs public List<MethodConstraint> firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs
//TODO: //TODO:
return cs_m; return cs_m;
} }
/** */
/**
* Def. FGG: dritte Zeile von cs_m * Def. FGG: dritte Zeile von cs_m
* {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs} * {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs}
*/ *//*
public List<MethodConstraint> secondTransitiveSubtypeForMethodTypes() { public List<MethodConstraint> secondTransitiveSubtypeForMethodTypes() {
//TODO: //TODO:
return cs_m; return cs_m;
} }
/** */
/**
* Def. FGG: vierte Zeile von cs_m * Def. FGG: vierte Zeile von cs_m
* {T <. Object | (T is a type variable in a type of a node of the method/constructor m in cl_\sigma), * {T <. Object | (T is a type variable in a type of a node of the method/constructor m in cl_\sigma),
* (\existsnot T': T <. T') \in cs)} * (\existsnot T': T <. T') \in cs)}
*/ *//*
public List<MethodConstraint> hasNoSupertypeForMethodTypes() { public List<MethodConstraint> hasNoSupertypeForMethodTypes() {
//TODO: //TODO:
return cs_m; return cs_m;
} }
/** */
/**
* nimm die Menge cs_cl aus cs_m raus * nimm die Menge cs_cl aus cs_m raus
*/ *//*
public List<MethodConstraint> methodTypesWithoutClassTypes() { public List<MethodConstraint> methodTypesWithoutClassTypes() {
//TODO: //TODO:
return cs_m; return cs_m;
} }
*/
@ -145,5 +173,4 @@ public class FamilyOfGeneratedGenerics {
} }
*/ */
} }

View File

@ -0,0 +1,76 @@
package de.dhbwstuttgart.bytecode.gGenericsAli;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import java.util.HashMap;
import java.util.Set;
public class PositionFinder{
static HashMap<String, Position> posOfTphs = new HashMap<String, Position>();
public enum Position{
METHOD,
CONSTRUCTOR,
FIELD
}
public static HashMap<String, Position> 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) {
posOfTphs.put(tph, Position.METHOD);
}
public static void putPositionInField(String tph) {
posOfTphs.put(tph, Position.FIELD);
}
public static void putPositionInConstructor(String tph) {
posOfTphs.put(tph, Position.CONSTRUCTOR);
}
static class Walker extends AbstractASTWalker{
Boolean inMethod = false;
Boolean inConstructor = false;
@Override
public void visit(TypePlaceholder tph) {
if (inMethod) {
if (inConstructor) {
putPositionInConstructor(tph.getName());
}
putPositionInMethod(tph.getName());
} else {
putPositionInField(tph.getName());
}
}
@Override
public void visit(Field field) {
super.visit(field);
}
@Override
public void visit(Method method) {
inMethod = true;
super.visit(method);
}
@Override
public void visit(Constructor cons) {
inConstructor = true;
super.visit(cons);
}
}
}

View File

@ -135,8 +135,8 @@ public class GeneratedGenericsFinder implements ASTVisitor {
classOrInterface.accept(tphExtractor); classOrInterface.accept(tphExtractor);
//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.addConstraintToClassConstraint(); // fogg.getClassConstraints();
tphsClass = tphExtractor.tphsClass; tphsClass = tphExtractor.tphsClass;
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass); simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
@ -152,8 +152,8 @@ public class GeneratedGenericsFinder implements ASTVisitor {
if(ggResult != null) if(ggResult != null)
generatedGenericsForSF.addGenericGeneratorResultClass(ggResult); generatedGenericsForSF.addGenericGeneratorResultClass(ggResult);
} }
} }

View File

@ -1,13 +1,20 @@
package constraintSimplify; package constraintSimplify;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics;
import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder;
import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder;
import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
@ -24,7 +31,11 @@ public class FamilyOfGenerics {
@Test @Test
public void generateBC() throws Exception { 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(){ public static SourceFile generateAST(){
@ -32,7 +43,9 @@ public class FamilyOfGenerics {
ArrayList<Field> fields = new ArrayList<>(); ArrayList<Field> fields = new ArrayList<>();
ArrayList<Method> methods = new ArrayList<>(); ArrayList<Method> methods = new ArrayList<>();
methods.add(generateMethod()); fields.add(generateField("fld1"));
String[] paramNames = {"a"};
methods.add(generateMethod("testMethode", paramNames));
classes.add(new ClassOrInterface(Modifier.PUBLIC, new JavaClassName("Test"), fields, Optional.empty(), methods, classes.add(new ClassOrInterface(Modifier.PUBLIC, new JavaClassName("Test"), fields, Optional.empty(), methods,
new ArrayList<>(), generateEmptyGenericDeclList(), new ArrayList<>(), generateEmptyGenericDeclList(),
@ -42,16 +55,23 @@ public class FamilyOfGenerics {
return new SourceFile("Test.jav", classes, new HashSet<>()); return new SourceFile("Test.jav", classes, new HashSet<>());
} }
public static Method generateMethod(){ public static Method generateMethod(String methodName, String[] paramNames){
ArrayList<FormalParameter> parameters = new ArrayList<>(); ArrayList<FormalParameter> parameters = new ArrayList<>();
FormalParameter fp = new FormalParameter("param1", TypePlaceholder.fresh(new NullToken()), new NullToken()); for(String str: paramNames) {
parameters.add(fp); FormalParameter fp = new FormalParameter(str, TypePlaceholder.fresh(new NullToken()), new NullToken());
parameters.add(fp);
}
ParameterList parameterList = new ParameterList(parameters, new NullToken()); ParameterList parameterList = new ParameterList(parameters, new NullToken());
return new Method(Modifier.PUBLIC, "testMethode", TypePlaceholder.fresh(new NullToken()), parameterList, return new Method(Modifier.PUBLIC, methodName, TypePlaceholder.fresh(new NullToken()), parameterList,
new Block(new ArrayList<>(), new NullToken()), generateEmptyGenericDeclList(), new NullToken()); new Block(new ArrayList<>(), new NullToken()), generateEmptyGenericDeclList(), new NullToken());
} }
public static GenericDeclarationList generateEmptyGenericDeclList(){ public static GenericDeclarationList generateEmptyGenericDeclList(){
return new GenericDeclarationList(new ArrayList<>(), new NullToken()); 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());
}
} }

View File

@ -0,0 +1,47 @@
package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint;
import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics;
import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class TestExample42 {
public List<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, PositionFinder.Position> fillPosOfTphs() {
HashMap<String, PositionFinder.Position> posOfTphs = new HashMap<>();
posOfTphs.put("K", PositionFinder.Position.FIELD);
posOfTphs.put("L", PositionFinder.Position.METHOD);
posOfTphs.put("M", PositionFinder.Position.METHOD);
posOfTphs.put("N", PositionFinder.Position.METHOD);
posOfTphs.put("P", PositionFinder.Position.METHOD);
posOfTphs.put("Q", PositionFinder.Position.METHOD);
posOfTphs.put("U", PositionFinder.Position.METHOD);
posOfTphs.put("V", PositionFinder.Position.METHOD);
posOfTphs.put("W", PositionFinder.Position.METHOD);
posOfTphs.put("Z", PositionFinder.Position.METHOD);
return posOfTphs;
}
@Test
public void genericTest() {
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs());
System.out.println(classConstraints);
}
}