From 14821575bd6089120921961259a68da0fa1047ef Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Wed, 26 May 2021 20:57:14 +0200 Subject: [PATCH] Add implementation of source code manipulation --- .../JavaTXCompilerWildcards.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java index 8a4f49a5..5c1d0b05 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/JavaTXCompilerWildcards.java @@ -1,15 +1,26 @@ 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.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 @@ -71,4 +82,78 @@ public class JavaTXCompilerWildcards ConstraintSet constraints = super.getConstraints(); return ConstraintsGenerationUtils.generateAndMergeConstraints(tphMap, constraints); } + + 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); + + } + + private void generateSourceCode (File inputFile, File outputDir, 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))){ + 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) { + 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); + } + } + + } }