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:
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).
Where can we get the full documentation for this?
It has been very usefull at work (and to spread the NetBeans gospel :-))
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!
Project Jackpot resurrected!
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?
Hi there,
One question: I'd like to be able to transform this:
@ServiceLocator
private myServiceLocator;
to this
@ServiceLocator
private transient myServiceLocator;
Is there a way to write a declarative Hint for this?