forked from JavaTX/JavaCompilerCore
ASTBytecodeTest erstellt
This commit is contained in:
parent
54a11c8779
commit
0c63695f7b
79
test/bytecode/ASTBytecodeTest.java
Normal file
79
test/bytecode/ASTBytecodeTest.java
Normal 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();
|
||||||
|
}
|
@ -1,3 +0,0 @@
|
|||||||
class ExtendsObject extends Object{
|
|
||||||
|
|
||||||
}
|
|
@ -2,33 +2,27 @@ package bytecode.types;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import bytecode.BytecodeTest;
|
import bytecode.ASTBytecodeTest;
|
||||||
import plugindevelopment.TypeInsertTester;
|
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||||
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;
|
|
||||||
|
|
||||||
public class ExtendsObjectTest extends BytecodeTest{
|
|
||||||
@Override
|
public class ExtendsObjectTest extends ASTBytecodeTest{
|
||||||
protected void init() {
|
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
|
||||||
testName = "ExtendsObject";
|
|
||||||
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
|
@Test
|
||||||
@ -48,5 +42,19 @@ public class ExtendsObjectTest extends BytecodeTest{
|
|||||||
fail();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user