diff --git a/src/de/dhbwstuttgart/strucTypes/Solve.java b/src/de/dhbwstuttgart/strucTypes/Solve.java index bc0b11d8..232b53ec 100644 --- a/src/de/dhbwstuttgart/strucTypes/Solve.java +++ b/src/de/dhbwstuttgart/strucTypes/Solve.java @@ -14,6 +14,7 @@ import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceWithConstrai import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException; import de.dhbwstuttgart.strucTypes.visitor.InferTypes; +import de.dhbwstuttgart.strucTypes.visitor.TypeExtract; import de.dhbwstuttgart.strucTypes.visitor.TypeVar; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; @@ -61,21 +62,22 @@ public class Solve { subst.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp())).collect(Collectors.toSet()), tphs); - // 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 + // add type variables of clsA as GenericTypeVar to values of + // tNew typeVar.getTypeVars().stream().map(tph -> new GenericTypeVar(tph.getName(), bounds, offset, offset)) .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 + // add type variables of cs as GenericRefType to values of + // tNew 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(gtv -> { @@ -83,7 +85,15 @@ public class Solve { values.add(gtv); }; }); - // TODO typevar(superclass) + // add generics of all superclasses and superinterfaces of clsA to + // values of tNew + TypeExtract typeExtract = new TypeExtract(); + clsA.accept(typeExtract); + typeExtract.getGenerics().forEach(gtv -> { + if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) { + values.add(gtv); + }; + }); GenericDeclarationList tNew = new GenericDeclarationList(values, offset); if (!consistent(cs)) { diff --git a/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java b/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java index 1def5716..bdfa69ad 100644 --- a/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java +++ b/src/de/dhbwstuttgart/strucTypes/visitor/InferTypes.java @@ -189,7 +189,6 @@ public class InferTypes implements ASTReturnVisitor { @Override public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { - RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(typePlaceholder); if (inferredType instanceof TypePlaceholder) { TypePlaceholder inferredTypeTPH = (TypePlaceholder) inferredType; @@ -334,6 +333,7 @@ public class InferTypes implements ASTReturnVisitor { @Override public This visit(This aThis) { + // als Typ wird immer ein neuer TypePlaceholder zugeordnet. return aThis; } diff --git a/src/de/dhbwstuttgart/strucTypes/visitor/TypeExtract.java b/src/de/dhbwstuttgart/strucTypes/visitor/TypeExtract.java index e8246644..7df4d76e 100644 --- a/src/de/dhbwstuttgart/strucTypes/visitor/TypeExtract.java +++ b/src/de/dhbwstuttgart/strucTypes/visitor/TypeExtract.java @@ -9,6 +9,8 @@ import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceFactory; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -25,6 +27,7 @@ public class TypeExtract extends DefaultASTVisitor { private List fields = new ArrayList<>(); private List methods = new ArrayList<>(); private List constructors = new ArrayList<>(); + private List generics = new ArrayList<>(); private boolean typeVariable = false; private boolean initialClass = true; @@ -40,6 +43,10 @@ public class TypeExtract extends DefaultASTVisitor { return constructors; } + public List getGenerics() { + return generics; + } + public Optional getField(String fieldName) { return this.fields.stream().filter(f -> f.getName().equals(fieldName)) // keine statische Polymorphie zugelassen @@ -66,6 +73,7 @@ public class TypeExtract extends DefaultASTVisitor { if (c != null && this.initialClass) c.accept(this); }); + classOrInterface.getGenerics().accept(this); this.initialClass = false; // superClass(Object) -> Object => unendliche Rekursionstiefe! if (!classOrInterface.getClassName().equals(new JavaClassName("java.lang.Object"))) { @@ -82,12 +90,12 @@ public class TypeExtract extends DefaultASTVisitor { @Override public void visit(Field field) { - this.fields.add(field); + this.fields.add(field); } @Override public void visit(Method method) { - this.methods.add(method); + this.methods.add(method); } @Override @@ -100,4 +108,15 @@ public class TypeExtract extends DefaultASTVisitor { this.constructors.add(constructor); } + @Override + public void visit(GenericDeclarationList genericTypeVars) { + genericTypeVars.forEach(gtv -> gtv.accept(this)); + } + + @Override + public void visit(GenericTypeVar genericTypeVar) { + if (!this.generics.contains(genericTypeVar)) { + this.generics.add(genericTypeVar); + } + } } diff --git a/test/strucType/TestSolve.java b/test/strucType/TestSolve.java index 844a2fd3..72882043 100644 --- a/test/strucType/TestSolve.java +++ b/test/strucType/TestSolve.java @@ -73,8 +73,8 @@ public class TestSolve { // Alle Klassen aus allen SourceFiles List availableClasses = compiler.getAvailableClasses(sourceFile); FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses); - System.out.println("\nFinite Closure:"); - System.out.println(finiteClosure); +// System.out.println("\nFinite Closure:"); +// System.out.println(finiteClosure); Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct); ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass(); System.out.println("\nSolved Class:");