add filter for stuff in ast

This commit is contained in:
Till Schnell 2021-05-26 21:19:39 +02:00
parent fe36b34a80
commit a9281a0db2
2 changed files with 62 additions and 7 deletions

View File

@ -0,0 +1,40 @@
package de.dhbwstuttgart.inferWildcards;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class FindInAstVisitor
{
public static boolean find (SourceFile file, TypePlaceholder tph) {
FindInAstVisitor findInAstVisitor = new FindInAstVisitor(tph);
return findInAstVisitor.find(file);
}
private boolean find;
private TypePlaceholder tph;
public FindInAstVisitor (TypePlaceholder tph) {
this.tph = tph;
find = false;
}
public boolean find (SourceFile file) {
CostumASTWalker walker = new CostumASTWalker();
walker.visit(file);
return find;
}
private class CostumASTWalker extends AbstractASTWalker{
@Override
public void visit (TypePlaceholder typePlaceholder) {
if (typePlaceholder.equals(tph))
find = true;
else
super.visit(typePlaceholder);
}
}
}

View File

@ -83,6 +83,15 @@ public class JavaTXCompilerWildcards
return ConstraintsGenerationUtils.generateAndMergeConstraints(tphMap, constraints);
}
/**
* Generate the source code for manipulated Java Generic Types to a new .java
* file.
*
* @param outputDir {@link File}
* @throws IOException if an i/o exception during file reading and
* writing occurs
* @throws ClassNotFoundException see {@link #typeInference()}
*/
public void generateSourceCode (File outputDir) throws IOException, ClassNotFoundException {
if (!outputDir.exists() && outputDir.isDirectory()) {
boolean mkdirs = outputDir.mkdirs();
@ -94,12 +103,19 @@ public class JavaTXCompilerWildcards
List<ResultSet> typeInference = typeInference();
ResultSet resultSet = typeInference.get(0);
for (Map.Entry<File, SourceFile> e : sourceFiles.entrySet())
generateSourceCode(e.getKey(), outputDir, resultSet);
for (Map.Entry<File, SourceFile> e : sourceFiles.entrySet()) {
List<Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric>> list = tphMap.entrySet().stream().filter(d -> FindInAstVisitor.find(e.getValue(), d.getKey())).sorted( (c1, c2) -> Integer.compare(c1.getValue().getOffset().getStartIndex(),
c2.getValue().getOffset().getStartIndex())).collect(Collectors.toList());
generateSourceCode(e.getKey(), outputDir, list, resultSet);
}
}
private void generateSourceCode (File inputFile, File outputDir, ResultSet results)
private void generateSourceCode (File inputFile, File outputDir,
List<Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric>> tphs,
ResultSet results)
throws IOException {
File outputFile = new File(outputDir, inputFile.getName());
@ -111,12 +127,11 @@ public class JavaTXCompilerWildcards
try(BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))){
List<Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric>> list = tphMap.entrySet().stream().sorted( (c1, c2) -> Integer.compare(c1.getValue().getOffset().getStartIndex(),
c2.getValue().getOffset().getStartIndex())).collect(Collectors.toList());
int readIdx = 0;
for (Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> e : list) {
for (Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> e : tphs) {
Token token = e.getValue().getOffset();
// read the characters before the token