Eclipse Templates
I use Eclipse templates everyday. Eclipse templates are code shortcuts; they can help you write code faster and with less errors.
You may know some of the templates that come predefined in Eclipse, such as sysout, try, ifelse, for, runnable and public_method, just to name a few. If so, then you know that, if in any java context, you type ‘sysout Ctrl + Space’ content assist will replace what you have typed with the following code:
System.out.println();
To see all of the predefined templates in Eclipse, go to:
Preferences > Java > Editor > Templates

The real value in Eclipse templates is in writing your own custom templates. For the code that you cannot stub out with generation tools, you can write your own templates to decrease development time.
Some Examples
If you use log4j for all of your applications’ logging, you might consider creating some templates that generate method extrance and exit logging lines…
Template Name: begLog
if(logger.isDebugEnabled()){
logger.debug("Start ${enclosing_method}(${enclosing_method_arguments})");
logger.debug("param1: '"+param1+"'");
logger.debug("param2: "+param2);
}
Template Name: endLog
if(logger.isDebugEnabled())logger.debug("Finish ${enclosing_method}()");
In these custom templates, I have used two predefined eclipse template variables: enclosing_method and enclosing_method_arguments. Once I type the name of my template plus ‘Ctrl + Space’, the code that I have saved a the template code will replace what I have written and Eclipse provides a number of template variables (some are specific to java, but not all are) that can be used substitute various values into the code that will be generated.
Additionally, you might write a template to declare your log4j logger…
Template Name: logger
${:import(org.apache.log4j.Logger)}
private static final Logger logger = Logger.getLogger(${file}.class);
Lastly, if you write custom templates that you think developers in your group might find useful, you can export all or any templates as an xml file that can then be shared and imported by others. Select only those templates that you want to share and then click ‘Export’ to save your templates as xml.
FAQ
Where can I find a complete list of Eclipse template variables?
At eclipse.org, you can find more information about eclipse templates and a complete list of eclipse template varables.
Some Template Variables
${cursor}
${dollar}
${enclosing_method}
${enclosing_method_arguments}
${enclosing_project}
${enclosing_type}
${file}
${line_selection}
${primary_type_name}
${return_type}
${time}
${user}
${word_selection}
${year}
Some Java Specific Template Variables
${array}
${array_element}
${array_type}
${collection}
${index}
${iterator}
${iterable}
${iterable_element}
${iterable_type}
${todo}
How do Eclipse templates differ from Eclipse Code Templates?
Window > Preferences > Java > Code Style > Code Templates
You may have noticed that in the preferences dialog box, you can edit ‘Code Templates’ in addition to templates. These are the rules Eclipse follows to generate classes, methods, comments and all sorts of auto-completion kinds of things it does. For instance, if you right click on a java file and select, source, generate constructor, Eclipse will use the code template to generate the constructor for you—complete with comment.

