Creating a getter for a static field in Java using Objecweb ASM -


alright i'm trying create getter inside classa returns static field inside classb using objectweb asm. classes start out this:

classa:

public class classa {  } 

classb:

public class classb {      static int secret = 123;  } 

and i'm trying dump classa once decompiled:

public class classa {      public int getsecretint(){         return classb.secret;     } } 

so far have been able return fields inside classa i'm not sure how go returning static fields inside other classes.

what can do: (this adds method classa returns variable inside self)

        methodnode mn = new methodnode(acc_public, gettername, "()" + fielddescriptor, signature, null);          mn.instructions.add(new varinsnnode(aload, 0));         mn.instructions.add(new fieldinsnnode(isstatic ? getstatic : getfield, cn.name, fieldname, fielddescriptor));         mn.instructions.add(new insnnode(retinsn));          mn.visitmaxs(3, 3);         mn.visitend();         cn.methods.add(mn); 

what want make method generate return static value classb.

basically make it:

return classb.secret; 

generally, easiest way right calls asm use asmifier class on bytecode (.class file) compiled java code.

that said, getting static field class straightforward , should not require use of asmifier. assuming have methodvisitor mw obtained classwriter (if want generate bytecode directly say), like:

mw.visitfieldinsn(opcodes.getstatic, "classb", "secret", "i"); mw.visitinsn(opcodes.ireturn); 

no need load "this" (aload 0). also, don't have compute frames , locals yourself, can use classwriter.compute_frames , compute_maxs asm you. , code show, have 1 variable ("this" implicit) , need 1 stack element (for value of classb.secret).


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -