0

I am using the JDI to debug a program. I am trying to change the value of a field in a certain ObjectReference. I have the Field object, as I can find it through

ObjectReference.referencetype().allFields()

However, I cannot modify that field using

ObjectReference.setValue(Field paramField, Value paramValue)

since that only works on Fields inside this class or its direct superclass -- does not included higher classes up the hierarchy.

Is it possible to change the value of a Field in a class higher than the direct super class? If so, I would appreciate any help to do so!

Thank you,

Dean

EDIT: I am getting the following Error thrown at the line where I call ObjectReference.setValue():

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.sun.tools.jdi.MirrorImpl.validateMirror(MirrorImpl.java:49)
at com.sun.tools.jdi.ObjectReferenceImpl.setValue(ObjectReferenceImpl.java:214)
Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22

2 Answers2

2
ObjectReference.setValue(Field paramField, Value paramValue)

Works for all the fields no matter how up in the hierarchy the Field lies.

Example:

public class AClass {

    private int a = 10;

    public void a() {
        System.out.println("a=" + a);
    }

}
public class BClass extends AClass{

    private int b = 20;

    public void b() {
        System.out.println("b=" + b);
    }

}
public class CClass extends BClass{

    public void checkValues() {
        a();
        b();
    }
}

Here's the code to change the value of the field a in its higher super class A

StackFrame currentFrame = mEvent.thread().frame(0);

ObjectReference objectReference = currentFrame.thisObject();

List<Field> fields = objectReference.referenceType().allFields();
System.out.println(fields);

objectReference.setValue(fields.get(1), vm.mirrorOf(30));

Here's the output to confirm the value change:

a=30
b=20
devilpreet
  • 749
  • 4
  • 18
0

With org.apache.commons.beanutils.PropertyUtils you can get/set even inherited field like that:

PropertyUtils.getNestedProperty(obj, "property")