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 6f70cb9f..c82ddc6f 100644 Binary files a/BCEL/bcelifier/Lambda1.class and b/BCEL/bcelifier/Lambda1.class differ 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 02092f66..76fe2494 100644 Binary files a/BCEL/bcelifier/This.class and b/BCEL/bcelifier/This.class differ diff --git a/BCEL/bcelifier/ThisCreator.java b/BCEL/bcelifier/ThisCreator.java index 573b162e..68cc58aa 100644 --- a/BCEL/bcelifier/ThisCreator.java +++ b/BCEL/bcelifier/ThisCreator.java @@ -11,7 +11,7 @@ public class ThisCreator implements Constants { private ClassGen _cg; public ThisCreator() { - _cg = new ClassGen("bcelifier.This", "java.lang.Object", "", 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); + } + } +}