Add generation of wildcard constraints for generic types in source

This commit is contained in:
Till Schnell 2021-04-25 11:23:12 +02:00
parent 4009a28333
commit cfce2f55ac
10 changed files with 77 additions and 41 deletions

View File

@ -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<Pair> generateAndMergeConstraints (
Map<? extends TypePlaceholder, ? extends RefType> tphMap,
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap,
ConstraintSet<Pair> constraints) {
ConstraintSet<Pair> 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<Pair> generateConstraints (Map<? extends TypePlaceholder, ? extends RefType> tphMap) {
public static ConstraintSet<Pair> generateConstraints (
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap) {
ConstraintSet<Pair> constraintSet = new ConstraintSet<>();
tphMap.forEach( (tph, refType) -> {
ConstraintSet<Pair> 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<Pair> generateConstraints (RefType refType, TypePlaceholder tph) {
public static ConstraintSet<Pair> generateConstraints (RefTypeOrTPHOrWildcardOrGeneric refType,
TypePlaceholder tph) {
ConstraintSet<Pair> constraintSet = new ConstraintSet<>();
Set<Constraint<Pair>> oderConstraints = new HashSet<>();
constraintSet.addOderConstraint(oderConstraints);

View File

@ -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<? extends TypePlaceholder, ? extends RefType> tphMap;
private final Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> getTphMap () {
public Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> getTphMap () {
return tphMap;
}

View File

@ -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<TypePlaceholder, RefType> tphMap;
private final Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> tphMap;
/**
* Constructor for a {@code ReplaceTypeparamVisitor}.
@ -81,7 +82,7 @@ public class ReplaceTypeparamVisitor
*
* @return {@link Map} of {@link TypePlaceholder} and {@link RefType}
*/
public Map<? extends TypePlaceholder, ? extends RefType> getTphMap () {
public Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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;

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTypePlaceholder (JavaTXCompiler compiler) {
public static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTypePlaceholder (
JavaTXCompiler compiler) {
Map<File, SourceFile> sourceFiles = compiler.getSourceFiles();
ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor();
sourceFiles.forEach( (k, v) -> v.accept(visitor));

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTph (JavaTXCompiler javaTXCompiler) {
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
private static List<ResultSet> generateExpectedTypeInferResult (JavaTXCompilerWildcards compiler) {
Map<? extends TypePlaceholder, ? extends RefType> tphMap = compiler.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = compiler.getTphMap();
ArrayList<ResultSet> list = new ArrayList<>();
@ -79,7 +81,8 @@ public class TestInferWildcardsFields
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefType> generateTph = generateTph(javaTXCompiler);
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> tphMap = javaTXCompilerWildcards.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTph (JavaTXCompiler javaTXCompiler) {
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> generateTph = generateTph(javaTXCompiler);
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> tphMap = javaTXCompilerWildcards.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTph (JavaTXCompiler javaTXCompiler) {
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> generateTph = generateTph(javaTXCompiler);
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> tphMap = javaTXCompilerWildcards.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTph (JavaTXCompiler javaTXCompiler) {
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> generateTph = generateTph(javaTXCompiler);
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> tphMap = javaTXCompilerWildcards.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTph (JavaTXCompiler javaTXCompiler) {
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> generateTph = generateTph(javaTXCompiler);
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> tphMap = javaTXCompilerWildcards.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);

View File

@ -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<? extends TypePlaceholder, ? extends RefType> generateTph (JavaTXCompiler javaTXCompiler) {
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> generateTph = generateTph(javaTXCompiler);
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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<? extends TypePlaceholder, ? extends RefType> tphMap = javaTXCompilerWildcards.getTphMap();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);