Watching variable to see if it changes value - java

I'm debugging my code to see if the variable is receiving new values during execution.
How can I watch it and make it stops when the value is changed by something?
I've got a CheckList and set the selectedValues with some values, and for some reason, the values selected are disappearing.

Use the outline view (Window -> Show View -> Outline) to select your variable. Right-click it and select Toggle Watchpoint. It will create a breakpoint for your variable. Right-click this breakpoint, select properties and leave only the checkbox "Field modification" enabled.
I think that this cannot be done for variables declared inside methods, although you can use it for class or instance variables.

You could use a breakpoint with a condition for the value.

You could set a breakpoint on the setter method for the value.

If the debugger does appear to help. esp if the problem doesn't appear when you debug it. You can add logging which can show you where a change is made. e.g.
log.info("Changed "+changeDescription, new Throwable("HERE"));

Related

Eclipse debugger issues with variable display

In my java eclipse debugger the variables field is always empty and the step over and step into icons are disabled help me with this issues
I did try all the options from google like adding checking variables attributes in preferences menu but am still unable to find a solution help me friends
This is a issue that is faced sometimes, somethings you might try out are:-
-> Close variable window and open again
-> Reset the perspective and see if variables now show up.
-> Close the variables tab and then reset the perspective, this should reopen the working variables tab.
-> If none of them still works , you can right click on the variable and select inspect, then it should come up in a popup window
Incase you do not know how to show vairables view, Window --> Show View --> Variables
Reference
In order to see the value of variables, you have to set a breakpoint.
See the documentation here: EclipseDebugging
If you already set it, try resetting the perspective

Eclipse in debug mode do not show variable content?

I am not sure if it is recent issue or it was from the "beginning". Is it normal behavior that we can't see the value of variable in debug mode when we tick on it? (It says ctr+shift+I to watch).
You can also use ctrl + shift + D to display the selected content.
Whatever you are trying to get the contents debug should reach that code.
No, this isn't normal behaviour in Eclipse. As soon as a thread hits a breakpoint and enables the debugging perspective, you should be able to inspect your variables either by hovering, Ctrl+Shift+I or with "Inspect" in your context menu.
In what state is Eclipse when you are trying to watch the variable?

Setting a breakpoint in bytecode

As the title says: is it possible to set a breakpoint in classes with no source available? Does any debugger support this? Bonus points for being able to view the stack and local variables.
edit: Sorry for being unclear from start, but I'm not interested in method breakpoints. I want to set a breakpoint inside the method.
If you are using eclipse, you can set breakpoints on the methods in the class using the outline view.
You can set a method breakpoint using the outline view of the class in question. Then the debugger breaks at the first line of the method.(taken from How to put breakpoint in class without having it's source?)
Right click on the method in eclipse outline view and select Toggle Method Breakpoint
P.S. I have tried this and it works for me.

Is there any way to set breakpoints on all methods of a class?

Is there any way to set breakpoints on all methods of a given class?
I have a huge (2300 lines) legacy class and I need to set breakpoints on all method calls to understand how this mess works.
You can follow the steps below:
Run -> View breakpoints -> Add -> Java Method Breakpoints
Class pattern -> full reference of your class (e.g., mypackage.MyClass)
Method Name -> * (i.e., asterisk wild card)
I have discovered workaround :
1. I have set "Toggle Brakepoint" hotkey to Alt+Numpad 0.
2. After that you can click on first method
3. Use "Toggle Brakepoint"
4. Alt+Down - goto Next Method. ( Alt+Up - goto Previous Method. )
5. Repeat 3 step.
This is similar to Sergey Senkov's answer, but without hotkeys.
In the structure view, click on the first method. Repeat the following for each method:
Context Menu Key
M to toggle the method breakpoint.
Down
There is a plugin for idea:
Simple Toggle All Method Breakpoint.
It allows you to breakpoint all methods / clear all method's breakpoints in one click from context menu on class in Project view.
As Andrey Lavrukhin suggested, there is Simple Toggle All Method Breakpoint, install it through Settings -> Plugins. Works perfectly.
The only way you'll be able to do what you want is to set method breakpoints on each and every method with the class in question. You need to click on the left hand gutter next to the method - a little red circle with 4 dots will appear and you may get a warning saying method level breakpoints can impact performance. You can then further configure the breakpoint (by rightclicking on it, or select shift+F8) and set it so that it breaks on entry, exit or both
I'm afraid theres no way to do this in a single step/setting.

Watching variables contents in Eclipse IDE

How can I watch the contents of several variables (for example, TreeSet's) simultaneously? I can watch contents of one TreeSet, clicking on it in "Variables" window, but I have no idea how to do that for several variables.
You can use Expressions windows: while debugging, menu window -> Show View -> Expressions, then it has place to type variables of which you need to see contents
You can add a watchpoint for each variable you're interested in.
A watchpoint is a special breakpoint that stops the execution of an application whenever the value of a given expression changes, without specifying where it might occur. Unlike breakpoints (which are line-specific), watchpoints are associated with files. They take effect whenever a specified condition is true, regardless of when or where it occurred. You can set a watchpoint on a global variable by highlighting the variable in the editor, or by selecting it in the Outline view.
You can do so by these ways.
Add watchpoint and while debugging you can see variable in debugger window perspective under variable tab.
OR
Add System.out.println("variable = " + variable); and see in console.
And how about selecting the text you want to watch, and then using the shortcut "Ctrl + shift + I"

Categories