2014-06-16 60 views
0

我正在使用JDI來調試程序。我正在嘗試更改某個ObjectReference中字段的值。我有Field對象,因爲我可以通過繼承字段上的JDI - ObjectReference.setValue()

ObjectReference.referencetype().allFields() 

但是找到它,我可以用

ObjectReference.setValue(Field paramField, Value paramValue) 

,因爲這個類或其直接超類中的字段,只有工作不修改場 - 不不包括更高層次的層次結構。

是否有可能改變一個字段的值比直接超類更高? 如果是這樣,我將不勝感激任何幫助!

謝謝

院長

編輯:我得到)在該行拋出下面的錯誤,我打電話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) 

回答

0

隨着org.apache.commons .beanutils.PropertyUtils你可以得到/設置甚至像這樣的繼承字段:

PropertyUtils.getNestedProperty(obj, "property") 
+0

謝謝,但我想訪問另一個進程的變量,PropertyUtils會這樣做嗎? –

1
ObjectReference.setValue(Field paramField, Value paramValue) 

無論領域層次如何,都適用於所有領域。

實施例:

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(); 
    } 
} 

這裏的改變在其較高的超類的字段的值的代碼甲

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)); 

下面是輸出確認值變化:

a=30 
b=20