From cfce2f55acb12d63032d5e875ea2c6c6b4c7c158 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Sun, 25 Apr 2021 11:23:12 +0200 Subject: [PATCH] Add generation of wildcard constraints for generic types in source --- .../ConstraintsGenerationUtils.java | 17 ++++++++------- .../JavaTXCompilerWildcards.java | 9 ++++---- .../ReplaceTypeparamVisitor.java | 21 ++++++++++++++----- .../TypePlaceholderReplaceUtils.java | 4 +++- .../TestInferWildcardsFields.java | 12 +++++++---- .../inferWildcards/TestInferWildcardsMap.java | 11 ++++++---- .../TestInferWildcardsMapNested.java | 11 ++++++---- .../TestInferWildcardsNested.java | 11 ++++++---- .../TestInferWildcardsParamType.java | 11 ++++++---- .../TestInferWildcardsSingle.java | 11 ++++++---- 10 files changed, 77 insertions(+), 41 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java index 3cad91fd..03b855d8 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java @@ -6,7 +6,6 @@ import java.util.Set; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; -import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -32,13 +31,14 @@ public final class ConstraintsGenerationUtils * 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 tphMap {@link Map} of {@link TypePlaceholder} and + * {@link RefTypeOrTPHOrWildcardOrGeneric} * @param constraints {@link ConstraintSet} over {@link Pair} to merge to * @return The same instance {@code constraints} provided including the merged * constraints */ public static ConstraintSet generateAndMergeConstraints ( - Map tphMap, + Map tphMap, ConstraintSet constraints) { ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); constraints.addAll(generateConstraints); @@ -48,11 +48,13 @@ public final class ConstraintsGenerationUtils /** * Generate the constraints for a map of type placeholder and RefType. * - * @param tphMap {@link Map} of {@link TypePlaceholder} and {@link RefType} + * @param tphMap {@link Map} of {@link TypePlaceholder} and + * {@link RefTypeOrTPHOrWildcardOrGeneric} * @return {@link ConstraintSet} of {@link Pair} containing the constraints to * infer the matching wildcard type. */ - public static ConstraintSet generateConstraints (Map tphMap) { + public static ConstraintSet generateConstraints ( + Map tphMap) { ConstraintSet constraintSet = new ConstraintSet<>(); tphMap.forEach( (tph, refType) -> { ConstraintSet constraintSet2 = generateConstraints(refType, tph); @@ -65,12 +67,13 @@ public final class ConstraintsGenerationUtils * Generate the constraints for a single RefType type placeholder pair to infer * the wildcards for the generic parameter type. * - * @param refType {@link RefType} + * @param refType {@link RefTypeOrTPHOrWildcardOrGeneric} * @param tph {@link TypePlaceholder} * @return {@link ConstraintSet} of {@link Pair} generated containing the * constraints. */ - public static ConstraintSet generateConstraints (RefType refType, TypePlaceholder tph) { + public static ConstraintSet generateConstraints (RefTypeOrTPHOrWildcardOrGeneric refType, + TypePlaceholder tph) { ConstraintSet constraintSet = new ConstraintSet<>(); Set> oderConstraints = new HashSet<>(); constraintSet.addOderConstraint(oderConstraints); diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java index ea9de26f..8a4f49a5 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.Map; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -33,7 +33,7 @@ public class JavaTXCompilerWildcards /** * Generated Type placeholder and the implementation type represented. */ - private final Map tphMap; + private final Map tphMap; public JavaTXCompilerWildcards (File... sourceFile) throws IOException, ClassNotFoundException { super(sourceFile); @@ -59,9 +59,10 @@ public class JavaTXCompilerWildcards /** * Return the Type Placeholder generated. * - * @return {@link Map} over {@link TypePlaceholder} and {@link RefType} + * @return {@link Map} over {@link TypePlaceholder} and + * {@link RefTypeOrTPHOrWildcardOrGeneric} */ - public Map getTphMap () { + public Map getTphMap () { return tphMap; } diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java index ef308f09..eb8ae531 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java @@ -10,6 +10,7 @@ import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -28,7 +29,7 @@ public class ReplaceTypeparamVisitor /** * Containing the replaced RefType and the mapping TPH */ - private final Map tphMap; + private final Map tphMap; /** * Constructor for a {@code ReplaceTypeparamVisitor}. @@ -81,7 +82,7 @@ public class ReplaceTypeparamVisitor * * @return {@link Map} of {@link TypePlaceholder} and {@link RefType} */ - public Map getTphMap () { + public Map getTphMap () { return tphMap; } @@ -118,6 +119,15 @@ public class ReplaceTypeparamVisitor // Generate TPH TypePlaceholder tph = generateTypePlaceholder(nextRefType); + // Replace in AST + listIterator.set(tph); + } + else if (next instanceof GenericRefType) { + GenericRefType nextGenericRefType = (GenericRefType) next; + + // Generate TPH + TypePlaceholder tph = generateTypePlaceholder(nextGenericRefType); + // Replace in AST listIterator.set(tph); } @@ -131,12 +141,13 @@ public class ReplaceTypeparamVisitor } /** - * Generate the TPH for a {@link RefType} and saves the mapping. + * Generate the TPH for a {@link RefTypeOrTPHOrWildcardOrGeneric} and saves the + * mapping. * - * @param t {@link RefType} + * @param t {@link RefTypeOrTPHOrWildcardOrGeneric} * @return {@link TypePlaceholder} generated */ - private TypePlaceholder generateTypePlaceholder (RefType t) { + private TypePlaceholder generateTypePlaceholder (RefTypeOrTPHOrWildcardOrGeneric t) { TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); tphMap.put(tph, t); return tph; diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java b/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java index 7d185a51..c6758467 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/TypePlaceholderReplaceUtils.java @@ -6,6 +6,7 @@ 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.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; /** @@ -30,7 +31,8 @@ public final class TypePlaceholderReplaceUtils * @return {@link Map} over {@link TypePlaceholder} and the {@link RefType} * replaced */ - public static Map generateTypePlaceholder (JavaTXCompiler compiler) { + public static Map generateTypePlaceholder ( + JavaTXCompiler compiler) { Map sourceFiles = compiler.getSourceFiles(); ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor(); sourceFiles.forEach( (k, v) -> v.accept(visitor)); diff --git a/src/test/java/inferWildcards/TestInferWildcardsFields.java b/src/test/java/inferWildcards/TestInferWildcardsFields.java index ae2bd63c..f465ee54 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsFields.java +++ b/src/test/java/inferWildcards/TestInferWildcardsFields.java @@ -22,6 +22,7 @@ import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -52,14 +53,15 @@ public class TestInferWildcardsFields return new JavaTXCompilerWildcards(files1); } - private static Map generateTph (JavaTXCompiler javaTXCompiler) { + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { System.out.println("\nReplacements:"); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); } private static List generateExpectedTypeInferResult (JavaTXCompilerWildcards compiler) { - Map tphMap = compiler.getTphMap(); + Map tphMap = compiler.getTphMap(); ArrayList list = new ArrayList<>(); @@ -79,7 +81,8 @@ public class TestInferWildcardsFields System.out.println("\n--------- Test Generate TPH --------------\n"); JavaTXCompiler javaTXCompiler = getStandardCompiler(); - Map generateTph = generateTph(javaTXCompiler); + Map generateTph = generateTph( + javaTXCompiler); System.out.println(generateTph); @@ -91,7 +94,8 @@ public class TestInferWildcardsFields System.out.println("\n--------- Test Generate Constraints --------------\n"); JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); - Map tphMap = javaTXCompilerWildcards.getTphMap(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); System.out.println(generateConstraints); diff --git a/src/test/java/inferWildcards/TestInferWildcardsMap.java b/src/test/java/inferWildcards/TestInferWildcardsMap.java index 261b4e18..497e75c6 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsMap.java +++ b/src/test/java/inferWildcards/TestInferWildcardsMap.java @@ -16,7 +16,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards; import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -44,7 +44,8 @@ public class TestInferWildcardsMap return new JavaTXCompilerWildcards(files1); } - private static Map generateTph (JavaTXCompiler javaTXCompiler) { + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { System.out.println("\nReplacements:"); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); @@ -55,7 +56,8 @@ public class TestInferWildcardsMap System.out.println("\n--------- Test Generate TPH --------------\n"); JavaTXCompiler javaTXCompiler = getStandardCompiler(); - Map generateTph = generateTph(javaTXCompiler); + Map generateTph = generateTph( + javaTXCompiler); System.out.println(generateTph); @@ -67,7 +69,8 @@ public class TestInferWildcardsMap System.out.println("\n--------- Test Generate Constraints --------------\n"); JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); - Map tphMap = javaTXCompilerWildcards.getTphMap(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); System.out.println(generateConstraints); diff --git a/src/test/java/inferWildcards/TestInferWildcardsMapNested.java b/src/test/java/inferWildcards/TestInferWildcardsMapNested.java index cb51959c..5f99c5ce 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsMapNested.java +++ b/src/test/java/inferWildcards/TestInferWildcardsMapNested.java @@ -16,7 +16,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards; import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -44,7 +44,8 @@ public class TestInferWildcardsMapNested return new JavaTXCompilerWildcards(files1); } - private static Map generateTph (JavaTXCompiler javaTXCompiler) { + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { System.out.println("\nReplacements:"); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); @@ -55,7 +56,8 @@ public class TestInferWildcardsMapNested System.out.println("\n--------- Test Generate TPH --------------\n"); JavaTXCompiler javaTXCompiler = getStandardCompiler(); - Map generateTph = generateTph(javaTXCompiler); + Map generateTph = generateTph( + javaTXCompiler); System.out.println(generateTph); @@ -67,7 +69,8 @@ public class TestInferWildcardsMapNested System.out.println("\n--------- Test Generate Constraints --------------\n"); JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); - Map tphMap = javaTXCompilerWildcards.getTphMap(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); System.out.println(generateConstraints); diff --git a/src/test/java/inferWildcards/TestInferWildcardsNested.java b/src/test/java/inferWildcards/TestInferWildcardsNested.java index 6ee64b7a..1b0ab1f5 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsNested.java +++ b/src/test/java/inferWildcards/TestInferWildcardsNested.java @@ -16,7 +16,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards; import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -44,7 +44,8 @@ public class TestInferWildcardsNested return new JavaTXCompilerWildcards(files1); } - private static Map generateTph (JavaTXCompiler javaTXCompiler) { + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { System.out.println("\nReplacements:"); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); @@ -55,7 +56,8 @@ public class TestInferWildcardsNested System.out.println("\n--------- Test Generate TPH --------------\n"); JavaTXCompiler javaTXCompiler = getStandardCompiler(); - Map generateTph = generateTph(javaTXCompiler); + Map generateTph = generateTph( + javaTXCompiler); System.out.println(generateTph); @@ -67,7 +69,8 @@ public class TestInferWildcardsNested System.out.println("\n--------- Test Generate Constraints --------------\n"); JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); - Map tphMap = javaTXCompilerWildcards.getTphMap(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); System.out.println(generateConstraints); diff --git a/src/test/java/inferWildcards/TestInferWildcardsParamType.java b/src/test/java/inferWildcards/TestInferWildcardsParamType.java index 0536fdb9..17c3cbd6 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsParamType.java +++ b/src/test/java/inferWildcards/TestInferWildcardsParamType.java @@ -16,7 +16,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards; import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -44,7 +44,8 @@ public class TestInferWildcardsParamType return new JavaTXCompilerWildcards(files1); } - private static Map generateTph (JavaTXCompiler javaTXCompiler) { + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { System.out.println("\nReplacements:"); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); @@ -55,7 +56,8 @@ public class TestInferWildcardsParamType System.out.println("\n--------- Test Generate TPH --------------\n"); JavaTXCompiler javaTXCompiler = getStandardCompiler(); - Map generateTph = generateTph(javaTXCompiler); + Map generateTph = generateTph( + javaTXCompiler); System.out.println(generateTph); @@ -67,7 +69,8 @@ public class TestInferWildcardsParamType System.out.println("\n--------- Test Generate Constraints --------------\n"); JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); - Map tphMap = javaTXCompilerWildcards.getTphMap(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); System.out.println(generateConstraints); diff --git a/src/test/java/inferWildcards/TestInferWildcardsSingle.java b/src/test/java/inferWildcards/TestInferWildcardsSingle.java index 09fdbedb..0d871de7 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsSingle.java +++ b/src/test/java/inferWildcards/TestInferWildcardsSingle.java @@ -16,7 +16,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards; import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -44,7 +44,8 @@ public class TestInferWildcardsSingle return new JavaTXCompilerWildcards(files1); } - private static Map generateTph (JavaTXCompiler javaTXCompiler) { + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { System.out.println("\nReplacements:"); return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); @@ -55,7 +56,8 @@ public class TestInferWildcardsSingle System.out.println("\n--------- Test Generate TPH --------------\n"); JavaTXCompiler javaTXCompiler = getStandardCompiler(); - Map generateTph = generateTph(javaTXCompiler); + Map generateTph = generateTph( + javaTXCompiler); System.out.println(generateTph); @@ -67,7 +69,8 @@ public class TestInferWildcardsSingle System.out.println("\n--------- Test Generate Constraints --------------\n"); JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); - Map tphMap = javaTXCompilerWildcards.getTphMap(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); System.out.println(generateConstraints);