diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java new file mode 100644 index 00000000..808f9579 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java @@ -0,0 +1,40 @@ +package de.dhbwstuttgart.inferWildcards; + +import java.util.HashMap; +import java.util.Map; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public class ReplaceTypeparamVisitor + extends AbstractASTWalker +{ + + private final Map tphMap; + + public ReplaceTypeparamVisitor () { + this.tphMap = new HashMap<>(); + } + + @Override + public void visit (RefType refType) { + if (!refType.getParaList().isEmpty()) { + System.out.println("Type: " + refType); + generateTypePlaceholder(refType); + } + super.visit(refType); + } + + private TypePlaceholder generateTypePlaceholder (RefType t) { + TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); + tphMap.put(tph, t); + return tph; + } + + public Map getTphMap () { + return tphMap; + } +} diff --git a/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java b/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java index b5b7818a..8d66011f 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java +++ b/src/test/java/inferWildcards/TestInferWildcardsJavaTx.java @@ -1,6 +1,7 @@ package inferWildcards; import java.io.File; +import java.util.Map; import javax.tools.JavaCompiler; import javax.tools.StandardJavaFileManager; @@ -10,6 +11,8 @@ import org.junit.Before; import org.junit.Test; import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.inferWildcards.ReplaceTypeparamVisitor; +import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -31,8 +34,17 @@ public class TestInferWildcardsJavaTx File[] files1 = { new File(resourcePath + "/TestClassWildcards.java") }; JavaTXCompiler javaTXCompiler = new JavaTXCompiler(files1); + + // Manipulate AST + Map sourceFiles = javaTXCompiler.getSourceFiles(); + ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor(); + sourceFiles.entrySet().forEach(e -> e.getValue().accept(visitor)); + System.out.println(visitor.getTphMap()); + + // Constraints ConstraintSet constraints = javaTXCompiler.getConstraints(); - System.out.println(constraints); + + // System.out.println(constraints); } }