From d6fb9ff2c7b5bd6a947e4a8e6462f674dd7ada13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 15 Oct 2015 17:46:13 +0200 Subject: [PATCH 01/13] ConditionTest angelegt ForTest gefixed --- src/de/dhbwstuttgart/syntaxtree/Method.java | 3 +- .../syntaxtree/statement/Binary.java | 23 +++++++- .../syntaxtree/statement/ForStmt.java | 54 ++++++++++++++++--- .../syntaxtree/statement/IfStmt.java | 2 - .../syntaxtree/statement/LocalOrFieldVar.java | 9 +++- test/bytecode/Condition.jav | 7 +++ test/bytecode/ConditionTest.java | 34 ++++++++++++ test/bytecode/ForTest.jav | 2 +- test/bytecode/IfStatement.jav | 2 +- test/bytecode/SingleClassTester.java | 2 +- 10 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 test/bytecode/Condition.jav create mode 100644 test/bytecode/ConditionTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index ba8cba98..3d4a60ce 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -541,8 +541,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable ret.add(this.block.TYPEStmt(localAss)); // eine Verknüpfung mit der Type Assumption aus dem Assumption Set // und dem ermittelten Typ der Methode: - ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), - this.returntype.TYPE(localAss, this))); + ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), this.returntype.TYPE(localAss, this))); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java index 60b2657b..73467924 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java @@ -6,8 +6,13 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; +import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.GOTO; +import org.apache.commons.bcel6.generic.IF_ICMPNE; +import org.apache.commons.bcel6.generic.InstructionConstants; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NOP; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -228,8 +233,22 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { if(this.getReturnType().getName().equals(new JavaClassName("String"))){ throw new TypeinferenceException("Zeichenketten zusammenfügen ist noch nicht unterstützt",this); } - //TODO: Je nachdem welches der Rückgabetyp ist, müssen die linke und rechte Seite unboxt und addiert werden. - return null; + + linkeSeite.append(rechteSeite); + + //TODO: bytecode (Bis jetzt wird jeder Condition als EQUALS behandelt) + //TODO: bytecode autoboxing + + BranchInstruction if_icmpneInstruction = new IF_ICMPNE(null); + linkeSeite.append(if_icmpneInstruction); + linkeSeite.append(InstructionConstants.ICONST_1); + + BranchInstruction gotoInstruction = new GOTO(null); + + linkeSeite.append(gotoInstruction); + if_icmpneInstruction.setTarget(linkeSeite.append(InstructionConstants.ICONST_0)); + gotoInstruction.setTarget(linkeSeite.append(new NOP())); + return linkeSeite; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 6f5ab521..dc8526ae 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -4,8 +4,13 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.GOTO; +import org.apache.commons.bcel6.generic.IF_ICMPGE; +import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NOP; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -22,10 +27,13 @@ import de.dhbwstuttgart.syntaxtree.operator.RelOp; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.SingleConstraint; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.Unify; @@ -91,8 +99,13 @@ public class ForStmt extends Statement @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { - // TODO Auto-generated method stub - return null; + //TODO: TYPEStmt + ConstraintsSet ret = new ConstraintsSet(); + this.setType(TypePlaceholder.fresh(this)); + + ret.add(body_Loop_block.TYPEStmt(assumptions)); + + return ret; } public int getTypeLineNumber() { @@ -100,9 +113,9 @@ public class ForStmt extends Statement } @Override -public JavaCodeResult printJavaCode(ResultSet resultSet) { - // TODO Auto-generated method stub - throw new NotImplementedException(); + public JavaCodeResult printJavaCode(ResultSet resultSet) { + // TODO printJavaCode + return new JavaCodeResult(); } @Override @@ -121,7 +134,34 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { @Override public InstructionList genByteCode(ClassGenerator _cg) { - // TODO Bytecode - throw new NotImplementedException(); + /* + 0: iconst_0 + 1: istore_1 + 2: iload_1 + 3: bipush 10 + 5: if_icmpge 14 + 8: iinc 1, 1 + 11: goto 2 + 14: return + */ + + InstructionList il = new InstructionList(); + + il.append(head_Initializer.genByteCode(_cg)); + + InstructionHandle ih = il.append(head_Condition.genByteCode(_cg)); + + BranchInstruction branch = new IF_ICMPGE(null); + il.append(branch); + + il.append(body_Loop_block.genByteCode(_cg)); + + il.append(head_Loop_expr.genByteCode(_cg)); + + il.append(new GOTO(ih)); + + branch.setTarget(il.append(new NOP())); + + return il; } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index a8eac123..91cb548e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -179,8 +179,6 @@ public class IfStmt extends Statement @Override public InstructionList genByteCode(ClassGenerator _cg) { - InstructionFactory _factory = new InstructionFactory(_cg, _cg.getConstantPool()); - InstructionList il = new InstructionList(); IfInstruction ifInstruction = new IFEQ(null); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java index 8a39cbd0..50c2bdf8 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java @@ -13,6 +13,7 @@ import org.apache.commons.bcel6.generic.ObjectType; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.SCExcept; @@ -152,7 +153,13 @@ public class LocalOrFieldVar extends Expr if(this.isFieldAccess){ il.append(cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), this.get_Name(), this.getType().getBytecodeType(cg), Constants.GETFIELD)); } - il.append(cg.getInstructionFactory().createLoad(this.getType().getBytecodeType(cg), this.get_Name())); + + Type type = this.getType(); + org.apache.commons.bcel6.generic.Type byteCodeType = type.getBytecodeType(cg); + + String name = this.get_Name(); + + il.append(cg.getInstructionFactory().createLoad(byteCodeType, name)); return il; } diff --git a/test/bytecode/Condition.jav b/test/bytecode/Condition.jav new file mode 100644 index 00000000..4f68e8ab --- /dev/null +++ b/test/bytecode/Condition.jav @@ -0,0 +1,7 @@ +class Condition{ + + Boolean method() { + return 0 == 1; + } + +} \ No newline at end of file diff --git a/test/bytecode/ConditionTest.java b/test/bytecode/ConditionTest.java new file mode 100644 index 00000000..851d6426 --- /dev/null +++ b/test/bytecode/ConditionTest.java @@ -0,0 +1,34 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.junit.Test; + +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; + +public class ConditionTest { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "Condition.jav"; + public final static String outputFile = "Condition.class"; + + @Test + public void test() { + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + } + +} diff --git a/test/bytecode/ForTest.jav b/test/bytecode/ForTest.jav index 7343a1b8..649b71b0 100644 --- a/test/bytecode/ForTest.jav +++ b/test/bytecode/ForTest.jav @@ -1,6 +1,6 @@ class IfElse{ method(){ - while(true){ + for( i = 0; i < 10 ; i = i++){ } } diff --git a/test/bytecode/IfStatement.jav b/test/bytecode/IfStatement.jav index 476c9bc0..d9fdb16c 100644 --- a/test/bytecode/IfStatement.jav +++ b/test/bytecode/IfStatement.jav @@ -1,7 +1,7 @@ class IfStatement{ method(flag){ if(flag){ - + } } } \ No newline at end of file diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index d769fe98..58dbae58 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -20,7 +20,7 @@ public class SingleClassTester { try { compiler.parse(new File(inputFile)); Menge bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); - System.out.println(bytecode); + //System.out.println(bytecode); bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); } catch (IOException | yyException e) { e.printStackTrace(); From 0a17be3c4f61713cae02741f3a51200db16782d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 15 Oct 2015 19:12:38 +0200 Subject: [PATCH 02/13] =?UTF-8?q?Abstrakter=20Test=20f=C3=BCr=20BytecodeTe?= =?UTF-8?q?sts=20TypedVectorTest=20angelegt:=20funktoniert=20schon=20Overl?= =?UTF-8?q?oadingTest=20angelegt:=20N=C3=A4chstes=20Ziel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/bytecode/BytecodeTest.java | 57 ++++++++++++++++++++++++++++ test/bytecode/Overloading.jav | 12 ++++++ test/bytecode/OverloadingTest.java | 56 +++++++++++++++++++++++++++ test/bytecode/SingleClassTester.java | 1 + test/bytecode/TypedVector.jav | 7 ++++ test/bytecode/TypedVectorTest.java | 36 ++++++++++++++++++ 6 files changed, 169 insertions(+) create mode 100644 test/bytecode/BytecodeTest.java create mode 100644 test/bytecode/Overloading.jav create mode 100644 test/bytecode/OverloadingTest.java create mode 100644 test/bytecode/TypedVector.jav create mode 100644 test/bytecode/TypedVectorTest.java diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java new file mode 100644 index 00000000..8fac8161 --- /dev/null +++ b/test/bytecode/BytecodeTest.java @@ -0,0 +1,57 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +import junit.framework.TestCase; + +import org.junit.BeforeClass; +import org.junit.Test; + +public abstract class BytecodeTest extends TestCase{ + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public static String testFile; + public static String outputFile; + + protected String testName; + + public BytecodeTest(){ + init(); + + if(testName != null){ + + testFile = testName+".jav"; + outputFile = testName+".class"; + + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + }else{ + throw new RuntimeException("rootDirectory, testFile or outputFile is null."); + } + } + + protected abstract void init(); + + protected Class getClassToTest(){ + Class classToTest = null; + try { + File file = new File(rootDirectory); + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + + ClassLoader classLoader = new URLClassLoader(urls); + + classToTest = classLoader.loadClass(testName); + + + } catch (Exception e) { + throw new RuntimeException(e); + } + + return classToTest; + } +} diff --git a/test/bytecode/Overloading.jav b/test/bytecode/Overloading.jav new file mode 100644 index 00000000..ce94fb7d --- /dev/null +++ b/test/bytecode/Overloading.jav @@ -0,0 +1,12 @@ +import java.util.Vector; + +class Overloading{ + + void method(Vector v) { + + } + + void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/OverloadingTest.java b/test/bytecode/OverloadingTest.java new file mode 100644 index 00000000..4b348971 --- /dev/null +++ b/test/bytecode/OverloadingTest.java @@ -0,0 +1,56 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Vector; + +import org.junit.Test; + +public class OverloadingTest extends BytecodeTest{ + @Override + protected void init() { + testName = "Overloading"; + } + + @Test + public void testString() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + + @Test + public void testInteger() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index 34ff10a9..323aaf53 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -27,6 +27,7 @@ public class SingleClassTester { //System.out.println(bytecode); bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); } catch (IOException | yyException e) { + Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); e.printStackTrace(); TestCase.fail(); }finally{ diff --git a/test/bytecode/TypedVector.jav b/test/bytecode/TypedVector.jav new file mode 100644 index 00000000..a19b45bd --- /dev/null +++ b/test/bytecode/TypedVector.jav @@ -0,0 +1,7 @@ +import java.util.Stack; + +class TypedVector{ + public void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/TypedVectorTest.java b/test/bytecode/TypedVectorTest.java new file mode 100644 index 00000000..f5c2a9e4 --- /dev/null +++ b/test/bytecode/TypedVectorTest.java @@ -0,0 +1,36 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Stack; +import java.util.Vector; + +import org.junit.Test; + +public class TypedVectorTest extends BytecodeTest{ + @Override + protected void init() { + testName = "TypedVector"; + } + + @Test + public void test() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } +} From 3a1808880135aa3e8e157074dc072b074419bd08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 15 Oct 2015 22:16:18 +0200 Subject: [PATCH 03/13] =?UTF-8?q?Erste=20=C3=84nderung=20an=20RefType=20Te?= =?UTF-8?q?sts=20f=C3=BCr=20Extends=20angelegt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/IfStmt.java | 3 +- .../syntaxtree/type/RefType.java | 18 ++++++- test/bytecode/BytecodeTest.java | 6 +-- test/bytecode/ForTest.java | 2 + test/bytecode/LambdaExpr2.java | 2 + test/bytecode/PostDecrement.java | 2 + test/bytecode/PostIncrement.java | 2 + test/bytecode/types/ExtendsObject.jav | 3 ++ test/bytecode/types/ExtendsObjectTest.java | 48 +++++++++++++++++++ test/bytecode/types/ExtendsVector.jav | 4 ++ test/bytecode/types/ExtendsVectorString.jav | 5 ++ .../types/ExtendsVectorStringTest.java | 48 +++++++++++++++++++ test/bytecode/types/ExtendsVectorTest.java | 48 +++++++++++++++++++ test/bytecode/{ => types}/Overloading.jav | 0 .../bytecode/{ => types}/OverloadingTest.java | 5 +- test/bytecode/{ => types}/TypedVector.jav | 0 .../bytecode/{ => types}/TypedVectorTest.java | 5 +- 17 files changed, 194 insertions(+), 7 deletions(-) create mode 100644 test/bytecode/types/ExtendsObject.jav create mode 100644 test/bytecode/types/ExtendsObjectTest.java create mode 100644 test/bytecode/types/ExtendsVector.jav create mode 100644 test/bytecode/types/ExtendsVectorString.jav create mode 100644 test/bytecode/types/ExtendsVectorStringTest.java create mode 100644 test/bytecode/types/ExtendsVectorTest.java rename test/bytecode/{ => types}/Overloading.jav (100%) rename test/bytecode/{ => types}/OverloadingTest.java (89%) rename test/bytecode/{ => types}/TypedVector.jav (100%) rename test/bytecode/{ => types}/TypedVectorTest.java (84%) diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index e0b4936f..0998e4d3 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -178,7 +178,8 @@ public class IfStmt extends Statement } @Override - public InstructionList genByteCode(ClassGenerator _cg) { + public InstructionList genByteCode(ClassGenerator _cg) { + InstructionFactory _factory = _cg.getInstructionFactory(); InstructionList il = new InstructionList(); IfInstruction ifInstruction = new IFEQ(null); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 5b96e961..ac0577da 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -814,11 +814,24 @@ public class RefType extends ObjectType implements IMatchable } public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new org.apache.commons.bcel6.generic.ObjectType(this.getTypeName()); + return new org.apache.commons.bcel6.generic.ObjectType(this.getBytecodeSignature(cg)); } @Override public String getBytecodeSignature(ClassGenerator cg) { + //Bsp.: Ljava/util/Vector; + StringBuilder sb = new StringBuilder(); + + sb.append(getTypeName()); + + if(parameter != null){ + for(Type type: parameter){ + sb.append("%"); + sb.append(type.getBytecodeSignature(cg)); + } + } + + /* String paramString = ""; if(this.parameter != null && this.parameter.size()>0){ paramString+="<"; @@ -834,6 +847,9 @@ public class RefType extends ObjectType implements IMatchable String typeSignature = this.getBytecodeType(cg).getSignature(); typeSignature = typeSignature.substring(0, typeSignature.length()-1); return typeSignature+paramString+";"; + */ + + return sb.toString(); } } diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java index 8fac8161..eb1c2aae 100644 --- a/test/bytecode/BytecodeTest.java +++ b/test/bytecode/BytecodeTest.java @@ -14,9 +14,9 @@ import org.junit.BeforeClass; import org.junit.Test; public abstract class BytecodeTest extends TestCase{ - public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; - public static String testFile; - public static String outputFile; + public String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public String testFile; + public String outputFile; protected String testName; diff --git a/test/bytecode/ForTest.java b/test/bytecode/ForTest.java index fea1e7c5..497dc375 100644 --- a/test/bytecode/ForTest.java +++ b/test/bytecode/ForTest.java @@ -1,5 +1,6 @@ package bytecode; +import org.junit.Ignore; import org.junit.Test; public class ForTest { @@ -8,6 +9,7 @@ public class ForTest { public final static String outputFile = "ForTest.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/LambdaExpr2.java b/test/bytecode/LambdaExpr2.java index ff607c6c..83308ca5 100644 --- a/test/bytecode/LambdaExpr2.java +++ b/test/bytecode/LambdaExpr2.java @@ -7,6 +7,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.junit.Ignore; import org.junit.Test; import plugindevelopment.TypeInsertTester; @@ -27,6 +28,7 @@ public class LambdaExpr2 { public final static String outputFile = "LambdaExpr2.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/PostDecrement.java b/test/bytecode/PostDecrement.java index 075cf135..b0dbee20 100644 --- a/test/bytecode/PostDecrement.java +++ b/test/bytecode/PostDecrement.java @@ -7,6 +7,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.junit.Ignore; import org.junit.Test; import plugindevelopment.TypeInsertTester; @@ -27,6 +28,7 @@ public class PostDecrement { public final static String outputFile = "PostDecrement.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/PostIncrement.java b/test/bytecode/PostIncrement.java index 8c0b3930..da1fe1e3 100644 --- a/test/bytecode/PostIncrement.java +++ b/test/bytecode/PostIncrement.java @@ -7,6 +7,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.junit.Ignore; import org.junit.Test; import plugindevelopment.TypeInsertTester; @@ -27,6 +28,7 @@ public class PostIncrement { public final static String outputFile = "PostIncrement.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/types/ExtendsObject.jav b/test/bytecode/types/ExtendsObject.jav new file mode 100644 index 00000000..758def9c --- /dev/null +++ b/test/bytecode/types/ExtendsObject.jav @@ -0,0 +1,3 @@ +class ExtendsObject extends Object{ + +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsObjectTest.java b/test/bytecode/types/ExtendsObjectTest.java new file mode 100644 index 00000000..43648fdf --- /dev/null +++ b/test/bytecode/types/ExtendsObjectTest.java @@ -0,0 +1,48 @@ +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; + +public class ExtendsObjectTest extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsObject"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct(){ + try{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/types/ExtendsVector.jav b/test/bytecode/types/ExtendsVector.jav new file mode 100644 index 00000000..efd0240f --- /dev/null +++ b/test/bytecode/types/ExtendsVector.jav @@ -0,0 +1,4 @@ +import java.util.Vector; + +class ExtendsVector extends Vector{ +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsVectorString.jav b/test/bytecode/types/ExtendsVectorString.jav new file mode 100644 index 00000000..7829ecd0 --- /dev/null +++ b/test/bytecode/types/ExtendsVectorString.jav @@ -0,0 +1,5 @@ +import java.util.Vector; + +class ExtendsVectorString extends Vector{ + +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsVectorStringTest.java b/test/bytecode/types/ExtendsVectorStringTest.java new file mode 100644 index 00000000..10f4bdfe --- /dev/null +++ b/test/bytecode/types/ExtendsVectorStringTest.java @@ -0,0 +1,48 @@ +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; + +public class ExtendsVectorStringTest extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsVectorString"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct(){ + try{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/types/ExtendsVectorTest.java b/test/bytecode/types/ExtendsVectorTest.java new file mode 100644 index 00000000..aefe5d4b --- /dev/null +++ b/test/bytecode/types/ExtendsVectorTest.java @@ -0,0 +1,48 @@ +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; + +public class ExtendsVectorTest extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsVector"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct(){ + try{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/Overloading.jav b/test/bytecode/types/Overloading.jav similarity index 100% rename from test/bytecode/Overloading.jav rename to test/bytecode/types/Overloading.jav diff --git a/test/bytecode/OverloadingTest.java b/test/bytecode/types/OverloadingTest.java similarity index 89% rename from test/bytecode/OverloadingTest.java rename to test/bytecode/types/OverloadingTest.java index 4b348971..7c8ba2ce 100644 --- a/test/bytecode/OverloadingTest.java +++ b/test/bytecode/types/OverloadingTest.java @@ -1,4 +1,4 @@ -package bytecode; +package bytecode.types; import static org.junit.Assert.*; @@ -7,10 +7,13 @@ import java.util.Vector; import org.junit.Test; +import bytecode.BytecodeTest; + public class OverloadingTest extends BytecodeTest{ @Override protected void init() { testName = "Overloading"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; } @Test diff --git a/test/bytecode/TypedVector.jav b/test/bytecode/types/TypedVector.jav similarity index 100% rename from test/bytecode/TypedVector.jav rename to test/bytecode/types/TypedVector.jav diff --git a/test/bytecode/TypedVectorTest.java b/test/bytecode/types/TypedVectorTest.java similarity index 84% rename from test/bytecode/TypedVectorTest.java rename to test/bytecode/types/TypedVectorTest.java index f5c2a9e4..5383b5b9 100644 --- a/test/bytecode/TypedVectorTest.java +++ b/test/bytecode/types/TypedVectorTest.java @@ -1,4 +1,4 @@ -package bytecode; +package bytecode.types; import static org.junit.Assert.*; @@ -8,10 +8,13 @@ import java.util.Vector; import org.junit.Test; +import bytecode.BytecodeTest; + public class TypedVectorTest extends BytecodeTest{ @Override protected void init() { testName = "TypedVector"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; } @Test From 1a07c186a6b0d66643415426ae84ca58ee29c26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 16 Oct 2015 10:39:34 +0200 Subject: [PATCH 04/13] =?UTF-8?q?generateBytecode=20von=20Class=20gibt=20e?= =?UTF-8?q?ine=20Menge=20an=20ByteCodeResults=20zur=C3=BCck,=20wird=20ben?= =?UTF-8?q?=C3=B6tigt=20um=20zus=C3=A4tzliche=20Klassen=20anzulegen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/core/MyCompiler.java | 4 +- src/de/dhbwstuttgart/core/MyCompilerAPI.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Class.java | 389 ++---------------- .../dhbwstuttgart/syntaxtree/SourceFile.java | 4 +- .../syntaxtree/type/RefType.java | 12 +- .../typeinference/TypeinferenceResultSet.java | 2 +- test/bytecode/BytecodeTest.java | 6 +- test/bytecode/SingleClassTester.java | 18 +- test/bytecode/types/ExtendsVector.jav | 1 + test/bytecode/types/TypedVector.jav | 2 +- test/bytecode/types/TypedVectorTest.java | 4 +- 11 files changed, 61 insertions(+), 383 deletions(-) diff --git a/src/de/dhbwstuttgart/core/MyCompiler.java b/src/de/dhbwstuttgart/core/MyCompiler.java index 74099f38..7b72e79b 100755 --- a/src/de/dhbwstuttgart/core/MyCompiler.java +++ b/src/de/dhbwstuttgart/core/MyCompiler.java @@ -860,10 +860,10 @@ public class MyCompiler implements MyCompilerAPI } @Override - public Menge generateBytecode(TypeinferenceResultSet typeinferenceResult) { + public Menge> generateBytecode(TypeinferenceResultSet typeinferenceResult) { //SourceFile parsedFile = this.m_AbstractSyntaxTree.firstElement(); //Class parsedClass = parsedFile.KlassenVektor.firstElement(); - Menge ret = new Menge<>(); + Menge> ret = new Menge<>(); for(SourceFile sf : this.m_AbstractSyntaxTree){ ret.addAll(sf.generateBytecode(typeinferenceResult)); } diff --git a/src/de/dhbwstuttgart/core/MyCompilerAPI.java b/src/de/dhbwstuttgart/core/MyCompilerAPI.java index 9c5d2a57..78860de4 100755 --- a/src/de/dhbwstuttgart/core/MyCompilerAPI.java +++ b/src/de/dhbwstuttgart/core/MyCompilerAPI.java @@ -127,6 +127,6 @@ public interface MyCompilerAPI * Dafür müssen die Schritte Parsen und typeReconstruction ausgeführt werden. * @return */ - public Menge generateBytecode(TypeinferenceResultSet rs); + public Menge> generateBytecode(TypeinferenceResultSet rs); } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 356e3298..b11ed15f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -15,7 +15,6 @@ import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; import org.apache.commons.bcel6.generic.MethodGen; -import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.logger.SectionLogger; @@ -75,11 +74,13 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I * @param resultSet - Fehlende Typen im Syntaxbaum werden nach diesem ResultSet aufgelöst * @return */ - public ByteCodeResult genByteCode(TypeinferenceResultSet resultSet) { + public Menge genByteCode(TypeinferenceResultSet resultSet) { InstructionFactory _factory; DHBWConstantPoolGen _cp; ClassGenerator _cg; + Menge results = new Menge(); + SectionLogger logger = Logger.getSectionLogger(this.getClass().getName(), Section.CODEGEN); logger.debug("Test"); @@ -112,10 +113,14 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I } ByteCodeResult code = new ByteCodeResult(_cg); - return code; + results.add(code); + + results.addAll(getGenericClasses(_cg)); + + return results; } - private Menge superif = new Menge(); + private Menge superif = new Menge(); public UsedId getPackageName() { @@ -593,112 +598,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I { return this.parahash; } - // ino.end - - - -/*static void string_rec(Hashtable ht){ - String record=""; - boolean isError=false; - record=record.concat("["); - for(Enumeration e=ht.elements(),k=ht.keys();e.hasMoreElements();){ - String s = (String)k.nextElement(); - Object o = e.nextElement(); - - record=record.concat(" "+s); - - if(o instanceof Type){ - record=record.concat(" = "+((Type)o).getName()); - } else if(o instanceof Hashtable){ - record=record.concat("= "); - string_rec((Hashtable)o); - if(e.hasMoreElements()) - record=record.concat(", "); - } else if(o instanceof String){ - record=record.concat(" = "+o); - if(e.hasMoreElements()) - record=record.concat(", "); - } - else { - record=("[FEHLER: string_rec: unbekannter Typ!!!!!!"); - isError=true; - } - } - record=record.concat("]"); - if(isError){ - parserlog.error(record); - }else{ - parserlog.debug(record); - } - }*/ - - /*static void string_rec(Menge v){ - String record=("{"); - for(Enumeration e=v.elements();e.hasMoreElements();){ - Type t = (Type)e.nextElement(); - record=record.concat(" "+t.getName()); - - if(e.hasMoreElements()) - record=record.concat(","); - } - record=record.concat("}"); - parserlog.debug(record); - }*/ - /*static void string_rec(String st, Hashtable ht){ - String record=(st); - boolean isError=false; - record=record.concat("["); - for(Enumeration e=ht.elements(),k=ht.keys();e.hasMoreElements();){ - String s = (String)k.nextElement(); - Object o = e.nextElement(); - - record=record.concat(" "+s); - - if(o instanceof Type){ - record=record.concat(" = "+((Type)o).getName()); - } - - else if(o instanceof Hashtable){ - record=record.concat("= "); - string_rec((Hashtable)o); - if(e.hasMoreElements()) - record=record.concat(", "); - } - else if(o instanceof String){ - record=record.concat(" = "+o); - if(e.hasMoreElements()) - record=record.concat(", "); - } - else { - record=("[FEHLER: string_rec: unbekannter Typ!!!!!! " +o); - isError = true; - } - } - record=record.concat("]"); - if(isError){ - parserlog.error(record); - }else{ - parserlog.debug(record); - } - }*/ - - /*static void string_rec(String st,Menge v) - { - String record=(st); - record=record.concat("{"); - for(Enumeration e=v.elements();e.hasMoreElements();) - { - Type t = (Type)e.nextElement(); - record=record.concat(" "+t.getName()); - if(e.hasMoreElements()) - { - record=record.concat(", "); - } - } - record=record.concat("}"); - parserlog.debug(record); - }*/ - ///////////////////////////////////////////////////////////////////////// // TypeReconstructionAlgorithmus @@ -771,165 +670,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I typinferenzLog.debug("Erstellte Constraints: "+oderConstraints, Section.TYPEINFERENCE); return oderConstraints; - - - /* - CReconstructionTupleSet retTupleSet = this.TRStart(methodList, V, V_fields_methods, supportData); - inferencelog.debug("Bin aus TRStart() zur�ck in TRProg()."); - ////////////////////////////// - // Neu Ergebnismenge A aller - // Typannahmen erzeugen: - ////////////////////////////// - inferencelog.debug("Erstelle Ergebnismenge..."); - Menge newA = new Menge(); - - // Alle bisherigen M�glichkeiten an Typkombinationen durchgehen: - Menge oldA = supportData.getA(); - for(int i=0; i retTupleIt = retTupleSet.getIterator(); - while(retTupleIt.hasNext()){ - CReconstructionTuple possibleTuple = retTupleIt.next(); - this.clear(possibleTuple.getAssumSet(), rememberLocals); - - // Neue Typinformationen mit alten Typinformationen vereinigen: - CTypeReconstructionResult newReconResult = oneReconResult.shallowCopy(); - newReconResult.addDataFromTupel(possibleTuple); - - - //PL 05-08-02 eingefuegt - //Fuegt Klassen und GenericTypeVars zu A hinzu - newReconResult.addClassName(this.getName()); - Menge genericsList = new Menge(); - for(int ii =0; ii getMethodList() { - - - if(this.methodList != null) return this.methodList; - //TODO: Unnötige Berechnungen im folgenden Code rauskürzen: - ////////////////////////////// - // Die Eingabedaten bauen: - ////////////////////////////// - inferencelog.debug("Baue Eingabedaten..."); - TypeAssumptions V_fields_methods = new TypeAssumptions(this.getName()); - Menge methodList = new Menge(); - Menge V = new Menge(); - Menge rememberLocals = new Menge(); - - ////////////////////////////// - // Alle Felder durchgehen: - // Zuerst alle Attribute, dann Methoden - // ge�ndert: hoth 06.04.2006 - - ////////////////////////////// - for(Field field : this.getFields()) - { - ////////////////////////////// - // Attribut: - ////////////////////////////// - - } - - for(Field field : this.getFields()) - { - ////////////////////////////// - // Methode: - ////////////////////////////// - if(field instanceof Method){ - Method method = (Method)field; - //if(method.get_Method_Name().equals(this.getName()) && ((method.getReturnType().equals(new mycompiler.mytype.Void(0))) || method.getReturnType() instanceof TypePlaceholder)){ - if(method.get_Method_Name().equals(this.getName()) ){ - method.set_Method_Name(""); - } - //hoth: 06.04.2006 - //durchlaufe Block und suche nach Objektvariablen fuer Offset-Markierung - Iterator fieldVarIterator = V_fields_methods.iterator(); - while (fieldVarIterator.hasNext()) - { - //Wenn ObjektVariable - CTypeAssumption dieAssum = fieldVarIterator.next(); - if(dieAssum instanceof CInstVarTypeAssumption) - { - Class.isFirstLocalVarDecl=false; - if(method.get_Block() != null) - method.get_Block().addOffsetsToAssumption(dieAssum,dieAssum.getIdentifier(),true); - } - } - - methodList.addElement(method); - - // F�r V_fields_methods: - CMethodTypeAssumption methodAssum = new CMethodTypeAssumption(this.getType(), method.get_Method_Name(), method.getReturnType(), method.getParameterCount(),method.getLineNumber(),method.getOffset(),new Menge(),method.getGenericMethodParameters()); // Typannahme bauen... - - - // Methode in V_Fields_methods ablegen - // Dabei wird die OverloadedMethodID ermittelt !! - // => Method setzenuct - - - V_fields_methods.add(methodAssum); - method.setOverloadedID(methodAssum.getHashSetKey().getOverloadedMethodID()); - - - // F�r die V_i: - CTypeAssumptionSet localAssum = new CTypeAssumptionSet(); - - // Bauen... - ParameterList parameterList = method.getParameterList(); - if(parameterList!=null){ - for(int i=0; i()); - //fuege Offsets fuer Parameter hinzu, hoth: 06.04.2006 - Class.isFirstLocalVarDecl=false; - - if(method.get_Block() != null) - method.get_Block().addOffsetsToAssumption(paraAssum,paraAssum.getIdentifier(),true); - - methodAssum.addParaAssumption(paraAssum); - - - - - // F�r die V_i: - CLocalVarTypeAssumption varAssum = new CLocalVarTypeAssumption(this.getName(), method.get_Method_Name(), method.getParameterCount(), method.getOverloadedID(),"1", para.getIdentifier(),para.getType(), para.getLineNumber(),para.getOffset(),new Menge()); - localAssum.addElement(varAssum); - rememberLocals.addElement(varAssum); - } - } - // ...und hinzuf�gen: - - V.addElement(localAssum); - } - } - - this.methodList = methodList; - return methodList; - - } -*/ /** * Ermittelt alle privaten Felder und Methoden der Klasse und Erstellt eine Assumption für diese. @@ -957,49 +698,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I this.typeAssumptions = assumptions; //Diese müssen anschließend nicht wieder generiert werden. return assumptions; -} - /* - public ConstraintsSet TYPE(Menge methodList, Menge fielddeclarationList, TypeAssumptions assumptions){ - ConstraintsSet ret = new ConstraintsSet(); - // Die Felddeklarationen werden zu den Assumptions hinzugefügt und gelten danach für jede Methode. - //TypeAssumptions assumptionsPlusFieldAssumptions = new TypeAssumptions(assumptions); - for(Expr expr : fielddeclarationList){ - //ret.add(expr.TYPEStmt(assumptionsPlusFieldAssumptions)); - ret.add(expr.TYPEStmt(assumptions)); - } - for(Method methode : methodList){ - //ret.add(methode.TYPE(assumptionsPlusFieldAssumptions)); - ret.add(methode.TYPE(assumptions)); - } - return ret; - } - */ + } - - // ino.method.RetType.23119.defdescription type=javadoc - /** - * Liefert den berechneten R�ckgabetyp f�r die �bergebene Methode zur�ck.
- * (siehe Algorithmus RetType, Martin Pl�micke) - *
Author: J�rg B�uerle - * @param me - * @param V - * @return - - // ino.end - // ino.method.RetType.23119.definition - private Type RetType(Method me, CTypeAssumptionSet V) - // ino.end - // ino.method.RetType.23119.body - { - CTypeAssumption assum = V.getElement(new CMethodKey(this.getName(), me.get_Method_Name(), me.getParameterCount(),me.getOverloadedID())); - if(assum instanceof CMethodTypeAssumption){ - return ((CMethodTypeAssumption)assum).getAssumedType(); - } - else return null; - } - // ino.end - - */ // ino.method.toString.23125.defdescription type=javadoc /** *
Author: Martin Pl�micke @@ -1037,48 +737,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I for(Field f : this.getFields()){ //f.wandleRefTypeAttributes2GenericAttributes(paralist); } - /* - Menge fieldsAndMethods=this.getFields(); - - // Alle Methoden und Instanzvariablen durchgehen - for(int i=0;i enumer = V_last.getHashtable().keys(); -// while(enumer.hasMoreElements()){ -// CTypeAssumption currentAssum = V_last.getElement(enumer.nextElement()); -// if(currentAssum instanceof CLocalVarTypeAssumption){ -// V_i.removeElement(currentAssum); -// } -// } -// -// } - - - // ino.method.getSimpleName.23140.defdescription type=javadoc /** * HOTI * Liefert bei Klassen die fullyQualified angegeben wurden @@ -1115,18 +775,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I */ public String printJavaCode(TypeinferenceResultSet reconstructionResult){ JavaCodeResult ret = new JavaCodeResult("class "); - - - - - //Generiere JavaCode der extends-Typen: - /* - String containedTypesJavaCode = ""; - for(Type containedType : containedTypes){ - containedTypesJavaCode += containedType.printJavaCode(resultSet)+ ", "; - } - if(containedTypesJavaCode.length()>1)containedTypesJavaCode = containedTypesJavaCode.substring(0, containedTypesJavaCode.length() -2); - */ JavaCodeResult classBodyCode = new JavaCodeResult(); if(this.modifiers!=null)classBodyCode.attach(this.modifiers.printJavaCode(reconstructionResult.getUnifiedConstraints())).attach(" "); @@ -1382,5 +1030,22 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I public boolean isInterface(){ return false; } + + private Collection getGenericClasses(ClassGenerator _cg) { + Collection results = new Menge<>(); + + //Super Klasse + String name = "java/util/Vectorjava/lang/String"; //getParentClass().getType().getBytecodeSignature(_cg); + + Type superClass = new Class("java/util/Vector",-1).getType(); + + _cg = new ClassGenerator(name, superClass, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); + + ByteCodeResult code = new ByteCodeResult(_cg); + + results.add(code); + + return results; + } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index f96cc32e..22c45fd7 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -1834,8 +1834,8 @@ public class SourceFile * Bisher wird nur der Bytecode der Klassen generiert. Nicht der Interfaces. * @return */ - public Collection generateBytecode(TypeinferenceResultSet rs) { - Menge ret = new Menge<>(); + public Menge> generateBytecode(TypeinferenceResultSet rs) { + Menge> ret = new Menge<>(); for(Class cl : this.KlassenVektor){ ret.add(cl.genByteCode(rs)); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index ac0577da..4f2e045d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -814,11 +814,12 @@ public class RefType extends ObjectType implements IMatchable } public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new org.apache.commons.bcel6.generic.ObjectType(this.getBytecodeSignature(cg)); + return new org.apache.commons.bcel6.generic.ObjectType(this.get_Name()); } @Override public String getBytecodeSignature(ClassGenerator cg) { + /* //Bsp.: Ljava/util/Vector; StringBuilder sb = new StringBuilder(); @@ -826,12 +827,14 @@ public class RefType extends ObjectType implements IMatchable if(parameter != null){ for(Type type: parameter){ - sb.append("%"); + sb.append(""); //TODO: einen geeignete Delemiter suchen sb.append(type.getBytecodeSignature(cg)); } } - /* + return sb.toString(); + */ + String paramString = ""; if(this.parameter != null && this.parameter.size()>0){ paramString+="<"; @@ -847,9 +850,6 @@ public class RefType extends ObjectType implements IMatchable String typeSignature = this.getBytecodeType(cg).getSignature(); typeSignature = typeSignature.substring(0, typeSignature.length()-1); return typeSignature+paramString+";"; - */ - - return sb.toString(); } } diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java index 9a38d298..819d310b 100755 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java @@ -119,7 +119,7 @@ public class TypeinferenceResultSet * Dabei wird die codegen-Methode der inferierten Klasse mit diesem ResultSet aufgerufen. */ public ByteCodeResult codegen(){ - ByteCodeResult res = this.ownerOfResultSet.genByteCode(this); + ByteCodeResult res = this.ownerOfResultSet.genByteCode(this).firstElement(); return res; } diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java index eb1c2aae..35b8dc33 100644 --- a/test/bytecode/BytecodeTest.java +++ b/test/bytecode/BytecodeTest.java @@ -16,7 +16,7 @@ import org.junit.Test; public abstract class BytecodeTest extends TestCase{ public String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; public String testFile; - public String outputFile; + public String outputDirectory; protected String testName; @@ -26,9 +26,9 @@ public abstract class BytecodeTest extends TestCase{ if(testName != null){ testFile = testName+".jav"; - outputFile = testName+".class"; + outputDirectory = ""; - SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputDirectory); }else{ throw new RuntimeException("rootDirectory, testFile or outputFile is null."); } diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index 323aaf53..a5d958f0 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -3,6 +3,8 @@ package bytecode; import java.io.File; import java.io.IOException; +import org.apache.commons.bcel6.classfile.JavaClass; + import com.google.common.io.Files; import junit.framework.TestCase; @@ -18,20 +20,28 @@ import de.dhbwstuttgart.typeinference.Menge; public class SingleClassTester { - public static void compileToBytecode(String inputFile, String outputFile){ + public static void compileToBytecode(String inputFile, String outputDirectory){ LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); try { compiler.parse(new File(inputFile)); - Menge bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); + Menge> bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); //System.out.println(bytecode); - bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); + + for(ByteCodeResult result: bytecode.firstElement()){ + JavaClass javaClass = result.getByteCode().getJavaClass(); + + Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN); + + javaClass.dump(new File(outputDirectory+javaClass.getClassName()+".class")); + } + } catch (IOException | yyException e) { Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); e.printStackTrace(); TestCase.fail(); }finally{ - writeLog(outputFile+".log"); + writeLog(outputDirectory+".log"); } } diff --git a/test/bytecode/types/ExtendsVector.jav b/test/bytecode/types/ExtendsVector.jav index efd0240f..fed1dd07 100644 --- a/test/bytecode/types/ExtendsVector.jav +++ b/test/bytecode/types/ExtendsVector.jav @@ -1,4 +1,5 @@ import java.util.Vector; class ExtendsVector extends Vector{ + } \ No newline at end of file diff --git a/test/bytecode/types/TypedVector.jav b/test/bytecode/types/TypedVector.jav index a19b45bd..d64e4f5f 100644 --- a/test/bytecode/types/TypedVector.jav +++ b/test/bytecode/types/TypedVector.jav @@ -1,4 +1,4 @@ -import java.util.Stack; +import java.util.Vector; class TypedVector{ public void method(Vector v) { diff --git a/test/bytecode/types/TypedVectorTest.java b/test/bytecode/types/TypedVectorTest.java index 5383b5b9..ef32180b 100644 --- a/test/bytecode/types/TypedVectorTest.java +++ b/test/bytecode/types/TypedVectorTest.java @@ -9,6 +9,8 @@ import java.util.Vector; import org.junit.Test; import bytecode.BytecodeTest; +import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; public class TypedVectorTest extends BytecodeTest{ @Override @@ -32,7 +34,7 @@ public class TypedVectorTest extends BytecodeTest{ method.invoke(obj, stringVector); assertTrue(true); }catch(Exception e){ - e.printStackTrace(); + Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); fail(); } } From 6a8590ab0d0731365e024cbe856191670e43d916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 16 Oct 2015 13:22:08 +0200 Subject: [PATCH 05/13] FreshWildcardType muss von ObjectType ableiten, damit der Unifikationsalgorithmus funktioniert --- src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java | 2 +- test/bytecode/types/ExtendsVector.jav | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java index 33f153e0..40ddb895 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java @@ -8,7 +8,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; -public class FreshWildcardType extends Type { +public class FreshWildcardType extends ObjectType { private static String strNextName = "A"; private Menge upperBounds = new Menge(); diff --git a/test/bytecode/types/ExtendsVector.jav b/test/bytecode/types/ExtendsVector.jav index fed1dd07..f029967d 100644 --- a/test/bytecode/types/ExtendsVector.jav +++ b/test/bytecode/types/ExtendsVector.jav @@ -1,5 +1,5 @@ import java.util.Vector; -class ExtendsVector extends Vector{ +class ExtendsVector extends Vector{ } \ No newline at end of file From 50dda3041fc66c1e3c65d0771ca15b540d7145eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 16 Oct 2015 15:15:30 +0200 Subject: [PATCH 06/13] Beginn der Generierung der GenericKlasse, Problem ist der Konstruktor, dieser wird auf Objekt aufgerufen und nicht auf der SUperklasse --- src/de/dhbwstuttgart/syntaxtree/Class.java | 61 ++++++++++++++++++- .../syntaxtree/type/RefType.java | 19 +++++- .../types/ExtendsVectorStringTest.java | 17 ++---- 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index b11ed15f..d7b65693 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -24,6 +24,7 @@ import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.core.AClassOrInterface; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.parser.JavaClassName; +import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.misc.UsedId; import de.dhbwstuttgart.syntaxtree.modifier.Modifiers; import de.dhbwstuttgart.syntaxtree.statement.Block; @@ -1035,11 +1036,65 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I Collection results = new Menge<>(); //Super Klasse - String name = "java/util/Vectorjava/lang/String"; //getParentClass().getType().getBytecodeSignature(_cg); + String name = getType().getCombinedType(_cg); - Type superClass = new Class("java/util/Vector",-1).getType(); + Class superClass = new Class("java/util/Vector",-1); + Type superClassType = superClass.getType(); - _cg = new ClassGenerator(name, superClass, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); + //_cg = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); + + Class generatedClass = new Class(name, 0); + + Block konstruktorBlock = new Block(); + konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); + + generatedClass.addField(standardKonstruktor); + + results.addAll(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet()))); + + + /* + //Die Felder in Methoden Felder und Konstruktoren aufteilen: + Menge fieldDeclarations = new Menge<>(); + Menge constructors = new Menge<>(); + + Block konstruktorBlock = new Block(); + konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + + Method constructorMethod = Method.createEmptyMethod(konstruktorBlock, name, superClass); + + constructorMethod.parameterlist = new ParameterList(); + Constructor constructor = new Constructor(constructorMethod, superClass); + constructor.parserPostProcessing(superClass); + + constructors.add(constructor); + + InstructionList fieldInitializations = new InstructionList(); + for(FieldDeclaration f : fieldDeclarations){ + fieldInitializations.append(f.genByteCode(_cg)); + } + //Die Konstruktoren müssen die Feld initialisierungswerte beinhalten: + for(Constructor c : constructors){ + c.genByteCode(_cg, fieldInitializations); + } + */ + + /* + + Block konstruktorBlock = new Block(); + konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock,name, superClass), superClass); + + Menge constructors = new Menge<>(); + constructors.add(standardKonstruktor); + + //Die Konstruktoren müssen die Feld initialisierungswerte beinhalten: + for(Constructor c : constructors){ + c.genByteCode(_cg, new InstructionList()); + } + + */ ByteCodeResult code = new ByteCodeResult(_cg); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4f2e045d..cb04e0f2 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -814,7 +814,7 @@ public class RefType extends ObjectType implements IMatchable } public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new org.apache.commons.bcel6.generic.ObjectType(this.get_Name()); + return new org.apache.commons.bcel6.generic.ObjectType(getCombinedType(cg)); } @Override @@ -852,6 +852,23 @@ public class RefType extends ObjectType implements IMatchable return typeSignature+paramString+";"; } + public String getCombinedType(ClassGenerator cg){ + //Bsp.: Ljava/util/Vector; + StringBuilder sb = new StringBuilder(); + + sb.append(this.get_Name().replace(".", "%")); + + if(parameter != null){ + sb.append("%%"); + for(Type type: parameter){ + sb.append(((RefType) type).getCombinedType(cg)); + sb.append("%"); + } + } + + return sb.toString(); + } + } // ino.end diff --git a/test/bytecode/types/ExtendsVectorStringTest.java b/test/bytecode/types/ExtendsVectorStringTest.java index 10f4bdfe..8511499b 100644 --- a/test/bytecode/types/ExtendsVectorStringTest.java +++ b/test/bytecode/types/ExtendsVectorStringTest.java @@ -32,17 +32,12 @@ public class ExtendsVectorStringTest extends BytecodeTest{ } @Test - public void testConstruct(){ - try{ - Class cls = getClassToTest(); - - Constructor method = cls.getConstructor(new Class[]{}); - method.newInstance(); - assertTrue(true); - }catch(Exception e){ - e.printStackTrace(); - fail(); - } + public void testConstruct() throws Exception{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); } } From bafffe26adb64d9c009d406372612a796bb61ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Tue, 20 Oct 2015 14:07:57 +0200 Subject: [PATCH 07/13] Generierung von Generic Klasse auskommentiert --- src/de/dhbwstuttgart/syntaxtree/Class.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index d7b65693..5c66bc2b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -1038,20 +1038,20 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I //Super Klasse String name = getType().getCombinedType(_cg); - Class superClass = new Class("java/util/Vector",-1); - Type superClassType = superClass.getType(); + //Class superClass = new Class("java/util/Vector",-1); + //Type superClassType = superClass.getType(); //_cg = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); - Class generatedClass = new Class(name, 0); + //Class generatedClass = new Class(name, 0); - Block konstruktorBlock = new Block(); - konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); - Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); + //Block konstruktorBlock = new Block(); + //konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + //Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); - generatedClass.addField(standardKonstruktor); + //generatedClass.addField(standardKonstruktor); - results.addAll(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet()))); + //results.addAll(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet()))); /* From 632c2ca508649c7c418ff6a3671476770c2e6859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Tue, 20 Oct 2015 15:58:39 +0200 Subject: [PATCH 08/13] =?UTF-8?q?Genericklassen=20f=C3=BCr=20Superklassen,?= =?UTF-8?q?=20Fieldtypes=20und=20Methodenparameter=20werden=20generiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Class.java | 118 ++++++++---------- .../syntaxtree/type/RefType.java | 26 +--- test/bytecode/types/OverloadingTest.java | 2 + 3 files changed, 60 insertions(+), 86 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 5c66bc2b..2313f22c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -116,7 +116,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I ByteCodeResult code = new ByteCodeResult(_cg); results.add(code); - results.addAll(getGenericClasses(_cg)); + results.addAll(getGenericClasses()); return results; } @@ -1032,75 +1032,61 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I return false; } - private Collection getGenericClasses(ClassGenerator _cg) { + private Collection getGenericClasses() { Collection results = new Menge<>(); - //Super Klasse - String name = getType().getCombinedType(_cg); - - //Class superClass = new Class("java/util/Vector",-1); - //Type superClassType = superClass.getType(); - - //_cg = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); - - //Class generatedClass = new Class(name, 0); - - //Block konstruktorBlock = new Block(); - //konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); - //Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); - - //generatedClass.addField(standardKonstruktor); - - //results.addAll(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet()))); - - - /* - //Die Felder in Methoden Felder und Konstruktoren aufteilen: - Menge fieldDeclarations = new Menge<>(); - Menge constructors = new Menge<>(); - - Block konstruktorBlock = new Block(); - konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); - - Method constructorMethod = Method.createEmptyMethod(konstruktorBlock, name, superClass); - - constructorMethod.parameterlist = new ParameterList(); - Constructor constructor = new Constructor(constructorMethod, superClass); - constructor.parserPostProcessing(superClass); - - constructors.add(constructor); - - InstructionList fieldInitializations = new InstructionList(); - for(FieldDeclaration f : fieldDeclarations){ - fieldInitializations.append(f.genByteCode(_cg)); - } - //Die Konstruktoren müssen die Feld initialisierungswerte beinhalten: - for(Constructor c : constructors){ - c.genByteCode(_cg, fieldInitializations); - } - */ - - /* - - Block konstruktorBlock = new Block(); - konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); - Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock,name, superClass), superClass); - - Menge constructors = new Menge<>(); - constructors.add(standardKonstruktor); - - //Die Konstruktoren müssen die Feld initialisierungswerte beinhalten: - for(Constructor c : constructors){ - c.genByteCode(_cg, new InstructionList()); - } - - */ - - ByteCodeResult code = new ByteCodeResult(_cg); - - results.add(code); + for(Field field : this.fielddecl){ + Type type = field.getType(); + //Der Type des Feldes + if(type instanceof RefType){ + RefType refType = (RefType) type; + + if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){ + results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1))); + } + } + + if(field instanceof Method){ + Method method = (Method) field; + ParameterList parameterList = method.getParameterList(); + + //Die Typen der Methodenparameter + for(FormalParameter parameter: parameterList){ + Type parameterType = parameter.getType(); + + if(parameterType instanceof RefType){ + RefType refType = (RefType) parameterType; + + if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){ + results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1))); + } + } + } + } + } return results; } + + private Menge generateGenericClass(String name, Class superClass){ + //TODO: bytecode -- Generics hinzufügen + //Type superClassType = superClass.getType(); + + //TODO: bytecode + //ClassGenerator genericClassGenerator = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); + + //TODO: bytecode -- Namen der neuen Klasse + Class generatedClass = new Class(name, 0); + + //TODO: bytecode -- alle Konstruktoren generieren + Block konstruktorBlock = new Block(); + konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); + standardKonstruktor.parserPostProcessing(generatedClass); + + generatedClass.addField(standardKonstruktor); + + return generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())); + } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index cb04e0f2..56caea6c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -820,21 +820,6 @@ public class RefType extends ObjectType implements IMatchable @Override public String getBytecodeSignature(ClassGenerator cg) { /* - //Bsp.: Ljava/util/Vector; - StringBuilder sb = new StringBuilder(); - - sb.append(getTypeName()); - - if(parameter != null){ - for(Type type: parameter){ - sb.append(""); //TODO: einen geeignete Delemiter suchen - sb.append(type.getBytecodeSignature(cg)); - } - } - - return sb.toString(); - */ - String paramString = ""; if(this.parameter != null && this.parameter.size()>0){ paramString+="<"; @@ -842,28 +827,29 @@ public class RefType extends ObjectType implements IMatchable while(it.hasNext()){ Type param = it.next(); paramString+=param.getBytecodeSignature(cg); - //if(it.hasNext()) - //paramString+=";"; //kein Delimiter zwischen den Typen } paramString+=">"; } String typeSignature = this.getBytecodeType(cg).getSignature(); typeSignature = typeSignature.substring(0, typeSignature.length()-1); return typeSignature+paramString+";"; + */ + return "L"+getCombinedType(cg)+";"; } public String getCombinedType(ClassGenerator cg){ //Bsp.: Ljava/util/Vector; StringBuilder sb = new StringBuilder(); - sb.append(this.get_Name().replace(".", "%")); - - if(parameter != null){ + if(parameter != null && parameter.size() > 0){ + sb.append(this.get_Name().replace(".", "%")); sb.append("%%"); for(Type type: parameter){ sb.append(((RefType) type).getCombinedType(cg)); sb.append("%"); } + }else{ + sb.append(this.get_Name()); } return sb.toString(); diff --git a/test/bytecode/types/OverloadingTest.java b/test/bytecode/types/OverloadingTest.java index 7c8ba2ce..9e6b3a22 100644 --- a/test/bytecode/types/OverloadingTest.java +++ b/test/bytecode/types/OverloadingTest.java @@ -16,6 +16,8 @@ public class OverloadingTest extends BytecodeTest{ rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; } + + @Test public void testString() { try{ From bfb00ac1d1d1e1056e892904f528e12fe1de849b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 22 Oct 2015 20:40:33 +0200 Subject: [PATCH 09/13] =?UTF-8?q?ClassGenerator=20hat=20eine=20Map=20von?= =?UTF-8?q?=20zus=C3=A4tzlichen=20Klassen=20die=20generiert=20werden=20m?= =?UTF-8?q?=C3=BCssen=20MyCompiler=20und=20MyCompilerApi=20aufger=C3=A4umt?= =?UTF-8?q?=20ASTFactory=20Klasse=20angefangen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/ClassGenerator.java | 12 + src/de/dhbwstuttgart/core/MyCompiler.java | 600 +----------------- src/de/dhbwstuttgart/core/MyCompilerAPI.java | 8 +- src/de/dhbwstuttgart/syntaxtree/Class.java | 14 +- .../dhbwstuttgart/syntaxtree/SourceFile.java | 4 +- .../syntaxtree/factory/ASTFactory.java | 42 ++ .../syntaxtree/type/RefType.java | 7 +- .../typeinference/ByteCodeResult.java | 9 +- .../typeinference/TypeinferenceResultSet.java | 3 +- test/bytecode/SingleClassTester.java | 29 +- 10 files changed, 129 insertions(+), 599 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java diff --git a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java index 0f8012ac..514b1821 100644 --- a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import org.apache.commons.bcel6.classfile.BootstrapMethod; import org.apache.commons.bcel6.classfile.BootstrapMethods; @@ -29,6 +31,8 @@ public class ClassGenerator extends ClassGen{ private Type superClass; private Menge usedTPHs = new Menge<>(); + + private Map extraClasses = new HashMap(); public ClassGenerator(String name, Type superClass, String string, short accPublic, String[] strings, TypeinferenceResultSet resultSet) { @@ -156,4 +160,12 @@ public class ClassGenerator extends ClassGen{ return ret; } + public void addExtraClass(ClassGenerator cg){ + extraClasses.put(cg.getClassName(), cg); + } + + public Map getExtraClasses() { + return extraClasses; + } + } diff --git a/src/de/dhbwstuttgart/core/MyCompiler.java b/src/de/dhbwstuttgart/core/MyCompiler.java index 7b72e79b..60ea5bb2 100755 --- a/src/de/dhbwstuttgart/core/MyCompiler.java +++ b/src/de/dhbwstuttgart/core/MyCompiler.java @@ -1,9 +1,6 @@ -// ino.module.MyCompiler.8569.package + package de.dhbwstuttgart.core; -// ino.end - -// ino.module.MyCompiler.8569.import import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; @@ -48,67 +45,28 @@ import de.dhbwstuttgart.typeinference.exceptions.ParserError; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; - -// ino.class.MyCompiler.21258.declaration -public class MyCompiler implements MyCompilerAPI -// ino.end -// ino.class.MyCompiler.21258.body -{ - // ino.attribute.NO_LINENUMBER.21262.decldescription type=line +public class MyCompiler implements MyCompilerAPI{ // PL: Der Zusammenhang zwischen paralist und vParaOrg muesste // noch geklaert werden 05-01-07 - // ino.end - // ino.attribute.NO_LINENUMBER.21262.declaration public static final int NO_LINENUMBER = -1; - // ino.end - - // ino.attribute.codegenlog.21265.decldescription type=line - // Logger - // ino.end - // ino.attribute.codegenlog.21265.declaration - //protected static Logger codegenlog = Logger.getLogger("codegen"); - // ino.end - // ino.attribute.inferencelog.21268.declaration + protected static Logger inferencelog = Logger.getLogger(MyCompiler.class.getName()); - // ino.end - // ino.attribute.parserlog.21271.declaration - //protected static Logger parserlog = Logger.getLogger("parser"); - // ino.end - - // ino.attribute.OutputDir.21274.declaration + protected String OutputDir = ""; - // ino.end + public Menge testPair = null; - // ino.attribute.m_AbstractSyntaxTree.21280.decldescription type=javadoc - /** - * Der abstrake Syntaxbaum - *
Autor: J�rg B�uerle - */ - // ino.end - // ino.attribute.m_AbstractSyntaxTree.21280.declaration - private Menge m_AbstractSyntaxTree = new Menge(); - // ino.end - - // ino.method.MyCompiler.21283.defdescription type=javadoc /** * Author: J�rg B�uerle
* Der private Konstruktor. Es soll von au�en kein Compiler angelegt werden * k�nnen, sondern nur eine API zur Verf�gung gestellt werden. * @param logger Konfiguration für Debug Ausgabe TODO */ - // ino.end - // ino.method.MyCompiler.21283.definition - private MyCompiler() - // ino.end - // ino.method.MyCompiler.21283.body - { + private MyCompiler(){ this.init(); } - // ino.end - - // ino.method.getAPI.21286.defdescription type=javadoc + /** * Author: Jörg Bäuerle
* Stellt eine neue Instanz der CompilerAPI zur Verf�gung. @@ -116,116 +74,11 @@ public class MyCompiler implements MyCompilerAPI * um eine Quellcode-Datei zu kompilieren. * @return Die Compiler-API */ - // ino.end - // ino.method.getAPI.21286.definition - public static MyCompilerAPI getAPI(LoggerConfiguration loggerConfig) - // ino.end - // ino.method.getAPI.21286.body - { + public static MyCompilerAPI getAPI(LoggerConfiguration loggerConfig){ Logger.setStandardConfiguration(loggerConfig); return new MyCompiler(); } - // ino.end - public Menge testPair = null; - - // ino.method.wandleGeneric2RefType.21289.defdescription type=javadoc - /** - * Author: Thomas Ott
- * Ersetzt in der Superklassenparameterliste einer Klasse, diejenigen - * GenericTypeVars, zu denen es eine Klasse gibt, die gleich hei�t. - * Beim Parsen werden n�mlich vom Jay nur GenericTypeVars erzeugt und keine - * RefTypes. Dies wird durch diese Methode nachgeholt.
- * Bsp.: class JoergsTolleKlasse extends MartinsSuperklasse - *
Wie man an diesem Beispiel sieht, kann nur eine Superklasse instantiierte - * Typvariablen zwischen den eckigen Klammern stehen haben, nicht jedoch die - * abgeleitete Klasse. - * - * @param className Klassenname der aktuell betrachteten Klasse - * @param Parameter Parameter der Superklasse - * @param KlassenVektor - - // ino.end - // ino.method.wandleGeneric2RefType.21289.definition - public static void wandleGeneric2RefType(Menge Parameter, Menge KlassenVektor ) - // ino.end - // ino.method.wandleGeneric2RefType.21289.body - { - //wandleGeneric2RefType SOLLTE NICHT NUR FUER LISTEN - //VON TYPEN FUNKTIONIEREN PL 05-01-19 - // otth: GenericTypeVar in Superklassenparameterlisten in RefTypes mit Parameterlsite = NULL umwandeln, - // falls: f�r GenericTypeVar existiert eine gleichnamige Klasse - if(Parameter == null) return; - - for( int i = 0; i < Parameter.size(); i++) - { - Type TempParameter = Parameter.elementAt(i); - inferencelog.debug("Nr. des Parameters: " + i); - - // an dieser Stelle: Parametername - if ( TempParameter instanceof GenericTypeVar) - { - inferencelog.debug("Generic, WANDLE: " + TempParameter.getName()); - // existiert f�r GenericTypeVar eine deklarierte Klasse - for( int k = 0; k < KlassenVektor.size(); k++) - { - if( KlassenVektor.elementAt(k).getSimpleName().equals(TempParameter.getSimpleName()) ) - { - // Klasse existiert, darf aber keine Parameterliste in der Definition haben - if( KlassenVektor.elementAt(k).get_ParaList().size() == 0 ) - { - RefType RNeu = new RefType( TempParameter.getName().toString(), null,TempParameter.getOffset()); - inferencelog.debug( "Vorher: " + Parameter ); - // i-te Stelle ersetzen - Parameter.set( i, RNeu ); - inferencelog.debug( "GenericTypeVar " + TempParameter.getName() + " umwandeln..." ); - inferencelog.debug( "Nachher: " + Parameter ); - } - else - { - //parserlog.error( "SEMANTIK-CHECK-FEHLER: Parameter " + TempParameter.getName() + " muss weitere Parameter besitzen (laut Klassendefinition)" ); - //FIXME Throw exception instead of simple exit - System.exit( 1 ); - } - } - else { - inferencelog.debug("Ist echter Generic, wird nicht ersetzt!"); - } - } // end for - } // end if - else - { - inferencelog.debug("Nicht Generic, WANDLE nicht: " + TempParameter.getName()); - // RefType --> u.U. rekursiv weiterwandeln - if(TempParameter instanceof RefType) - { - RefType R = (RefType)TempParameter; - if( R.get_ParaList() != null ) - wandleGeneric2RefType( R.get_ParaList(), KlassenVektor ); - } - else if(TempParameter instanceof ITypeContainer) - { - Type T = ((ITypeContainer)TempParameter).getContainedType(); - if(T instanceof RefType) - { - RefType R = (RefType)T; - if( R.get_ParaList() != null ) - wandleGeneric2RefType( R.get_ParaList(), KlassenVektor ); - } - } - else - { - inferencelog.error("Internal Error"); - System.exit( 1 ); - } - } - } //end for - } //end wandleGeneric2RefType - // ino.end - */ - - - // ino.method.parse.21292.defdescription type=javadoc /** * Parst den Quellcode und baut den abstrakten Syntaxbaum auf. Danach wird * automatisch der von Thomas Ott implementierte Algorithmus @@ -236,246 +89,19 @@ public class MyCompiler implements MyCompilerAPI * @throws IOException * @throws JavaParser.yyException */ - // ino.end - // ino.method.parse.21292.definition - private void parse_backup(Reader reader) - throws IOException, JavaParser.yyException - // ino.end - // ino.method.parse.21292.body - { - /* - parserlog.info("#########################################"); - parserlog.info("# Parsen - START #"); - parserlog.info("#########################################\n"); - - ////////////////////////////////////// - // Alte Daten l�schen: - ////////////////////////////////////// - m_AbstractSyntaxTree = null; - - ////////////////////////////////////// - // Scanner und Parser erzeugen: - ////////////////////////////////////// - Scanner scanner = new Scanner(reader); - JavaParser parser = new JavaParser(); - - ////////////////////////////////////// - // Parsen ==> Ergebnis: srcFile = Abstrakter Syntaxbaum - ////////////////////////////////////// - SourceFile srcFile = (SourceFile) parser.yyparse( scanner ); - this.testPair = parser.testPair; - - parserlog.info( "Parsen war erfolgreich!\n"); - - //PL 05-07-31 verschoben nach SourceFile.java in Methode typeReconstruction - // otth: TypePlaceholders in Superklassenparameterlisten in RefTypes mit Parameterlsite = NULL umwandeln, - // falls: f�r TypePlaceholder existiert eine gleichnamige Klasse - // Superklasse suchen - //for( int i = 0; i < srcFile.KlassenVektor.size(); i++ ) - //{ - //Class tempKlasse = (Class)srcFile.KlassenVektor.elementAt( i ); - - //PL 05-07-30 ausgetauscht um alle Typedeklarationen zu wandeln - // if( tempKlasse.superclassid != null && tempKlasse.get_ParaList() != null ) - // { - // // aktuelle Klasse hat Superklasse und Parameter - // Menge Parameter = tempKlasse.superclassid.get_ParaList(); - // this.wandleGeneric2RefType(Parameter, srcFile.KlassenVektor ); - // } - //wandleGeneric2RefType(tempKlasse.getContainedTypes(), srcFile.KlassenVektor ); - - //} - - // otth: echte Konstruktoren von Methodendeklarationen ohne Typen unterscheiden - if ( srcFile != null ) - { - Class tempKlasse = null; - ClassBody tempKlassBody = null; - Menge tempMengeFieldDecl; - mycompiler.myclass.Field tempFieldDecl = null; - String strKlasse; - - for( int i = 0; i < srcFile.KlassenVektor.size(); i++ ) - { - - // Unterscheidung zwischen Class und Interfaces - if (srcFile.KlassenVektor.elementAt(i) instanceof Class) { - tempKlasse = (Class)srcFile.KlassenVektor.elementAt( i ); - tempKlassBody = tempKlasse.get_ClassBody(); - } else { - tempKlasse = null; - tempKlassBody = null; - } - - if ( tempKlassBody != null ) - { - strKlasse = tempKlasse.getName(); - parserlog.debug("T->Felddeklarationen f�r die Klasse:" + strKlasse); - parserlog.debug( "------------------------------------"); - - // Schleife �ber alle fielddeclarations - tempMengeFieldDecl = tempKlassBody.getFields(); - for( int k = 0; k < tempMengeFieldDecl.size(); k++ ) - { - tempFieldDecl = tempMengeFieldDecl.elementAt(k); - if( tempFieldDecl instanceof Constructor ) - { - //parserlog.debug("T->Konstruktor: " + ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name() + " - ReturnType: " + tempFieldDecl.getTypeName()); - - // pr�fen, ob Construktorname == Klassenname - falls nein: Construktor in Methode umwandeln !!! - String strConstName = ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name(); // Konstruktorname - if ( !strConstName.equals( strKlasse ) ) - { - // Element k durch neues ersetzen! - - Method Methode = new Method(); - Method Konstruktor = (Constructor)tempFieldDecl; - - // Elementweise vom Konstruktor in die Methode kopieren - Methode.set_Block( Konstruktor.get_Block() ); - Methode.setParameterList( Konstruktor.getParameterList() ); - Methode.set_ExceptionList( Konstruktor.get_ExceptionList() ); - Methode.setReturnType( Konstruktor.getReturnType() ); - Methode.setDeclIdMenge( Konstruktor.getDeclIdMenge() ); - // types_in_parameterlist wird wohl erst sp�ter und intern gef�llt - - // R�ckgabetyp = Objekt der Klasse 'TypePlaceholder' - - // #JB# 31.03.2005 - // ########################################################### - Methode.setReturnType(TypePlaceholder.fresh(Methode)); - //Methode.setReturnType( new TypePlaceholder("###NEU###") ); - // ########################################################### - - // Element an der Position k durch neues ersetzen! - tempMengeFieldDecl.setElementAt( Methode, k ); - } - } - if( tempFieldDecl instanceof Method && !(tempFieldDecl instanceof Constructor) ) - { - //parserlog.debug("T->Methode: " + ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name() + " - ReturnType: " + tempFieldDecl.getTypeName()); - } - - } - - // Debugg-Infos - parserlog.debug(""); - parserlog.debug("T->NEUE Felddeklarationen f�r die Klasse:" + strKlasse); - parserlog.debug( "-----------------------------------------"); - for( int k = 0; k < tempMengeFieldDecl.size(); k++ ) - { - tempFieldDecl = tempMengeFieldDecl.elementAt(k); - //parserlog.debug("T->" + ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name() + " - Typ: " + tempFieldDecl + " - ReturnType: " + tempFieldDecl.getTypeName()); - } - - // otth: TypePlaceholders durchnummerieren mit A, B, ... - parserlog.debug(""); - parserlog.debug(""); - parserlog.debug("Suche TypePlaceholders in den FieldDecls:"); - parserlog.debug( "------------------------------------------"); - - ParameterList tempParameterList; - Menge tempVFktPara; - FormalParameter tempFP; - Method tempMethod; - Type tempReturn; - Type tempType; - for( int k = 0; k < tempMengeFieldDecl.size(); k++ ) - { - tempFieldDecl = tempMengeFieldDecl.elementAt(k); - tempMethod = null; - - if( tempFieldDecl instanceof Method ) - { - tempMethod = (Method)tempFieldDecl; - tempReturn = tempMethod.getReturnType(); - - // Funktionen ohne definierten R�ckgabetyp suchen!!! - if( tempReturn instanceof TypePlaceholder ) - { - // Methode mit nicht-definiertem R�ckgabetyp gefunden! - - // #JB# 31.03.2005 - // ########################################################### - // Wird bereits �ber fresh() gemacht!! - //tempReturn.setName( TypePlaceholder.makeNewName() ); - // ########################################################### - parserlog.debug(""); - parserlog.debug("Methode ohne Rueckgabetyp: " + tempMethod.get_Method_Name() + " --> " + tempReturn.getName()); - } - else - { - parserlog.debug(""); - parserlog.debug("Methode mit Rueckgabetyp / Konstruktor: " + tempMethod.get_Method_Name()); - } - - // Methoden-Funktionsparameter durchsuchen - tempParameterList = tempMethod.getParameterList(); - if ( tempParameterList != null ) - { - tempVFktPara = tempParameterList.sc_get_Formalparalist(); - for( int l = 0; l < tempVFktPara.size(); l++ ) - { - tempFP = tempVFktPara.elementAt(l); - tempType = tempFP.getType(); - if( tempType instanceof TypePlaceholder ) - { - if( tempType != null ) - { - // neuer Name berechnen - - // #JB# 31.03.2005 - // ########################################################### - // Wird bereits �ber fresh() gemacht!! - //tempType.setName( TypePlaceholder.makeNewName() ); - // ########################################################### - } - } - parserlog.debug(""); - parserlog.debug(" Parameter: " + tempFP.get_Name() + " --> " + tempType.getName()); - } - } - else - { - parserlog.debug(""); - parserlog.debug(" Methode hat keine Parameter!"); - } - } - } - } //end if - } //end for - } //end if - m_AbstractSyntaxTree = srcFile; - - parserlog.info("#########################################"); - parserlog.info("# Parsen - ENDE #"); - parserlog.info("#########################################\n"); - */ - } // end Methode parse() - // ino.end - + private void parse_backup(Reader reader) throws IOException, JavaParser.yyException{ + + } ///////////////////////////////////////////////////////////////////////////////////////////////// // Implementierte API-Methoden: ///////////////////////////////////////////////////////////////////////////////////////////////// - // ino.method.init.21295.defdescription type=javadoc /** * Author: J�rg B�uerle
* Initialisiert den Compiler */ - // ino.end - // ino.method.init.21295.definition - public void init() - // ino.end - // ino.method.init.21295.body - { + public void init(){ TypePlaceholder.deleteRegistry(); - - // Log4J fuer die Ausgabe vorbereiten - //DOMConfigurator.configure("log4j.xml"); } - // ino.end - - // ino.method.parse.21298.defdescription type=javadoc /** * Author: J�rg B�uerle
* Ruft die Parse-Methode. @@ -484,22 +110,13 @@ public class MyCompiler implements MyCompilerAPI * @throws IOException Wenn was schief l�uft. * @throws JavaParser.yyException Wenn ein Fehler beim Parsen auftritt. */ - // ino.end - // ino.method.parse.21298.definition - public SourceFile parse(File file) - throws FileNotFoundException, IOException, JavaParser.yyException - // ino.end - // ino.method.parse.21298.body - { + public SourceFile parse(File file) throws FileNotFoundException, IOException, JavaParser.yyException{ FileReader fr = new FileReader(file); SourceFile ret = this.parse2SyntaxTree(fr); - this.m_AbstractSyntaxTree.add(ret); fr.close(); return ret; } - // ino.end - - // ino.method.typeReconstruction.21304.defdescription type=javadoc + /** * Author: J�rg B�uerle
* Ruft den Typrekonstruktionsalgorithmus auf. @@ -508,16 +125,7 @@ public class MyCompiler implements MyCompilerAPI * ist. @throws CTypeReconstructionException Wenn ein Fehler bei der * Typrekonstruktion auftritt. */ - // ino.end - // ino.method.typeReconstruction.21304.definition - public Menge typeReconstruction() - throws NullPointerException, CTypeReconstructionException - // ino.end - // ino.method.typeReconstruction.21304.body - { - if(m_AbstractSyntaxTree==null){ - throw new NullPointerException("Es wurde noch kein Abstrakter Syntaxbaum erstellt!"); - } + public Menge typeReconstruction(Menge m_AbstractSyntaxTree) throws NullPointerException, CTypeReconstructionException{ inferencelog.info("##########################################", Section.TYPEINFERENCE); inferencelog.info("# TypeReconstruction-Algorithmus - START #", Section.TYPEINFERENCE); inferencelog.info("##########################################\n", Section.TYPEINFERENCE); @@ -535,7 +143,6 @@ public class MyCompiler implements MyCompilerAPI return result; } - // ino.end /** * Erstellt die FunN-Assumptions @@ -559,45 +166,13 @@ public class MyCompiler implements MyCompilerAPI return ret; } - - /** - * Author: J�rg B�uerle
- * Generiert den Bytecode und das Class-File f�r den Syntaxbaum. - * @throws NullPointerException Wenn noch kein abstrakter Syntaxbaum vorhanden - * ist. - - @Override - public Menge codeGeneration(ResultSet result) - throws NullPointerException, JVMCodeException - { - if(m_AbstractSyntaxTree==null){ - throw new NullPointerException("Es wurde noch kein Abstrakter Syntaxbaum erstellt!"); - } - codegenlog.info("Beginn der Codegenerierung ..."); - - Menge ret = new Menge(); - - for(SourceFile sf : m_AbstractSyntaxTree){ - ret.addAll(sf.codegen(result)); - } - - codegenlog.info("Codegenerierung beendet!"); - return ret; - }*/ - - // ino.method.main.21313.defdescription type=javadoc /** * Die Main-Funktion, �ber die der Compiler auch per Konsole gestartet * werden kann. * @param args Klassendatei */ - // ino.end - // ino.method.main.21313.definition - public static void main(String[] args) - // ino.end - // ino.method.main.21313.body - { + public static void main(String[] args){ MyCompilerAPI compiler = MyCompiler.getAPI(new LoggerConfiguration()); // Hier koennten ggf. Aenderungen der Ausgabeeinstellungen @@ -617,33 +192,9 @@ public class MyCompiler implements MyCompilerAPI System.err.println(e); System.exit(0); } - - ///////////////////////// - // Semantik-Check: - ///////////////////////// - // try { - // compiler.semanticCheck(); - // } catch (NullPointerException e) { - // System.out.println("Fehler beim Aufrufen des Semantik-Checks:"); - // System.out.println(e); - // System.exit(0); - // } catch (SCException e) { - // e.fehlerausgabe(); - // System.exit(0); - // } - - ///////////////////////// - // Code-Generierung: - ///////////////////////// - //compiler.codeGeneration(); - } - // ino.end + } - // ino.method.setOutputDir.21316.definition - public void setOutputDir(String dir) - // ino.end - // ino.method.setOutputDir.21316.body - { + public void setOutputDir(String dir){ char c = dir.charAt(dir.length()-1); if (c != '/' & c != '\\') dir = dir + "/"; OutputDir = dir; @@ -652,75 +203,11 @@ public class MyCompiler implements MyCompilerAPI File f = new File(dir); f.mkdirs(); } - // ino.end - // ino.method.getOutputDir.21319.definition - public String getOutputDir() - // ino.end - // ino.method.getOutputDir.21319.body - { + public String getOutputDir(){ return OutputDir; } - // ino.end -/* - // ino.method.getFullyQualifiedNameFromClassname.21322.definition - public static String getFullyQualifiedNameFromClassname(String typ, ImportDeclarations declarations) - // ino.end - // ino.method.getFullyQualifiedNameFromClassname.21322.body - { - String ret=null; - // Es ist kein FullyQualifiedName => In den Imports die Klasse suchen - for(int j=0;j de.dhbwstuttgart.typeinference.Menge x; - * @param containedTypes Alle Typen, die die Klasse beinhaltet - * @param name Alle Klassen, die es in den BasicAssumptions und im - * AbstractSyntaxTree gibt @param declarations Alle Import-Declarations - - // ino.end - // ino.method.makeRefTypesFullyQualified.21325.definition - public static void makeRefTypesFullyQualified(Menge containedTypes, ImportDeclarations declarations) - // ino.end - // ino.method.makeRefTypesFullyQualified.21325.body - { - // HOTI 8.5.06 Anhand der ContainedTypes alle Variablen aendern - for( int i = 0; i < containedTypes.size(); i++) - { - Type tempParameter = (Type)(containedTypes.elementAt(i)); - // Nat�rlich nur RefTypes updaten - if(tempParameter instanceof RefType){ - RefType typ=(RefType)tempParameter; - UsedId fullyQualifiedName=UsedId.createFromQualifiedName(typ.getTypeName(),typ.getOffset()); - // Kein FullyQualifiedName - if(fullyQualifiedName.name.size()==1){ - String newType=getFullyQualifiedNameFromClassname(typ.getSimpleName(),declarations); - if(newType!=null){ - typ.setName(newType); - } - - } - if(typ.get_ParaList()!=null){ - makeRefTypesFullyQualified(typ.get_ParaList(),declarations); - } - } - } - } - // ino.end - */ /** * @author Arne Lüdtke * Ersetzt alle GTVs durch TPHs mit gleichem Namen. Arbeitet Rekursiv. @@ -790,8 +277,10 @@ public class MyCompiler implements MyCompilerAPI /** * Diese Funktion nimmt einen Menge von Dateinamen. Alle diese Dateien werden zu einem SyntaxBaum geparst. + * @return */ - public void parse(Menge filenames) throws ParserError { + public Menge parse(Menge filenames) throws ParserError { + Menge m_AbstractSyntaxTree = new Menge(); for(String filename : filenames){ StringBuffer fileData = new StringBuffer(); @@ -816,58 +305,25 @@ public class MyCompiler implements MyCompilerAPI StringReader srcreader = new StringReader(fileData.toString()); //Den aus der Datei ausgelesenen Quellcode zu einem Syntaxbaum parsen: - this.m_AbstractSyntaxTree.add(parse2SyntaxTree(srcreader)); // Alle Dateien nacheinander hintereinander anhängen... - - } - /* - String gesamterSrc = ""; - //Hier werden alle übergebenen Dateinamen abgearbeitet: - for(String filename : filenames){ - try { - StringBuffer fileData = new StringBuffer(); - BufferedReader reader = new BufferedReader( - new FileReader(filename)); - char[] buf = new char[1024]; - int numRead=0; - while((numRead=reader.read(buf)) != -1){ - String readData = String.valueOf(buf, 0, numRead); - fileData.append(readData); - } - reader.close(); - gesamterSrc += fileData.toString() + "\n"; // Alle Dateien nacheinander hintereinander anhängen... - } catch (Exception e) { - e.printStackTrace(); - throw new TypinferenzException("Die übergebenen Dateien konnten nicht zum Parsen eingelesen werden."); - } + m_AbstractSyntaxTree.add(parse2SyntaxTree(srcreader)); // Alle Dateien nacheinander hintereinander anhängen... } - try { - // und anschließend zum Parsen übergeben. - this.parse(gesamterSrc.toString()); - } catch (Exception e) { - e.printStackTrace(); - //throw new TypinferenzException("Fehler beim Parsen"); - } - - */ + return m_AbstractSyntaxTree; } @Override public SourceFile parse(String sourceCode) { - SourceFile ret = this.parse2SyntaxTree(new StringReader(sourceCode)); - this.m_AbstractSyntaxTree.add(ret); - return ret; + return parse2SyntaxTree(new StringReader(sourceCode)); } @Override - public Menge> generateBytecode(TypeinferenceResultSet typeinferenceResult) { + public Menge generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult) { //SourceFile parsedFile = this.m_AbstractSyntaxTree.firstElement(); //Class parsedClass = parsedFile.KlassenVektor.firstElement(); - Menge> ret = new Menge<>(); - for(SourceFile sf : this.m_AbstractSyntaxTree){ + Menge ret = new Menge<>(); + for(SourceFile sf : m_AbstractSyntaxTree){ ret.addAll(sf.generateBytecode(typeinferenceResult)); } return ret; } } -// ino.end diff --git a/src/de/dhbwstuttgart/core/MyCompilerAPI.java b/src/de/dhbwstuttgart/core/MyCompilerAPI.java index 78860de4..f0eac42f 100755 --- a/src/de/dhbwstuttgart/core/MyCompilerAPI.java +++ b/src/de/dhbwstuttgart/core/MyCompilerAPI.java @@ -85,8 +85,7 @@ public interface MyCompilerAPI */ // ino.end // ino.method.typeReconstruction.21340.declaration - public Menge typeReconstruction() - throws NullPointerException, TypeinferenceException; + public Menge typeReconstruction(Menge m_AbstractSyntaxTree) throws NullPointerException, CTypeReconstructionException; // ino.end // ino.method.setOutputDir.21349.decldescription type=javadoc @@ -112,8 +111,9 @@ public interface MyCompilerAPI /** * Parst zusammenhängende JavaKlassen in verschiedenen Dateien. * @param filenames - Eine Liste von Quellcodedateien, welche gseparst werden sollen + * @return */ - public void parse(Menge filenames) throws ParserError; + public Menge parse(Menge filenames) throws ParserError; /** * Parst den SourceCode einer Datei. @@ -127,6 +127,6 @@ public interface MyCompilerAPI * Dafür müssen die Schritte Parsen und typeReconstruction ausgeführt werden. * @return */ - public Menge> generateBytecode(TypeinferenceResultSet rs); + public Menge generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult); } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 2313f22c..bfa79b33 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -75,7 +75,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I * @param resultSet - Fehlende Typen im Syntaxbaum werden nach diesem ResultSet aufgelöst * @return */ - public Menge genByteCode(TypeinferenceResultSet resultSet) { + public ByteCodeResult genByteCode(TypeinferenceResultSet resultSet) { InstructionFactory _factory; DHBWConstantPoolGen _cp; ClassGenerator _cg; @@ -113,12 +113,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I c.genByteCode(_cg, fieldInitializations); } - ByteCodeResult code = new ByteCodeResult(_cg); - results.add(code); - - results.addAll(getGenericClasses()); - - return results; + return new ByteCodeResult(_cg); } private Menge superif = new Menge(); @@ -1032,6 +1027,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I return false; } + /* private Collection getGenericClasses() { Collection results = new Menge<>(); @@ -1067,7 +1063,9 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I return results; } + */ + /* private Menge generateGenericClass(String name, Class superClass){ //TODO: bytecode -- Generics hinzufügen //Type superClassType = superClass.getType(); @@ -1080,6 +1078,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I //TODO: bytecode -- alle Konstruktoren generieren Block konstruktorBlock = new Block(); + konstruktorBlock.setType(new de.dhbwstuttgart.syntaxtree.type.Void(konstruktorBlock, 0)); konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); standardKonstruktor.parserPostProcessing(generatedClass); @@ -1088,5 +1087,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I return generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())); } + */ } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 22c45fd7..56222f1a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -1834,8 +1834,8 @@ public class SourceFile * Bisher wird nur der Bytecode der Klassen generiert. Nicht der Interfaces. * @return */ - public Menge> generateBytecode(TypeinferenceResultSet rs) { - Menge> ret = new Menge<>(); + public Menge generateBytecode(TypeinferenceResultSet rs) { + Menge ret = new Menge<>(); for(Class cl : this.KlassenVektor){ ret.add(cl.genByteCode(rs)); } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java new file mode 100644 index 00000000..29940082 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -0,0 +1,42 @@ +package de.dhbwstuttgart.syntaxtree.factory; + +import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.misc.DeclId; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; + +public class ASTFactory { + public static Method createMethod(String name, Block block, Class parent) { + Method method = new Method(0); + DeclId DImethod = new DeclId(); + //DImethod.set_Name(withSignature); //todo: bytecode signatur? + method.set_DeclId(DImethod); + method.set_Block(block); + method.parserPostProcessing(parent); + return method; + } + + public static Method createEmptyMethod(String withSignature, Class parent) { + return ASTFactory.createMethod("", new Block(), parent); + } + + public static Constructor createEmptyConstructor(Class parent){ + Block block = new Block(); + block.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0)); + block.statements.add(new SuperCall(block)); + + return ASTFactory.createConstructor(parent, block); + } + + public static Constructor createConstructor(Class superClass, Block block){ + return new Constructor(ASTFactory.createMethod("", block, superClass), superClass); + } + + public static ClassGenerator createClass(String className, Class superClass) { + // TODO Auto-generated method stub + return null; + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 56caea6c..e044c065 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -15,6 +15,7 @@ import de.dhbwstuttgart.myexception.SCException; import de.dhbwstuttgart.parser.JavaClassName; import de.dhbwstuttgart.syntaxtree.Class; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.misc.UsedId; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; @@ -834,6 +835,10 @@ public class RefType extends ObjectType implements IMatchable typeSignature = typeSignature.substring(0, typeSignature.length()-1); return typeSignature+paramString+";"; */ + + //TODO: bytecode woher bekommt ich die parent klasse + cg.addExtraClass(ASTFactory.createClass(getCombinedType(cg), null)); + return "L"+getCombinedType(cg)+";"; } @@ -845,7 +850,7 @@ public class RefType extends ObjectType implements IMatchable sb.append(this.get_Name().replace(".", "%")); sb.append("%%"); for(Type type: parameter){ - sb.append(((RefType) type).getCombinedType(cg)); + sb.append(((RefType) type).getCombinedType(cg).replace(".", "%")); sb.append("%"); } }else{ diff --git a/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java b/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java index c036b5da..8ed910ba 100644 --- a/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java +++ b/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java @@ -2,23 +2,26 @@ package de.dhbwstuttgart.typeinference; import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.bcel6.generic.ClassGen; import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; public class ByteCodeResult{ - private ClassGen byteCode; + private ClassGenerator byteCode; //TODO: unresolvedTPHs entfernen. BROKEN! private Menge unresolvedTPHs = new Menge(); - public ByteCodeResult(ClassGen byteCode){ + public ByteCodeResult(ClassGenerator byteCode){ this.byteCode = byteCode; } - public ClassGen getByteCode(){ + public ClassGenerator getByteCode(){ return byteCode; } diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java index 819d310b..5dbf463c 100755 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java @@ -119,8 +119,7 @@ public class TypeinferenceResultSet * Dabei wird die codegen-Methode der inferierten Klasse mit diesem ResultSet aufgerufen. */ public ByteCodeResult codegen(){ - ByteCodeResult res = this.ownerOfResultSet.genByteCode(this).firstElement(); - return res; + return this.ownerOfResultSet.genByteCode(this); } } diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index a5d958f0..46693780 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -2,12 +2,14 @@ package bytecode; import java.io.File; import java.io.IOException; +import java.util.Map; import org.apache.commons.bcel6.classfile.JavaClass; import com.google.common.io.Files; import junit.framework.TestCase; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.core.MyCompiler; import de.dhbwstuttgart.core.MyCompilerAPI; import de.dhbwstuttgart.logger.Logger; @@ -15,6 +17,7 @@ import de.dhbwstuttgart.logger.LoggerConfiguration; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.logger.Timewatch; import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.ByteCodeResult; import de.dhbwstuttgart.typeinference.Menge; @@ -24,18 +27,28 @@ public class SingleClassTester { LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); try { - compiler.parse(new File(inputFile)); - Menge> bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); + SourceFile sf = compiler.parse(new File(inputFile)); + + Menge sourceFiles = new Menge<>(); + sourceFiles.add(sf); + + Menge bytecode = compiler.generateBytecode(sourceFiles, compiler.typeReconstruction(sourceFiles).firstElement()); //System.out.println(bytecode); - for(ByteCodeResult result: bytecode.firstElement()){ - JavaClass javaClass = result.getByteCode().getJavaClass(); - - Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN); - - javaClass.dump(new File(outputDirectory+javaClass.getClassName()+".class")); + 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")); } + Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN); + + + } catch (IOException | yyException e) { Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); e.printStackTrace(); From 422f3b41446ef074df27114f14f14bc0acc5ec7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 23 Oct 2015 10:43:48 +0200 Subject: [PATCH 10/13] ASTFactory createClass --- .../dhbwstuttgart/bytecode/ClassGenerator.java | 5 ++--- .../syntaxtree/factory/ASTFactory.java | 16 ++++++++++++++-- .../dhbwstuttgart/syntaxtree/type/RefType.java | 5 ++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java index 514b1821..be42c2ba 100644 --- a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java @@ -32,10 +32,9 @@ public class ClassGenerator extends ClassGen{ private Menge usedTPHs = new Menge<>(); - private Map extraClasses = new HashMap(); + private Map extraClasses = new HashMap<>(); - public ClassGenerator(String name, Type superClass, String string, - short accPublic, String[] strings, TypeinferenceResultSet resultSet) { + public ClassGenerator(String name, Type superClass, String string, short accPublic, String[] strings, TypeinferenceResultSet resultSet) { super(name,superClass.get_Name(),string,accPublic,strings, new DHBWConstantPoolGen()); this.tiResult = resultSet; this.superClass = superClass; diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 29940082..fca21942 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -7,6 +7,9 @@ import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; public class ASTFactory { public static Method createMethod(String name, Block block, Class parent) { @@ -36,7 +39,16 @@ public class ASTFactory { } public static ClassGenerator createClass(String className, Class superClass) { - // TODO Auto-generated method stub - return null; + // TODO bytecode createClass + Class generatedClass = new Class(className, 0); + Block konstruktorBlock = new Block(); + konstruktorBlock.setType(new de.dhbwstuttgart.syntaxtree.type.Void(konstruktorBlock, 0)); + konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, className, superClass), superClass); + standardKonstruktor.parserPostProcessing(generatedClass); + + generatedClass.addField(standardKonstruktor); + + return generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode(); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index e044c065..f66c8b0c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -837,7 +837,10 @@ public class RefType extends ObjectType implements IMatchable */ //TODO: bytecode woher bekommt ich die parent klasse - cg.addExtraClass(ASTFactory.createClass(getCombinedType(cg), null)); + String combinedType = getCombinedType(cg); + if(!combinedType.equals(get_Name())){ + cg.addExtraClass(ASTFactory.createClass(getCombinedType(cg), getParentClass())); + } return "L"+getCombinedType(cg)+";"; } From fab73a4d62255e8d6214d3bc5c1672e2b6923980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 23 Oct 2015 10:46:55 +0200 Subject: [PATCH 11/13] =?UTF-8?q?ASTFactory.createClass=20gibt=20eine=20Cl?= =?UTF-8?q?ass=20statt=20ClassGenerator=20zur=C3=BCck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/syntaxtree/factory/ASTFactory.java | 4 ++-- src/de/dhbwstuttgart/syntaxtree/type/RefType.java | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index fca21942..a129c8d2 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -38,7 +38,7 @@ public class ASTFactory { return new Constructor(ASTFactory.createMethod("", block, superClass), superClass); } - public static ClassGenerator createClass(String className, Class superClass) { + public static Class createClass(String className, Class superClass) { // TODO bytecode createClass Class generatedClass = new Class(className, 0); Block konstruktorBlock = new Block(); @@ -49,6 +49,6 @@ public class ASTFactory { generatedClass.addField(standardKonstruktor); - return generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode(); + return generatedClass; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index f66c8b0c..ef49744d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -14,12 +14,17 @@ import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.myexception.SCException; import de.dhbwstuttgart.parser.JavaClassName; import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.misc.UsedId; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeInsertable; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.unify.CSubstitutionGenVar; @@ -839,7 +844,10 @@ public class RefType extends ObjectType implements IMatchable //TODO: bytecode woher bekommt ich die parent klasse String combinedType = getCombinedType(cg); if(!combinedType.equals(get_Name())){ - cg.addExtraClass(ASTFactory.createClass(getCombinedType(cg), getParentClass())); + // TODO bytecode createClass + Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getParentClass()); + + cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode()); } return "L"+getCombinedType(cg)+";"; From 77c06225a6b3cb8e83fe381b16b0c22bdcbc5c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 23 Oct 2015 10:56:56 +0200 Subject: [PATCH 12/13] Constructor ist von Type Void --- .../syntaxtree/factory/ASTFactory.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index a129c8d2..557ff35b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -13,9 +13,11 @@ import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; public class ASTFactory { public static Method createMethod(String name, Block block, Class parent) { + block.parserPostProcessing(parent); + Method method = new Method(0); DeclId DImethod = new DeclId(); - //DImethod.set_Name(withSignature); //todo: bytecode signatur? + DImethod.set_Name(name); method.set_DeclId(DImethod); method.set_Block(block); method.parserPostProcessing(parent); @@ -23,7 +25,7 @@ public class ASTFactory { } public static Method createEmptyMethod(String withSignature, Class parent) { - return ASTFactory.createMethod("", new Block(), parent); + return ASTFactory.createMethod(withSignature, new Block(), parent); } public static Constructor createEmptyConstructor(Class parent){ @@ -35,19 +37,19 @@ public class ASTFactory { } public static Constructor createConstructor(Class superClass, Block block){ - return new Constructor(ASTFactory.createMethod("", block, superClass), superClass); + block.parserPostProcessing(superClass); + + Method method = ASTFactory.createMethod("", block, superClass); + method.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0)); + + return new Constructor(method, superClass); } public static Class createClass(String className, Class superClass) { // TODO bytecode createClass - Class generatedClass = new Class(className, 0); - Block konstruktorBlock = new Block(); - konstruktorBlock.setType(new de.dhbwstuttgart.syntaxtree.type.Void(konstruktorBlock, 0)); - konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); - Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, className, superClass), superClass); - standardKonstruktor.parserPostProcessing(generatedClass); - - generatedClass.addField(standardKonstruktor); + Class generatedClass = new Class(className, 0); + generatedClass.addField(ASTFactory.createEmptyConstructor(superClass)); + generatedClass.parserPostProcessing(superClass); return generatedClass; } From f16171a8f6464097fadd73e3b0bbdd415fd5f5ec Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 23 Oct 2015 11:48:35 +0200 Subject: [PATCH 13/13] =?UTF-8?q?Object=20zu=20java.lang.Object=20als=20Su?= =?UTF-8?q?perklasse=20von=20Class=20im=20Standardkonstruktor=20=C3=A4nder?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Class.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index bfa79b33..debcf73e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -224,7 +224,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I } this.offset = offset; if(!name.equals("Object"))//Alle Klassen außer Object erben von Object: - this.superClass = new Class("Object", -1).getType(); + this.superClass = new Class("java.lang.Object", -1).getType(); } // ino.end @@ -1067,7 +1067,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I /* private Menge generateGenericClass(String name, Class superClass){ - //TODO: bytecode -- Generics hinzufügen + //TODO: bytecode -- Generics hinzuf�gen //Type superClassType = superClass.getType(); //TODO: bytecode