Thanks for the inputs!..it;s done. I checked the code of classExtender and
it works for me.
However, just to clear my concepts, I wanted to know,  what will the snippet
below do. I am putting in comments wherever i could understand. The code is
for the setter method.


//create the factory
InstructionFactory fac = new InstructionFactory(classGen, cp);
//load the This variable as it is at the index 0
InstructionHandle ih_0 = il.append(fac.createLoad(Type.OBJECT, 0));
//what will this do??. What variable is loaded?
il.append(fac.createLoad(Type.OBJECT, 1));

il.append(fac.createFieldAccess(classGen.getClassName(), field.getName(),
Type.getType(field.getSignature()), Constants.PUTFIELD));
//create the return instrauction. But how would it know, that it is to
create the return instrauction for a particular variable??
        InstructionHandle ih_5 = il.append(fac.createReturn(Type.VOID));
        mg.setMaxStack();
        mg.setMaxLocals();
        classGen.addMethod(mg.getMethod());
        il.dispose();
If you could please help me regarding the same.


Thanks and Regards,
Sapan.


-----Original Message-----
From: Mark R. Diggory [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 27, 2003 7:59 AM
To: BCEL Users List
Subject: Re: Generating method Code for setters


I have a Class in my BCEL taglibrary that constructs getters and setters
for a desired propery, your welcome to review it.

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/repast-jellytag/repast-jelly-
taglibrary/src/org/apache/commons/jelly/tags/bcel/ClassExtender.java?rev=1.1
&content-type=text/vnd.viewcvs-markup

see the createSetterMethod(...)

Also start learning to use BCELifier in the BCEL util library, the class
is a "Godsend"! You can write a demo class that has the code you what to
generate, then run it through BCELifier to see what the BCEL code would
look like to generate that Class.

-Mark

Stephen Kolaroff wrote:
> Hi
> I began my work w/ bcel with your problem.
> IBelow is a method a wrote. Investigate and enjoy:
>
>  void createAccessor(ClassGen cg, Field f) {
>    ConstantPoolGen cpg = cg.getConstantPool();
>    int accessFlags = f.getAccessFlags();
>    FieldGen fg = new FieldGen(f, cpg);
>    InstructionList il = new InstructionList();
>
>    String accessorName = createWrapperName(f.getName(), true);
>
>    MethodGen accessor = new MethodGen(accessFlags,
>                                      fg.getType(),
>                                      new Type[] {},
>                                      new String[] {},
>                                      accessorName,
>                                      cg.getClassName(),
>                                      il,
>                                      cpg);
>    InstructionFactory ins = new InstructionFactory(cg, cpg);
>    il.append(ins.ALOAD_0);
>    il.append(ins.createGetField(cg.getClassName(), fg.getName(),
> fg.getType()));
>    il.append(ins.createReturn(fg.getType()));
>
>    // must be after the whole method is ready
>    accessor.setMaxLocals();
>    accessor.setMaxStack();
>
>    cg.addMethod(accessor.getMethod());
>  }
>
>
> Sapan Shah wrote:
>
>> Hi,
>>
>> I want to generate the code for a method like
>>
>> public String getName(){
>>     return this.name;
>> }
>>
>> I am successful in creating the method using the MethodGen class, but
>> am not
>> able to figure out,
>> how do i create the instruction within and get an access to this and then
>> the name variable. After that, how do i pass the returnInstruction.
>> I tried using the InstructionFactory.createReturn() method, but i
>> guess, it
>> will only get the instruction. how do i get the variable on which the
>> instruction is to be executed.
>>
>> If you could please help me. Any code snippet would be highly
>> appreciated.
>>
>> Thanks and Regards,
>> ------------------------------------
>> Sapan Shah
>> Software Engineer
>> Patni Computer Systems Limited
>> A 78/9 GIDC  Electronics Estate,
>> Sector 25,
>> Gandhinagar
>> mob: (+91)98240 59280
>> http://www.patni.com
>> World-Wide Partnerships. World-Class Solutions
>>
>> This e-mail message may contain proprietary, confidential or legally
>> privileged information for the sole use of the person or entity to
>> whom this
>> message was originally addressed. Any review, e-transmission
>> dissemination
>> or other use of or taking of any action in reliance upon this
>> information by
>> persons or entities other than the intended recipient is prohibited.
>> If you
>> have received this e-mail in error kindly delete this e-mail from your
>> records. If it appears that this mail has been forwarded to you without
>> proper authority, please notify us immediately at [EMAIL PROTECTED] and
>> delete this mail.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to