2015-08-27 11:36:14 +00:00
|
|
|
package bytecode;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2015-10-22 18:40:33 +00:00
|
|
|
import java.util.Map;
|
2015-08-27 11:36:14 +00:00
|
|
|
|
2015-10-16 08:39:34 +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;
|
2015-10-22 18:40:33 +00:00
|
|
|
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;
|
2015-10-22 18:40:33 +00:00
|
|
|
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
2015-08-27 11:36:14 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.ByteCodeResult;
|
|
|
|
import de.dhbwstuttgart.typeinference.Menge;
|
2015-11-06 15:42:22 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
|
2015-08-27 11:36:14 +00:00
|
|
|
|
|
|
|
public class SingleClassTester {
|
2015-10-15 14:28:10 +00:00
|
|
|
|
2015-10-16 08:39:34 +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 {
|
2015-10-22 18:40:33 +00:00
|
|
|
SourceFile sf = compiler.parse(new File(inputFile));
|
|
|
|
|
|
|
|
Menge<SourceFile> sourceFiles = new Menge<>();
|
|
|
|
sourceFiles.add(sf);
|
|
|
|
|
2015-11-06 15:42:22 +00:00
|
|
|
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);
|
2015-10-16 08:39:34 +00:00
|
|
|
|
2015-10-22 18:40:33 +00:00
|
|
|
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"));
|
2015-10-16 08:39:34 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 18:40:33 +00:00
|
|
|
Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN);
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-27 11:36:14 +00:00
|
|
|
} catch (IOException | yyException e) {
|
2015-10-15 17:12:38 +00:00
|
|
|
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
|
|
|
}
|