Syntax Rules for Declarative Hints

See Also

A hint displays in the Java Source Editor as a result of code inspection that the IDE automatically runs on the sources in focus.
You are notified of an editor hint by a lightbulb icon that appears in the left margin of the Source view. You can read the hint by clicking the lightbulb icon or by pressing Alt-Enter. You can generate the code suggested by the hint by clicking the hint or by pressing Enter.

The syntax rules for declarative hints tell the IDE what kind of code structures to look for and how to transform them.

A rule format for a declarative hint is as follows:

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

Thus, the hint below

   $1 == null                                                                 
=> null == $1                                                                 
;;       

updates the following code:

   if (a == null) {
   System.err.println("a is null");
   }

to:

   if (null == a) {
   System.err.println("a is null");
   }   

This topic introduces the following components of a rule:

Source Pattern
Conditions
Target Pattern
Options

Source Pattern

The source pattern in a declaration can be either a Java expression, statement(s), class, variable, or method.

Identifiers starting with the dollar sign ($) represent variables (for example, java.util.Arrays.asList($param)). In the source pattern, first occurrences of a variable are bound to an actual subtree that exists in the code. Second and following occurrences of the variable in the actual subtree are verified against the subtree that is bound to the variable. A source pattern occurs in the text only if the actual subtree matches the subtree that is bound to the variable.

Identifiers starting and ending with the dollar sign ($) consume any number of tree nodes (for example, java.util.Arrays.asList($params$)).

Conditions

Both source and target patterns can specify additional conditions for a hint. Conditions must follow the :: sign according to the syntax rules.

Conditions have the following limitations:

$1.isDirectory() :: $1 instanceof java.io.File
=> !$1.isFile()
;;

Examples

Target Pattern

The syntax of a target pattern is similar to the syntax of a source pattern.

Special form: empty == remove

Options

Options allow for modifying and fine-tuning the behavior of a hint. The error or warning options allow to specify errors or warnings that are shown in the refactoring UI as appropriate. Suppress warnings allow to add @SuppressWarnings keys.

Examples

See Also
Using Hints in Source Code Analysis and Refactoring
Inspect and Transform Dialog Box

Legal Notices