JavaCompilerCore/BCEL/bcelifier/IfStatementCreator.java

72 lines
3.0 KiB
Java

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 ClassGenerator _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 MethodGenerator(0, Type.VOID, new Type[] { new ObjectType("java.lang.Boolean") }, new String[] { "arg0" }, "<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");
}
}