From 24671ceca4f39683099c3a270523c56d3a05ba4c Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 17 Jun 2015 12:03:54 +0200 Subject: [PATCH] =?UTF-8?q?EmptyClass=20Test=20funktionsf=C3=A4hig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Class.java | 5 ++- .../dhbwstuttgart/syntaxtree/Constructor.java | 21 +++++++--- .../syntaxtree/statement/Block.java | 16 +++----- .../syntaxtree/statement/MethodCall.java | 3 +- .../syntaxtree/statement/Statement.java | 2 +- .../syntaxtree/statement/SuperCall.java | 21 +++++++++- .../dhbwstuttgart/syntaxtree/type/Type.java | 2 + .../dhbwstuttgart/syntaxtree/type/Void.java | 5 +++ .../KomplexeMenge/KeineDoppeltenVerweise.java | 38 ++++++++++++++++++- .../{EmptyClass.java => EmptyClassTest.java} | 4 +- 10 files changed, 93 insertions(+), 24 deletions(-) rename test/bytecode/{EmptyClass.java => EmptyClassTest.java} (93%) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 2e710e0e..d00cca51 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -721,7 +721,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I assumptions.add(globalAssumptions); ConstraintsSet oderConstraints = new ConstraintsSet(); - + for(Type gparam : this.get_ParaList()){ if(gparam instanceof GenericTypeVar)assumptions.add(((GenericTypeVar)gparam).createAssumptions()); //Constraints für die Generischen Variablen erstellen und diese dem AssumptionsSet hinzufügen } @@ -744,7 +744,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I //ConstraintsSet oderConstraints = this.TYPE(this.getMethodList(), fieldInitializers, assumptions); - + this.superClass = this.superClass.TYPE(assumptions, this); + for(Field f:this.getFields()){ oderConstraints.add(f.TYPE(assumptions)); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index cb5914ad..566f4135 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -15,6 +15,7 @@ import de.dhbwstuttgart.parser.JavaClassName; import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.modifier.Modifiers; import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; @@ -39,6 +40,10 @@ public class Constructor extends Method { */ public Constructor(Method methode){ super(methode.get_Method_Name(), methode.getType(), methode.getParameterList(),methode.get_Block(), methode.getGenericDeclarationList(), methode.getOffset()); + //Sicherstellen, dass das erste Statement in der Methode ein SuperCall ist: + if(this.get_Block().get_Statement().size() <1 || ! (this.get_Block().get_Statement().firstElement() instanceof SuperCall)){ + this.get_Block().statements.add(0, new SuperCall(this.get_Block())); + } } @Override public TypeAssumptions createTypeAssumptions(Class classmember) { @@ -52,18 +57,22 @@ public class Constructor extends Method { @Override public void genByteCode(ClassGen cg) { ConstantPoolGen _cp = cg.getConstantPool(); - //InstructionFactory _factory = new InstructionFactory(cg, _cp); InstructionList il = new InstructionList(); //sollte nicht new sein sondern aus Block kommen Class parentClass = this.getParentClass(); - //Hier m�sste drin stehen: Kreiere einen Block, welcher ein Statement enth�lt, welches ein Super Statement ist. - //TODO: Alles dynamisch gestalten //init darf nur drin stehen, wenn Konstruktor; - this.method = new MethodGen(Constants.ACC_PUBLIC, org.apache.bcel.generic.Type.getReturnType(this.getType().get_Name()), org.apache.bcel.generic.Type.NO_ARGS , new String[] { }, "", parentClass.name, il, _cp); + this.method = new MethodGen(Constants.ACC_PUBLIC, this.getType().getBytecodeType(), org.apache.bcel.generic.Type.NO_ARGS , new String[] { }, "", parentClass.name, il, _cp); - super.genByteCode(cg); //Aufrufen um Iteration �ber Block zu starten - + //Iteration �ber Block starten + Block instructions = this.get_Block(); + InstructionList blockInstructions = instructions.genByteCode(cg); + + il.append(blockInstructions);//Die vom Block generierten Instructions an die InstructionList der Methode anfügen + + this.method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden) + + cg.addMethod(this.method.getMethod()); } // super statement muss drin sein // stmt genByteCode + im block genByteCode implementieren & dann Hierarchie ausprobieren diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index ab311454..fc744563 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -255,20 +255,16 @@ public class Block extends Statement } @Override - public void genByteCode(ClassGen cg) { + public InstructionList genByteCode(ClassGen cg) { //ConstantPoolGen _cp = cg.getConstantPool(); - InstructionFactory _factory = new InstructionFactory(cg, _cp); InstructionList il = new InstructionList(); - //Frage: Wenn Block von Statements erbt, und Block selbst keinen BCEL Code besitzt, ist das hier dann nicht eine Sackgasse? - - //Instructionhandle dynamisch - InstructionHandle ih_0 = il.append(_factory.createLoad(org.apache.bcel.generic.Type.OBJECT, 0)); - il.append(_factory.createInvoke(this.getParentClass().superclassid.toString(), "", org.apache.bcel.generic.Type.VOID, org.apache.bcel.generic.Type.NO_ARGS, Constants.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); + for(Statement stmt : this.get_Statement()){ + il.append(stmt.genByteCode(cg)); + } + //il.dispose(); - il.dispose(); - + return il; } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index e1b03479..45f2716b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -5,6 +5,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import java.util.Hashtable; import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.InstructionList; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.Logger; @@ -304,7 +305,7 @@ public class MethodCall extends Expr } @Override - public void genByteCode(ClassGen _cg) { + public InstructionList genByteCode(ClassGen _cg) { // TODO Auto-generated method stub } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java b/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java index 70d3e540..eb4431b2 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java @@ -129,7 +129,7 @@ public abstract class Statement extends SyntaxTreeNode implements IItemWithOffse } - public abstract void genByteCode(ClassGen _cg); + public abstract InstructionList genByteCode(ClassGen _cg); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java index 6abf8513..eea3f491 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java @@ -3,8 +3,14 @@ package de.dhbwstuttgart.syntaxtree.statement; // ino.end // ino.module.This.8654.import import java.util.Hashtable; -import de.dhbwstuttgart.typeinference.Menge; +import org.apache.bcel.Constants; +import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.InstructionFactory; +import org.apache.bcel.generic.InstructionHandle; +import org.apache.bcel.generic.InstructionList; + +import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; @@ -79,6 +85,19 @@ public class SuperCall extends ThisCall String argList = ""; if(this.getArgumentList() != null)argList = this.getArgumentList().printJavaCode(resultSet).getJavaCode(); return new JavaCodeResult("super("+argList+")"); + } + + @Override + public InstructionList genByteCode(ClassGen cg) { + InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); + InstructionList il = new InstructionList(); + Type superClass = this.getParentClass().getSuperClass(); + //Instructionhandle dynamisch + InstructionHandle ih_0 = il.append(_factory.createLoad(org.apache.bcel.generic.Type.OBJECT, 0)); + il.append(_factory.createInvoke(superClass.getName().toString(), "", org.apache.bcel.generic.Type.VOID, org.apache.bcel.generic.Type.NO_ARGS, Constants.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); + + return il; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/Type.java b/src/de/dhbwstuttgart/syntaxtree/type/Type.java index 7b577602..20a9b07e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/Type.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/Type.java @@ -304,5 +304,7 @@ public abstract class Type extends SyntaxTreeNode implements IItemWithOffset return this; } + public abstract org.apache.bcel.generic.Type getBytecodeType(); + } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/type/Void.java b/src/de/dhbwstuttgart/syntaxtree/type/Void.java index 773a5fe8..42c63af3 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/Void.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/Void.java @@ -78,6 +78,11 @@ public class Void extends RefType public Type checkTYPE(TypeAssumptions ass, SyntaxTreeNode method) { return this;//VOID ist immer korrekt, das wird vom Parser geprüft } + + @Override + public org.apache.bcel.generic.Type getBytecodeType() { + return org.apache.bcel.generic.Type.VOID; + } } // ino.end diff --git a/test/KomplexeMenge/KeineDoppeltenVerweise.java b/test/KomplexeMenge/KeineDoppeltenVerweise.java index d7c3f431..43eea37c 100644 --- a/test/KomplexeMenge/KeineDoppeltenVerweise.java +++ b/test/KomplexeMenge/KeineDoppeltenVerweise.java @@ -6,6 +6,8 @@ import java.util.Vector; import org.junit.Test; +import de.dhbwstuttgart.typeinference.EinzelElement; +import de.dhbwstuttgart.typeinference.KomplexeMenge; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.OderMenge; import de.dhbwstuttgart.typeinference.UndMenge; @@ -25,11 +27,45 @@ class TestKlasse { } } +class TestKlasseOderMenge extends OderMenge{ + Menge set = new Menge<>(); + + @Override + public Menge> getSet() { + return set; + } + + public void addItem(TestKlasse string) { + TestUndMenge toAdd = new TestKlasseUndMenge(); + toAdd.addItem(string); + set.add(toAdd); + } + public void addItems(TestKlasseUndMenge undMenge) { + set.add(undMenge); + } +} + +class TestKlasseUndMenge extends UndMenge{ + Menge> set = new Menge<>(); + + @Override + public Menge> getSet() { + return set; + } + public void addItem(TestKlasse string) { + set.add(new EinzelElement(string)); + } + + public void addItems(KomplexeMenge oderMenge) { + set.add(oderMenge); + } +} + public class KeineDoppeltenVerweise { @Test public void test() { - OderMenge oM1 = new OderMenge<>(); + OderMenge oM1 = new TestOderMenge(); OderMenge oM2 = new OderMenge<>(); UndMenge oM3 = new UndMenge<>(); oM1.addItem(new TestKlasse("Menge 1, Item 1")); diff --git a/test/bytecode/EmptyClass.java b/test/bytecode/EmptyClassTest.java similarity index 93% rename from test/bytecode/EmptyClass.java rename to test/bytecode/EmptyClassTest.java index 5ce92d79..20aa3349 100644 --- a/test/bytecode/EmptyClass.java +++ b/test/bytecode/EmptyClassTest.java @@ -20,11 +20,11 @@ import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; -public class EmptyClass { +public class EmptyClassTest { public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; public final static String testFile = "EmptyClass.jav"; - public final static String outputFile = "EmptyClassTest.class"; + public final static String outputFile = "EmptyClass.class"; @Test public void test() {