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]. Geertjan already blogged several times about this topic at [2] and [3]. 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="Convert to assertTrue/assertFalse">
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="Remove dead if-branch">
if (false){
$statements$
}
=>
;;
<!description="Add extra parenthesis">
$a && $b
=>
($a) && ($b)
;;
$a || $b
=>
($a) || ($b)
;;
<!description="Flip operands">
$a & $b
=>
$b & $a
;;
$a && $b
=>
$b && $a
;;
$a || $b
=>
$b || $a
;;
<!description="Flip &lt; &gt;">
$a < $b =>
$b > $a
;;
$a > $b
=>
$b < $a
;;

Those patterns can also be used to code more advanced hints [5] [6].

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” [7]

References:
[1] http://wiki.netbeans.org/JavaDeclarativeHintsFormat

[2] https://blogs.oracle.com/geertjan/entry/custom_declarative_hints_in_netbeans
[3] https://blogs.oracle.com/geertjan/entry/oh_no_override_oh_no
[4] https://blogs.oracle.com/geertjan/entry/oh_no_vector

[5] https://blogs.oracle.com/geertjan/entry/java_hint_in_netbeans_for
[6] https://blogs.oracle.com/geertjan/entry/netbeans_java_hints_quick_dirty

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

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s