JavaPatternMatching/BCEL/bcelifier/NewClass.java

61 lines
2.3 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 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[] { }, "<init>", "bcelifier.NewClass", 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.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", "<init>", 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"));
}
}