One of the most underutilised tools by a developer is their Intergrated Development Environment (IDE).  The two main common IDE’s in use for professional Java development are Eclipse and IntelliJ; the latter being my preferred choice.  There are many different features and functionalities that an IDE provides to help the developer produce higher quality code in an efficient manner. In my opinion, one of the features not used enough is Live Templating (IntelliJ) or Templates (Eclipse).  I thought I would write a short blog to document and highlight a few templates I have set up in my IntelliJ that are invaluable to my development experience.

With the release of IntelliJ 2018.2, JetBrains have improved their documentation around Live Templates and how to create them.  There are three types of live templates:

  1. Simple Templates
  2. Parameterised Templates
  3. Surround Templates

I shall provide examples of the first two as these are the most commonly used and a visual guide on how to set up your own.

Simple Templates

This is used when there are no dynamic variables to change – the code is static.  As an example, IntelliJ has pre-setup:

Abbreviation Code
sout
System.out.println();

Additional templates to the above that I have setup are:

Abbreviation Code Live Template
main
public static void main(String[] args){

}
public static void main(String[] args){
    $END$ 
}
LiveTemplateMain

Parameterised Templates

This is used when there are variables that require the user input.  The IDE has been programmed to try and calculate these automatically.  As an example, IntelliJ has setup:

Abbreviation Code
fori
for (int i = 0; i < ; i++) { }

 

Additional templates to the above that I have setup are:

Abbreviation Code Live Template
logger
public static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(.class)
public static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class)
test
@org.junit.Test
public void ()
{
}
@org.junit.Test
public void $NAME$()
{
   $END$
}

LiveTemplateLogger TemplateVariable LiveTemplateTest

To save having to set up the above custom live templates, which can be a tedious task, I have attached here a copy of my live template settings to be imported into your local version of IntelliJ.  I might add to this list over time so it is worth checking periodically for updates.

Importing Settings

As wordpress does not allow .jar attachments, upon download, please change the file extension to .jar (from .zip) before proceeding with the below steps directly taken from JetBrains documentation:

  1. On the File menu, click Import Settings.
  2. Specify the path to the JAR file with the exported live template configuration.
  3. In the Import Settings dialog box, select the Live templates check box and click OK.
  4. After restarting IntelliJ IDEA, you will see the imported live templates on the Live Templates page of the Settings / Preferences Dialog.

Attachments