diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 0332ce13..8fe32c6b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -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 supertypeGenPara) { + public Class(String name, Type superClass, Modifiers modifiers, Menge supertypeGenPara) { this(name,superClass,modifiers,0); if(supertypeGenPara == null)return; Menge 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)); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 567a3607..b55ea238 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -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 diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index b712296b..12c1a654 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -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("", block, superClass); + Method method = ASTFactory.createMethod("", 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 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 supertypeGenPara Class generatedClass = new Class(className, type, modifiers, supertypeGenPara); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 26b1806e..12777654 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -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()); } diff --git a/test/bytecode/ASTBytecodeTest.java b/test/bytecode/ASTBytecodeTest.java index 1558fa77..cd6d268e 100644 --- a/test/bytecode/ASTBytecodeTest.java +++ b/test/bytecode/ASTBytecodeTest.java @@ -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 sourceFiles = new Menge<>(); - sourceFiles.add(sourceFile); + sourceFiles.add(getSourceFile()); Menge 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 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!"; + } + + + + } diff --git a/test/bytecode/types/ExtendsObjectTest.java b/test/bytecode/types/ExtendsObjectTest.java index c5614bff..1bd04bb7 100644 --- a/test/bytecode/types/ExtendsObjectTest.java +++ b/test/bytecode/types/ExtendsObjectTest.java @@ -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"; + } } diff --git a/test/bytecode/types/ExtendsVectorStringTest.java b/test/bytecode/types/ExtendsVectorStringTest.java index cd248e97..d3ed9fb3 100644 --- a/test/bytecode/types/ExtendsVectorStringTest.java +++ b/test/bytecode/types/ExtendsVectorStringTest.java @@ -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 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"; + } } diff --git a/test/bytecode/types/ExtendsVectorTest.java b/test/bytecode/types/ExtendsVectorTest.java index 52cc9914..3856dbb3 100644 --- a/test/bytecode/types/ExtendsVectorTest.java +++ b/test/bytecode/types/ExtendsVectorTest.java @@ -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"; + } } diff --git a/test/bytecode/types/OverloadingAddition.jav b/test/bytecode/types/OverloadingAddition.jav index c7196c15..0b0607ac 100644 --- a/test/bytecode/types/OverloadingAddition.jav +++ b/test/bytecode/types/OverloadingAddition.jav @@ -1,11 +1,5 @@ class OverloadingAddition{ - methode(a){ return a+a; } - - methode(){ - methode(1); - methode(1.1); - } } \ No newline at end of file diff --git a/test/bytecode/types/OverloadingAdditionTest2.java b/test/bytecode/types/OverloadingAdditionTest2.java new file mode 100644 index 00000000..2669b7fa --- /dev/null +++ b/test/bytecode/types/OverloadingAdditionTest2.java @@ -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 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 results = new Menge<>(); + + for(String typeString: types){ + ParameterList parameterList = new ParameterList(); + + + Type type = new RefType(typeString, parameterList, 0); + + Menge 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); + } + */ +}