IntelliJ IDEA 2017.3 Help

Structural Search and Replace Examples

One statement

$Statement$;

Increasing the number of occurrences count to a certain number, you can find sequences of statements that contain up to the specified number of elements.

Method call

$Instance$.$MethodCall$($Arguments$)

This template matches method call expressions. If the number of occurrences is zero, it means that a method call can be omitted.

If statement

if ($Expr$) { $ThenStatements$; } else { $ElseStatements$; }

Search in comments and/or string literals

Consider one wants to find comments or literal containing 'foo'. Search template would be like $SomethingWeWantToFind$ or "$SomethingWeWantToFind$". In case one wants to find comments/string containing some particular words (say, foo as a word), this should be specified as a text constraint.

Search for constructors of the class

The search template Constructors of the class with default variable settings lets you find only one constructor in each class within the specified scope. If the class has several constructors then to find more than one, you need to set the Maximum count option of Occurrences count to Unlimited in the $Class$ variable. For more information, see Edit Variable Dialog.
Note that the class declarations will also be included into the find occurrences' list.

Add try/catch/finally code

If one wants to replace a statement with a try/catch/finally construct, the following pair of search and replace templates can be suggested. The search template is:

$Statements$;

with a certain maximum number of occurrences specified as a constraint.

The replacement template is:

try { $Statements$; } catch(Exception ex) { }

Finding all descendants of a class or all classes that implement a certain interface

Consider the following search templates:

class $Clazz$ extends $AnotherClass$ {}

or

class $Clazz$ implements $SomeInterface$ {}

As the text constraint for the variables $AnotherClass$ or $SomeInterface$, specify the name of the base class or implemented interface.

Finding all such methods

To look for the different implementations of the same interface method, use the following search template:

class $a$ { public void $show$(); }

Specify text constraint for the $show$ variable, and enable the option This variable is the target of the search.

Using @Modifier for finding package local and instance methods

IntelliJ IDEA suggests pre-defined templates for the package local and instance fields of a class. These templates make use of the @Modifier annotation, which helps describe search target, when there is no way to express it using the natural language means.

However, if you need to search for package local or instance methods, you will have to create the corresponding search templates yourself, applying the @Modifier annotation.

To specify criteria for finding all methods with the visibility modifiers package local and instance, use the following search template:

class $Class$ { @Modifier("packageLocal") @Modifier("Instance" ) $ReturnType$ $MethodName$($ParameterType$ $Parameter$); } }

Using 'Contained in Constraints' field in a search

The existing example uses the following template:

LOG.debug($params$);

Placing if('_a) { '_st*; } where _a and _st are variables and * denotes zero or more occurrences in Contained in Constraints field and selecting Invert condition checkbox of Complete Match variable will result a search of logging statements that are not contained in the if statement.

Searching for XML and HTML tags, attributes, and their values

The simplest template to search for a tag is <$a$/>

By placing constraints on the variable $a$, you can specify which tags you want to find. For example, if you specify the text/regexp constraint app.+, you’ll find the tags whose names start with app.

A more versatile template for searching in XML and HTML is <$tag$ $attribute$="$value$"/> By using this template with properly specified search settings and constraints, you can find practically anything that may occur in XML or HTML. For example, if you specify the text/regexp constraint width for the variable $attribute$, you’ll find all the tags that have the width attribute.

Using script constraints

IntelliJ IDEA structural search lets you use advanced constraints that cannot be specified using UI.

See the following template as an example:

ssr template with constraints
ssr script constraint

The constraint is specified using Groovy scripting language and IntelliJ IDEA PSI API for the language you are searching. The Groovy constraint is applicable to any language to which the structural search can be applied.

Last modified: 6 March 2018

See Also