Netbeans Debugger Code Evaluator and Variable Formatters--Project Evaluator
Description
The Evaluator project is a simple project defining classes for storing tennis results. In this demo we show how to use the debugger evaluator and how one can customize the variables view using so-called formatters. Note that the evaluator works on code snippets (not only expressions), so you can do almost everything ;-)
Requirements: NB 6.7
Intro
Have a look at the Evaluator project source codes--Main.java and Match.java. Match.java defines class Set storing data about one tennis set and also class Match storing tennis match result (i.e. players names and sets results). Main.java provides a trivial main method creating few match objects.
How to use evaluator and Formatters
Let's play with evaluating features, you might find them pretty useful in a real debugging...
- Toggle a line breakpoint into Main.java at line 23.
- Right-click the Evaluator project in the Projects window and choose Debug--the execution will suspend at the breakpoint.
- Go to the Variables view and have a look at m1 or m2 object. If you want to get an overview about its content, you will have to expand it several times. Boring?
- Press Ctrl+F9 (Main menu->Debug->Evaluate Expression) and paste to the Evaluate Code view following code:
int playerOne = 0;
int playerTwo = 0;
for (Set s : m1.sets) {
if (s.playerOneGames > s.playerTwoGames)
playerOne++;
else playerTwo++;
return m1.playerOneName + " vs. " + m1.playerTwoName + " > "
+ playerOne + "--" + playerTwo;
- Press Ctrl+Enter to evaluate the code and have a look at the result (first line in Variables view). In the Value column, you can see a readable summary of the object. Note that the evaluator accepts much more then expressions, you can use code snippet with e.g. cycles or conditions.
- You might also want to customize Variables view to show more readable results for your objects. Let's go to Variables view left margin and press the button 'Open Formatters Options'. Choose 'Add...' action and fill the form as follows:
Formatter Name--'My Match Formatter' or whatever you want
Class types: evaluator.Match
Check the 'Value formatted as a result of code snippet' and paste to its text area following code:
int playerOne = 0;
int playerTwo = 0;
for (Set s : sets) {
if (s.playerOneGames > s.playerTwoGames)
playerOne++;
else playerTwo++;
return playerOneName + " vs. " + playerTwoName + " > "
+ playerOne + "--" + playerTwo;
It would be nice to see results of individual sets when a Match node gets expanded. Let's check 'Children displayed as' and paste following code to the 'result of code snippet':
String[] children = new String[sets.length];
for (int i = 0; i < sets.length; i++)
children[i] = "[" + sets[i].playerOneGames + " - " + sets[i].playerTwoGames + "]";
return children;
- Choose OK button to close the form and then choose OK to close the options dialog. Have a look back at Variables view to your m1 and m2 objects. From now, Value column provides you with computed summaries updated by any step. Note that you can disable any formatter via Formatters options if you want.





