Virendra Oswal
Virendra Oswal's Blog

Virendra Oswal's Blog

Eclipse Code Templates Magic

Eclipse Code Templates Magic

Build your own Code Assist in Eclipse within 5 minutes.

Virendra Oswal's photo
Virendra Oswal
·Aug 9, 2021·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

As a developer in today's world, it's all about how productive we are and reduce manual tasks either via automation to not just improve developer efficiency but also faster development so as to build products faster and ship them out.

One aspect is to reduce boilerplate, but in some case, we still need to build code in certain standards as per organization or your own coding standards.

For example, using Log objects in most of our files so we make use of it to generate log events. Normally we copy-paste logger declaration from other files and update certain details like class or we type it out.

What if we build our own code assist, which generates the same code with Ctrl+Space like other in-build code assists.

In this blog post, we will check the same, how to do in Eclipse using its Code template extensions to make our life as a developer simple and super productive.

Pre-requisites

  • Eclipse IDE (Variant of same can work too, like Spring Tool Suite etc.)

Common Problem

If we see the below code this is quite commonly used code in Software Industry:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Random {
    Logger LOG = LoggerFactory.getLogger(Runner.class);

    public void generate() {
        LOG.info("Generating Random Data...");
    }
}

We define Logger object of type Slf4j for abstraction purposes and initialize using LoggerFactory API by using Current Class metadata so we can differentiate log events raised by different classes in a common log file. Most of the time, we as developers when we define a class we copy paste above Logger declaration and then organize imports. Then update Class Name, sometimes it may result in the class name not being updated which can cause confusion when we see log event mapped against the wrong class in a log file. Happens a lot of time :)

So to avoid such mistakes and improve developer productivity, we will build Code assist for logger declaration for the current class using eclipse Code Template.

Eclipse Code Templates

We may have already used a lot of in-build code assist, for example sysout as below generates System.out.println();

sysout_example.JPG

We will build a similar, with the name of log and when we type it out and press CTRL+Space it will show what code it will generate.

Let's roll.

  1. We will go to Windows --> Preferences --> Java --> Editor --> Templates preference__window.JPG
  2. Click on New
  3. Enter the following details as below: define_log_template.JPG Name A short name to identify the template (in this case 'log' for a template for slf4j declaration and initialization).
    Context The code context when the template will be available. e.g. Java templates will only be available for use when writing Java code.
    Description A short description of the template's code.
    Pattern The pattern is the actual code string that will be inserted into the editor whenever this template is selected.
    Use the Insert Variable button to select from a list of common variables, we have used $import to pull in required imports and $enclosing_type to get class where code assist will be applied.
  4. Once all filled, Click OK. We are ready.
  5. Now time to put log code assist to use, by typing out log and then CTRL+Space to show what code it will generate. log_code-assist-example.JPG
  6. Press Enter, Voila all required code is generated as expected without any fuss 2021-08-08 12-37-21.gif
  7. You can also Export templates as an XML file and share them with other developers in the team, and then can import the same as below export-import.JPG

This was just a small example of how to use Eclipse Code templates to build your own Code assists. It can be used to reduce repetitive boilerplate code for example File Handling, Database Prepared Statement creation, Hibernate Execution, etc.

Resources

Thank you for reading, If you have reached it so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback and suggestions!

I would love to connect with you at Twitter | LinkedIn.

 
Share this