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:
The source pattern in a declaration can be either a Java expression, statement(s), class, variable, or method.
<? import java.util.LinkedList; import java.util.ArrayList; ?> new LinkedList() => new ArrayList() ;; LinkedList $0; => ArrayList $0; ;;
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$)).
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
$component.show($v) :: $component instanceof java.awt.Component && $v instanceof boolean => $component.setVisible($v) ;;
$component.show() :: $component instanceof java.awt.Component => $component.setVisible(true) ;;
$file.toURL() :: $file instanceof java.io.File => $file.toURI().toURL() ;;
The syntax of a target pattern is similar to the syntax of a source pattern.
Special form: empty == remove
Variable Types | Description |
---|---|
$[a-zA-Z0-9_]+ | Any expression |
$[a-zA-Z0-9_]+; | Any statement |
$[a-zA-Z0-9_]+$ | Any number of subtrees (except statements) |
$[a-zA-Z0-9_]+$; | Any number of statements |
$_ | For patterns undefined and for target patterns and conditions that are automatically bound to the current matched region |
$$[a-zA-Z0-9_]+ | Reserved, do not use |
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
Code | Result |
---|---|
int i; => /*remove-from-parent*/ ;; |
Removes the int i; variable declaration from the surrounding block |
float f; => <!error="err"> ;; |
Shows an error to the user when used from the Inspect and Transform dialog box, will not remove the float f; variable declaration from the surrounding block |
double d; => <!error="err",remove-from-parent=true> ;; |
Shows an error to the user (in the Inspect and Transform dialog box) and will remove the "double d;" variable declaration from the surrounding block |
$1.isDirectory :: $1 instanceof java.io.File ;;