调试时,如何直接在IDE中观看其他对象的私有领域? [英] How can I watch private fields of other objects directly in the IDE when debugging?

查看:93
本文介绍了调试时,如何直接在IDE中观看其他对象的私有领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#是我最喜欢的语言,但在工作时要做Java。我想你可以说我非常喜欢使用Visual Studio IDE。我喜欢它的调试器之一是我可以把手表变量(表达式在Eclipse中)用于任何表达式。所以我可以在调试时仔细查看特定的公有或非公开的字段的值,调用方法等等。没有问题。

C# is the language I am most comfortable with but do Java when at work. I guess you can say I am very comfortable with using the Visual Studio IDE. One of the things I enjoyed about its debugger is that I can put watch variables ("Expressions" in Eclipse) for any expression. So I could take a peek at values of specific fields public or non-public, call methods, etc. with no problem as I am debugging.

似乎没有像我可以在Eclipse中做同样的事情。我必须要求调用该字段的相关getter方法(假设它有一个)。如果没有,我需要暂时添加一个,所以我可以查看它,并记得删除它,当我完成或我只是SOL。

It doesn't seem like I can do the same in Eclipse. I have to resort to calling the associated getter method for the field (assuming it has one). If it doesn't have one, I either have to temporarily add one so I can view it and remember to remove it when I'm done or I'm just SOL.

如何在一个私有字段上放置一个对象的表达式?或者我只是做错了,我必须制作特殊表达来查看他们?

(或者,有没有更好的方法来获取我想要的信息?)

How can I put a watch expression for an object on one of its private fields? Or am I just doing it wrong and I have to craft special expressions to view them?
(Or is there a better way for me to get the information that I want?)

例如,

我有一个 HttpServletRequest 并希望关注 requestURI 。如果我只是看请求变量,它列出了很多的字段,它只是太多的信息。我只对对象中的一件事情感兴趣,所以最小化这个,我想添加一个表的表达式。

I have an HttpServletRequest and want to keep an eye on the requestURI. If I just watch the request variable, it lists a lot of its fields and it's just too much information. I'm only interested in one thing in the object so to minimize this, I want to add a watch expression for that one thing.

字段名称显然是 requestURI 所以我添加了表达式: request.requestURI 。 Eclipse抛出一个错误,表示它不是一个字段(它只是不公开)。而不是直接到现场,我不得不去getter: request.getRequestURI(),这样做不错。但是我不能总是指望吸气剂可用。

The field name is apparently requestURI so I added the expression: request.requestURI. Eclipse throws an error saying it's not a field (it is, it's just not public). Instead of going directly to the field, I had to go to the getter: request.getRequestURI() and that works fine. But I can't always count on the getter being available.



变量视图



表达式视图

推荐答案

Eclipse评估表达式,考虑到声明类型的私有/受保护字段,而不是实际类型

The Eclipse evaluates expression considering the private/protected fields of Declared Type, not of Actual Type.

示例:

Number n1 = new Integer(1000);
Integer n2 = new Integer(1000);

表达式:

n1.value  can't be evaluate
n2.value  returns 1000, perfectly

另一种方法是使用 DetailFormatter (右键单击变量> New Detail Formatter)。

An alternative is use DetailFormatter (right-click in variable > New Detail Formatter).

Eclipse当显示类型对象的文本表示(例如,在变量视图中)时,将使用此表达式而不是toString()方法。

The Eclipse will use this expression instead of toString() method when the textual representation of a object of type is displayed (in Variables View, for example).

您的 DetailFormatter (for ApplicationHttpRequest )可以是:

Your DetailFormatter (for ApplicationHttpRequest) can be:

requestURI 

"URI: " + requestURI + "\n" +
"Path: " + servletPath

这篇关于调试时,如何直接在IDE中观看其他对象的私有领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