-Test für Stackmaptable ifstatement verbessert

- IFStatementCreator erstellt
This commit is contained in:
Enrico Schrödter 2016-04-26 15:52:22 +02:00
parent 8bda352bc9
commit 362be98c30
5 changed files with 112 additions and 21 deletions

View File

@ -0,0 +1,12 @@
package bcelifier;
class IfStatement{
Integer methode(Boolean b){
if(b){
return 1;
}else{
return 2;
}
}
}

View File

@ -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[] { }, "<init>", "bcelifier.IfStatement", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("java.lang.Object", "<init>", 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");
}
}

View File

@ -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();
}

View File

@ -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;
}
}
}
}

View File

@ -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);
}
}