forked from JavaTX/JavaCompilerCore
59 lines
1.9 KiB
Java
59 lines
1.9 KiB
Java
package bytecode;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
import org.apache.commons.bcel6.classfile.JavaClass;
|
|
|
|
import com.google.common.io.Files;
|
|
|
|
import junit.framework.TestCase;
|
|
import de.dhbwstuttgart.core.MyCompiler;
|
|
import de.dhbwstuttgart.core.MyCompilerAPI;
|
|
import de.dhbwstuttgart.logger.Logger;
|
|
import de.dhbwstuttgart.logger.LoggerConfiguration;
|
|
import de.dhbwstuttgart.logger.Section;
|
|
import de.dhbwstuttgart.logger.Timewatch;
|
|
import de.dhbwstuttgart.parser.JavaParser.yyException;
|
|
import de.dhbwstuttgart.typeinference.ByteCodeResult;
|
|
import de.dhbwstuttgart.typeinference.Menge;
|
|
|
|
public class SingleClassTester {
|
|
|
|
public static void compileToBytecode(String inputFile, String outputDirectory){
|
|
LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out);
|
|
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
|
|
try {
|
|
compiler.parse(new File(inputFile));
|
|
Menge<Menge<ByteCodeResult>> bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement());
|
|
//System.out.println(bytecode);
|
|
|
|
for(ByteCodeResult result: bytecode.firstElement()){
|
|
JavaClass javaClass = result.getByteCode().getJavaClass();
|
|
|
|
Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN);
|
|
|
|
javaClass.dump(new File(outputDirectory+javaClass.getClassName()+".class"));
|
|
}
|
|
|
|
} catch (IOException | yyException e) {
|
|
Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN);
|
|
e.printStackTrace();
|
|
TestCase.fail();
|
|
}finally{
|
|
writeLog(outputDirectory+".log");
|
|
}
|
|
}
|
|
|
|
private static void writeLog(String toFile){
|
|
String log = Logger.getWholeLog()+"\n";
|
|
log+=Timewatch.getTimewatch().dumpTimeData();
|
|
try {
|
|
Files.write(log.getBytes(),new File(toFile));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
TestCase.fail();
|
|
}
|
|
}
|
|
}
|