diff --git a/src/de/dhbwstuttgart/strucTypes/Solve.java b/src/de/dhbwstuttgart/strucTypes/Solve.java index 1ccc8a2a..bc0b11d8 100644 --- a/src/de/dhbwstuttgart/strucTypes/Solve.java +++ b/src/de/dhbwstuttgart/strucTypes/Solve.java @@ -63,22 +63,33 @@ public class Solve { // TODO Tnew List values = new ArrayList<>(); + // extract typeplaceholder (type variables) of clsA TypeVar typeVar = new TypeVar(inferredTypes); clsA.accept(typeVar); final ArrayList bounds = new ArrayList<>(); final NullToken offset = new NullToken(); + // add type variables of clsA as GenericTypeVar to values of GenericDeclarationList typeVar.getTypeVars().stream().map(tph -> new GenericTypeVar(tph.getName(), bounds, offset, offset)) - .forEach(values::add); + .forEach(gtv -> { + if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) { + values.add(gtv); + }; + }); + // add type variables of cs as GenericRefType to values of GenericDeclarationList cs.stream().flatMap(c -> Stream.of(c.getSubtype(), c.getSupertype())).filter(t -> t instanceof TypePlaceholder) .map(tph -> new GenericTypeVar(((TypePlaceholder) tph).getName(), bounds, offset, offset)) - .forEach(values::add); - //TODO typevar(superclass) - GenericDeclarationList Tnew = new GenericDeclarationList(values, offset); + .forEach(gtv -> { + if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) { + values.add(gtv); + }; + }); + // TODO typevar(superclass) + GenericDeclarationList tNew = new GenericDeclarationList(values, offset); if (!consistent(cs)) { throw new InconsistentConstraintsException(); } - return new ClassOrInterfaceWithConstraints(this.clsA.accept(new InferTypes(inferredTypes)), Tnew, + return new ClassOrInterfaceWithConstraints(this.clsA.accept(new InferTypes(inferredTypes, tNew)), tNew, this.constraints); } diff --git a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java index b1469f44..aacb73a2 100644 --- a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java +++ b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java @@ -88,7 +88,7 @@ public class TYPEExpr extends DefaultASTVisitor { * korrespondierenden Feld in der Instanz auf. * * @param fieldVar - * @return true, wenn ein passendes Feld in der Instanz gefungen wird. + * @return true, wenn ein passendes Feld in der Instanz gefunden wird. */ private boolean inferFieldVarTypes(FieldVar fieldVar) { if (fieldVar.receiver instanceof This) { @@ -108,6 +108,7 @@ public class TYPEExpr extends DefaultASTVisitor { // ermittelt den Typ ty0 von methodCall.receiver und mtype(m, ty0) TypeExtract methodTypeVisitor = new TypeExtract(); + // unterscheide methodcall.receiver == this if (methodCall.receiver instanceof ExpressionReceiver && ((ExpressionReceiver) methodCall.receiver).expr instanceof This) { this.aThis.accept(methodTypeVisitor); diff --git a/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java b/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java index 2542253d..1def5716 100644 --- a/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java +++ b/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java @@ -57,14 +57,28 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +/** + * Inferiert alle TypePlaceholder durch inferredTypes und erstzt sie durch + * GenericTypeVar, falls der Name des Typeplaceholder mit dem Namen eines + * generics in der GenericdeclarationList generics übereinstimmt. + * + * @author mvr + * + */ public class InferTypes implements ASTReturnVisitor { private InferredTypes inferredTypes = new InferredTypes(); + private GenericDeclarationList generics = new GenericDeclarationList(new ArrayList<>(), new NullToken()); public InferTypes(InferredTypes inferredTypes) { this.inferredTypes = inferredTypes; } + public InferTypes(InferredTypes inferredTypes, GenericDeclarationList generics) { + this.inferredTypes = inferredTypes; + this.generics = generics; + } + @Override public SourceFile visit(SourceFile sourceFile) { return new SourceFile(sourceFile.getPkgName(), @@ -171,12 +185,19 @@ public class InferTypes implements ASTReturnVisitor { @Override public SuperWildcardType visit(SuperWildcardType superWildcardType) { throw new NotImplementedException(); - } @Override public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { - return inferredTypes.infer(typePlaceholder); + + RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(typePlaceholder); + if (inferredType instanceof TypePlaceholder) { + TypePlaceholder inferredTypeTPH = (TypePlaceholder) inferredType; + if (generics.contains(inferredTypeTPH.getName())) { + return new GenericRefType(inferredTypeTPH.getName(), typePlaceholder.getOffset()); + } + } + return inferredType; } @Override @@ -342,7 +363,7 @@ public class InferTypes implements ASTReturnVisitor { @Override public SuperCall visit(SuperCall superCall) { - if(superCall.getArgumentList().getArguments() == null){ + if (superCall.getArgumentList().getArguments() == null) { return superCall; } return new SuperCall(superCall.getArgumentList().accept(this), superCall.getOffset()); diff --git a/src/de/dhbwstuttgart/strucTypes/visitor/TypeVar.java b/src/de/dhbwstuttgart/strucTypes/visitor/TypeVar.java index 27354536..82ad319e 100644 --- a/src/de/dhbwstuttgart/strucTypes/visitor/TypeVar.java +++ b/src/de/dhbwstuttgart/strucTypes/visitor/TypeVar.java @@ -8,6 +8,12 @@ import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +/** + * Extrahiert alle TypePlaceholder. + * + * @author mvr + * + */ public class TypeVar extends AbstractASTWalker { private Set typeVars = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java index eb3e79d9..c7ac2c30 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java @@ -23,6 +23,10 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable gtv.getName().equals(genericTypeVarName)); + } @Override public Iterator iterator() {