BytecodeTests erweitert

ASTFacotry überarbeitet
This commit is contained in:
Enrico Schrödter 2015-11-12 14:50:27 +01:00
parent dc0cf0cd34
commit 61891177a1
10 changed files with 336 additions and 64 deletions

View File

@ -240,14 +240,23 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
* @param modifiers
* @param supertypeGenPara - Eine Liste von Namen, welche die Generischen Parameter der Klasse darstellen.
*/
public Class(String name, Type superClass, Modifiers modifiers,
Menge<String> supertypeGenPara) {
public Class(String name, Type superClass, Modifiers modifiers, Menge supertypeGenPara) {
this(name,superClass,modifiers,0);
if(supertypeGenPara == null)return;
Menge<GenericTypeVar> gtvs = new Menge<>();
for(String gname : supertypeGenPara){
GenericTypeVar newGTV=new GenericTypeVar(gname,this,0);
gtvs.add(newGTV);
for(Object gname : supertypeGenPara){
if(gname instanceof String){
GenericTypeVar newGTV=new GenericTypeVar((String)gname,this,0);
gtvs.add(newGTV);
}else if(gname instanceof GenericTypeVar){
gtvs.add((GenericTypeVar) gname);
}else{
GenericTypeVar newGTV=new GenericTypeVar(gname.toString(),this,0);
gtvs.add(newGTV);
throw new RuntimeException(gname.toString());
}
}
this.setGenericParameter(new GenericDeclarationList(gtvs,0));
}

View File

@ -423,8 +423,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
// ino.end
// ino.method.toString.23605.body
{
return this.getType() + " " + this.get_Name()
+ ((block != null) ? block.toString() : "");
return this.getType() + " " + this.get_Name() + ((block != null) ? block.toString() : "");
}
// ino.end

View File

@ -4,6 +4,7 @@ import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.misc.DeclId;
@ -18,20 +19,23 @@ import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
public class ASTFactory {
public static Method createMethod(String name, Block block, Class parent) {
block.parserPostProcessing(parent);
public static Method createMethod(String name, ParameterList paralist, Block block, Class parent) {
Method method = new Method(0);
DeclId DImethod = new DeclId();
DImethod.set_Name(name);
method.set_DeclId(DImethod);
method.set_Block(block);
method.setParameterList(paralist);
block.parserPostProcessing(method);
method.parserPostProcessing(parent);
return method;
}
public static Method createEmptyMethod(String withSignature, Class parent) {
return ASTFactory.createMethod(withSignature, new Block(), parent);
return ASTFactory.createMethod(withSignature, new ParameterList(), new Block(), parent);
}
public static Constructor createEmptyConstructor(Class parent){
@ -39,19 +43,19 @@ public class ASTFactory {
block.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0));
block.statements.add(new SuperCall(block));
return ASTFactory.createConstructor(parent, block);
return ASTFactory.createConstructor(parent, new ParameterList(), block);
}
public static Constructor createConstructor(Class superClass, Block block){
public static Constructor createConstructor(Class superClass, ParameterList paralist, Block block){
block.parserPostProcessing(superClass);
Method method = ASTFactory.createMethod("<init>", block, superClass);
Method method = ASTFactory.createMethod("<init>", paralist, block, superClass);
method.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0));
return new Constructor(method, superClass);
}
public static Class createClass(String className, Type type, Modifiers modifiers, Menge<String> supertypeGenPara, SourceFile parent) {
public static Class createClass(String className, Type type, Modifiers modifiers, Menge supertypeGenPara, SourceFile parent) {
// TODO bytecode createClass
//String name, RefType superClass, Modifiers modifiers, Menge<String> supertypeGenPara
Class generatedClass = new Class(className, type, modifiers, supertypeGenPara);

View File

@ -837,7 +837,8 @@ public class RefType extends ObjectType implements IMatchable
//TODO: bytecode woher bekommt ich die parent klasse
String combinedType = getCombinedType(cg);
if(!combinedType.equals(getName().toString())){
Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getGenericClassType(), null, null, new SourceFile());
getSuperWildcardTypes();
Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getGenericClassType(), null, get_ParaList(), new SourceFile());
cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResults()).getByteCode());
}

View File

