Sunday, May 25, 2014

Eclipse Templates

There is always a saying of re-use. Write less and do more. We will see how to create the most used templates and use them while writing the code in Eclipse.

Built-in

Eclipse provides some built-in templates for "for", "while", "ifelse" etc. Enter a character and Press Ctrl+Space to get the list of templates defined starting with that character.

Usage:

For example, let's look the following example. I would like to iterate over a list and do some processing over it. Enter "for" and then Ctrl+Space. Eclipse shows a list of options for "for"


Select one of the template and Enter. The selected code will be replaced and the variables that you would like to replace will be highlighted.
Here, iterator is the variable highlighted. Change the first occurrence of the iterator, which will change all the others.
Finally, the code looks like.

How to create custom templates

Goto Preferences. Select Java -> Editor -> Templates. There we can find list of built-in templates provided by Eclipse.
Click on "New", Opens a dialogue and start writing.

Example:

I will show how to create a template for create logger and logging statements.

Logger

${:import(org.slf4j.Logger,
org.slf4j.LoggerFactory)}

private static final Logger ${log_name} = 
   LoggerFactory.getLogger(${enclosing_type}.class.getName());

Logger Statements

Debug Logging
if(${logger:var(org.slf4j.Logger)}.isDebugEnabled()) 
   ${logger:var(org.slf4j.Logger)}.debug(${loggerstring});
${cursor}
Info Logging
${logger:var(org.slf4j.Logger)}.info(${loggerstring});
${cursor}
Error Logging
${logger:var(org.slf4j.Logger)}.error(${loggerstring},${exception_variable_name});
${cursor}

Explanation:


  • All the statements/words which comes starting with $ are the variables which can be replaced
  • ${log_name} is the name of the Logger that we can replace when used the template
  • :import will import the necessary classes when the particular template is used.
  • var(some_class) will be replaced with the variable of type "some_class" in the context.
  • ${cursor} will be the position of the cursor after template insertion and modification of the variable names.

More and more

There are many other default variables/code definitions used.
  • ${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 templates are:

  • ${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.
Happy Learning!!!