Java reflection question -



Java reflection question -

i working on project uses reflection fields of running java application.

i managed fields, can not read or write them. illustration found on web:

class aclass = myobject.class field field = aclass.getfield("somefield"); myobject objectinstance = new myobject(); object value = field.get(objectinstance); field.set(objetinstance, value);

the problem utilize classes running jar file, , classes seek manipulate obtained classloader. instead of 'myobject.class' have '.class'. 'myobject' tried utilize classloader did not work.

if utilize '.class':

object value = field.get(theloadedclass);

i error:

java.lang.illegalargumentexception: can not set int field myclass.field java.lang.class

thanks.

this should help:

class aclass = myclassloader.loadclass("myobject"); // utilize class loader , qualified class name field field = aclass.getfield("somefield"); // can not utilize "myobject objectinstance = new myobject()" since class loaded different classloader 1 used obtain "aclass" // instead, utilize "newinstance()" method of class object objectinstance = aclass.newinstance(); object value = field.get(objectinstance); field.set(objetinstance, value);

java reflection jar illegalargumentexception

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -