From a9281a0db24778252760a4e6eaaf947c32f9ced0 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Wed, 26 May 2021 21:19:39 +0200 Subject: [PATCH] add filter for stuff in ast --- .../inferWildcards/FindInAstVisitor.java | 40 +++++++++++++++++++ .../JavaTXCompilerWildcards.java | 29 ++++++++++---- 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/inferWildcards/FindInAstVisitor.java diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/FindInAstVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/FindInAstVisitor.java new file mode 100644 index 00000000..5ed4049a --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/FindInAstVisitor.java @@ -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); + } + } +} diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java index 5c1d0b05..24849bec 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java @@ -83,23 +83,39 @@ 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(); if (!mkdirs) throw new IOException("Could not create output directory at: " + outputDir.getAbsolutePath()); } - + // TODO for all result sets List typeInference = typeInference(); ResultSet resultSet = typeInference.get(0); - for (Map.Entry e : sourceFiles.entrySet()) - generateSourceCode(e.getKey(), outputDir, resultSet); + + + for (Map.Entry e : sourceFiles.entrySet()) { + List> 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> 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> 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 e : list) { + for (Map.Entry e : tphs) { Token token = e.getValue().getOffset(); // read the characters before the token