From 26953665c9779b685e1c71f9fa3064bb65ffc4b3 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Tue, 6 Apr 2021 18:26:10 +0200 Subject: [PATCH] search for all generics in ast --- .../ReplaceTypeparamVisitor.java | 40 +++++++++++++++++++ .../TestInferWildcardsJavaTx.java | 14 ++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java 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); } }