JavaPatternMatching/BCEL/bcelifier/FieldDeclarationCreator.java

69 lines
2.8 KiB
Java

package bcelifier;
import org.apache.commons.bcel6.generic.*;
import org.apache.commons.bcel6.classfile.*;
import org.apache.commons.bcel6.*;
import java.io.*;
public class FieldDeclarationCreator implements Constants {
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
public FieldDeclarationCreator() {
_cg = new ClassGen("bcelifier.FieldDeclaration", "java.lang.Object", "FieldDeclaration.java", ACC_PUBLIC | ACC_SUPER, new String[] { });
_cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp);
}
public void create(OutputStream out) throws IOException {
createFields();
createMethod_0();
createMethod_1();
_cg.getJavaClass().dump(out);
}
private void createFields() {
FieldGen field;
field = new FieldGen(0, new ObjectType("java.lang.Integer"), "field", _cp);
_cg.addField(field.getField());
}
private void createMethod_0() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", "bcelifier.FieldDeclaration", 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, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("bcelifier.FieldDeclaration", "methode", new ObjectType("java.lang.Integer"), Type.NO_ARGS, Constants.INVOKEVIRTUAL));
il.append(_factory.createFieldAccess("bcelifier.FieldDeclaration", "field", new ObjectType("java.lang.Integer"), Constants.PUTFIELD));
InstructionHandle ih_12 = 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.Integer"), Type.NO_ARGS, new String[] { }, "methode", "bcelifier.FieldDeclaration", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createFieldAccess("bcelifier.FieldDeclaration", "field", new ObjectType("java.lang.Integer"), Constants.GETFIELD));
InstructionHandle ih_4 = 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.FieldDeclarationCreator creator = new bcelifier.FieldDeclarationCreator();
creator.create(new FileOutputStream("bcelifier.FieldDeclaration.class"));
}
}