Letzer Stand mit Pair Bildug waehrend des Typvars sammeln

This commit is contained in:
pl@gohorb.ba-horb.de 2021-02-16 15:29:24 +01:00
parent 905dfd8a27
commit f2d6dae4c7
7 changed files with 65 additions and 25 deletions

View File

@ -60,9 +60,17 @@ public class TPHExtractor extends AbstractASTWalker {
/* /*
* ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu * ordnet den Fieldnamen ihre jeweiligen Lambda-respraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu
*/ */
public HashMap<String,RefTypeOrTPHOrWildcardOrGeneric> nameToField; public HashMap<String,RefTypeOrTPHOrWildcardOrGeneric> nameToField;
/*
* ordnet den Variablennamen ihre jeweiligen Lambda-Respraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu
* wird in jeder Methode neu initialisiert
* Einträge werden nicht geloescht, da das Java-Programm korrekt ist und in umgebenden Bloecken nicht die gleichen Variablen vorkommen können
*/
public HashMap<String,RefTypeOrTPHOrWildcardOrGeneric> nameToLocalVar;
public final HashMap<String, Boolean> allTPHS = new HashMap<>(); public final HashMap<String, Boolean> allTPHS = new HashMap<>();
public final List<String> tphsClass = new ArrayList<>(); public final List<String> tphsClass = new ArrayList<>();
MethodAndTPH methodAndTph; MethodAndTPH methodAndTph;
@ -207,8 +215,9 @@ public class TPHExtractor extends AbstractASTWalker {
@Override @Override
public void visit(Method method) { public void visit(Method method) {
inMethod = true; inMethod = true;
this.nameToLocalVar = new HashMap<>();
String id = MethodUtility.createID(resolver,method); String id = MethodUtility.createID(resolver,method);
methodAndTph = new MethodAndTPH(id, (TypePlaceholder)method.getReturnType()); methodAndTph = new MethodAndTPH(id, method.getReturnType());
inLocalOrParamOrReturn = true; inLocalOrParamOrReturn = true;
method.getReturnType().accept(this); method.getReturnType().accept(this);
@ -232,7 +241,7 @@ public class TPHExtractor extends AbstractASTWalker {
.block .block
.statements .statements
.stream() .stream()
.filter(s -> s instanceof Assign) .filter(s -> s instanceof Assign && ((Assign)s).rightSide instanceof LambdaExpression)
.forEach(as -> .forEach(as ->
this.nameToField.put(((AssignToField)(((Assign)as).lefSide)).field.fieldVarName, this.nameToField.put(((AssignToField)(((Assign)as).lefSide)).field.fieldVarName,
((AssignToField)(((Assign)as).lefSide)).getType())) ; ((AssignToField)(((Assign)as).lefSide)).getType())) ;
@ -247,8 +256,10 @@ public class TPHExtractor extends AbstractASTWalker {
@Override @Override
public void visit(Constructor cons) { public void visit(Constructor cons) {
this.nameToLocalVar = new HashMap<>();
inMethod = false; inMethod = false;
//super.visit(cons); //super.visit(cons);
cons.getParameterList().accept(this); cons.getParameterList().accept(this);
if(cons.block != null) if(cons.block != null)
cons.block.accept(this); cons.block.accept(this);
@ -258,6 +269,12 @@ public class TPHExtractor extends AbstractASTWalker {
@Override @Override
public void visit(LambdaExpression lambdaExpression) { public void visit(LambdaExpression lambdaExpression) {
inLambdaExpression = true; inLambdaExpression = true;
lambdaExpression.params.getFormalparalist().forEach(param -> {
RefTypeOrTPHOrWildcardOrGeneric varType = resultSet.resolveType(param.getType()).resolvedType;
if (varType instanceof RefType && ((RefType)varType).getName().getClassName().endsWith("$$")) {
this.nameToLocalVar.put(param.getName(), param.getType());
}
});
super.visit(lambdaExpression); super.visit(lambdaExpression);
inLambdaExpression = false; inLambdaExpression = false;
} }
@ -267,7 +284,7 @@ public class TPHExtractor extends AbstractASTWalker {
super.visit(assign); super.visit(assign);
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(assign.rightSide.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(assign.rightSide.getType()).resolvedType;
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(assign.lefSide.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(assign.lefSide.getType()).resolvedType;
if (left instanceof TypePlaceholder && methodAndTph.getLocalTphs().contains(((TypePlaceholder)left).getName())) { if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
if (right instanceof TypePlaceholder) { if (right instanceof TypePlaceholder) {
methodAndTph.addPair(allPairs.stream().filter( methodAndTph.addPair(allPairs.stream().filter(
p -> (p.getLeft() instanceof TypePlaceholder && p -> (p.getLeft() instanceof TypePlaceholder &&
@ -284,21 +301,25 @@ public class TPHExtractor extends AbstractASTWalker {
public void visit(LocalVarDecl localVarDecl) { public void visit(LocalVarDecl localVarDecl) {
// inLocalOrParamOrReturn = inMethod; // inLocalOrParamOrReturn = inMethod;
super.visit(localVarDecl); super.visit(localVarDecl);
RefTypeOrTPHOrWildcardOrGeneric varType = resultSet.resolveType(localVarDecl.getType()).resolvedType;
if (varType instanceof RefType && ((RefType)varType).getName().getClassName().endsWith("$$")) {
this.nameToLocalVar.put(localVarDecl.getName(), localVarDecl.getType());
}
// inLocalOrParamOrReturn = false; // inLocalOrParamOrReturn = false;
} }
@Override @Override
public void visit(MethodCall methodCall) { public void visit(MethodCall methodCall) {
String fieldOrMethName; String fieldLocalvarOrMethName;
super.visit(methodCall); super.visit(methodCall);
if (nameToMeth.keySet().contains(fieldOrMethName = methodCall.name) //Method-call of a method in the actuial Sourcefile if (nameToMeth.keySet().contains(fieldLocalvarOrMethName = methodCall.name) //Method-call of a method in the actuial Sourcefile
|| (methodCall.name.equals("apply") && || (methodCall.name.equals("apply") &&
(methodCall.receiver instanceof ExpressionReceiver) && (methodCall.receiver instanceof ExpressionReceiver) &&
((((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar) && ((((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar) &&
nameToField.keySet().contains(fieldOrMethName = ((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName) nameToField.keySet().contains(fieldLocalvarOrMethName = ((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName)
|| (((ExpressionReceiver)methodCall.receiver).expr instanceof LocalVar) && || (((ExpressionReceiver)methodCall.receiver).expr instanceof LocalVar) &&
nameToField.keySet().contains(fieldOrMethName = ((LocalVar)((ExpressionReceiver)methodCall.receiver).expr).name) nameToLocalVar.keySet().contains(fieldLocalvarOrMethName = ((LocalVar)((ExpressionReceiver)methodCall.receiver).expr).name)
) )
//LOKALE VARIABLEN MUESSE NOCH IN nameToField EINGETRAGEN WERDEN //LOKALE VARIABLEN MUESSE NOCH IN nameToField EINGETRAGEN WERDEN
) )
@ -306,8 +327,10 @@ public class TPHExtractor extends AbstractASTWalker {
{ {
Iterator<RefTypeOrTPHOrWildcardOrGeneric> paraIt = Iterator<RefTypeOrTPHOrWildcardOrGeneric> paraIt =
methodCall.name.equals("apply") methodCall.name.equals("apply")
? ((RefType)(resultSet.resolveType((nameToField.get(fieldOrMethName))).resolvedType)).getParaList().iterator() ? (((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar)
: nameToMeth.get(methodCall.name) ? ((RefType)(resultSet.resolveType((nameToField.get(fieldLocalvarOrMethName))).resolvedType)).getParaList().iterator()
: ((RefType)(resultSet.resolveType((nameToLocalVar.get(fieldLocalvarOrMethName))).resolvedType)).getParaList().iterator()
: nameToMeth.get(fieldLocalvarOrMethName)
.getParameterList() .getParameterList()
.getFormalparalist() .getFormalparalist()
.stream() .stream()
@ -318,7 +341,7 @@ public class TPHExtractor extends AbstractASTWalker {
.forEach(x -> { .forEach(x -> {
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType;
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next()).resolvedType;
if (left instanceof TypePlaceholder && methodAndTph.getLocalTphs().contains(((TypePlaceholder)left).getName())) { if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
if (right instanceof TypePlaceholder) { if (right instanceof TypePlaceholder) {
methodAndTph.addPair(allPairs.stream().filter( methodAndTph.addPair(allPairs.stream().filter(
p -> (p.getLeft() instanceof TypePlaceholder && p -> (p.getLeft() instanceof TypePlaceholder &&
@ -339,7 +362,7 @@ public class TPHExtractor extends AbstractASTWalker {
if (!inLambdaExpression) { if (!inLambdaExpression) {
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(aReturn.retexpr.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(aReturn.retexpr.getType()).resolvedType;
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(methodAndTph.getReturnType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(methodAndTph.getReturnType()).resolvedType;
if (left instanceof TypePlaceholder && methodAndTph.getLocalTphs().contains(((TypePlaceholder)left).getName())) { if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
if (right instanceof TypePlaceholder) { if (right instanceof TypePlaceholder) {
methodAndTph.addPair(allPairs.stream().filter( methodAndTph.addPair(allPairs.stream().filter(
p -> (p.getLeft() instanceof TypePlaceholder && p -> (p.getLeft() instanceof TypePlaceholder &&

View File

@ -142,7 +142,8 @@ public class GeneratedGenericsFinder implements ASTVisitor {
System.out.println("fogg.classConstraints: "+ fogg.classConstraints); System.out.println("fogg.classConstraints: "+ fogg.classConstraints);
System.out.println("fogg.methodConstraintsWithPosition: "+ fogg.methodConstraintsWithPosition); System.out.println("fogg.methodConstraintsWithPosition: "+ fogg.methodConstraintsWithPosition);
/* ///*
//Fayez Ansatz Anfang
tphsClass = tphExtractor.tphsClass; tphsClass = tphExtractor.tphsClass;
//PL 2020-01-15 //PL 2020-01-15
//Es muss ggResult aus fogg gebildet werden //Es muss ggResult aus fogg gebildet werden
@ -156,13 +157,15 @@ public class GeneratedGenericsFinder implements ASTVisitor {
addMethodConstraints(simplifiedConstraints, ggResult, m); addMethodConstraints(simplifiedConstraints, ggResult, m);
} }
*/
if(ggResult != null) { //Hinzufuegen von Fayez ggResult if(ggResult != null) { //Hinzufuegen von Fayez ggResult
//generatedGenericsForSF.addGenericGeneratorResultClass(ggResult); generatedGenericsForSF.addGenericGeneratorResultClass(ggResult);
} }
// Fayez Ansatz Ende
//*/
//Ali Ansatz Anfang
List<GenericsGeneratorResult> listOfClassCons = new ArrayList<>(); List<GenericsGeneratorResult> listOfClassCons = new ArrayList<>();
for(TPHConstraint clCons: fogg.classConstraints) { for(TPHConstraint clCons: fogg.classConstraints) {
// ExtendsConstraint ec = new ExtendsConstraint(clCons.getLeft(), clCons.getRight()); // ExtendsConstraint ec = new ExtendsConstraint(clCons.getLeft(), clCons.getRight());
@ -186,10 +189,11 @@ public class GeneratedGenericsFinder implements ASTVisitor {
ggResultAlternative = new GenericsGeneratorResultForClass(className, listOfClassCons, ggRfaM); ggResultAlternative = new GenericsGeneratorResultForClass(className, listOfClassCons, ggRfaM);
if(ggResultAlternative != null) {//hinzufuegen von Alis ggResult if(ggResultAlternative != null) {//hinzufuegen von Alis ggResult
generatedGenericsForSF.addGenericGeneratorResultClass(ggResultAlternative); //generatedGenericsForSF.addGenericGeneratorResultClass(ggResultAlternative);
System.out.println(generatedGenericsForSF); System.out.println(generatedGenericsForSF);
} }
System.out.println(ggResultAlternative); System.out.println(ggResultAlternative);
//Ali Ansatz Ende
} }

View File

@ -69,7 +69,8 @@ public class FamilyOfGeneratedGenerics {
ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> methodConstraints1Pairs, ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> methodConstraints1Pairs,
List<MethodAndTPH> listOfMethodsAndTph) { List<MethodAndTPH> listOfMethodsAndTph) {
List<MethodConstraint> cs_m = new ArrayList<>(); List<MethodConstraint> cs_m = new ArrayList<>();
//List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); //List<MethodConstraint> methodConstraints1 =
typeOfTheMethodInClSigma(cs, posOfTphs);
List<MethodConstraint> methodConstraints1 = List<MethodConstraint> methodConstraints1 =
methodConstraints1Pairs.stream() methodConstraints1Pairs.stream()

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.result.GenericInsertPair; import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
import de.dhbwstuttgart.typeinference.result.ResultPair; import de.dhbwstuttgart.typeinference.result.ResultPair;
@ -20,9 +21,9 @@ public class MethodAndTPH {
/* /*
* Returntyp der zugehoerigen Methode in er abstrakten Syntax * Returntyp der zugehoerigen Methode in er abstrakten Syntax
*/ */
TypePlaceholder returnType; RefTypeOrTPHOrWildcardOrGeneric returnType;
public MethodAndTPH(String name, TypePlaceholder returnType) { public MethodAndTPH(String name, RefTypeOrTPHOrWildcardOrGeneric returnType) {
this.id = name; this.id = name;
this.returnType = returnType; this.returnType = returnType;
} }
@ -50,7 +51,7 @@ public class MethodAndTPH {
basePairs.add(p); basePairs.add(p);
} }
public TypePlaceholder getReturnType() { public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return returnType; return returnType;
} }

View File

@ -126,6 +126,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{
public void visit(Assign assign) { public void visit(Assign assign) {
assign.lefSide.accept(this); assign.lefSide.accept(this);
assign.rightSide.accept(this); assign.rightSide.accept(this);
assign.rightSide.getType().accept((ASTVisitor) this);
} }
@Override @Override
@ -186,6 +187,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{
public void visit(MethodCall methodCall) { public void visit(MethodCall methodCall) {
methodCall.receiver.accept(this); methodCall.receiver.accept(this);
methodCall.getArgumentList().accept(this); methodCall.getArgumentList().accept(this);
methodCall.getArgumentList().getArguments().forEach(a -> a.getType().accept((ASTVisitor) this));
} }
@ -212,6 +214,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{
@Override @Override
public void visit(Return aReturn) { public void visit(Return aReturn) {
aReturn.retexpr.accept(this); aReturn.retexpr.accept(this);
aReturn.getType().accept((ASTVisitor) this);
} }
@Override @Override

View File

@ -31,8 +31,14 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope
private ExceptionList exceptionlist; private ExceptionList exceptionlist;
private GenericDeclarationList generics; private GenericDeclarationList generics;
private final RefTypeOrTPHOrWildcardOrGeneric returnType; private final RefTypeOrTPHOrWildcardOrGeneric returnType;
public final Boolean isInherited; public final Boolean isInherited;
/*
* its Constraints
* eingefuegt PL 2021-02-15
*/
public final ConstraintSet constraints = new ConstraintSet();
public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block, public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block,
GenericDeclarationList gtvDeclarations, Token offset) { GenericDeclarationList gtvDeclarations, Token offset) {
super(offset); super(offset);

View File

@ -32,8 +32,10 @@ public class TYPE {
private ConstraintSet getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) { private ConstraintSet getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) {
ConstraintSet ret = new ConstraintSet(); ConstraintSet ret = new ConstraintSet();
ConstraintSet methConstrains;
for(Method m : cl.getMethods()){ for(Method m : cl.getMethods()){
ret.addAll(getConstraintsMethod(m,info, cl)); ret.addAll(methConstrains = getConstraintsMethod(m,info, cl));
m.constraints.addAll(methConstrains);
} }
for(Constructor m : cl.getConstructors()){ for(Constructor m : cl.getConstructors()){
ret.addAll(getConstraintsConstructor(m,info, cl)); ret.addAll(getConstraintsConstructor(m,info, cl));