[prev in list] [next in list] [prev in thread] [next in thread]
List: bcel-user
Subject: RE: Extract Constructor Code to new method
From: Felix Dorner <FDorner () zed ! com>
Date: 2009-07-02 14:54:52
Message-ID: FC95BD382E2CF447BAA1B2AF1F9FFFE602628AD26F () MX034ZED0001 ! TIFDOM ! COM
[Download RAW message or body]
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-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic