From 4ca0e1e5f669fbf49c20fd9a79f2a15e20518331 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Fri, 9 Apr 2021 20:13:15 +0200 Subject: [PATCH] Add a child class of the compiler to implement the wildcard infer algorithm --- .../JavaTXCompilerWildcards.java | 48 +++++++++++++++++++ .../TypePlaceholderReplaceUtils.java | 40 ++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java create mode 100644 src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java new file mode 100644 index 00000000..b8654d83 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java @@ -0,0 +1,48 @@ +package de.dhbwstuttgart.inferWildcards; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; + +public class JavaTXCompilerWildcards + extends JavaTXCompiler +{ + + private final Map tphMap; + + public JavaTXCompilerWildcards (File... sourceFile) throws IOException, ClassNotFoundException { + super(sourceFile); + this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this); + } + + public JavaTXCompilerWildcards (File sourceFile, Boolean log) throws IOException, ClassNotFoundException { + super(sourceFile, log); + this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this); + } + + public JavaTXCompilerWildcards (List sourceFiles) throws IOException, ClassNotFoundException { + super(sourceFiles); + this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this); + } + + public JavaTXCompilerWildcards (List sources, List contextPath) + throws IOException, ClassNotFoundException { + super(sources, contextPath); + this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this); + } + + @Override + public ConstraintSet getConstraints () throws ClassNotFoundException, IOException { + ConstraintSet constraints = super.getConstraints(); + ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); + constraints.addAll(generateConstraints); + return constraints; + } +} diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java b/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java new file mode 100644 index 00000000..7db70f6b --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java @@ -0,0 +1,40 @@ +package de.dhbwstuttgart.inferWildcards; + +import java.io.File; +import java.util.Map; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +/** + * Class providing utilities to generate the Type placeholder for parameter + * types which shall be inferred. + * + * @author Till Schnell + * @version 1.0 + */ +public final class TypePlaceholderReplaceUtils +{ + + private TypePlaceholderReplaceUtils () { + throw new AssertionError("No TypePlaceholderReplaceUtils instance for you"); + } + + /** + * Return the map of the generated type placeholder for the associate parameter + * type + * + * @param compiler {@link JavaTXCompiler} to get the source file from + * @return {@link Map} over {@link TypePlaceholder} and the {@link RefType} + * replaced + */ + public static Map generateTypePlaceholder (JavaTXCompiler compiler) { + Map sourceFiles = compiler.getSourceFiles(); + ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor(); + sourceFiles.forEach( (k, v) -> v.accept(visitor)); + return visitor.getTphMap(); + } + +}