Adding Custom templates in Eclipse for faster Java development

Dear Friends,

As we saw in the last tutorial Eclipse templates for faster java development that eclipse provides some inbuilt templates which we can use to expedite the coding.Apart from that we can create our own custom templates as well for the code which we find repetitive in nature and thus can save our precious time.

So to create a new custom template ,yo need to go to Windows->Preferences



Type Java in the search box and then go to Editor ->Templates


You will see list of inbuilt templates provided by eclipse already.

To add your own template,click on New...


In the Name ,give name which you want to give to the shorthand that will be visible when you will press Ctrl + Space

In Description ,you can mention briefly what this shorthand is about

Context is Java

Automatically insert if checked will automatically insert the code snippet for you on writing shorthand word and pressing ctrl + space,if there is no other matching template is available.
For example,if you write sysout and press ctrl+space,it will write System.out.println(); for you  when automatically Insert is checked.However,if automatically Insert is not checked ,then template proposal will be displayed ,which you need to select then

Pattern : This is the place where your template goes.

I am taking here four examples
1.List Iterator
2.Map Iterator
3.Logger
4.NullCheck



1.List Iterator

Name          : listIterator
Description : iterate over list
Context       : Java
Pattern        :

Iterator<${type}> itr = listVar.iterator();
while(itr.hasNext()){
${type} str = (${type})itr.next();
}



2. Map Iterator :

Name          : mapIterator
Description : iterate over map
Context       : Java
Pattern        :

for (Entry<${keyType:argType(map, 0)},${valueType:argType(map, 1)}> entry : ${map:var(java.util.Map)}.entrySet()) {
  ${keyType} key = entry.getKey();
  ${valueType} value = entry.getValue();
  ${cursor}
}



3. Logger :

Name          : logger
Description : logger Entry
Context       : Java
Pattern        :
private static final Logger logger = Logger.getLogger(${enclosing_type}.class);


4)  Null Check

Name          : NullCheck
Description : check for null value
Context       : Java
Pattern        :

 if (${var} != null) {
 ${line_selection}${cursor}
}



Java editor template variables

There are two types of variables which can be used while writing patterns for java code.These variables are resolved to their actual value when a template is evaluated.

Two types of variables
1) General template variable
2) Java specific template variable

We can use variables in two ways as follow :

Simple variables as below :
${array}

This defines a variable with name array,which can be referenced multiple times and that will resolve to array.

Full variables as below :
${array:var(java.util.iterator)}

This defines a variable with name as array that will resolve to local variable of type java.util.Iterator.It can be referenced multiple times by giving its name without type like ${array}

1) General template variable

VariableDescription
${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 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.
${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.

2) Java specific template variables


VariableDescription
${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.


Any suggestions,feedback,questions on the post are welcome.