Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change values of private fields during debug?

For the debugging purposes I need to change the value of the private field. I use Eclipse for debugging, and I am able to change variables during debug process, but I have no access to the private variables. I tried to use a reflection in a change value view to set the field as "accessible" manually, but it looks like it doesn`t work. Do you know any IDE/framework/plugin or something that could allow it?

like image 545
MaSEL Avatar asked Nov 29 '25 22:11

MaSEL


2 Answers

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

Here you can right click on the member variable which you want to change and select change value option, which pops up a separate window to change the value. which will take into effect from then onwards.

like image 183
GuruKulki Avatar answered Dec 01 '25 11:12

GuruKulki


Yuo can use reflection to set field value (Spring provides convenient ReflectionTestUtil):

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

Also you shouldn't have any problems with setting private field's value in the debugger, it doesn't actually matter if it is private or not.

like image 35
Tomasz Nurkiewicz Avatar answered Dec 01 '25 12:12

Tomasz Nurkiewicz