Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Netbeans Variable Formatters?

Tags:

netbeans

By default when viewing watches/variables of objects in Netbeans, it shows its address instead of its value. This is quite tiresome since I have to expand the variable to see its real value (e.g. for Double, Integer, Date, etc). As it turns out, Netbeans has "Variable formatters" but there is hardly any documentation that i can find for it.

How would I go about displaying e.g. a simple Date variable in a human readable format in the Watches/Variables window? I don't fully understand the "Edit Variable Formatter" dialog.

I was able to properly do it for Double and Integer by using the following code snippet: toString()

So the code seems to run in the context of the Double/Integer class. How would I refer to the actual variable if I need to do something more advanced such as:

return DateHelpers.formatDate(dateVariableName??, "yyyy-MM-dd");
like image 788
AtliB Avatar asked Oct 27 '11 12:10

AtliB


2 Answers

In the variables view, you have a small $ icon (at the top left) which tooltip says :"Show variable value as toString() or formatted value".

Just click that, it will show you the "value" of those variables.

EDIT: If you want to add a variable formatter, it's very simple. On the variable formatter view, just click the "Add ..." button then:

  • In "Formatter Name" put the name of you formatter eg. "My Date formatter"
  • "Class Types" put your complete class name eg. java.util.Date
  • Select "Value formatted as a result of code snippet" and type the code to apply. For instance:

toString()

but if you want to manipulate the data or display some other thing you can. For instance:

toString() + " (" + getTime() + ")"

Which will display the time in human readable format plus the time as a long.

Don't forget to select the $ icon on the view to apply your formatter.

like image 187
Alex Garcia Avatar answered Oct 05 '22 23:10

Alex Garcia


My answer won't solve the question. It will rather echo the previous answers. First, I haven't seen the $ sign in the variables-panel in NetBeans. Seems it was replaced with a context menu in current versions.

I haven't found the answer to the actual question, as of how you would reference the variable to debug, within the "Variable Formatters" Dialog. Something like "this" or "$1" isn't working definitely. Also the facility does not seem to know about Standard Java JRE classes like SimpleDateFormatter.

So when debugging Java JRE classes, I guess you have to live with what they're offering in terms of public methods.

Here's a workaround for the especially user friendly Date class, if you're stuck with a JDK below version 8 (as me). Just create a new Variable Formatter in NetBeans via

Tools > Options > Java > Variable Formatters > Add

Then in the "Class Types" editfield enter:

java.util.Date

Under "Value formatted as a result of code snippet" use one of the next snippets.

// German format - "dd.MM.yyyy hh:mm"
((getDate() < 10) ? ("0" + getDate()) : getDate()) + "." + ((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "." + (getYear() + 1900) + " " + ((getHours() < 10) ? "0" + getHours() : getHours()) + ":" + ((getMinutes() < 10) ? "0" + getMinutes() : getMinutes()) + ":" + ((getSeconds() < 10) ? "0" + getSeconds() : getSeconds())

// US format - "MM/dd/yyyy hh:mm"
((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "/" + ((getDate() < 10) ? ("0" + getDate()) : getDate()) + "/" + (getYear() + 1900) + " " + ((getHours() < 10) ? "0" + getHours() : getHours()) + ":" + ((getMinutes() < 10) ? "0" + getMinutes() : getMinutes()) + ":" + ((getSeconds() < 10) ? "0" + getSeconds() : getSeconds())

// ISO-8601 - "yyyy-MM-dd hh:mm"
(getYear() + 1900) + "-" + ((getMonth() < 9) ? ("0" + (getMonth() + 1)) : (getMonth() + 1) ) + "-" + ((getDate() < 10) ? ("0" + getDate()) : getDate()) + " " + ((getHours() < 10) ? ("0" + getHours()) : getHours()) + ":" + ((getMinutes() < 10) ? ("0" + getMinutes()) : getMinutes()) + ":" + ((getSeconds() < 10) ? ("0" + getSeconds()) : getSeconds())

The next snippet could also come in handy, when your lost in the the debug-output overkill of an instance of java.util.Calendar:

// German format - "dd.MM.yyyy hh:mm"
((get(5) < 10) ? ("0" + get(5)) : get(5)) + "." + ((get(2) < 9) ? ("0" + (get(2) + 1)) : (get(2) + 1) ) + "." + (get(1)) + " " + ((get(10) < 10) ? "0" + get(10) : get(10)) + ":" + ((get(12) < 10) ? "0" + get(12) : get(12)) + ":" + ((get(13) < 10) ? "0" + get(13) : get(13))
like image 37
JackLeEmmerdeur Avatar answered Oct 05 '22 23:10

JackLeEmmerdeur