Want to learn more about how to add custom templates in Eclipse? Check out this post on how to implement Eclipse Custom Templates for Boost 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, without wasting your time lets jump 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.?
[tmh_article_ads]
A pattern is a place where your template goes, below I have included four pattern examples: ⮯
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 the 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 a 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.
[tmh_article_ads]
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:
${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:
${date}
This evaluates the current date.
Enclosing Method:
${enclosing_method}
This evaluates the name of the enclosing method.
Method Arguments:
${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.
Package:
${enclosing_package}
This evaluates the name of the enclosing package.
Enclosing Project:
${enclosing_project}
This evaluates the name of the enclosing project.
Enclosing Type:
${enclosing_type}
This evaluates the name of the enclosing type.
File:
${file}
This evaluates the name of the file.
Line Selection:
${line_selection}
This evaluates the content of all currently selected lines.
Type Name:
${primary_type_name}
This evaluates the name primary type of the current compilation unit.
Return Type:
${return_type}
This evaluates the return type of the enclosing method.
Time:
${time}
This evaluates to the current time.
User:
${user}
This evaluates the username.
Current Word Selection:
${word_selection}
This evaluates the content of the current text selection.
Year:
${year}
This evaluates the current year.
Java-Specific Template Variables
Array:
${array}
The above method evaluates the proposal for an array visible in the current scope.
Array Element:
${array_element}
The above evaluates the name for a new local variable for an element of the ${array}
variable match.
Array Type:
${array_type}
This evaluates to the element type of the ${array}
variable match.
Collection:
${collection}
This evaluates a proposal for a collection visible in the current scope.
Exception Variable Name:
${exception_variable_name}
This is the exception variable name in catch blocks.
Index:
${index}
This evaluates a proposal for an undeclared array index.
Iterator:
${iterator}
The above evaluates an unused name for a new local variable of type java.util.Iterator
.
Iterable:
${iterable}
This evaluates the proposal for an iterable or array visible in the current scope.
Iterable Element:
${iterable_element}
This evaluates the name for a new local variable for an element of the ${iterable}
variable match.
Thanks for reading! Please share this post with someone to whom it might be helpful. ?
Share your thoughts