Hi Abdullah,

> InstructionList il = new InstructionList(); Method
> theOldConstructor = cg.containsMethod("<>", "()V"); MethodGen
> theNewConstructor = new MethodGen(Constants.ACC_PUBLIC,
> Type.VOID, Type.NO_ARGS, NO_STRINGS, "<init>",
> cg.getClassName(), il, cpg);
> il.append(ifact.createInvoke(cg.getSuperclassName(),
> "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
> il.append(InstructionConstants.RETURN);
> cg.replaceMethod(theOldConstructor,
> theNewConstructor.getMethod()); il.dispose(); cg.setConstantPool(cpg);


Wouldn't this just 'delete' the old constructor, and create a new 'default' one?
What misses is to move the code that's inside the old constructor into an 
ordinairy method. What I came up with so far was to:

/* look up the default constructor */
Method constructor_method = getConstructors().get(0);
ConstantPoolGen cpgen = new ConstantPoolGen(jc.getConstantPool());

MethodGen constructor = new MethodGen(constructor_method, jc.getClassName(), 
new ConstantPoolGen(jc.getConstantPool()));
InstructionList constructor_code = constructor.getInstructionList();

/* a new method. Later I'd need to find a unique name.. I also copy the 
constructor's instruction list*/
MethodGen newmethod = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, new Type[] 
{}, new String[] {}, "xaxaxa", jc.getClassName(),  constructor_code.copy() , 
cpgen);

InstructionHandle[] newmethod_instructions = 
newmethod.getInstructionList().getInstructionHandles();

/* remove the first two instructions (the one that loads the object variable 
this,
And the invocation of the super-constructor... */
try {
        newmethod.getInstructionList().delete(newmethod_instructions[0], 
newmethod_instructions[1]);
} catch (TargetLostException e) {
        /* not only the target is lost here.. But me too.. :-)
        e.printStackTrace();
}

... Now I'd probably need to handle
 * local variables
 * exception handlers
..

What I don't really pick up is the instruction target exceptions (which in fact 
get thrown by several LineNumberGen's in my tests..)

Felix

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscr...@jakarta.apache.org
For additional commands, e-mail: bcel-user-h...@jakarta.apache.org

Reply via email to