ASTBytecodeTest erstellt

This commit is contained in:
Enrico Schrödter 2015-11-10 17:54:27 +01:00
parent 54a11c8779
commit 0c63695f7b
3 changed files with 110 additions and 26 deletions

View File

@ -0,0 +1,79 @@
package bytecode;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import org.apache.commons.bcel6.classfile.JavaClass;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
public abstract class ASTBytecodeTest {
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
protected String testName = "";
protected SourceFile sourceFile = new SourceFile();
protected TypeinferenceResults results = new TypeinferenceResults();
protected Class getClassToTest(){
Class classToTest = null;
try {
ClassLoader classLoader = getClassLoader();
classToTest = classLoader.loadClass(testName);
} catch (Exception e) {
throw new RuntimeException(e);
}
return classToTest;
}
protected ClassLoader getClassLoader() throws Exception{
File file = new File(rootDirectory);
URL url = file.toURL();
URL[] urls = new URL[]{url};
return new URLClassLoader(urls);
}
public ASTBytecodeTest(){
init();
LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out);
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
Menge<SourceFile> sourceFiles = new Menge<>();
sourceFiles.add(sourceFile);
Menge<ByteCodeResult> bytecode = compiler.generateBytecode(sourceFiles, results);
ByteCodeResult result = bytecode.firstElement();
JavaClass javaClass = result.getByteCode().getJavaClass();
javaClass.dump(new File(rootDirectory+javaClass.getClassName()+".class"));
System.out.println(new File(rootDirectory+javaClass.getClassName()+".class").getAbsolutePath());
for(ClassGenerator cg: result.getByteCode().getExtraClasses().values()){
JavaClass jc = cg.getJavaClass();
jc.dump(new File(rootDirectory+jc.getClassName()+".class"));
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
protected abstract void init();
}

View File

@ -1,3 +0,0 @@
class ExtendsObject extends Object{
}

View File

@ -2,33 +2,27 @@ package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Vector;
import junit.framework.TestCase;
import org.junit.Test;
import bytecode.BytecodeTest;
import plugindevelopment.TypeInsertTester;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
import bytecode.ASTBytecodeTest;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
public class ExtendsObjectTest extends BytecodeTest{
@Override
protected void init() {
testName = "ExtendsObject";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
public class ExtendsObjectTest extends ASTBytecodeTest{
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
protected String testName = "ExtendsObjectTest2";
protected void init(){
/*
class ExtendsObject extends Object{
}
*/
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass("ExtendsObjectTest2", null, null, null, sourceFile);
sourceFile.addElement(classToTest);
}
@Test
@ -48,5 +42,19 @@ public class ExtendsObjectTest extends BytecodeTest{
fail();
}
}
@Test
public void testSupertype(){
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
assertEquals("java.lang.Object", cls.getSuperclass().getName());
}catch(Exception e){
e.printStackTrace();
fail();
}
}
}