我可以在调试期间更改私有字段的值吗?

发布于 2024-10-25 05:12:42 字数 134 浏览 8 评论 0 原文

出于调试目的,我需要更改私有字段的值。 我使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-11-01 05:12:42

在 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.

原谅过去的我 2024-11-01 05:12:42

你可以使用反射来设置字段值(Spring提供了方便的 ReflectionTestUtil):

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

此外,在调试器中设置私有字段的值不应该有任何问题,它是否私有实际上并不重要。

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.

三寸金莲 2024-11-01 05:12:42

刚刚用 eclipse 测试过 - 没问题。测试应用程序是这样的:

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

我在虚拟线上设置了一个 BP,启动了调试器,然后将变量视图中的 i 值从 5 更改为 6,继续,输出为 6。

只有一件事:也许您无法在变量视图中看到私有变量。打开视图菜单(带有三角形的按钮),选择Java并打开缺少的项目。静态常量默认被禁用 - 也许这就是你的问题。

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.

多像笑话 2024-11-01 05:12:42

由于您将变量定义为私有,并且您想要在调试级别修改该值,因此任何 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文