forked from JavaTX/JavaCompilerCore
NewClassCreator
This commit is contained in:
parent
0048d5f305
commit
4c908abede
9
BCEL/bcelifier/NewClass.java
Normal file
9
BCEL/bcelifier/NewClass.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package bcelifier;
|
||||||
|
|
||||||
|
public class NewClass {
|
||||||
|
public NewClass(Integer i){}
|
||||||
|
void methode2(){
|
||||||
|
new NewClass(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
60
BCEL/bcelifier/NewClassCreator.java
Normal file
60
BCEL/bcelifier/NewClassCreator.java
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
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_PUBLIC | 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(ACC_PUBLIC, Type.VOID, new Type[] { new ObjectType("java.lang.Integer") }, new String[] { "arg0" }, "<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.VOID, Type.NO_ARGS, new String[] { }, "methode2", "bcelifier.NewClass", il, _cp);
|
||||||
|
|
||||||
|
InstructionHandle ih_0 = il.append(_factory.createNew("bcelifier.NewClass"));
|
||||||
|
il.append(InstructionConstants.DUP);
|
||||||
|
il.append(new PUSH(_cp, 1));
|
||||||
|
il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Constants.INVOKESTATIC));
|
||||||
|
il.append(_factory.createInvoke("bcelifier.NewClass", "<init>", Type.VOID, new Type[] { new ObjectType("java.lang.Integer") }, Constants.INVOKESPECIAL));
|
||||||
|
il.append(InstructionConstants.POP);
|
||||||
|
InstructionHandle ih_12 = il.append(_factory.createReturn(Type.VOID));
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user