Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding Custom Templates in Eclipse for Faster Java Development

DZone's Guide to

Adding Custom Templates in Eclipse for Faster Java Development

Want to learn more about how to add custom templates in Eclipse? Check out this post on how to implement custom templates for faster Java development.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

As we saw in the last tutorial, Eclipse Templates for Faster Java Development,  Eclipse provides inbuilt templates that we can use to expedite the coding. Apart from that, we can create our own custom templates, as well as for the code, which we find repetitive in nature and can save us a lot of time.

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

First, you will type Java in the search box and then go to Editor ->Templates

You will see the list of inbuilt templates provided by Eclipse already. To add your own template, click on New:

You will provide your name to the shorthand that will be visible when you will press Ctrl + Space.

In Description, you can mention briefly what this shorthand is about — the context is Java.

If checked, it will automatically insert the code snippet for you by writing shorthand word and pressing Ctrl + space, if there is no other matching template is available.

For example, if you write sysout and press CRTL + Space, it will write System.out.println(); for you, when automatically insert is checked. However, if automatically insert is not checked, then a template proposal will be displayed, which you then need to select.

A pattern is a place where your template goes, below I have included four pattern examples:

1. List Iterator  
2. Map Iterator  
3. Logger  
4. NullCheck

List Iterator

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

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


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}
}


Logger

Name: logger 
Description : logger Entry
Context : Java
Pattern:

private static final Logger logger = Logger.getLogger(${enclosing_type}.class);


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 that 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 follows:

Simple variables as below :
${array}


This defines a variable with name array, which can be referenced multiple times and will resolve to an array. Also, we can use the following:

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


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

General Template Variable

${cursor}


This template variable specifies the cursor position when the template edit mode is left. This is useful when the cursor should jump to another place rather than to the end of the template on leaving template edit mode.

${date}


This evaluates the current date.

${dollar}


This evaluates the dollar symbol  $. Alternatively, two dollars can be used:  $$.

${enclosing_method}


This evaluates the name of the enclosing method.

${enclosing_method_arguments}


This evaluates 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}


This evaluates the name of the enclosing package.

${enclosing_project}


This evaluates the name of the enclosing project.

${enclosing_type}


This evaluates the name of the enclosing type.

${file}


This evaluates the name of the file.

${line_selection}


This evaluates the content of all currently selected lines.

${primary_type_name}


This evaluates the name primary type of the current compilation unit.

${return_type}


This evaluates the return type of the enclosing method.

${time}


This evaluates to the current time.

${user}


This evaluates the username.

${word_selection}


This evaluates the content of the current text selection.

${year}


This evaluates the current year.

2) Java-Specific Template Variables

${array}


The above method evaluates the proposal for an array visible in the current scope.

${array_element}


The above evaluates the name for a new local variable for an element of the ${array} variable match.

${array_type}


This evaluates to the element type of the ${array} variable match.


${collection}


This evaluates a proposal for a collection visible in the current scope.

${exception_variable_name}


This is the exception variable name in catch blocks.

${index}


This evaluates a proposal for an undeclared array index.

${iterator}


The above evaluates an unused name for a new local variable of type  java.util.Iterator.

${iterable}


This evaluates the proposal for an iterable or array visible in the current scope.

${iterable_element}


This evaluates the name for a new local variable for an element of the ${iterable} variable match.

s

This evaluates the element type of the ${iterable} variable match.

${todo}


Lastly, this evaluates a proposal for the currently specified default task tag.

Thanks for reading! Please share this post with someone to whom it might be helpful. 

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
eclipse ,java ,tutorial ,custom templates ,templates

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}