@ -14,22 +14,17 @@ import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
public abstract class ASTBytecodeTest {
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
protected static String testName = "No Testname defined!";
protected static SourceFile sourceFile = new SourceFile();
protected static TypeinferenceResults results = new TypeinferenceResults();
public abstract class ASTBytecodeTest {
protected Class getClassToTest(){
Class classToTest = null;
try {
ClassLoader classLoader = getClassLoader();
classToTest = classLoader.loadClass(testName);
classToTest = classLoader.loadClass(getTestName());
} catch (Exception e) {
@ -40,7 +35,7 @@ public abstract class ASTBytecodeTest {
}
protected ClassLoader getClassLoader() throws Exception{
File file = new File(rootDirectory);
File file = new File(getRootDirectory());
URL url = file.toURL();
URL[] urls = new URL[]{url};
@ -48,29 +43,53 @@ public abstract class ASTBytecodeTest {
}
public ASTBytecodeTest(){
System.out.println("ASTBytecodeTest");
LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out);
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
TypeinferenceResults results = getResults();
Menge<SourceFile> sourceFiles = new Menge<>();
sourceFiles.add(sourceFile);
sourceFiles.add(getSourceFile());
Menge<ByteCodeResult> bytecode = compiler.generateBytecode(sourceFiles, results);
ByteCodeResult result = bytecode.firstElement();
String rootDirectory = getRootDirectory();
JavaClass javaClass = result.getByteCode().getJavaClass();
javaClass.dump(new File(rootDirectory+javaClass.getClassName()+".class"));
System.out.println(new File(rootDirectory+javaClass.getClassName()+".class").getAbsolutePath());
javaClass.dump(new File(rootDirectory+javaClass.getClassName()+".class"));
for(ClassGenerator cg: result.getByteCode().getExtraClasses().values()){
JavaClass jc = cg.getJavaClass();
jc.dump(new File(rootDirectory+jc.getClassName()+".class"));
}
}catch(Exception e){
System.out.print(e.getMessage());
throw new RuntimeException(e);
}
}
public SourceFile getSourceFile() {
return new SourceFile();
}
public TypeinferenceResults getResults() {
Menge<TypeinferenceResultSet> results = new Menge<>();
results.add(new TypeinferenceResultSet(getSourceFile().KlassenVektor.get(0), new Menge(), new ResultSet()));
return new TypeinferenceResults(results);
}
public String getRootDirectory() {
return System.getProperty("user.dir")+"/test/bytecode/";
}
public String getTestName() {
return "No Testname defined!";
}
}

View File

@ -8,23 +8,21 @@ import org.junit.BeforeClass;
import org.junit.Test;
import bytecode.ASTBytecodeTest;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
public class ExtendsObjectTest extends ASTBytecodeTest{
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
protected static String testName = "ExtendsObjectTest";
@BeforeClass
public static void init(){
public class ExtendsObjectTest extends ASTBytecodeTest{
public SourceFile getSourceFile(){
/*
class ExtendsObject extends Object{
}
*/
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(testName, null, null, null, sourceFile);
SourceFile sourceFile = new SourceFile();
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(getTestName(), null, null, null, sourceFile);
sourceFile.addElement(classToTest);
return sourceFile;
}
@Test
@ -32,7 +30,7 @@ public class ExtendsObjectTest extends ASTBytecodeTest{
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Class cls = classLoader.loadClass(getTestName());
Object obj = cls.newInstance();
@ -50,7 +48,7 @@ public class ExtendsObjectTest extends ASTBytecodeTest{
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Class cls = classLoader.loadClass(getTestName());
assertEquals("java.lang.Object", cls.getSuperclass().getName());
}catch(Exception e){
@ -58,5 +56,17 @@ public class ExtendsObjectTest extends ASTBytecodeTest{
fail();
}
}
@Override
public String getRootDirectory() {
return super.getRootDirectory()+"types/";
}
@Override
public String getTestName() {
return "ExtendsObjectTest";
}
}

View File

@ -9,6 +9,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import bytecode.ASTBytecodeTest;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
@ -16,12 +17,8 @@ import de.dhbwstuttgart.typeinference.Menge;
public class ExtendsVectorStringTest extends ASTBytecodeTest{
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
protected static String testName = "ExtendsVectorString";
@BeforeClass
public static void init(){
public SourceFile getSourceFile(){
/*
import java.util.Vector;
@ -29,11 +26,15 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{
}
*/
SourceFile sourceFile = new SourceFile();
Menge<Type> parameter = new Menge<>();
parameter.add(new RefType("java.lang.String", sourceFile, 0));
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(testName, new RefType("java.util.Vector", parameter, sourceFile, 0), null, null, sourceFile);
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(getTestName(), new RefType("java.util.Vector", parameter, sourceFile, 0), null, null, sourceFile);
sourceFile.addElement(classToTest);
return sourceFile;
}
@Test
@ -41,7 +42,7 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Class cls = classLoader.loadClass(getTestName());
assertEquals("java.util.Vector", cls.getSuperclass().getName());
}catch(Exception e){
@ -55,7 +56,7 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Class cls = classLoader.loadClass(getTestName());
Object obj = cls.newInstance();
@ -70,5 +71,17 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{
throw new RuntimeException(e);
}
}
@Override
public String getRootDirectory() {
return super.getRootDirectory()+"types/";
}
@Override
public String getTestName() {
return "ExtendsVectorString";
}
}

View File

@ -8,17 +8,13 @@ import org.junit.BeforeClass;
import org.junit.Test;
import bytecode.ASTBytecodeTest;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.RefType;
public class ExtendsVectorTest extends ASTBytecodeTest{
public static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
protected static String testName = "ExtendsVectorTest";
@BeforeClass
public static void init(){
public class ExtendsVectorTest extends ASTBytecodeTest{
public SourceFile getSourceFile(){
/*
import java.util.Vector;
@ -26,8 +22,12 @@ public class ExtendsVectorTest extends ASTBytecodeTest{
}
*/
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(testName, new RefType("java.util.Vector", sourceFile, 0), null, null, sourceFile);
SourceFile sourceFile = new SourceFile();
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(getTestName(), new RefType("java.util.Vector", sourceFile, 0), null, null, sourceFile);
sourceFile.addElement(classToTest);
return sourceFile;
}
@Test
@ -35,7 +35,7 @@ public class ExtendsVectorTest extends ASTBytecodeTest{
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Class cls = classLoader.loadClass(getTestName());
assertEquals("java.util.Vector", cls.getSuperclass().getName());
}catch(Exception e){
@ -43,5 +43,17 @@ public class ExtendsVectorTest extends ASTBytecodeTest{
fail();
}
}
@Override
public String getRootDirectory() {
return super.getRootDirectory()+"types/";
}
@Override
public String getTestName() {
return "ExtendsVector";
}
}

View File

@ -1,11 +1,5 @@
class OverloadingAddition{
methode(a){
return a+a;
}
methode(){
methode(1);
methode(1.1);
}
}

View File

@ -0,0 +1,211 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.BeforeClass;
import org.junit.Test;
import org.omg.CORBA.TypeCodeHolder;
import bytecode.ASTBytecodeTest;
import bytecode.SourceFileBytecodeTest;
import de.dhbwstuttgart.bytecode.TypePlaceholderType;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.misc.DeclId;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Expr;
import de.dhbwstuttgart.syntaxtree.statement.IntLiteral;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.TypeinferenceResults;
public class OverloadingAdditionTest2 extends ASTBytecodeTest{
public SourceFile getSourceFile(){
/*
class OverloadingAddition{
methode(a){
return a+a;
}
}
*/
SourceFile sourceFile = new SourceFile();
de.dhbwstuttgart.syntaxtree.Class classToTest = ASTFactory.createClass(getTestName(), null, null, null, sourceFile);
ParameterList parameterList = new ParameterList();
System.out.println("Type");
Type type = new RefType("java.lang.Integer", parameterList, 0);
//Type type = TypePlaceholder.getInstance("OverloadingAdditionTestTypePlaceholder_A");
System.out.println("type: "+type.toString());
DeclId declId = new DeclId("a");
declId.set_Paratyp(new Menge());
FormalParameter formalParameter = new FormalParameter(declId);
formalParameter.setType(type);
parameterList.set_AddParameter(formalParameter);
IntLiteral additionExpression = new IntLiteral();
additionExpression.set_Int(2);
Return returnStatment = new Return(0, 0);
returnStatment.set_ReturnExpr(additionExpression);
returnStatment.setReturnType(type);
Menge<Statement> statements = new Menge<>();
statements.add(returnStatment);
Block block = new Block();
block.set_Statement_Menge(statements);
block.setType(type);
de.dhbwstuttgart.syntaxtree.Method method = ASTFactory.createMethod("method", parameterList, block, classToTest);
method.setType(type);
classToTest.addField(method);
sourceFile.addElement(classToTest);
return sourceFile;
}
@Override
public String getRootDirectory() {
return super.getRootDirectory()+"types/";
}
@Override
public String getTestName() {
return "OverloadingAddition";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(getTestName());
Object obj = cls.newInstance();
assertTrue(true);
}
@Test
public void testMethodWithInteger(){
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(getTestName());
Object obj = cls.newInstance();
Integer param = new Integer(1);
Class[] params = new Class[1];
params[0] = param.getClass();
Method method = cls.getDeclaredMethod("method", params);
Object result = method.invoke(obj, param);
assertEquals(2, (Integer) result);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testMethodWithDouble(){
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(getTestName());
Object obj = cls.newInstance();
Double param = new Double(1.5);
Class[] params = new Class[1];
params[0] = param.getClass();
Method method = cls.getDeclaredMethod("method", params);
Object result = method.invoke(obj, param);
assertEquals(3.0, (Double) result);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testMethodWithString(){
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(getTestName());
Object obj = cls.newInstance();
String param = new String("abc");
Class[] params = new Class[1];
params[0] = param.getClass();
Method method = cls.getDeclaredMethod("method", params);
Object result = method.invoke(obj, param);
assertEquals("abcabc", (String) result);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/*
public TypeinferenceResults getResults() {
String[] types = {
"java.lang.Integer",
"java.lang.Double",
"java.lang.String"
};
Menge<TypeinferenceResultSet> results = new Menge<>();
for(String typeString: types){
ParameterList parameterList = new ParameterList();
Type type = new RefType(typeString, parameterList, 0);
Menge<Pair> pairs = new Menge<>();
pairs.add(new Pair(TypePlaceholder.fresh("OverloadingAdditionTestTypePlaceholder_A", getSourceFile().KlassenVektor.get(0)), type));
results.add(new TypeinferenceResultSet(getSourceFile().KlassenVektor.get(0), pairs, new ResultSet(pairs)));
}
return new TypeinferenceResults(results);
}
*/
}