简体   繁体   English

使用Java进行调试时,是否有办法检测ANY变量是否设置为给定值?

[英]When Debugging in Java, is there a way to detect if ANY variable is set to a given value?

I have a very large app that takes a value and makes it disappear SOMEWHERE and I want to see where it is going. 我有一个非常大的应用程序,需要一个值并使它在某处消失,我想知道它的去向。 So what I'm looking for the is the value of the variable so I can find out what variable is holding it. 所以我要寻找的是变量的值,所以我可以找出持有它的变量。 I'm hoping there is some sort of technique, possibly involving a classloader or reflection? 我希望有某种技术,可能涉及类加载器或反射?

Don't think there is an easy way to do it through the debugger, as it lets you specify the variable names, not the value names :) 不要以为有一种简单的方法可以通过调试器进行操作,因为它可以让您指定变量名,而不是值名:)

You should be able to get handles to your classes and iterate through them and iterate through each field in the class, like in: java: get all variable names in a class 您应该能够获取类的句柄并进行遍历,并遍历类中的每个字段,例如: java:获取类中的所有变量名

Based on your value you are looking for, you probably know the type you are seeking, so I'd recommend adding some type checks before you do your checks. 根据您要查找的值,您可能知道要查找的类型,因此建议您在进行检查之前添加一些类型检查。 If you have a method do this for you, then you can build a list with the results, which you would then be able to inspect easily with the debugger. 如果您有为您执行此操作的方法,则可以构建一个包含结果的列表,然后您可以使用调试器轻松对其进行检查。

If you are using Eclipse, or most IDE's for that matter, you can set break points, and use the debugger to step through your processes and see what each variables value is at that point in the code. 如果您使用的是Eclipse或大多数IDE,则可以设置断点,并使用调试器逐步执行过程,并查看代码中每个变量在该点的含义。

If you aren't using Eclipse, or most IDE's for that matter, you might consider doing so, so that you can use the debug feature. 如果您没有使用Eclipse或大多数IDE,则可以考虑这样做,以便可以使用调试功能。

depends on your IDE but the common ones (Eclipse, Netbeans, IntelliJ) allow you to set watches - variables which you'll see their values during debug. 取决于您的IDE,但常见的变量(Eclipse,Netbeans,IntelliJ)允许您设置watches -在调试期间将看到其值的变量。 Also, if you set breakpoints - you can hover over the variable and it'll show its value. 另外,如果您设置了断点-您可以将鼠标悬停在变量上,它将显示其值。

Have you looked into using an IDE such as Eclipse? 您是否考虑过使用Eclipse之类的IDE? There are some pretty nifty debugging features that let you track variables throughout the program and set break points in the code where the program will pause and you can look at all states and even change them. 有一些非常漂亮的调试功能,可让您跟踪整个程序中的变量并在代码中设置断点,程序将在该断点处暂停,您可以查看所有状态甚至更改它们。

If you want to capture a certain value during the iteration of a loop, Catch the value programatically using a If loop and the value. 如果要在循环迭代期间捕获某个值,请使用If循环和该值以编程方式捕获该值。

You can use some intercept to identify the class that has last set the value. 您可以使用一些拦截来识别最后设置该值的类。

Are you getting any exception ? 你有什么例外吗?

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

相关问题 有什么方法可以将RAM位置(在Java中)设置为变量,以便我们可以检索值? - Is there any way to set RAM location (in java) to a variable so that we can retrieve value? 查看在Eclipse中进行调试时设置变量值的位置 - See where variable value is set when debugging in Eclipse IntelliJ中的Java IDEA:如何在调试时查看变量值? - Java in IntelliJ IDEA: How to view the variable value when debugging? 在Java中,如何检测将int值设置为long变量的源 - In Java, how detect source where int value was set to long variable 当我们在Java中将map的键转换为小写时,有没有简单的方法来合并值集? - Is there any simple way to merge value set when we convert key of map to lower case in Java? Java - 在构造函数中设置后,任何使变量不可修改的方法? - Java - Any way to make variable unmodifiable after being set in the constructor? 有什么办法可以 output 变量名而不是值? [JAVA] - Is there any way I can output the variable name instead of the value? [JAVA] 从 Java Set 中获取 *any* 值的好方法? - Good way to get *any* value from a Java Set? 有什么方法可以检测 Java 中的 RTL 语言吗? - Is there any way to detect an RTL language in Java? 如何在调试 Java 代码时使用 Scanner 类对象在变量中设置值? - How can i set value in variable using Scanner class object while debugging Java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM