java 在 IntelliJ 调试中保存对象的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36356019/
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
Save state of object in IntelliJ debug?
提问by Moritz Schmitz v. Hülst
Does anyone know whether it is possible to save the state of an object while debugging?
有谁知道在调试时是否可以保存对象的状态?
I would like to compare the state of an object in two different execution cycles.
我想比较两个不同执行周期中对象的状态。
Bonus Question:Anyone with experience in writing IntelliJ plugins? Does the IntelliJ SDK allow to access debug values in debug mode? Read them out of IntelliJ cache and write them to disk?
额外问题:任何有编写 IntelliJ 插件经验的人?IntelliJ SDK 是否允许在调试模式下访问调试值?从 IntelliJ 缓存中读取它们并将它们写入磁盘?
采纳答案by Andrew Terekhine
As a very simple solution you can use the Fully Expand Tree Nodeaction for objects in Variablesor Watchesviews. This action is bound to Numpad *key (*) by default and opens the whole object tree. Then you select all the elements of opened object tree with shift and copy them to clipboard.
作为一个非常简单的解决方案,您可以使用完全展开树节点为目标的行动变量或手表的看法。此操作默认绑定到Numpad *键 ( *) 并打开整个对象树。然后用 shift 选择打开的对象树的所有元素并将它们复制到剪贴板。
回答by Josep Oriol Soler
My best workaround to save an object state is to use the Evaluate tool when I have the object in the desired state and using Gson library convert it to a JSON, then at the test setup I copy the JSON as a String and convert it to a Java object again. May be a bit rude but for really large and complex objects it can save you a lot of time.
我保存对象状态的最佳解决方法是在对象处于所需状态并使用 Gson 库将其转换为 JSON 时使用 Evaluate 工具,然后在测试设置中我将 JSON 复制为字符串并将其转换为再次是Java对象。可能有点粗鲁,但对于非常大和复杂的对象,它可以为您节省大量时间。
回答by maaw
To expand on Josep's answer, your best bet is to import Google's Gson libraryinto your project and run:
要扩展 Josep 的答案,最好的办法是将Google 的 Gson 库导入您的项目并运行:
Gson gson = new Gson();
gson.toJson(yourObject);
Then copy the value from the result.
然后从结果中复制值。
Here's an example on IntelliJ IDEA:
这是一个关于 IntelliJ IDEA 的例子:
回答by Anton Skovorodko
In IntelliJ 2016, you have an option to "View Text" when you right click on a variable in "Variables" window while debugging
在 IntelliJ 2016 中,当您在调试时右键单击“变量”窗口中的变量时,您可以选择“查看文本”