java - Read value of variable with Javassist -
i'm using javassist fields on class, using following code:
for (ctfield ctf : ctclass.getdeclaredfields()) { system.out.println(ctf.getname()); } thus, variables of class accessing, displayed on screen , works well.
what want know is possible access value of of these variables?
thanks help!
the way values of these variables if have instance of object want values (since different instances may have different values).
object instance = ... ... (ctfield ctf : ctclass.getdeclaredfields()) { field f = instance.getclass().getdeclaredfield(ctf.getname()); f.setaccessible(true); object value = f.get(instance); } if you're trying access static fields don't need instance , can f.get(null) in code above.
also, if using sort of profiling along java instrumentation or , not have instances of objects inspecting, viable strategy add static field collection of instances each class (using javassist) , transform constructors (using insertafter) add this field. can use same reflection method in example new field each class care , have reference instances.
Comments
Post a Comment