From b59aabeea59f4fe2cd66b029f19555e7fa5608d9 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Tue, 6 Apr 2021 18:38:01 +0200 Subject: [PATCH] generate the constraints for all tph --- .../inferWildcards/ConstraintsGenerationUtils.java | 11 +++++++++++ .../inferWildcards/ReplaceTypeparamVisitor.java | 10 +++++++--- .../java/inferWildcards/TestInferWildcardsJavaTx.java | 11 ++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java index 10b5c315..b1c35c15 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.inferWildcards; import java.util.HashSet; +import java.util.Map; import java.util.Set; import de.dhbwstuttgart.parser.NullToken; @@ -21,6 +22,16 @@ public final class ConstraintsGenerationUtils throw new AssertionError("No ConstraintsGenerationUtils instance for you"); } + @SuppressWarnings("rawtypes") + public static ConstraintSet generateConstraints (Map tphMap) { + ConstraintSet constraintSet = new ConstraintSet<>(); + tphMap.forEach( (tph, refType) -> { + ConstraintSet constraintSet2 = generateConstraints(refType, tph); + constraintSet.addAll(constraintSet2); + }); + return constraintSet; + } + @SuppressWarnings({ "unchecked", "rawtypes" }) public static ConstraintSet generateConstraints (RefType refType, TypePlaceholder tph) { ConstraintSet constraintSet = new ConstraintSet<>(); diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java index 808f9579..43e3a75b 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.inferWildcards; import java.util.HashMap; +import java.util.List; import java.util.Map; import de.dhbwstuttgart.parser.NullToken; @@ -13,7 +14,7 @@ public class ReplaceTypeparamVisitor extends AbstractASTWalker { - private final Map tphMap; + private final Map tphMap; public ReplaceTypeparamVisitor () { this.tphMap = new HashMap<>(); @@ -23,7 +24,10 @@ public class ReplaceTypeparamVisitor public void visit (RefType refType) { if (!refType.getParaList().isEmpty()) { System.out.println("Type: " + refType); - generateTypePlaceholder(refType); + List paraList = refType.getParaList(); + paraList.stream().filter(e -> e instanceof RefType).map(RefType.class::cast) + .forEach(this::generateTypePlaceholder); + } super.visit(refType); } @@ -34,7 +38,7 @@ public class ReplaceTypeparamVisitor return tph; } - public Map getTphMap () { + public Map getTphMap () { return tphMap; } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java b/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java index 8d66011f..bf631082 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java +++ b/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java @@ -11,8 +11,11 @@ import org.junit.Before; import org.junit.Test; import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.ReplaceTypeparamVisitor; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -39,7 +42,13 @@ public class TestInferWildcardsJavaTx Map sourceFiles = javaTXCompiler.getSourceFiles(); ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor(); sourceFiles.entrySet().forEach(e -> e.getValue().accept(visitor)); - System.out.println(visitor.getTphMap()); + + // Generate Constraints + Map tphMap = visitor.getTphMap(); + System.out.println(tphMap); + ConstraintSet generatedConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); + System.out.println(generatedConstraints); + // Constraints ConstraintSet constraints = javaTXCompiler.getConstraints();