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 132981eb..22b87b20 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java @@ -1,16 +1,27 @@ package de.dhbwstuttgart.inferWildcards; +import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; +import java.nio.file.Files; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; + +import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.result.ResolvedType; +import de.dhbwstuttgart.typeinference.result.ResultSet; /** * Extension of the JavaTX Compiler providing the capabilities to inference @@ -72,4 +83,91 @@ public class JavaTXCompilerWildcards ConstraintSet constraints = super.getConstraints(); 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()) { + 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, + List> tphs, + ResultSet results) + throws IOException { + + File outputFile = new File(outputDir, inputFile.getName()); + + Files.deleteIfExists(outputFile.toPath()); + + if(!outputFile.createNewFile()) + throw new IOException("File could not be created at: " + outputFile.getAbsolutePath()); + + try(BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))){ + + + int readIdx = 0; + + for (Map.Entry e : tphs) { + Token token = e.getValue().getOffset(); + + // read the characters before the token + int startIndex = token.getStartIndex(); + char[] read = new char[startIndex - readIdx]; + + int i = reader.read(read); + if (i != read.length) + throw new IOException("Could not read the assumed number of character, read " + i + + " assumed " + read.length); + + readIdx += read.length; + + // Write to output file + writer.write(read); + + // Write the new type + ResolvedType resolveType = results.resolveType(e.getKey()); + String string = resolveType.resolvedType.toString(); + writer.write(string); + + // Read the replaced type and count the read counter + int length = token.getStopIndex() - token.getStartIndex() + 1; + reader.read(new char[length]); + readIdx += length; + } + + // Read the rest of the file. + while (true) { + char[] tmp = new char[1024]; + int read = reader.read(tmp); + if (read == -1) + break; + + writer.write(tmp, 0, read); + } + } + + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsFields.java b/src/test/java/inferWildcards/TestInferWildcardsFields.java index 1bb2fca7..70989f8b 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsFields.java +++ b/src/test/java/inferWildcards/TestInferWildcardsFields.java @@ -136,4 +136,13 @@ public class TestInferWildcardsFields assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsMap.java b/src/test/java/inferWildcards/TestInferWildcardsMap.java index 27d21198..9de5be75 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsMap.java +++ b/src/test/java/inferWildcards/TestInferWildcardsMap.java @@ -113,4 +113,13 @@ public class TestInferWildcardsMap assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsMapNested.java b/src/test/java/inferWildcards/TestInferWildcardsMapNested.java index d884c828..8649aa8d 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsMapNested.java +++ b/src/test/java/inferWildcards/TestInferWildcardsMapNested.java @@ -114,4 +114,13 @@ public class TestInferWildcardsMapNested assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsNested.java b/src/test/java/inferWildcards/TestInferWildcardsNested.java index 5f936c08..27046b04 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsNested.java +++ b/src/test/java/inferWildcards/TestInferWildcardsNested.java @@ -114,4 +114,12 @@ public class TestInferWildcardsNested assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsParamType.java b/src/test/java/inferWildcards/TestInferWildcardsParamType.java index fc28ec7d..22790a26 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsParamType.java +++ b/src/test/java/inferWildcards/TestInferWildcardsParamType.java @@ -113,4 +113,13 @@ public class TestInferWildcardsParamType assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsSingle.java b/src/test/java/inferWildcards/TestInferWildcardsSingle.java index 11cc9b27..6c68b8fc 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsSingle.java +++ b/src/test/java/inferWildcards/TestInferWildcardsSingle.java @@ -113,4 +113,13 @@ public class TestInferWildcardsSingle assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java b/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java index 74c4dc2e..9468f86f 100644 --- a/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java +++ b/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java @@ -113,4 +113,13 @@ public class TestInferWildcardsSingleLib assertThat("Generation Succeeded", null, CoreMatchers.anything()); } + + @Test + public void testGenrationSourceCode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } } diff --git a/src/test/resources/inferWildcards/.gitignore b/src/test/resources/inferWildcards/.gitignore new file mode 100644 index 00000000..dff83649 --- /dev/null +++ b/src/test/resources/inferWildcards/.gitignore @@ -0,0 +1,2 @@ +/generatedSC/ +/generatedBC/