JavaPatternMatching/test/bytecode/JavaTXCompilerTest.java

91 lines
3.1 KiB
Java
Raw Normal View History

2017-08-30 16:08:05 +00:00
package bytecode;
import de.dhbwstuttgart.bytecode.BytecodeGen;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.SourceFile;
2017-11-29 13:45:15 +00:00
import de.dhbwstuttgart.typeinference.result.ResultPair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
2017-08-30 16:08:05 +00:00
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.*;
2017-10-05 17:21:30 +00:00
public class JavaTXCompilerTest {
2017-08-30 16:08:05 +00:00
private static final String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
2017-08-30 16:08:05 +00:00
private static final List<File> filesToTest = new ArrayList<>();
2018-01-10 10:36:29 +00:00
protected String fileName = "";
2017-08-30 16:08:05 +00:00
@Test
public void test() throws IOException, java.lang.ClassNotFoundException {
2017-10-06 11:31:36 +00:00
System.out.println(rootDirectory);
filesToTest.add(new File(rootDirectory+fileName+".jav"));
System.out.println(rootDirectory+fileName+".jav");
2017-10-05 17:21:30 +00:00
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
2017-10-06 11:31:36 +00:00
System.out.println("test");
2017-08-30 16:08:05 +00:00
for(File f : filesToTest){
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
2017-11-29 13:45:15 +00:00
List<ResultSet> typeinferenceResult = compiler.typeInference();
HashMap<String,byte[]> bytecode = this.getBytecode(compiler.sourceFiles.get(f), typeinferenceResult.get(0));
for(ResultPair ep : typeinferenceResult.get(0).results) {
System.out.println(ep.getLeft() + " ->" + ep.getRight());
}
String name = "";
int pos = f.getName().lastIndexOf(".");
if(pos != -1) {
name = f.getName().substring(0, pos);
}
2018-01-10 10:36:29 +00:00
this.writeClassFile(bytecode);
2017-08-30 16:08:05 +00:00
}
}
2017-11-29 13:45:15 +00:00
public HashMap<String,byte[]> getBytecode(SourceFile sf, ResultSet resultSet) {
2017-08-30 16:08:05 +00:00
HashMap<String,byte[]> classFiles = new HashMap<>();
2017-11-29 13:45:15 +00:00
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,resultSet);
2017-08-30 16:08:05 +00:00
bytecodeGen.visit(sf);
return bytecodeGen.getClassFiles();
}
2018-01-10 10:36:29 +00:00
public void writeClassFile(HashMap<String,byte[]> classFiles) {
2017-08-30 16:08:05 +00:00
FileOutputStream output;
2018-01-10 10:36:29 +00:00
for(String name : classFiles.keySet()) {
byte[] bytecode = classFiles.get(name);
try {
System.out.println("generating"+name+ ".class file");
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class"));
output.write(bytecode);
output.close();
System.out.println(name+".class file generated");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2017-08-30 16:08:05 +00:00
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}