From 931e90e7f27b1e0f9d8fbc7730a86d4b2987b096 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 12 Mar 2014 16:32:50 +0100 Subject: [PATCH] =?UTF-8?q?Erster=20Bytecodegeneration-Test=20angef=C3=BCg?= =?UTF-8?q?t.=20codegen()-Methode=20angepasst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mycompiler/MyCompiler.java | 18 ++++++------ src/mycompiler/MyCompilerAPI.java | 5 +++- src/mycompiler/SourceFile.java | 9 ++++-- src/mycompiler/myclass/Class.java | 22 ++++++++++----- .../TypeinferenceResultSet.java | 8 ++++++ test/bytecode/EmptyClassTest.java | 28 +++++++++++++++++++ 6 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 test/bytecode/EmptyClassTest.java diff --git a/src/mycompiler/MyCompiler.java b/src/mycompiler/MyCompiler.java index a40d0554..957a26dc 100755 --- a/src/mycompiler/MyCompiler.java +++ b/src/mycompiler/MyCompiler.java @@ -13,6 +13,7 @@ import java.io.Reader; import java.io.StringReader; import java.util.Vector; +import mycompiler.mybytecode.ClassFile; import mycompiler.myclass.Class; import mycompiler.myclass.ClassBody; import mycompiler.myclass.Constructor_Backup; @@ -42,6 +43,7 @@ import org.apache.log4j.xml.DOMConfigurator; import com.sun.corba.se.spi.orbutil.fsm.Guard.Result; import com.sun.org.apache.xerces.internal.impl.xs.identity.Field; +import typinferenz.ResultSet; import typinferenz.TypinferenzException; // ino.end import typinferenz.assumptions.TypeAssumptions; @@ -534,30 +536,30 @@ public class MyCompiler implements MyCompilerAPI - // ino.method.codeGeneration.21310.defdescription type=javadoc /** * 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. */ - // ino.end - // ino.method.codeGeneration.21310.definition - public void codeGeneration() + @Override + public Vector codeGeneration(ResultSet result) throws NullPointerException, JVMCodeException - // ino.end - // ino.method.codeGeneration.21310.body { if(m_AbstractSyntaxTree==null){ throw new NullPointerException("Es wurde noch kein Abstrakter Syntaxbaum erstellt!"); } codegenlog.info("Beginn der Codegenerierung ..."); - //m_AbstractSyntaxTree.codegen(); + Vector ret = new Vector(); + + for(SourceFile sf : m_AbstractSyntaxTree){ + ret.addAll(sf.codegen(result)); + } codegenlog.info("Codegenerierung beendet!"); + return ret; } - // ino.end // ino.method.main.21313.defdescription type=javadoc /** diff --git a/src/mycompiler/MyCompilerAPI.java b/src/mycompiler/MyCompilerAPI.java index 9120cb74..a0d3349e 100755 --- a/src/mycompiler/MyCompilerAPI.java +++ b/src/mycompiler/MyCompilerAPI.java @@ -7,6 +7,9 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Vector; + +import typinferenz.ResultSet; +import mycompiler.mybytecode.ClassFile; import mycompiler.myexception.CTypeReconstructionException; import mycompiler.myexception.JVMCodeException; import mycompiler.myparser.JavaParser; @@ -94,7 +97,7 @@ public interface MyCompilerAPI */ // ino.end // ino.method.codeGeneration.21346.declaration - public void codeGeneration() + public Vector codeGeneration(ResultSet result) throws NullPointerException, JVMCodeException; // ino.end diff --git a/src/mycompiler/SourceFile.java b/src/mycompiler/SourceFile.java index 4dd5c1d5..46c353e0 100755 --- a/src/mycompiler/SourceFile.java +++ b/src/mycompiler/SourceFile.java @@ -3,11 +3,13 @@ package mycompiler; // ino.end // ino.module.SourceFile.8722.import +import java.util.Collection; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; +import mycompiler.mybytecode.ClassFile; import mycompiler.myclass.BasicAssumptionClass; import mycompiler.myclass.Class; import mycompiler.myclass.ImportDeclarations; @@ -216,11 +218,12 @@ public class SourceFile */ // ino.end // ino.method.codegen.21397.definition - public void codegen() + public Vector codegen(ResultSet result) throws JVMCodeException // ino.end // ino.method.codegen.21397.body { + Vector ret = new Vector(); codegenlog.info("Anzahl der Interfaces: " + Integer.toString(InterfaceVektor.size())); for(int i = 0; i < InterfaceVektor.size(); i++) { @@ -230,8 +233,9 @@ public class SourceFile codegenlog.info("Anzahl der Klassen: " + Integer.toString(KlassenVektor.size())); for(int i = 0; i < KlassenVektor.size(); i++) { - KlassenVektor.elementAt(i).codegen(this); + ret.add(KlassenVektor.elementAt(i).codegen(result)); } + return ret; } // ino.end @@ -1539,5 +1543,6 @@ public class SourceFile //this.filename = filename; } + } // ino.end diff --git a/src/mycompiler/myclass/Class.java b/src/mycompiler/myclass/Class.java index 35f9dbfe..7dacfbe1 100755 --- a/src/mycompiler/myclass/Class.java +++ b/src/mycompiler/myclass/Class.java @@ -299,8 +299,14 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface + /** + * Generiert die ClassFile für diese Klasse. + * @param typeinferenceResult - Das ResultSet einer Typinferierung oder null, falls alle Typen eindeutig feststehen. + * @return + * @throws JVMCodeException + */ // ino.method.codegen.23071.definition - public void codegen(SourceFile sf) + public ClassFile codegen(ResultSet typeinferenceResult) throws JVMCodeException // ino.end // ino.method.codegen.23071.body @@ -316,12 +322,13 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface } // Handling der Package - String pkgName = ""; - if (sf.getPackageName() != null) { - pkgName = sf.getPackageName().get_codegen_UsedId() + "/"; - } + //String pkgName = ""; + //if (sf.getPackageName() != null) { + // pkgName = sf.getPackageName().get_codegen_UsedId() + "/"; + //} - classfile.add_class(getName(), pkgName, superClass, getAccessFlags()); + //geändert von Andreas Stadelmeier: pkgName wird nicht mehr aus dem SourceFile ausgelesen: + classfile.add_class(getName(), pkgName.get_Name_1Element(), superClass, getAccessFlags()); // Handling fuer Superinterfaces classfile.addSuperInterfaces(getSuperInterfaces()); @@ -340,9 +347,10 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface classfile.add_method("", "()V", null, null, null, (short)0, this.paralist, false); } - classfile.codegen(); + //classfile.codegen(); codegenlog.info("Compilierung erfolgreich abgeschlossen, "+ getName() + ".class erstellt."); + return classfile; } public void codegen(ClassFile classfile, Vector paralist) diff --git a/src/mycompiler/mytypereconstruction/TypeinferenceResultSet.java b/src/mycompiler/mytypereconstruction/TypeinferenceResultSet.java index 933e8985..60d59836 100755 --- a/src/mycompiler/mytypereconstruction/TypeinferenceResultSet.java +++ b/src/mycompiler/mytypereconstruction/TypeinferenceResultSet.java @@ -121,5 +121,13 @@ public class TypeinferenceResultSet return ret; } + /** + * Startet die Bytecodegenerierung dieser Lösung. + * Dabei wird die codegen-Methode der inferierten Klasse mit diesem ResultSet aufgerufen. + */ + public void codegen(){ + + } + } // ino.end diff --git a/test/bytecode/EmptyClassTest.java b/test/bytecode/EmptyClassTest.java new file mode 100644 index 00000000..db89b73a --- /dev/null +++ b/test/bytecode/EmptyClassTest.java @@ -0,0 +1,28 @@ +package bytecode; + +import java.util.Vector; + +import junit.framework.TestCase; +import mycompiler.MyCompiler; +import mycompiler.MyCompilerAPI; +import mycompiler.myexception.JVMCodeException; +import mycompiler.mytypereconstruction.TypeinferenceResultSet; + +import org.junit.Test; + +public class EmptyClassTest extends TestCase { + + @Test + public void test(){ + MyCompilerAPI compiler = MyCompiler.getAPI(); + compiler.parse("class EmptyClass{}"); + try { + compiler.codeGeneration(null); + } catch (NullPointerException | JVMCodeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + assertTrue("Test erfolgreich",true); + } + +}