Eclipse Zone is brought to you in partnership with:

Berend has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

Visual Guide to Templates in Eclipse

02.04.2008
| 42009 views |
  • submit to reddit

Templates are a structured description of coding patterns that reoccur in source code. Eclipse supports the use of templates to fill in commonly used source patterns. This is a visual guide on how to use existing templates or create new ones.

The Java editor supports the use of templates to fill in commonly used source patterns. Templates are inserted using content assist (Ctrl+Space).

Existing Templates

In Eclipse you can use the built in code templates to make the task of typing the same code over and over again a lot easier.

The built in templates can be found here:

Window->Preferences->Java[+]->Editor[+]->Templates (select)

 

Code templates have certain open spaces for you to fill in given the scenario for which the template is being used. I’ll give you a nice example using a for loop.

Typing the code for a for loop to run through an array of Strings so that you can perform some sort of action with every String in the array can be a tedious task. Using templates in Eclipse can make this task as easy as eating pie, just type for in the editor and press CTRL-SPACE:

 

 

 

 

 

 

 

 

 

 

After pressing enter you get the following code:

 

 

 

 

 

 

As you can see Eclipse selected a sensible default for the array you want to loop over (in this case, the only array in the method). Eclipse allows you to replace the value, notice the dropdown where other available arrays would be listed if there were any.

 

 

 

 

 

When you have tabbed through all the available fields, the cursor will be placed in a sensible position, this way you can complete your for loop.

New Templates

Eclipse isn’t able to provide every template a developer is ever going to need. What Eclipse does is give every Eclipse user the ability to add their own templates

All you need to create a template is an understanding of how to write Java code and use template variables. Variables are resolved to their concrete value when the template is evaluated in its context. Variables may be specified using simple or full syntax:

Simple variables take the following form: ${array}
This defines a variable with name ‘array’ that will resolve to an array. It can be referenced multiple times as is.

Full variables take the following form: ${it:var(java.util.Iterator)}
This defines a variable with name ‘it’ that will resolve to a local variable of type java.util.Iterator. It can be referenced multiple times by simply giving its name without the type: ${it}.

I’ll give you a few of the General Template Variables you can use:

  • ${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} Evaluates to the current date.
  • ${dollar} Evaluates to the dollar symbol ‘$’. Alternatively, two dollars can be used: ‘$$’.
  • ${enclosing_method} Evaluates to the name of the enclosing name.
  • ${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.
  • ${line_selection} Evaluates to content of all currently selected lines.
  • ${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.
  • ${user} Evaluates to the user name.
  • ${word_selection} Evaluates to the content of the current text selection.
  • ${year} Evaluates to the current year.

Java Specific Template Variables:

  • ${id:field(type)} Evaluates to a field in the current scope that is a subtype of the given type. If no type is specified, any non-primitive field matches. Example: ${count:field(int)}
  • ${id:var(type)} Evaluates to a field, local variable or parameter visible in the current scope that is a subtype of the given type. If no type is specified, any non-primitive variable matches. Example: ${array:var(java.lang.Object[])}
  • ${id:localVar(type)} Evaluates to a local variable or parameter visible in the current scope that is a subtype 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 an 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)}.
  • ${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.
  • ${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.

Example

Last but not least i’ll show you how to make a new template that will print a line to the console (log) containing the enclosing class and the method your in, the template also gives you the possibility to enter a variable that will be printed:

 

 

When you type ’sop’ (Ctrl-Space) in the editor you get the following code replacing ’sop’:

 

 

 

 

 

 

 

 

 

You can then replace ‘name’ with the name of the variable, Eclipse will automatically replace the second instance of name too.

 

 

 

 

 

 

 

 

References
Published at DZone with permission of its author, Berend Jansen. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Arek Stryjski replied on Mon, 2008/02/04 - 12:36pm

I'm not using Eclipse, and I'm a bit surprised

This kind of functionality was present in JBuilder 2005 or maybe even JBuilder X , so 4-5 years ago.
I was never thinking this is the most advanced IDE, and now I'm even more surprised it didn't survived competition with Eclipse and NetBeans.

Peter Schuebl replied on Mon, 2008/02/04 - 3:59pm

.. and it is something that eclipse has for a couple of years as well :-)

Still, too many developers don't know about it and it is good to make them aware of this functionality - especially with visual examples.

Rick Ross replied on Mon, 2008/02/04 - 4:57pm in response to: Arek Stryjski

I'm not sure it's even a "comparative" situation? This piece is just a handy visual guide to help people learn and remember some complicated and powerful commands for templates in Eclipse. I don't think it meant to suggest in any way that any other IDE or editor is better, worse, or even involved at all.

Respect to JBuilder, and I don't think there's any subtext to this guide that suggests otherwise. 

Val Huber replied on Fri, 2010/10/22 - 7:09am

I want to make a template for a method that encodes an instance variable name, eg, a setter. I am trying to figure out how to code a template variable that drives code-assist to provide a list of variables. Simple ${name} works well in the context of a method, but does not work in context of class and preceded by text public void set${name}. Suggestions?

Gautam Dev replied on Sun, 2011/04/24 - 3:08pm

Good article. You can use fast code eclipse plug-in to create dynamic templates with any number of fields from a class. It nicely complements the eclipse built-in templates features.
--Gautam

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.