From 947d145cf185222abefa56548f570885034bc6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Sat, 31 Oct 2015 18:48:46 +0100 Subject: [PATCH] New Instruction implementier, ArgumentList fehlt noch --- BCEL/bcelifier/JavaToBCEL.java | 3 +- BCEL/bcelifier/Lambda1.class | Bin 519 -> 982 bytes BCEL/bcelifier/Lambda1Creator.java | 70 ------------------ BCEL/bcelifier/NewClass.java | 60 +++++++++++++++ BCEL/bcelifier/This.class | Bin 188 -> 263 bytes BCEL/bcelifier/ThisCreator.java | 2 +- .../syntaxtree/statement/Assign.java | 7 +- .../syntaxtree/statement/NewClass.java | 19 ++++- test/bytecode/BytecodeTest.java | 2 +- test/bytecode/NewClass.jav | 7 ++ test/bytecode/NewClassTest.java | 61 +++++++++++++++ 11 files changed, 154 insertions(+), 77 deletions(-) create mode 100644 BCEL/bcelifier/NewClass.java create mode 100644 test/bytecode/NewClass.jav create mode 100644 test/bytecode/NewClassTest.java diff --git a/BCEL/bcelifier/JavaToBCEL.java b/BCEL/bcelifier/JavaToBCEL.java index 10c74c32..b16b62c7 100644 --- a/BCEL/bcelifier/JavaToBCEL.java +++ b/BCEL/bcelifier/JavaToBCEL.java @@ -23,7 +23,7 @@ public class JavaToBCEL { public JavaToBCEL(){ try { - new BCELifier(new ClassParser(rootDirectory+"Lambda1.class").parse(), new FileOutputStream(new File(rootDirectory+"Lambda1Creator.java"))).start(); + //new BCELifier(new ClassParser(rootDirectory+"Lambda1.class").parse(), new FileOutputStream(new File(rootDirectory+"Lambda1Creator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"This.class").parse(), new FileOutputStream(new File(rootDirectory+"ThisCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"IntLiteral.class").parse(), new FileOutputStream(new File(rootDirectory+"IntLiteralCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"MethodCall.class").parse(), new FileOutputStream(new File(rootDirectory+"MethodCallCreator.java"))).start(); @@ -32,6 +32,7 @@ public class JavaToBCEL { new BCELifier(new ClassParser(rootDirectory+"LocalVarAccess.class").parse(), new FileOutputStream(new File(rootDirectory+"LocalVarAccessCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"Wildcard.class").parse(), new FileOutputStream(new File(rootDirectory+"WildcardCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"BooleanValue.class").parse(), new FileOutputStream(new File(rootDirectory+"BooleanValueCreator.java"))).start(); + new BCELifier(new ClassParser(rootDirectory+"NewClass.class").parse(), new FileOutputStream(new File(rootDirectory+"NewClass.java"))).start(); } catch (ClassFormatException | IOException e) { e.printStackTrace(); diff --git a/BCEL/bcelifier/Lambda1.class b/BCEL/bcelifier/Lambda1.class index 6f70cb9f8c3a91f4bffc693831ac5d4811fd8616..c82ddc6f846f03d4d1a3a34518db83997a3f2ae5 100644 GIT binary patch literal 982 zcmbVLO>fgc5Ph38-qbZDZAhU&KPYKQ0yGqH3*tZ|1X7@gRK0JKZM&_rj_h@%{43x9 zQV;w9#BV~(#zt`|^nedLv$JpB%)DKH|9Somz%x8`;h?-kesvzpxar~+Ryb~R++lE@ zE3M26hJ3woz%bVdhLWMwRa(A_M|~OYi@r}0K9c5mdbV6|bWg;YX!$}PwLZjJTSS|o z?2A!ceqewX~gTO>46yvv3{;1^OZW+ZA$Et@l ztaEI5xQ9)S`yOi8Vp!}Cq^}N@3|lE7(aaj!>z~Mhaj}gDE()mAWMQls8ujj_#P+`? znbGwok&z=J31Y)gO(s`C>wT!S>6uW95shA%CR_}YW@8KTsd%-Xku2%3-8kTAcxYnB z!!@hL=B%pHXThmV=Obmrp%|DToG>hqy1#cEB(c*Cg41|R^1jhph8#m9{Mt>C5i1!XEcw}N7=}Q#Qn)w{@-Cz^6K2|U8H3@!uNa1$Tr6=!`3Bz{x%Q8 z+SNFEO$GWP?3V{!5{k6W({7b4BU@;G1^Yw}yf zCPAz2VPfTQC(flW7%Ybe?vNiJj`=-jsJfo;<&6}voi_~V z3>DS2eIDN07w%YiiGd=++=&cja>}5$I|B;_%rfRsVkjgdIbo>Toc+CzZ7OP diff --git a/BCEL/bcelifier/Lambda1Creator.java b/BCEL/bcelifier/Lambda1Creator.java index 7a0d7c38..e69de29b 100644 --- a/BCEL/bcelifier/Lambda1Creator.java +++ b/BCEL/bcelifier/Lambda1Creator.java @@ -1,70 +0,0 @@ -package bcelifier; - -import org.apache.commons.bcel6.generic.*; -import org.apache.commons.bcel6.classfile.*; -import org.apache.commons.bcel6.*; -import java.io.*; - -public class Lambda1Creator implements Constants { - private InstructionFactory _factory; - private ConstantPoolGen _cp; - private ClassGen _cg; - - public Lambda1Creator() { - _cg = new ClassGen("bcelifier.Lambda1", "java.lang.Object", "Lambda1.java", ACC_PUBLIC | ACC_SUPER, new String[] { }); - - _cp = _cg.getConstantPool(); - _factory = new InstructionFactory(_cg, _cp); - } - - public void create(OutputStream out) throws IOException { - createMethod_0(); - createMethod_1(); - createMethod_2(); - _cg.getJavaClass().dump(out); - } - - private void createMethod_0() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", "bcelifier.Lambda1", il, _cp); - - InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID)); - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_1() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(0, new ObjectType("java.lang.Runnable"), Type.NO_ARGS, new String[] { }, "methode", "bcelifier.Lambda1", il, _cp); - - InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); - InstructionHandle ih_1 = il.append(_factory.createReturn(Type.OBJECT)); - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_2() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(ACC_PRIVATE | ACC_SYNTHETIC, Type.VOID, Type.NO_ARGS, new String[] { }, "lambda$methode$0", "bcelifier.Lambda1", il, _cp); - - InstructionHandle ih_0 = il.append(_factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC)); - il.append(_factory.createLoad(Type.OBJECT, 0)); - il.append(_factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.OBJECT }, Constants.INVOKEVIRTUAL)); - InstructionHandle ih_7 = il.append(_factory.createReturn(Type.VOID)); - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - public static void main(String[] args) throws Exception { - bcelifier.Lambda1Creator creator = new bcelifier.Lambda1Creator(); - creator.create(new FileOutputStream("bcelifier.Lambda1.class")); - } -} diff --git a/BCEL/bcelifier/NewClass.java b/BCEL/bcelifier/NewClass.java new file mode 100644 index 00000000..49132e9a --- /dev/null +++ b/BCEL/bcelifier/NewClass.java @@ -0,0 +1,60 @@ +package bcelifier; + +import org.apache.commons.bcel6.generic.*; +import org.apache.commons.bcel6.classfile.*; +import org.apache.commons.bcel6.*; +import java.io.*; + +public class NewClassCreator implements Constants { + private InstructionFactory _factory; + private ConstantPoolGen _cp; + private ClassGen _cg; + + public NewClassCreator() { + _cg = new ClassGen("bcelifier.NewClass", "java.lang.Object", "NewClass.java", ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + public void create(OutputStream out) throws IOException { + createMethod_0(); + createMethod_1(); + _cg.getJavaClass().dump(out); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(0, Type.VOID, Type.NO_ARGS, new String[] { }, "", "bcelifier.NewClass", il, _cp); + + InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID)); + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(0, Type.STRING, new Type[] { Type.STRING }, new String[] { "arg0" }, "method", "bcelifier.NewClass", il, _cp); + + InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.String")); + il.append(InstructionConstants.DUP); + il.append(_factory.createLoad(Type.OBJECT, 1)); + il.append(_factory.createInvoke("java.lang.String", "", Type.VOID, new Type[] { Type.STRING }, Constants.INVOKESPECIAL)); + il.append(_factory.createStore(Type.OBJECT, 2)); + InstructionHandle ih_9 = il.append(_factory.createLoad(Type.OBJECT, 2)); + InstructionHandle ih_10 = il.append(_factory.createReturn(Type.OBJECT)); + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + public static void main(String[] args) throws Exception { + bcelifier.NewClassCreator creator = new bcelifier.NewClassCreator(); + creator.create(new FileOutputStream("bcelifier.NewClass.class")); + } +} diff --git a/BCEL/bcelifier/This.class b/BCEL/bcelifier/This.class index 02092f665ea920d84229747f1e23319abff52c12..76fe2494dcf8bff25d6482660902d9e1c415a6b5 100644 GIT binary patch delta 178 zcmdnP*v@2g>ff$?3=9k=4E$US%nUs247}_Ne2ffiHko;uC3cJq%o>_uj0`N!`6;Q4 z4E#Qsd8vM-xk;%-A&E&j6SWj185y{O^Gl18Q{6IifGRjcGBS(xvJ%S@Cwe&r$TBbj zAp--e)^-NQjX(w?13Qpp0}C<(SsVff$?3=9k=3_M&6%nY3D3|#CC+>8utHko;uC3cJq%o>_uj0`N!`6&}^ q6egBB83-~k0wDtftJZb~#*IJ*2(UA700r5AEIz0-lhy{X3", ACC_PUBLIC | ACC_SUPER, new String[] { }); + _cg = new ClassGen("bcelifier.This", "java.lang.Object", "This.java", ACC_PUBLIC | ACC_SUPER, new String[] { }); _cp = _cg.getConstantPool(); _factory = new InstructionFactory(_cg, _cp); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index a0ed7d72..b60b1b26 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -184,8 +184,8 @@ public class Assign extends Expr InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); InstructionList il = expr2.genByteCode(cg);//expr2 rechte expr - //TODO: ^^ - counterAssign++; //macht STORE f�r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.) + + /* String expr2Type = expr2.getType().get_Name().toString(); @@ -214,6 +214,9 @@ public class Assign extends Expr //Es wird momentan immer von RefType ausgegangen: il.append(new ASTORE(counterAssign)); + //TODO: ^^ + counterAssign++; //macht STORE f�r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.) + return il; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index cc6d68b3..64655cc0 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -6,8 +6,13 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import org.apache.commons.bcel6.Constants; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.InstructionConstants; +import org.apache.commons.bcel6.generic.InstructionFactory; +import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NEW; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -208,8 +213,18 @@ public class NewClass extends Expr @Override public InstructionList genByteCode(ClassGenerator _cg) { - // TODO Bytecode - throw new NotImplementedException(); + //TODO bytecode + InstructionFactory _factory = _cg.getInstructionFactory(); + InstructionList il = new InstructionList(); + + String name = type.get_Name(); + + il.append(_factory.createNew(name)); + il.append(InstructionConstants.DUP); + il.append(_factory.createInvoke(name, "", org.apache.commons.bcel6.generic.Type.VOID, org.apache.commons.bcel6.generic.Type.NO_ARGS, Constants.INVOKESPECIAL)); + //il.append(InstructionConstants.POP); + + return il; } diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java index 92c38e19..0c86ae55 100644 --- a/test/bytecode/BytecodeTest.java +++ b/test/bytecode/BytecodeTest.java @@ -52,7 +52,7 @@ public abstract class BytecodeTest extends TestCase{ } protected ClassLoader getClassLoader() throws Exception{ - File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/"); + File file = new File(rootDirectory); URL url = file.toURL(); URL[] urls = new URL[]{url}; diff --git a/test/bytecode/NewClass.jav b/test/bytecode/NewClass.jav new file mode 100644 index 00000000..6343c9d0 --- /dev/null +++ b/test/bytecode/NewClass.jav @@ -0,0 +1,7 @@ +class NewClass{ + String method() { + String a; + a = new String(); + return a; + } +} \ No newline at end of file diff --git a/test/bytecode/NewClassTest.java b/test/bytecode/NewClassTest.java new file mode 100644 index 00000000..7cb425ea --- /dev/null +++ b/test/bytecode/NewClassTest.java @@ -0,0 +1,61 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; + +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 NewClassTest extends BytecodeTest{ + @Override + protected void init() { + testName = "NewClass"; + } + + @Test + public void testInit() throws Exception { + ClassLoader cl = getClassLoader(); + + cl.loadClass(testName).newInstance(); + } + + @Test + public void testMethod() throws Exception { + try{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + + File file = new File(rootDirectory); + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + + + Class[] params = new Class[0]; + + Method method = cls.getDeclaredMethod("method", params); + Object returnValue = method.invoke(obj); + assertTrue(returnValue instanceof String); + }catch(Exception e){ + throw new RuntimeException(e); + } + } +}