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]
