From 09a447cc4816f1c664053fef3173f880b3e82ca6 Mon Sep 17 00:00:00 2001 From: "Fikus, Evelyn (WWI2012D)" Date: Mon, 22 Jun 2015 14:49:22 +0200 Subject: [PATCH] =?UTF-8?q?Versuch=20bcel=20f=C3=BCr=20StringLiteral?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/Return.java | 1 + .../syntaxtree/statement/StringLiteral.java | 12 ++++- test/bytecode/StringLitTest.jav | 7 +++ test/bytecode/StringLitTest.java | 45 +++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/bytecode/StringLitTest.jav create mode 100644 test/bytecode/StringLitTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java index 1fba9e323..a4b6dfbc0 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java @@ -142,6 +142,7 @@ public class Return extends Statement il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); + return il; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.java b/src/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.java index f1a87e3b7..a5dcec2b9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.java @@ -1,11 +1,14 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.typeinference.Menge; + // ino.end // ino.module.StringLiteral.8653.import import java.util.Hashtable; import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.InstructionFactory; +import org.apache.bcel.generic.InstructionList; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.Logger; @@ -103,9 +106,14 @@ public class StringLiteral extends Literal } @Override - public void genByteCode(ClassGen _cg) { - // TODO Auto-generated method stub + public InstructionList genByteCode(ClassGen cg) { + InstructionList il = new InstructionList(); + InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); + + il.append(_factory.ASTORE_1.createNew(this.getString())); + + return il; } } diff --git a/test/bytecode/StringLitTest.jav b/test/bytecode/StringLitTest.jav new file mode 100644 index 000000000..6397f8c76 --- /dev/null +++ b/test/bytecode/StringLitTest.jav @@ -0,0 +1,7 @@ +class StringLitTest{ + + +Object method() { s = ""; } + + +} \ No newline at end of file diff --git a/test/bytecode/StringLitTest.java b/test/bytecode/StringLitTest.java new file mode 100644 index 000000000..6032c6a17 --- /dev/null +++ b/test/bytecode/StringLitTest.java @@ -0,0 +1,45 @@ +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 StringLitTest { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "StringLitTest.jav"; + public final static String outputFile = "StringLitTest.class"; + + @Test + public void test() { + LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); + MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); + try { + compiler.parse(new File(rootDirectory + testFile)); + compiler.typeReconstruction(); + ByteCodeResult bytecode = compiler.generateBytecode(); + System.out.println(bytecode); + bytecode.getByteCode().getJavaClass().dump(new File(rootDirectory + outputFile)); + } catch (IOException | yyException e) { + e.printStackTrace(); + TestCase.fail(); + } + } + +}