Can I change the values ​​of private fields during debugging?

For debugging purposes, I need to change the value of a private field. I use Eclipse for debugging, and I can change variables during the debugging process, but I do not have access to private variables. I tried using reflection in the representation of the change value to set the field as “accessible” manually, but it does not seem to work. Do you know any IDE / framework / plugin or something that could allow this?

+3
source share
4 answers

In Eclipse you can go to variable view which lists all your variables.

Here, you can right-click the member variable that you want to change and select the change value option , which displays a separate window to change the value. which will operate from then on.

+3
source

Just tested with eclipse - no problem. The testing application was like this:

 public class DebugTest {
   private static int i = 5;
   public static void main(String[] args) {
     System.out.println(args.length);  // dummy line to set a breakpoint
     System.out.println(i);
   }
 }

I installed BP on this dummy line, started the debugger, then I changed the value for iin the Variables view from five to six, continued, and the result was 6.

: , Variables. ( ), Java . - , .

+1

(Spring ReflectionTestUtil):

  Class<?> c = foo.getClass();
  Field field = c.getDeclaredField("valid");
  field.setAccessible(true);
  field.set(valid, Boolean.FALSE);

, , .

0

varible , , IDE/. IDE .

-2

All Articles