Time for action – inspecting and watching variables
Finally, it's worth seeing what the Variables view can do.
- Create a breakpoint at the start of the
execute
method. - Click on the hello world icon again.
- Highlight the
openInformation
call and navigate to Run | Step Into Selection. - Select the
title
variable in the the Variables view. - Modify where it says Hello in the bottom half of the variables view and change it to Goodbye:
- Save the value with Ctrl + S (or Cmd + S on macOS).
- Click on resume, and the newly updated title can be seen in the dialog.
- Click on the hello world icon again.
- With the debugger stopped in the
execute
method, highlight the event in the Variables view. - Right-click on the value and choose Inspect (by navigating to Ctrl + Shift + I or Cmd + Shift + I on macOS) and the value is opened in the Expressions view:
- Click on Add new expression at the bottom of the Expressions view.
- Add
new java.util.Date()
and the right-hand side will show the current time. - Right-click on the
new java.util.Date()
and choose Re-evaluate Watch Expression. The right-hand-side pane shows the new value. - Step through the code line by line, and notice that the watch expression is re-evaluated after each step.
- Disable the watch expression by right-clicking on it and choosing Disable.
- Step through the code line by line, and the watch expression will not be updated.
What just happened?
The Eclipse debugger has many powerful features, and the ability to inspect (and change) the state of the program is one of the more important ones.
Watch expressions, when combined with conditional breakpoints, can be used to find out when data becomes corrupted or used to show the state of a particular object's value.
Expressions can also be evaluated based on objects in the variables view, and code completion is available to select methods, with the result being shown with Display.
Pop quiz: debugging
Q1. How can an Eclipse plug-in be launched in debug mode?
Q2. How can certain packages be avoided when debugging?
Q3. What are the different types of breakpoints that can be set?
Q4. How can a loop that only exhibits a bug after 256 iterations be debugged?
Q5. How can a breakpoint be set on a method when its argument is null?
Q6. What does inspecting an object do?
Q7. How can the value of an expression be calculated?
Q8. How can multiple statements be executed in breakpoint conditions?
Have a go hero – working with breakpoints
Using a conditional breakpoint to stop at a certain method is fine if the data is simple, but sometimes there needs to be more than one expression. Although it is possible to use multiple statements in the breakpoint condition definition, the code is not very reusable. To implement additional reusable functionality, the breakpoint can be delegated to a breakpoint utility class.
- Create a
Utility
class in thecom.packtpub.e4.hello.ui.handlers
package with astatic
methodbreakpoint
that returns atrue
value if the breakpoint should stop, andfalse
otherwise:public class Utility { public static boolean breakpoint() { System.out.println("Breakpoint"); return false; } }
- Create a conditional breakpoint in the
execute
method that callsUtility.breakpoint()
. - Click on the hello world icon again, and the message will be printed to the host Eclipse's Console view. The breakpoint will not stop.
- Modify the
breakpoint
method to returntrue
instead offalse
. Run the action again. The debugger will stop. - Modify the
breakpoint
method to take the message as an argument, along with a Boolean value that is returned to say whether the breakpoint should stop. - Set up a conditional breakpoint with the expression:
Utility.breakpoint( ((org.eclipse.swt.widgets.Event)event.trigger).stateMask != 0, "Breakpoint")
- Modify the breakpoint method to take a variable
Object
array, and use that in conjunction with the message to useString.format()
for the resulting message:Utility.breakpoint( ((org.eclipse.swt.widgets.Event)event.trigger).stateMask != 0, "Breakpoint %s %h", event, java.time.Instant.now())