@SuppressWarnings completion

Why it was made
Although NetBeans is capable of suggesting and auto-inserting @SuppressWarnings, it doesn't actually provide code completion or documentation for these values. Indeed, as I've blogged about before, it is tricky to track down the exact enumeration and semantics of these magic values. This is due to the fact that they are entirely dependent on the compiler and IDE. This plugin adds support for the values currently supported by NetBeans 6.1, namely "cast", "deprecation", "divzero", "empty-statement", "empty", "fallthrough", "finally", "serial" and "unchecked". It also tries to explain how and when to use them.



How it was made
Creating plugins for NetBeans is relatively easy if you start by grabbing existing code and have invested in the RCP book. Especially blogs like Geertjan's and Sandip's are virtual goldmines. So for this one, I used the Geertjan's blog entry entitled Caret Aware Code Completion. I had some trouble hooking into the Java parser as the tutorial shows, whenever invoking completion, there would be a lag between obtaining the caret position and resolving the current element. I did not figure out why this is, other than for some reason, the mapping from caret position to the underlying AST went out of sync. So instead I ended up relying only on the build-in Java lexer, which did not exhibit the same lag. This means there were a little bit of manual parsing to do, to assert if indeed the context invites for @SuppressWarnings values. The following scenarios had to be accounted for:


@SuppressWarnings(STRING_LITERAL
@SuppressWarnings({STRING_LITERAL
@SuppressWarnings({STRING_LITERAL, STRING_LITERAL
@SuppressWarnings(value=STRING_LITERAL
@SuppressWarnings(value={STRING_LITERAL, STRING_LITERAL



In other words, we have to consume the Java token stream in reverse, from any open of closed STRING_LITERAL, and pass the following finite-state machine:



This is implemented by a simple recursive descent parser, which looks like this. Should you care to use it for your own parsing of a DSL inside Java String tokens, you can download the complete source code here.


Where to get it
There might still be some bugs and perhaps I a missing something, but if you want to give it a spin, you can download the plugin from NetBeans plugin central.

The hardest thing was to gather information regarding the valid values, since it differs from compiler to compiler and the various IDE's. If you do happen to find a bug or something incorrect, please let me know so I can correct it.

Comments

Anonymous said…
Some information about the usage would be helpful - I've tried to set @SuppressWarnings("serial") to suppress a warning about serialization and nothing happened, no completion was offered by your plugin (NB6.1, Patch 3, Windows).
Perhaps there is a problem when it is inherited from superclasses?
Casper Bang said…
I don't understand if code completion did not work for you. Did it not work for serial or in general. Here's how it looks on my NB6.5:
http://82.103.135.236/serial.png

Regarding usage, write @SuppressWarnings(" and invoke code completion (usually Ctrl+Space) should pop up the completion list as usual.
Anonymous said…
It did not work in general - the IDE offers its proposal, but nothing that your picture shows is happening, no completion list, no explanation text, instead the IDE-internal "No suggestions" is shown.
Anonymous said…
Strange ... at home it doesn't work at all, at office it works after a restart of the IDE.

At least the IDE must be restarted after installation of the plugin, perhaps your plugin should advise a restart?
Casper Bang said…
True, it should advice that. I will update the plugin. Thanks for the feedback.
Anonymous said…
After a reboot it is working now at home as well. :-)

Popular posts from this blog

Oracle SQLDeveloper 4 on Debian/Ubuntu/Mint

@SuppressWarnings values

Rejsekort Scanner