forked from JavaTX/JavaCompilerCore
merge
This commit is contained in:
commit
87fb9e5a33
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Pair> 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<ResultSet> typeInference = typeInference();
|
||||
ResultSet resultSet = typeInference.get(0);
|
||||
|
||||
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,
|
||||
List<Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric>> 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<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
2
src/test/resources/inferWildcards/.gitignore
vendored
Normal file
2
src/test/resources/inferWildcards/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/generatedSC/
|
||||
/generatedBC/
|
Loading…
Reference in New Issue
Block a user