Custom Declarative Hints in NetBeans IDE 7.1

In NetBeans IDE 7.1, when you're in the "Inspect and Transform" dialog (accessible from the Refactor menu, among other places), and click "New", you will see this, which is the start of a custom declarative hint:

Rule Format:

<source-pattern> :: <conditions>
=> <target-pattern> :: <conditions>
=> <target-pattern> :: <conditions>
;;

Example:

$f.toURL() :: $f instanceof java.io.File
=> $f.toURI().toURL()
;;

Basics:

  • Java expression, statement(s), class, variable, method
  • Identifiers starting with $ represent variables: a tree node will be bound to them: $1, $lock, etc.
  • Identifiers starting and ending with $ consume any number of tree nodes:
    • java.util.Arrays.asList($param)
    • java.util.Arrays.asList($params$)

Special Forms:

  • Statement: $statement; (more: $statements$;)
  • 0 or 1: if ($cond) $then; else $else$;
  • Modifiers: $mods$ $type$ $variableName;
  • Multiple catches: try {} $catches$ finally {}
  • More special forms for specific uses

Conditions:

  • $variable instanceof <type>
  • Predefined: hasModifier($variable, PRIVATE)
  • Custom conditions (may use pattern matching)
  • Condition result can be negated (!)
  • && works on condition results

Target Patterns:

  • Similar to source patterns
  • May use variables from source pattern
  • Special form: empty == remove

Demos:

  • $string.equals(“”) => $string.isEmpty()
  • new String($string) => $string

Related Blog Entries:

Comments:

Would this be an idea to use this new cool feature for helping people in migrating to the RCP?

There are some standard ways of migrating the usage of certain APIs from Swing to RCP (e.g. replace panels by JComponents, consiter using RequestProcessor instead of SwingUtil.invokeLater, ...). It would be extremely helpful for the newcomers if some the experienced RCP guys out there created a library of "RCP-migration hints" that would point out the places in code that need to be migrated and maybe even generate some skeleton solutions (or at least contain the URL to a relevant tutorial).

Posted by Andrzej Olszak on November 01, 2011 at 11:25 PM PDT #

Where can we get the full documentation for this?
It has been very usefull at work (and to spread the NetBeans gospel :-))

Posted by alied on January 04, 2012 at 02:22 AM PST #

one more question:
I have successfully created a very useful hint to refactor the usage of logger.debug() and enclose it in a if. However, if the statement is already within the if, still matches. Is there a way to know if the statement is already checked?
this is the hint:

<!description="logger.debug() called without checking if Debug is enabled">
$l.debug($Object); :: $l instanceof org.apache.log4j.Logger
=> if ($l.isDebugEnabled()) $l.debug($Object);
;;

One thing amazed me (and my Eclipse die-hard Technical Leader ;-)) was that it already added braces and formatted the code. Great job!

Posted by alied on January 04, 2012 at 05:03 AM PST #

Project Jackpot resurrected!

Posted by pron on January 15, 2012 at 10:59 AM PST #

This feature has been indispensable. My question relays as to checking for multiple instanceof types.

For example:
$l.error($thrown); :: $l instanceof org.slf4j.Logger :: $thrown instanceof java.lang.Thrown
=>
$l.error($thrown.getLocalizedMessage(), $thrown);
;;

They 'syntax' pattern for finding those messages. I've tried the double colons, comma and space. To no avail. Is this possible or am I stretching the functionality of this awesome feature?

Posted by Jason Cech on February 03, 2012 at 02:08 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today