Java Reference
In-Depth Information
with one or more template variables, which you define through the
Edit variables
button. A template should be one of these formed Java constructions:
An expression, such as
new
UserAccount()
■
A statement or sequence of statements, like
index
=
0;
■
A class designator, like
class
UserAccount
implements
Credentials
{}
■
A line or block comment, such as
//
this
should
never
happen
■
The power of templates lies in using variable references. Any code symbol—such
as a method, parameter, or class name—can be replaced with a variable refer-
ence; this substitution also works on comments and
String
literals. Typically, you
don't use explicit statements like the previous examples in your templates; rather,
you use variable placeholders and then specify values for each variable you want
to restrict in the search.
Many basic code structures are already written for you, in the form of
templates. They serve both as useful structured searches and as exam-
ples for how to author your own templates. They're available through
the
Copy existing template
button of the
Structural Search
dialog.
TIP
Working with variables
As in live templates, you specify variables by surrounding their name with
$
sym-
bols, such as
$search-class$
or
$returnType$
. The names themselves are arbi-
trary. As an example, this template represents a
try/catch
block:
try {
$TryStatement$;
} catch($ExceptionType$ $ExceptionDcl$) {
$CatchStatement$;
}
During the search, your template is compared with the source code syntactically
rather than textually, using
IDEA
's internal understanding of Java code structure
and syntax. This means that whitespace and formatting aren't considered signi-
ficant; nor are semantically equivalent differences such as the order of class fields,
methods, or references in an implements list.
Without specifying any constraints on the variable,
IDEA
matches anything for
the variables and relies solely on the Java statement structures—in this case, the
try
and
catch
keywords. This example search finds all
try/catch
blocks in the
project (or whatever scope you have specified). Again, exact layout doesn't matter.
Taking the example a step further, let's say you're interested only in
try/catch