出于调试目的,我需要更改私有字段的值。
我使用 Eclipse 进行调试,并且可以在调试过程中更改变量,但无法访问私有变量。我尝试在更改值视图中使用反射来手动将字段设置为“可访问”,但看起来不起作用。
您知道任何 IDE/框架/插件或可以允许它的东西吗?
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?
发布评论
评论(4)
在 Eclipse 中,您可以转到变量视图,其中列出了所有变量。
在这里,您可以右键单击要更改的成员变量,然后选择更改值选项,这会弹出一个单独的窗口来更改值。并自那时起生效。
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.
你可以使用反射来设置字段值(Spring提供了方便的
ReflectionTestUtil
):此外,在调试器中设置私有字段的值不应该有任何问题,它是否私有实际上并不重要。
Yuo can use reflection to set field value (Spring provides convenient
ReflectionTestUtil
):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.
刚刚用 eclipse 测试过 - 没问题。测试应用程序是这样的:
我在虚拟线上设置了一个 BP,启动了调试器,然后将变量视图中的
i
值从 5 更改为 6,继续,输出为 6。只有一件事:也许您无法在变量视图中看到私有变量。打开视图菜单(带有三角形的按钮),选择Java并打开缺少的项目。静态常量默认被禁用 - 也许这就是你的问题。
Just tested with eclipse - no problem. The test application was like this:
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.
由于您将变量定义为私有,并且您想要在调试级别修改该值,因此任何 IDE/插件都不会发生这种情况。每个 IDE 都会遵循基本原则。
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.