Home

 

Setting breakpoints in a Java class

Breakpoints are indicators to the debugger that it should stop execution at that point in the code and let the user inspect the current state and step through through the code. Breakpoints can be set to always trigger when the execution point reaches them, or when a certain condition has been met (conditional).

In the ITSO RedBank sample application, before the balance of an account is updated after withdrawal of funds from an account, the new balance is compared to see if it goes below zero. If there are adequate funds, the withdrawal will complete. If there are not enough funds in the account, an Exception is thrown from the Account class and the showException.jsp is displayed to the user showing an appropriate message.

In this example, we set a breakpoint where the logic tests that the amount to withdraw does not exceed the amount that exists in the account:

In the Enterprise Explorer, select and expand RAD7JPA Æ src Æ itso.bank.entities, and open Account.java in the Java editor.

Locate the processTransaction method.

Tip: You can use the Outline view or expand Account.java in the Enterprise Explorer to find the processTransaction method quickly in the source code.

Place the cursor in the gray bar (along the left edge of the editor area) on the following line of code in the processTransaction method:

if (balance.compareTo(amount) < 0)

Double-click to set a breakpoint marker (Figure | 4-6).

Figure 24-6 Setting a breakpoint in Java

Note: Enabled breakpoints are indicated with a blue circle. Installed breakpoints have an additional check mark overlay. A breakpoint can only be installed when the class the breakpoint is located in has been loaded by the VM.

Right-click the breakpoint and select Breakpoint Properties.

In the Breakpoint Properties window, you can change the details of the breakpoint (Figure | 4-7).

Figure 24-7 Breakpoint properties

If the Hit Count property is set, it causes the breakpoint to be triggered only when the line has been executed as many times as the hit count specified. After being triggered, the breakpoint is disabled.
Selecting Enable Condition allows breakpoints to trigger only when the condition specified in the entry field evaluates to true. This condition is a Java expression. Note that you can use code assist (Ctrl+Space) to see the fields and methods that you can use in this expression. When this condition is enabled, the breakpoint is marked with a question mark on the breakpoint, which indicates that it is a conditional breakpoint.

For example, select Enable Condition, enter the expression amount.doubleValue() >= 50.00, select condition is 'true'. Now the breakpoint will only trigger on transactions of $50 or more.

Click OK to close the breakpoint properties.

You can now run the application and trigger the breakpoint. Note that it is not necessary to restart the Application Server for the new breakpoint to work.

To trigger the breakpoint, perform the following steps:

In the home page, click the RedBank tab, enter 444-44-4444 as the customer number and click Submit.

In the List Accounts page, click the first account (004-444001).

In the Account Details page, select Withdraw and enter 50 in the Amount field.

Click Submit. The processTransaction method is executed and the new breakpoint triggered.
ibm.com/redbooks