diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java new file mode 100644 index 00000000..5680e620 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ConstraintsGenerationUtils.java @@ -0,0 +1,55 @@ +package de.dhbwstuttgart.inferWildcards; + +import java.util.HashSet; +import java.util.Set; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +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; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; + +public final class ConstraintsGenerationUtils +{ + + private ConstraintsGenerationUtils () { + throw new AssertionError("No ConstraintsGenerationUtils instance for you"); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static ConstraintSet generateConstraints (JavaClassName name, TypePlaceholder tph) { + ConstraintSet constraintSet = new ConstraintSet<>(); + Set> oderConstraints = new HashSet<>(); + constraintSet.addOderConstraint(oderConstraints); + + Constraint c = new Constraint<>(); + oderConstraints.add(c); + + // single type + RefType refType = new RefType(name, new NullToken()); + ConstraintsGenerationUtils.addToConstraint(c, tph, refType); + + // extends type + ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken()); + ConstraintsGenerationUtils.addToConstraint(c, tph, extendsWildcardType); + + // super type + SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken()); + ConstraintsGenerationUtils.addToConstraint(c, tph, superWildcardType); + + return constraintSet; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private static void addToConstraint (Constraint c, TypePlaceholder tph, RefTypeOrTPHOrWildcardOrGeneric type) { + Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT); + c.add(pair); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java index aefd1c87..37f47ab8 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.inferWildcards; +import java.util.HashMap; import java.util.List; +import java.util.Map; import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.ParameterizedTypeTree; @@ -10,6 +12,9 @@ import com.sun.source.tree.TypeParameterTree; import com.sun.source.tree.VariableTree; import com.sun.source.util.TreeScanner; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; public class InferWildcardsVisitor @@ -18,10 +23,18 @@ public class InferWildcardsVisitor @SuppressWarnings("rawtypes") private final ConstraintSet constraintsSet; + private final Map placeholderMapping; public InferWildcardsVisitor () { super(); this.constraintsSet = new ConstraintSet<>(); + this.placeholderMapping = new HashMap<>(); + } + + private TypePlaceholder generateTypePlaceholder (Tree t) { + TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); + placeholderMapping.put(tph, t); + return tph; } @SuppressWarnings("rawtypes") @@ -31,8 +44,16 @@ public class InferWildcardsVisitor List typeArguments = node.getTypeArguments(); typeArguments.forEach(t -> { - ConstraintSet s = t.accept(new TypeParamVisitor(), null); - constraintsSet.addAll(s); + // generate Type Placeholder + TypePlaceholder tph = generateTypePlaceholder(t); + + // Search for the type parameter + List foundClassNames = t.accept(new TypeParamVisitor(), null); + foundClassNames.forEach(name -> { + ConstraintSet constraints = ConstraintsGenerationUtils.generateConstraints(name, tph); + constraintsSet.addAll(constraints); + + }); }); return super.visitParameterizedType(node, p); @@ -71,4 +92,8 @@ public class InferWildcardsVisitor public ConstraintSet getConstraintSet() { return constraintsSet; } + + public Map getPlaceholderMapping () { + return placeholderMapping; + } } diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java index 53d0eeee..e9f9ec1c 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java @@ -1,71 +1,30 @@ package de.dhbwstuttgart.inferWildcards; -import java.util.HashSet; -import java.util.Set; +import java.util.ArrayList; +import java.util.List; import com.sun.source.tree.IdentifierTree; import com.sun.source.util.TreeScanner; -import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; -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; -import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -@SuppressWarnings("rawtypes") public class TypeParamVisitor - extends TreeScanner + extends TreeScanner, Void> { public TypeParamVisitor () { } - @SuppressWarnings("unchecked") - private static ConstraintSet generateConstraints (IdentifierTree type) { - ConstraintSet constraintSet = new ConstraintSet<>(); - Set> oderConstraints = new HashSet<>(); - constraintSet.addOderConstraint(oderConstraints); - Constraint c = new Constraint<>(); - oderConstraints.add(c); - - // generate Type Placeholder - TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); - - // single type - RefType refType = new RefType(new JavaClassName(type.getName().toString()), new NullToken()); - TypeParamVisitor.addToConstraint(c, tph, refType); - - // extends type - ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken()); - TypeParamVisitor.addToConstraint(c, tph, extendsWildcardType); - - // super type - SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken()); - TypeParamVisitor.addToConstraint(c, tph, superWildcardType); - - return constraintSet; - } - - @SuppressWarnings({ "unchecked" }) - private static void addToConstraint (Constraint c, TypePlaceholder tph, RefTypeOrTPHOrWildcardOrGeneric type) { - Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT); - c.add(pair); + @Override + public List visitIdentifier (IdentifierTree node, Void p) { + ArrayList list = new ArrayList<>(); + list.add(new JavaClassName(node.getName().toString())); + return list; } @Override - public ConstraintSet visitIdentifier (IdentifierTree node, Void p) { - return TypeParamVisitor.generateConstraints(node); - } - - @Override - public ConstraintSet reduce (ConstraintSet r1, ConstraintSet r2) { + public List reduce (List r1, List r2) { r1.addAll(r2); return r1; } diff --git a/src/test/java/inferWildcards/TestInferWildcards.java b/src/test/java/inferWildcards/TestInferWildcards.java index cce67c9d..06c780a1 100644 --- a/src/test/java/inferWildcards/TestInferWildcards.java +++ b/src/test/java/inferWildcards/TestInferWildcards.java @@ -3,6 +3,7 @@ package inferWildcards; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.Map; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; @@ -13,9 +14,11 @@ import org.junit.Before; import org.junit.Test; import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.Tree; import com.sun.source.util.JavacTask; import de.dhbwstuttgart.inferWildcards.InferWildcardsVisitor; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; public class TestInferWildcards @@ -48,9 +51,15 @@ public class TestInferWildcards InferWildcardsVisitor inferWildcardsVisitor = new InferWildcardsVisitor(); t.accept(inferWildcardsVisitor, null); + + // Generated Constraints ConstraintSet constraintSet = inferWildcardsVisitor.getConstraintSet(); System.out.println("Constraints: " + constraintSet); - } + // Generated TPH + Map placeholderMapping = inferWildcardsVisitor.getPlaceholderMapping(); + System.out.println("Placeholder: " + placeholderMapping.toString()); + + } } }