Re: Extract Constructor Code to new method

Abdullah Odeh Al-Zaghameem <[email protected]> Thu, 2 Jul 2009 06:31:07 -0700 (PDT)
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
--0-2040129848-1246541467=:67686
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,
=A0
One basic solution is to catch the old constructor and inject the new one. =
The following code do it in simple way:
=A0
InstructionList il =3D new InstructionList();
Method theOldConstructor =3D cg.containsMethod("<>", "()V");
MethodGen theNewConstructor =3D new MethodGen(Constants.ACC_PUBLIC, Type.VO=
ID, Type.NO_ARGS, NO_STRINGS, "<init>", cg.getClassName(), il, cpg);
il.append(ifact.createInvoke(cg.getSuperclassName(), "<init>", Type.VOID, T=
ype.NO_ARGS, Constants.INVOKESPECIAL));
il.append(InstructionConstants.RETURN);
cg.replaceMethod(theOldConstructor, theNewConstructor.getMethod());=20
il.dispose();
cg.setConstantPool(cpg);
=A0
=A0
Regards

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D
A b d u l l a h=A0=A0 O.=A0 A l - Z a g h a m e e m
Technical University of=A0 Berlin
Germany

--- On Thu, 7/2/09, Felix Dorner <[email protected]> wrote:


From: Felix Dorner <[email protected]>
Subject: Extract Constructor Code to new method
To: "[email protected]" <[email protected]>
Date: Thursday, July 2, 2009, 11:23 AM


Hey,

Given a no-arg constructor, I'd like to insert a new public method and move=
 all instructions into that new method. After moving that code, the constru=
ctor would look like one that's automatically inserted by the compiler, i.e=
. only calls <init> of its superclass and returns.

I've never done anything with BCEL, though I can follow the general informa=
tion pages. Still, I have no Idea how hard my problem is and therefore want=
ed to ask here for some guidance.


Thanks,
Felix
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

=0A=0A=0A      
--0-2040129848-1246541467=:67686--