Reputation: 525
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?
Upvotes: 3
Views: 3053
Reputation: 9488
As your are delcaring varible as private and you want to modifiy the value at debug level, this will not happen with any of the IDE/plugins. Every IDE will follow the basic principals.
Upvotes: -2
Reputation: 26428
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.
Upvotes: 3
Reputation: 114837
Just tested with eclipse - no problem. The test 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 set a BP on that dummy line, started the debugger, then I changed the value for i
in the Variables view from five to six, continued and the output was 6.
There's just one thing: maybe, you can't see the private variables in your Variables view. Open the views menu (the button with the triangle), select Java and switch on the missing items. Static constants was disabled by default - maybe that's your problem.
Upvotes: 1
Reputation: 341003
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.
Upvotes: 1