java - Modify value of non static variable - reflection -


have non-static int variable define in:

 public class mainactivity extends appcompatactivity {       @sharedfield(key = "sample")     public  int data = 2;      sharedbind sharedbind = new sharedbind();      @sharedmethod(key = "sample")     public void methodtest() {         log.e("test", "" + data);     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         sharedbind.bind(this); // run binding  }  public void onbuttonclick(){ sharedbind.runtest("sample"); } } 

and sharedbind

public class sharedbind  {  hashmap<string, arraylist<bindhandler>> fieldbindhandlerhashmap = new hashmap(); hashmap<string, arraylist<bindhandler>> methodbindhandlerhashmap = new hashmap();  public void bind(object target) {      class<?> obj = target.getclass();     (method method : obj.getdeclaredmethods()) {          // if method annotated @test         if (method.isannotationpresent(sharedmethod.class)) {               annotation annotation = method.getannotation(sharedmethod.class);             sharedmethod sharedmethod = (sharedmethod) annotation;             arraylist<bindhandler> methodbindhandlers = methodbindhandlerhashmap.get(sharedmethod.key());             if (methodbindhandlers == null)                 methodbindhandlers = new arraylist<>();              methodbindhandlers.add(new bindhandler(method, target, sharedmethod));              methodbindhandlerhashmap.put(sharedmethod.key(), methodbindhandlers);         }      }     (field field : obj.getdeclaredfields()) {         if (field.isannotationpresent(sharedfield.class)) {             annotation annotation = field.getannotation(sharedfield.class);             sharedfield sharedview = (sharedfield) annotation;             arraylist<bindhandler> fieldbindhandlers = fieldbindhandlerhashmap.get(sharedview.key());             if (fieldbindhandlers == null)                 fieldbindhandlers = new arraylist<>();             fieldbindhandlers.add(new bindhandler(field, target, sharedview));             fieldbindhandlerhashmap.put(sharedview.key(), fieldbindhandlers);             log.e("done", "mdf");              log.e("result", field.getgenerictype().tostring());          }     } }    public void runtest(string key) {      arraylist<bindhandler> viewbinds = fieldbindhandlerhashmap.get(key);     if (viewbinds != null) {         (bindhandler bindhandler : viewbinds) {             bindhandler.gettargetfield().setaccessible(true);                 try {                     bindhandler.gettargetfield().setint(bindhandler.gettarget(), 234);                 } catch (illegalaccessexception e) {                     e.printstacktrace();             }         }     }       arraylist<bindhandler> methodbinds = methodbindhandlerhashmap.get(key);     if (methodbinds != null) {         (bindhandler bindhandler : methodbinds) {             try {                 bindhandler.getmethod().invoke(bindhandler.gettarget().getclass().newinstance());             } catch (exception e) {                 e.printstacktrace();             }         }     } } } 

and bindhandler

public class bindhandler {     private field targetfield;     private operationtype operationtype;     private sharedobj sharedobj;     private method method;     private object target;       public bindhandler(field targetfield, object target, sharedfield sharedview) {         this.targetfield = targetfield;         this.target = target;         this.sharedobj = sharedview.datatype();         this.operationtype = sharedview.operationtype();     } } 

and trying modify content as:

bindhandler.gettargetfield().setint(bindhandler.gettarget(), 234); 

this doesn't work. when change variable in mainactivity :

@fields(key = "sample") static public  int data = 2; 

it works. how make work non-static variable. thanks


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -