diff --git a/BCEL/bcelifier/IfStatement.java b/BCEL/bcelifier/IfStatement.java new file mode 100644 index 00000000..8f16e0d4 --- /dev/null +++ b/BCEL/bcelifier/IfStatement.java @@ -0,0 +1,12 @@ +package bcelifier; + +class IfStatement{ + Integer methode(Boolean b){ + if(b){ + return 1; + }else{ + return 2; + } + } + +} \ No newline at end of file diff --git a/BCEL/bcelifier/IfStatementCreator.java b/BCEL/bcelifier/IfStatementCreator.java new file mode 100644 index 00000000..058ca8e9 --- /dev/null +++ b/BCEL/bcelifier/IfStatementCreator.java @@ -0,0 +1,71 @@ +package bcelifier; + +import org.apache.commons.bcel6.generic.*; + +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.bytecode.MethodGenerator; +import de.dhbwstuttgart.typeinference.TypeinferenceResults; + +import org.apache.commons.bcel6.*; +import java.io.*; + +public class IfStatementCreator { + private InstructionFactory _factory; + private ConstantPoolGen _cp; + private ClassGen _cg; + + public IfStatementCreator() { + + TypeinferenceResults typeinferenceResults = null; + _cg = new ClassGenerator("bcelifier.IfStatement", new RefType("java.lang.Object", null, 0), "IfStatement.java", Const.ACC_SUPER, new String[] { }, typeinferenceResults); + + _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.IfStatement", il, _cp); + + InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID)); + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGenerator(0, new ObjectType("java.lang.Integer"), new Type[] { new ObjectType("java.lang.Boolean") }, new String[] { "arg0" }, "methode", "bcelifier.IfStatement", il, _cp); + + il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); + il.append(_factory.createInvoke("java.lang.Boolean", "booleanValue", Type.BOOLEAN, Type.NO_ARGS, Const.INVOKEVIRTUAL)); + BranchInstruction ifeq_4 = InstructionFactory.createBranchInstruction(Const.IFEQ, null); + il.append(ifeq_4); + il.append(new PUSH(_cp, 1)); + il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Const.INVOKESTATIC)); + il.append(InstructionFactory.createReturn(Type.OBJECT)); + InstructionHandle ih_12 = il.append(new PUSH(_cp, 2)); + il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Const.INVOKESTATIC)); + il.append(InstructionFactory.createReturn(Type.OBJECT)); + ifeq_4.setTarget(ih_12); + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + } + + public static void main(String[] args) throws Exception { + bcelifier.IfStatementCreator creator = new bcelifier.IfStatementCreator(); + creator.create(new FileOutputStream("bcelifier.IfStatement.class")); + + System.out.println("bcelifier.IfStatement.class"); + } +} diff --git a/BCEL/bcelifier/JavaToBCEL.java b/BCEL/bcelifier/JavaToBCEL.java index 2c87bc2f..0d162b00 100644 --- a/BCEL/bcelifier/JavaToBCEL.java +++ b/BCEL/bcelifier/JavaToBCEL.java @@ -24,16 +24,16 @@ 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+"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(); - new BCELifier(new ClassParser(rootDirectory+"FieldDeclaration.class").parse(), new FileOutputStream(new File(rootDirectory+"FieldDeclarationCreator.java"))).start(); - new BCELifier(new ClassParser(rootDirectory+"Null.class").parse(), new FileOutputStream(new File(rootDirectory+"NullCreator.java"))).start(); - 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+"NewClassCreator.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(); + //new BCELifier(new ClassParser(rootDirectory+"FieldDeclaration.class").parse(), new FileOutputStream(new File(rootDirectory+"FieldDeclarationCreator.java"))).start(); + //new BCELifier(new ClassParser(rootDirectory+"Null.class").parse(), new FileOutputStream(new File(rootDirectory+"NullCreator.java"))).start(); + //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+"NewClassCreator.java"))).start(); + new BCELifier(new ClassParser(rootDirectory+"IfStatement.class").parse(), new FileOutputStream(new File(rootDirectory+"IfStatementCreator.java"))).start(); } catch (ClassFormatException | IOException e) { e.printStackTrace(); } diff --git a/test/bytecode/stackmaptable/IfStatement.jav b/test/bytecode/stackmaptable/IfStatement.jav index ba78a2be..1451af17 100644 --- a/test/bytecode/stackmaptable/IfStatement.jav +++ b/test/bytecode/stackmaptable/IfStatement.jav @@ -1,10 +1,10 @@ class IfStatement{ -Integer methode(Boolean b){ - if(b){ - return 1; + Integer methode(Boolean b){ + if(b){ + return 1; }else{ - return 2; + return 2; } -} + } } \ No newline at end of file diff --git a/test/bytecode/stackmaptable/IfStatementTest.java b/test/bytecode/stackmaptable/IfStatementTest.java index 18a0a046..7c10e42b 100644 --- a/test/bytecode/stackmaptable/IfStatementTest.java +++ b/test/bytecode/stackmaptable/IfStatementTest.java @@ -3,14 +3,22 @@ package bytecode.stackmaptable; import org.junit.Test; import bytecode.SingleClassTester; +import bytecode.SourceFileBytecodeTest; -public class IfStatementTest { - public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/stackmaptable/"; - public final static String testFile = "IfStatement.jav"; - public final static String outputFile = "IfStatement.class"; +public class IfStatementTest extends SourceFileBytecodeTest{ + @Override + protected void init() { + testName = "IfStatement"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/stackmaptable/"; + } @Test - public void test() { - SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory); + public void testConstruct() throws Exception{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + assertTrue(true); } }