JavaCompilerCore/test/bytecode/SingleClassTester.java

75 lines
2.4 KiB
Java
Raw Normal View History

2015-08-27 11:36:14 +00:00
package bytecode;
import java.io.File;
import java.io.IOException;
import java.util.Map;
2015-08-27 11:36:14 +00:00
import org.apache.commons.bcel6.classfile.JavaClass;
2015-10-15 14:28:10 +00:00
import com.google.common.io.Files;
2015-08-27 11:36:14 +00:00
import junit.framework.TestCase;
import de.dhbwstuttgart.bytecode.ClassGenerator;
2015-08-27 11:36:14 +00:00
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
2015-10-15 14:28:10 +00:00
import de.dhbwstuttgart.logger.Logger;
2015-08-27 11:36:14 +00:00
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
2015-10-15 14:28:10 +00:00
import de.dhbwstuttgart.logger.Timewatch;
2015-08-27 11:36:14 +00:00
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.syntaxtree.SourceFile;
2015-08-27 11:36:14 +00:00
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
2015-08-27 11:36:14 +00:00
public class SingleClassTester {
2015-10-15 14:28:10 +00:00
public static void compileToBytecode(String inputFile, String outputDirectory){
2015-08-27 11:36:14 +00:00
LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out);
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
SourceFile sf = compiler.parse(new File(inputFile));
Menge<SourceFile> sourceFiles = new Menge<>();
sourceFiles.add(sf);
TypeinferenceResults results = new TypeinferenceResults(compiler.typeReconstruction(sourceFiles));
Menge<ByteCodeResult> bytecode = compiler.generateBytecode(sourceFiles, results);
2015-10-15 15:46:13 +00:00
//System.out.println(bytecode);
ByteCodeResult result = bytecode.firstElement();
JavaClass javaClass = result.getByteCode().getJavaClass();
javaClass.dump(new File(outputDirectory+javaClass.getClassName()+".class"));
for(ClassGenerator cg: result.getByteCode().getExtraClasses().values()){
JavaClass jc = cg.getJavaClass();
jc.dump(new File(outputDirectory+jc.getClassName()+".class"));
}
Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN);
2015-08-27 11:36:14 +00:00
} catch (IOException | yyException e) {
Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN);
2015-08-27 11:36:14 +00:00
e.printStackTrace();
TestCase.fail();
2015-10-15 14:28:10 +00:00
}finally{
2015-11-09 08:38:49 +00:00
writeLog(outputDirectory+"bytecode.log");
2015-08-27 11:36:14 +00:00
}
}
2015-10-15 14:28:10 +00:00
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();
}
}
2015-08-27 11:36:14 +00:00
}