Add implementation of source code manipulation
This commit is contained in:
parent
74b9b024ee
commit
14821575bd
@ -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<Pair> 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<ResultSet> typeInference = typeInference();
|
||||
ResultSet resultSet = typeInference.get(0);
|
||||
|
||||
for (Map.Entry<File, SourceFile> 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<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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user