search for all generics in ast

This commit is contained in:
Till Schnell 2021-04-06 18:26:10 +02:00
parent 465f82e967
commit 26953665c9
2 changed files with 53 additions and 1 deletions

View File

@ -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<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> 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<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> getTphMap () {
return tphMap;
}
}

View File

@ -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<File, SourceFile> sourceFiles = javaTXCompiler.getSourceFiles();
ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor();
sourceFiles.entrySet().forEach(e -> e.getValue().accept(visitor));
System.out.println(visitor.getTphMap());
// Constraints
ConstraintSet<Pair> constraints = javaTXCompiler.getConstraints();
System.out.println(constraints);
// System.out.println(constraints);
}
}