当 Java 对象的状态发生变化时,Eclipse 调试器中是否有一种方法可以得到通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1709929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:08:23  来源:igfitidea点击:

Is there a way in the Eclipse debugger to be notified when the state of a Java object changes?

javaeclipsedebugging

提问by Uri

I'm debugging some legacy code where we have a cached object that appears to be changed externally.

我正在调试一些遗留代码,其中我们有一个似乎在外部更改的缓存对象。

If I know the object identifier for it (while debugging), is there some way to "watch" it so that if some other code in the same thread or another thread attempts to modify its state it'll trigger my debug?

如果我知道它的对象标识符(在调试时),有没有办法“观察”它,以便如果同一线程或另一个线程中的其他代码尝试修改其状态,它将触发我的调试?

I can't use just an expression watch for it since there may be references to that object elsewhere and as there are many instances of the same class.

我不能只使用表达式监视它,因为在其他地方可能有对该对象的引用,并且同一个类有许多实例。

采纳答案by Kelly S. French

Set a breakpoint in the code you want to stop in when the value changes.

在值更改时要停止的代码中设置断点。

  • Start in the breakpoint view.
  • Select the breakpoint
  • right-click and goto the "breakpoint properties"
  • Check the 'Enable Condition' box
  • in the text field enter the name of the variable to watch
  • select the 'value of condition changes' radio button
  • 在断点视图中开始。
  • 选择断点
  • 右键单击并转到“断点属性”
  • 选中“启用条件”框
  • 在文本字段中输入要观察的变量的名称
  • 选择“条件变化值”单选按钮

回答by BalusC

If it is declared somewhere as a class or instance variable (should be, how else could you cacheit), then you can also just set a breakpoint on the particular line. It will be called watchpointand will by default be triggered on accessand modification(configureable through breakpoint properties).

如果它在某处被声明为类或实例变量(应该是,否则你怎么能缓存它),那么你也可以在特定的行上设置一个断点。它将被调用watchpoint并默认在访问修改时触发(可通过断点属性进行配置)。