extract generation and merge to a static utility function

This commit is contained in:
Till Schnell 2021-04-09 20:25:10 +02:00
parent 4ca0e1e5f6
commit 88440b873a
3 changed files with 31 additions and 21 deletions

View File

@ -28,6 +28,22 @@ public final class ConstraintsGenerationUtils
throw new AssertionError("No ConstraintsGenerationUtils instance for you"); throw new AssertionError("No ConstraintsGenerationUtils instance for you");
} }
/**
* Generate the constraints for a map of type placeholder and RefType and merge
* these to the already provided constraints.
*
* @param tphMap {@link Map} of {@link TypePlaceholder} and {@link RefType}
* @param constraints {@link ConstraintSet} over {@link Pair} to merge to
* @return The same instance {@code constraints} provided including the merged
* constraints
*/
public static ConstraintSet<Pair> generateAndMergeConstraints (Map<TypePlaceholder, RefType> tphMap,
ConstraintSet<Pair> constraints) {
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
constraints.addAll(generateConstraints);
return constraints;
}
/** /**
* Generate the constraints for a map of type placeholder and RefType. * Generate the constraints for a map of type placeholder and RefType.
* *

View File

@ -41,8 +41,6 @@ public class JavaTXCompilerWildcards
@Override @Override
public ConstraintSet<Pair> getConstraints () throws ClassNotFoundException, IOException { public ConstraintSet<Pair> getConstraints () throws ClassNotFoundException, IOException {
ConstraintSet<Pair> constraints = super.getConstraints(); ConstraintSet<Pair> constraints = super.getConstraints();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); return ConstraintsGenerationUtils.generateAndMergeConstraints(tphMap, constraints);
constraints.addAll(generateConstraints);
return constraints;
} }
} }

View File

@ -9,8 +9,7 @@ import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.ReplaceTypeparamVisitor; import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
@ -24,7 +23,6 @@ public class TestInferWildcardsJavaTx
@Before @Before
public void setup () { public void setup () {
resourcePath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards"; resourcePath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
} }
private JavaTXCompiler getCompiler (String filename) throws ClassNotFoundException, IOException { private JavaTXCompiler getCompiler (String filename) throws ClassNotFoundException, IOException {
@ -32,18 +30,16 @@ public class TestInferWildcardsJavaTx
return new JavaTXCompiler(files1); return new JavaTXCompiler(files1);
} }
private ReplaceTypeparamVisitor generateTph (JavaTXCompiler javaTXCompiler) { private Map<TypePlaceholder, RefType> generateTph (JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:"); System.out.println("\nReplacements:");
Map<File, SourceFile> sourceFiles = javaTXCompiler.getSourceFiles();
ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor(); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
sourceFiles.forEach( (k, v) -> v.accept(visitor));
return visitor;
} }
private ConstraintSet<Pair> getGeneratedConstraints (ReplaceTypeparamVisitor visitor) { private ConstraintSet<Pair> getGeneratedConstraints (Map<TypePlaceholder, RefType> tphMap) {
System.out.println("\nGenerated TPH:"); System.out.println("\nGenerated TPH:");
Map<TypePlaceholder, RefType> tphMap = visitor.getTphMap();
System.out.println(tphMap); System.out.println(tphMap);
return ConstraintsGenerationUtils.generateConstraints(tphMap); return ConstraintsGenerationUtils.generateConstraints(tphMap);
} }
@ -65,10 +61,10 @@ public class TestInferWildcardsJavaTx
JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsSingle.java"); JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsSingle.java");
// Manipulate AST // Manipulate AST
ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); Map<TypePlaceholder, RefType> tphMap = generateTph(javaTXCompiler);
// Generate Constraints // Generate Constraints
ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(visitor); ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(tphMap);
// System.out.println(generatedConstraints); // System.out.println(generatedConstraints);
// Constraints // Constraints
@ -85,10 +81,10 @@ public class TestInferWildcardsJavaTx
JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsNested.java"); JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsNested.java");
// Manipulate AST // Manipulate AST
ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); Map<TypePlaceholder, RefType> tphMap = generateTph(javaTXCompiler);
// Generate Constraints // Generate Constraints
ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(visitor); ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(tphMap);
// System.out.println(generatedConstraints); // System.out.println(generatedConstraints);
// Constraints // Constraints
@ -101,10 +97,10 @@ public class TestInferWildcardsJavaTx
JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsMap.java"); JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsMap.java");
// Manipulate AST // Manipulate AST
ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); Map<TypePlaceholder, RefType> tphMap = generateTph(javaTXCompiler);
// Generate Constraints // Generate Constraints
ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(visitor); ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(tphMap);
// System.out.println(generatedConstraints); // System.out.println(generatedConstraints);
// Constraints // Constraints
@ -117,10 +113,10 @@ public class TestInferWildcardsJavaTx
JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsMapNested.java"); JavaTXCompiler javaTXCompiler = getCompiler("TestClassWildcardsMapNested.java");
// Manipulate AST // Manipulate AST
ReplaceTypeparamVisitor visitor = generateTph(javaTXCompiler); Map<TypePlaceholder, RefType> tphMap = generateTph(javaTXCompiler);
// Generate Constraints // Generate Constraints
ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(visitor); ConstraintSet<Pair> generatedConstraints = getGeneratedConstraints(tphMap);
// System.out.println(generatedConstraints); // System.out.println(generatedConstraints);
// Constraints // Constraints