简体   繁体   English

在运行时比较eclipse中的2个对象(在调试时)?

[英]Compare 2 objects in eclipse in runtime (while debugging)?

Can I compare 2 Objects using the Eclipse - Expressions tool, am trying to debug a Java program. 我可以使用Eclipse - Expressions工具比较2个对象,我正在尝试调试Java程序。 The max I can do is, add both the objects to Expressions and manually expand the object to compare, I wish there was like, select 2 Object (of same kind - Class, Of course) and say "Compare" and Eclipse parses both the objects and highlights all the differences.. 我可以做的最大的事情是,将两个对象添加到表达式并手动展开对象进行比较,我希望有类似的,选择2个对象(同类 - 类,当然)并说“比较”和Eclipse解析两个对象并突出显示所有差异..

PS: I'm not lazy to do this manually, its just that the Object I'm dealing with is very complicated, like its got 10 levels or arraylist of Objects :) PS:我并不懒于手动执行此操作,它只是我正在处理的对象非常复杂,就像它有10个级别或对象的arraylist :)

Not a elegant solution but it has worked well for me in the past is to include something like gson or other json parser in your classpath. 不是一个优雅的解决方案,但它在过去对我来说效果很好,就是在你的类路径中加入类似gson或其他json解析器的东西。 Then in the Display view type: 然后在“显示”视图中键入:

new Gson().gson.toJson(yourObject1)  

and

new Gson().gson.toJson(yourObject2)

Then use the json ouput save it to files and use a file comparison tool. 然后使用json输出将其保存到文件并使用文件比较工具。 Longwinded but with complex objects its often worth it. Longwinded但复杂的物体往往是值得的。

They can be as detailed or simple as you want them to be. 它们可以像您希望的那样详细或简单。 Using a equals expression in the Display view may tel you if two objects are the same, but if their different, it won't tell you what those differences are. 如果两个对象相同,则可以在“显示”视图中使用等号表达式,但如果两个对象不同,则不会告诉您这些差异是什么。 Consider using Eclipse detail formatters. 考虑使用Eclipse详细格式化程序。 They are awesome. 他们真棒。

Eclipse's Inspect view can often be littered with unwanted data that just clutters your mind and what your really interested in. The ouput you decide to display is your choice with detail formatters. Eclipse的Inspect视图通常会充斥着不必要的数据,这些数据会让您的思绪变得混乱,您真正感兴趣的是。您决定显示的输出是您选择的详细格式化程序。

See the link below for an idea of how to use them. 请参阅以下链接,了解如何使用它们。

http://www.howardism.org/Technical/Eclipse/Eclipse_Detail_Formatter.html http://www.howardism.org/Technical/Eclipse/Eclipse_Detail_Formatter.html

Find "Display" view under debug category. 在调试类别下找到“显示”视图。 It is not open by default in the java debug perspective. 默认情况下,它不会在java调试透视图中打开。 You will be able to execute pretty much arbitrary java statements during the debug session. 您将能够在调试会话期间执行几乎任意的java语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM