NetBeans IDE: Custom hints

Another mostly unnoticed but powerful feature of NetBeans is the possibility of creating custom hints.

Custom hints (a.k.a custom inspections or quickfixes in other IDEs) are available since NetBeans 7.1. They match patterns in Java code and can provide a transformation. This enables you to create small refactorings based on the AST without coding. A typical usecase is the type-safe migration of API – instead of the error-prone plain text-based “search and replace” approach.

You can create your own hint in a special DSL as described in [1],[2],[3]. Geertjan already blogged several times about this topic at [4] and [5]. Yes, the UI for creating/enabling the hints is not very intuitive, but it works.

The best thing is the integration in the editor:

Your custom hints

  • are available in the Java editor like the “standard” hints 2013-02-12_23h45_13
  • can be applied to a file/package/project or even all opened projects. It includes also a changes-preview. This is pretty useful to do apply transformations in a batch.2013-02-12_23h44_25

Here some very basic examples:

<!description=&quot;Convert to assertTrue/assertFalse&quot;>
org.junit.Assert.assertEquals(true, $any)
=> org.junit.Assert.assertTrue($any)
;;
org.junit.Assert.assertEquals($msg, true, $any)
=> org.junit.Assert.assertTrue($msg, $any)
;;
org.junit.Assert.assertEquals(false, $any)
=> org.junit.Assert.assertFalse($any)
;;
org.junit.Assert.assertEquals($msg, false, $any)
=> org.junit.Assert.assertFalse($msg, $any)
;;
<!description=&quot;Remove dead if-branch&quot;>
if (false){
$statements$
}
=>
;;
<!description=&quot;Add extra parenthesis&quot;>
$a && $b
=>
($a) && ($b)
;;
$a || $b
=>
($a) || ($b)
;;
<!description=&quot;Flip operands&quot;>
$a & $b
=>
$b & $a
;;
$a && $b
=>
$b && $a
;;
$a || $b
=>
$b || $a
;;
<!description=&quot;Flip &lt; &gt;&quot;>
$a < $b =>
$b > $a
;;
$a > $b
=>
$b < $a
;;
<!description="JUL to SLF4J">
$jul.info($msg) :: $jul instanceof java.util.logging.Logger && $msg instanceof java.lang.String
=> org.slf4j.Logger.info($msg)
;;

Those patterns can also be used to code more advanced hints [7] [8].

Give it a try and share your patterns! Which selfmade hints do you use on a regular basis?

FYI: Intellj IDEA provides a similar feature called “structural replace” [9]

Update from 2015/02: Added new links

References:
[1] http://wiki.netbeans.org/JavaDeclarativeHintsFormat
[2] https://bitbucket.org/jlahoda/jackpot30/wiki/RulesLanguageAdditionalDocs
[3] https://bitbucket.org/jlahoda/jackpot30/wiki/RulesLanguage

[4] https://blogs.oracle.com/geertjan/entry/custom_declarative_hints_in_netbeans
[5] https://blogs.oracle.com/geertjan/entry/oh_no_override_oh_no
[6] https://blogs.oracle.com/geertjan/entry/oh_no_vector

[7] https://blogs.oracle.com/geertjan/entry/java_hint_in_netbeans_for
[8] https://blogs.oracle.com/geertjan/entry/netbeans_java_hints_quick_dirty

[9] http://www.jetbrains.com/idea/documentation/ssr.html

NetBeans IDE: Verification of plugins and update center URLs for non-verified plugins

You developed a cool NetBeans plugin? Now you want it be to be available via the official update center, so that it can be installed directly from the NetBeans IDE via the built-in Plugins Manager and gets more attention from the users?

Then you have to adhere to the verification and quality rules defined in [1] and [2]. You have to define a license, sign the package and so on. Your plugin will also be tested from several trusted community members and they give a go or a no-go. For some plugin developers this process seems to be too much overhead and too time-consuming. They do not request a verification and therefore their plugin is not available in the Plugins Manager from your IDE. That is a pity, because this way you miss many useful plugins (Geertjans plugins, Compare to clipboard, JVisualVM samples, …). For example there are only 67 of 136 plugins verified for NB 7.2.

Of course you could download the plugin manually from the plugin portal at [3] and install them manually, but this not very comfortable IMHO. It is also not reasonable for me to contact all the authors and beg them for getting their plugins verified. So i opened a ticket [4] for an update center which contains those non-verified plugins. And guess what. The NB team now provides such URLs:

As warning: Using these URLs you are going to install non-verified, experimental and non-quality-checked plugins, which could make your NetBeans IDE unstable/slow down or even crash. Only install plugins you know and trust. You can be sure that the NetBeans developers will not support any issues regarding these plugins.

You want to have more quality plugins available and don’t want to go this inofficial way of experimental update centers? So please encourage the authors of your favorite plugins to request a verification. Thank you!

[1] http://wiki.netbeans.org/FaqPluginRequirements
[2] http://wiki.netbeans.org/PluginPortalQualityCriteria
[3] http://plugins.netbeans.org/
[4] http://netbeans.org/bugzilla/show_bug.cgi?id=225104
[5] http://deadlock.netbeans.org/hudson/job/nbms-and-javadoc/lastStableBuild/artifact/nbbuild/nbms/updates.xml.gz