Java Editor Template Variables

Template variables may be used in the template pattern. Variables are resolved to their concrete value when the template is evaluated in its context. Variables may be specified using simple or full syntax:

If there are several possible matches for a variable, they may be presented as proposals to the user.

General Template Variables

Both Java and Javadoc context define the following variables:

Variable Description
${cursor} Specifies the cursor position when the template edit mode is left. This is useful when the cursor should jump to another place than to the end of the template on leaving template edit mode.
${date}
${id:date(format[, locale])}
Evaluates to the current date in the specified format and locale. format and locale are optional parameters. format is a pattern compatible with java.text.SimpleDateFormat. locale is an RFC 3066 locale ID, typically in the form en_US for language en and country US.

Examples:
${date}
${iso:date('yyyy-MM-dd HH:mm:ss Z')}
${d:date('EEEE dd MM yyyy', 'fr_CH')}

${dollar} Evaluates to the dollar symbol $. Alternatively, two dollars can be used: $$.
${enclosing_method} Evaluates to the name of the enclosing method.
${enclosing_method_arguments} Evaluates to a comma separated list of argument names of the enclosing method. This variable can be useful when generating log statements for many methods.
${enclosing_package} Evaluates to the name of the enclosing package.
${enclosing_project} Evaluates to the name of the enclosing project.
${enclosing_type} Evaluates to the name of the enclosing type.
${file} Evaluates to the name of the file.
${id:line_selection[(default)]} Evaluates to the selected text for multiple lines. 'default' is an optional parameter, which specifies the text if the selected text is empty.

Examples:
${line_selection}
${currentLine:line_selection(myStringVariable)}
${currentLine:line_selection('"A default text"')}
${primary_type_name} Evaluates to the name primary type of the current compilation unit.
${return_type} Evaluates to the return type of the enclosing method.
${time} Evaluates to the current time. Use the 'date' variable if you want to specify the format.
${user} Evaluates to the user name.
${id:word_selection[(default)]} Evaluates to the selected text. 'default' is an optional parameter, which specifies the text if the selected text is empty.

Examples:
${word_selection}
${currentWord:word_selection(myStringVariable)}
${currentWord:word_selection('"Press me"')}
${year} Evaluates to the current year.

Java Specific Template Variables

The Java context additionally defines the following variables. Note: In the table below, id is a user-chosen name of a new variable.

Variable Description
${id:field(type[,type]*)} Evaluates to a field in the current scope that is a subtype of any of the given types. If no type is specified, any non-primitive field matches.

Example:
${count:field(int)}

${id:var(type[,type]*)} Evaluates to a field, local variable or parameter visible in the current scope that is a subtype of any of the given types. If no type is specified, any non-primitive variable matches.

Example:
${container:var(java.util.List, 'java.lang.Object[]')}

${id:localVar(type[,type]*)} Evaluates to a local variable or parameter visible in the current scope that is a subtype of any of the given type. If no type is specified, any non-primitive local variable matches.

${array} is a shortcut for ${array:localVar('java.lang.Object[]')}, but also matches arrays of primitive types.
${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays.

${id:argType(variable, n)} Evaluates to the nth type argument of the referenced template variable. The reference should be the name of another template variable. Resolves to java.lang.Object if the referenced variable cannot be found or is not a parameterized type.

Example:
${type:argType(vector, 0)} ${first:name(type)} = ${vector:var(java.util.Vector)}.get(0)

${id:elemType(variable)} Evaluates to the element type of the referenced template variable. The reference should be the name of another template variable that resolves to an array or an instance of java.lang.Iterable. The elemType variable type is similar to ${id:argType(reference,0)}, the difference being that it also resolves the element type of an array.

${array_type} is a shortcut for ${array_type:elemType(array)}.
${iterable_type} is a shortcut for ${iterable_type:elemType(iterable)}.

${id:newName(reference)} Evaluates to a non-conflicting name for a new local variable of the type specified by the reference. The reference may either be a Java type name or the name of another template variable. The generated name respects the code style settings.

${index} is a shortcut for ${index:newName(int)}.
${iterator} is a shortcut for ${iterator:newName(java.util.Iterator)}.
${array_element} is a shortcut for ${array_element:newName(array)}.
${iterable_element} is a shortcut for ${iterable_element:newName(iterable)}.

${id:newType(qualifiedTypeName)} Evaluates to a type name given the fully qualified Java type name. Evaluates to a simple type name and an import if no conflicting type exists. Evaluates to a fully qualified type name otherwise.

Example:
${type:newType(java.util.Iterator)}

${:import(type[,type]*)} Adds an import statement for each type. Does nothing if the import already exists. Does nothing if a conflicting import exists. Evaluates to nothing.

Example:
${:import(java.util.List, java.util.Collection)}

${:importStatic([qualifiedName[,qualifiedName]*])} Adds a static import statement for each qualified name that is not already imported. The qualifiedName is the fully qualified name of a static field or method, or it is the qualified name of a type plus a .* suffix, enclosed in single quotes ''. Does nothing if a conflicting import exists. Evaluates to nothing.

Example:
${is:importStatic(java.util.Collections.EMPTY_SET, 'java.lang.System.*')}

${id:link(proposal[,proposal]*)} Evaluates to id if the list of proposals is empty, evaluates to the first proposal otherwise. The evaluated value is put into linked mode. A proposal window shows all the given proposals.

Example:
java.util.Collections.${kind:link(EMPTY_SET, EMPTY_LIST, EMPTY_MAP)}

${array} Evaluates to a proposal for an array visible in the current scope.
${array_element} Evaluates to a name for a new local variable for an element of the ${array} variable match.
${array_type} Evaluates to the element type of the ${array} variable match.
${collection} Evaluates to a proposal for a collection visible in the current scope.
${exception_variable_name} Exception variable name in catch blocks.
${index} Evaluates to a proposal for an undeclared array index.
${iterator} Evaluates to an unused name for a new local variable of type java.util.Iterator.
${iterable} Evaluates to a proposal for an iterable or array visible in the current scope.
${iterable_element} Evaluates to a name for a new local variable for an element of the ${iterable} variable match.
${iterable_type} Evaluates to the element type of the ${iterable} variable match.
${todo} Evaluates to a proposal for the currently specified default task tag.

Related concepts

Templates

Related reference

Editing templates
Templates preference page
Java content assist
Task tag preferences
Code templates preferences
Code style preferences