modified: src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java

modified:   src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java
	modified:   src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java
	modified:   src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java
	modified:   src/main/java/de/dhbwstuttgart/syntaxtree/Method.java
	modified:   src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java
	modified:   src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java
Pairs sammeln rueckgaengig gemacht
This commit is contained in:
pl@gohorb.ba-horb.de 2021-02-16 16:27:17 +01:00
parent f2d6dae4c7
commit bbdf220324
7 changed files with 13 additions and 224 deletions

View File

@ -9,7 +9,6 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
@ -25,18 +24,8 @@ import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -52,32 +41,12 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TPHExtractor extends AbstractASTWalker {
// Alle TPHs der Felder werden iKopf der Klasse definiert
// alle TPHs der Klasse: (TPH, is in Method?)
/*
* ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu
*/
public HashMap<String, Method> nameToMeth;
/*
* ordnet den Fieldnamen ihre jeweiligen Lambda-respraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu
*/
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 List<String> tphsClass = new ArrayList<>();
MethodAndTPH methodAndTph;
Boolean inMethod = false;
boolean inLocalOrParamOrReturn = false;
Boolean inLambdaExpression = false;
public final ArrayList<MethodAndTPH> ListOfMethodsAndTph = new ArrayList<>();
final ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> allPairs = new ArrayList<>();
@ -86,7 +55,7 @@ public class TPHExtractor extends AbstractASTWalker {
private Resolver resolver;
public TPHExtractor() {
}
public void setResultSet(ResultSet resultSet) {
@ -215,9 +184,8 @@ public class TPHExtractor extends AbstractASTWalker {
@Override
public void visit(Method method) {
inMethod = true;
this.nameToLocalVar = new HashMap<>();
String id = MethodUtility.createID(resolver,method);
methodAndTph = new MethodAndTPH(id, method.getReturnType());
methodAndTph = new MethodAndTPH(id);
inLocalOrParamOrReturn = true;
method.getReturnType().accept(this);
@ -231,152 +199,23 @@ public class TPHExtractor extends AbstractASTWalker {
ListOfMethodsAndTph.add(methodAndTph);
}
@Override
public void visit(ClassOrInterface classOrInterface) {
Optional<Constructor> optInitialConstructor = classOrInterface.getfieldInitializations();
this.nameToField = new HashMap<>();
if (optInitialConstructor.isPresent()) {
classOrInterface.getfieldInitializations()
.get()
.block
.statements
.stream()
.filter(s -> s instanceof Assign && ((Assign)s).rightSide instanceof LambdaExpression)
.forEach(as ->
this.nameToField.put(((AssignToField)(((Assign)as).lefSide)).field.fieldVarName,
((AssignToField)(((Assign)as).lefSide)).getType())) ;
}
this.nameToMeth = new HashMap<>();
for(Method m : classOrInterface.getMethods()) {
this.nameToMeth.put(m.getName(), m);
}
super.visit(classOrInterface);
}
@Override
public void visit(Constructor cons) {
this.nameToLocalVar = new HashMap<>();
inMethod = false;
//super.visit(cons);
cons.getParameterList().accept(this);
if(cons.block != null)
cons.block.accept(this);
inMethod = true;
}
@Override
public void visit(LambdaExpression lambdaExpression) {
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);
inLambdaExpression = false;
}
@Override
public void visit(Assign assign) {
super.visit(assign);
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(assign.rightSide.getType()).resolvedType;
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(assign.lefSide.getType()).resolvedType;
if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
if (right instanceof TypePlaceholder) {
methodAndTph.addPair(allPairs.stream().filter(
p -> (p.getLeft() instanceof TypePlaceholder &&
p.getRight() instanceof TypePlaceholder) &&
((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) &&
((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName())
).findFirst()
.get());
}
}
}
@Override
public void visit(LocalVarDecl localVarDecl) {
// inLocalOrParamOrReturn = inMethod;
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;
}
@Override
public void visit(MethodCall methodCall) {
String fieldLocalvarOrMethName;
super.visit(methodCall);
if (nameToMeth.keySet().contains(fieldLocalvarOrMethName = methodCall.name) //Method-call of a method in the actuial Sourcefile
|| (methodCall.name.equals("apply") &&
(methodCall.receiver instanceof ExpressionReceiver) &&
((((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar) &&
nameToField.keySet().contains(fieldLocalvarOrMethName = ((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName)
|| (((ExpressionReceiver)methodCall.receiver).expr instanceof LocalVar) &&
nameToLocalVar.keySet().contains(fieldLocalvarOrMethName = ((LocalVar)((ExpressionReceiver)methodCall.receiver).expr).name)
)
//LOKALE VARIABLEN MUESSE NOCH IN nameToField EINGETRAGEN WERDEN
)
)
{
Iterator<RefTypeOrTPHOrWildcardOrGeneric> paraIt =
methodCall.name.equals("apply")
? (((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar)
? ((RefType)(resultSet.resolveType((nameToField.get(fieldLocalvarOrMethName))).resolvedType)).getParaList().iterator()
: ((RefType)(resultSet.resolveType((nameToLocalVar.get(fieldLocalvarOrMethName))).resolvedType)).getParaList().iterator()
: nameToMeth.get(fieldLocalvarOrMethName)
.getParameterList()
.getFormalparalist()
.stream()
.map(p -> p.getType())
.iterator();
methodCall.getArgumentList()
.getArguments()
.forEach(x -> {
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType;
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next()).resolvedType;
if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
if (right instanceof TypePlaceholder) {
methodAndTph.addPair(allPairs.stream().filter(
p -> (p.getLeft() instanceof TypePlaceholder &&
p.getRight() instanceof TypePlaceholder) &&
((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) &&
((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName())
).findFirst()
.get());
}
}
});
}
}
@Override
public void visit(Return aReturn) {
super.visit(aReturn);
if (!inLambdaExpression) {
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(aReturn.retexpr.getType()).resolvedType;
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(methodAndTph.getReturnType()).resolvedType;
if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
if (right instanceof TypePlaceholder) {
methodAndTph.addPair(allPairs.stream().filter(
p -> (p.getLeft() instanceof TypePlaceholder &&
p.getRight() instanceof TypePlaceholder) &&
((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) &&
((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName())
).findFirst()
.get());
}
}
}
}
@Override
public void visit(LocalVar localVar) {
// inLocalOrParamOrReturn = inMethod;

View File

@ -66,7 +66,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
*
*/
public class GeneratedGenericsFinder implements ASTVisitor {
private final TPHExtractor tphExtractor;
private final TPHExtractor tphExtractor = new TPHExtractor();
private Collection<ResultSet> listOfResultSets;
private SourceFile sf;
private List<String> tphsClass;
@ -86,7 +86,6 @@ public class GeneratedGenericsFinder implements ASTVisitor {
public GeneratedGenericsFinder(SourceFile sf, Collection<ResultSet> listOfResultSets) {
this.sf = sf;
this.listOfResultSets = listOfResultSets;
this.tphExtractor = new TPHExtractor();
}
public GenericGenratorResultForSourceFile findGeneratedGenerics() {

View File

@ -4,14 +4,10 @@ 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 de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.result.ResultPair;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import org.objectweb.asm.Type;
public class FamilyOfGeneratedGenerics {
@ -19,7 +15,7 @@ public class FamilyOfGeneratedGenerics {
// 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, List<PairTphMethod<PositionFinder.Position, String>>> posOfTPHs = new HashMap<>();
public List<ClassConstraint> classConstraints = new ArrayList<>();
//public List<MethodConstraint> methodConstraints = new ArrayList<>();
public List<MethodConstraint> methodConstraints = new ArrayList<>();
public HashMap<String, List<MethodConstraint>> methodConstraintsWithPosition = new HashMap<>();
private static final String objectType = Type.getInternalName(Object.class);
@ -28,7 +24,7 @@ public class FamilyOfGeneratedGenerics {
this.allConstraints = tphExtractor.allCons;
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
this.classConstraints = getClassConstraints(allConstraints,posOfTPHs);
//this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, null, tphExtractor.ListOfMethodsAndTph);
this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph);
this.methodConstraintsWithPosition = getMethodConstraintsWithPosition(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph);
}
@ -63,20 +59,9 @@ public class FamilyOfGeneratedGenerics {
return cs_cl;
}
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs,
List<ClassConstraint> cs_cl,
HashMap<String, List<PairTphMethod<PositionFinder.Position, String>>> posOfTphs,
ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> methodConstraints1Pairs,
List<MethodAndTPH> listOfMethodsAndTph) {
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, List<PairTphMethod<PositionFinder.Position, String>>> posOfTphs, List<MethodAndTPH> listOfMethodsAndTph) {
List<MethodConstraint> cs_m = new ArrayList<>();
//List<MethodConstraint> methodConstraints1 =
typeOfTheMethodInClSigma(cs, posOfTphs);
List<MethodConstraint> methodConstraints1 =
methodConstraints1Pairs.stream()
.map(p -> new MethodConstraint(p.getLeft().getName(), p.getRight().getName(), Relation.EXTENDS))
.collect(Collectors.toList());
List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
for (MethodConstraint cons: methodConstraints1) {
if (!checkForDuplicates(cons, cs_m)) {
cs_m.add(cons);
@ -137,7 +122,7 @@ public class FamilyOfGeneratedGenerics {
}
}
}
listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod, method.getBasePairs(),listOfMethodsAndTph);
listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod,listOfMethodsAndTph);
tempMethodConstraintsWithPosition.put(currentMethod, listOfThisMethod);
}
}
@ -264,7 +249,7 @@ public class FamilyOfGeneratedGenerics {
}
return tempMC;
}
/**
* Def. FGG: zweite Zeile von cs_m

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
import de.dhbwstuttgart.typeinference.result.ResultPair;
@ -16,16 +15,9 @@ public class MethodAndTPH {
private final ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> pairs = new ArrayList<>();
// tphs of local variables and parameters
private final ArrayList<String> localTphs = new ArrayList<>();
private final ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> basePairs = new ArrayList<>();
/*
* Returntyp der zugehoerigen Methode in er abstrakten Syntax
*/
RefTypeOrTPHOrWildcardOrGeneric returnType;
public MethodAndTPH(String name, RefTypeOrTPHOrWildcardOrGeneric returnType) {
public MethodAndTPH(String name) {
this.id = name;
this.returnType = returnType;
}
public ArrayList<String> getTphs() {
@ -47,16 +39,4 @@ public class MethodAndTPH {
return localTphs;
}
public void addPair(ResultPair<TypePlaceholder, TypePlaceholder> p) {
basePairs.add(p);
}
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return returnType;
}
public ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> getBasePairs() {
return basePairs;
}
}

View File

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

View File

@ -45,12 +45,6 @@ public class SourceFile extends SyntaxTreeNode{
public List<ClassOrInterface> getClasses() {
return KlassenVektor;
}
public List<Method> getAllMethods() {
List<Method> ret = new ArrayList<>();
getClasses().forEach(cl -> ret.addAll(cl.getMethods()));
return ret;
}
@Override
public void accept(ASTVisitor visitor) {

View File

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