Home > java > Eclipse tips and tricks – Part 1

Eclipse tips and tricks – Part 1

Eclipse is a pretty good IDE to develop java apps on. However there are some features that you might not be using every day that can save you a lot of time.

Writing your own code templates is one of them. To write your own code template do the following

Go to Window -> Preferences. Select Java -> Editor -> Templates. You can add your own templates here. Templates are nothing but the code that auto completes when you press ctrl+space. For example type syso and ctrl+space. This will automatically fill in System.out.println() into the editor.

Eclipse code templates:
eclipse_code_template
Code template variables:

Eclipse can automatically fill out dynamic code by sniffing out variables in a template. Here are a few you should make yourself aware of

${cursor} - Place the cursor here after code completion
${enclosing_method} – Replace this with the method name where the code completion was called.
${enclosing_method_arguments} – Replace this with the enclosing method arguments
${enclosing_type} – The enclosing class name
${import} – Dynamically import some classes
${importStatic} – Same as above but with static imports
${time} – Current time
${todo} - The //TODO eclipse task

We can now go ahead and define some useful templates. When you type the name of any of these templates and press ctrl+space your custom code will be auto completed. Click on the New button to add a template

Name:call
Description:Log once inside a method. When you type call and ctrl+space a small snippet is auto completed that will print out to the console the method name that is being called and its arguments.

System.out.println("Called: Class: ${enclosing_type} Method: ${enclosing_method} Args: ${enclosing_method_arguments}");

Name:go
Description:Write a program with a main method real quick. Just type go and ctrl+space and be on your way to writing a small program.

public static void main (String... args)
{
    new ${enclosing_type}().go();
}
 
public void go()
{
    ${cursor}
}

Name:log
Description:Automatically instantiate a log4j static variable for this class. Also import the necessary classes that we need for log4j. Of course we assume that log4j is in the classpath :mrgreen:

private static Logger s_logger = LogManager
.getLogger(${enclosing_type}.class.getName());
${imp:import(org.apache.log4j.LogManager,
org.apache.log4j.Logger)}

Name:deb
Description:Log4j debug statement (Do the same for info,warn)

s_logger.debug("${cursor}");

Name:err
Description:Log4j error. Print an error message and print the stack trace on the console.

s_logger.error("An error occurred: ${cursor}", e);

Name:tryf
Description: A variation of the try catch code completion. This one includes finally also

try {
    ${line_selection}${cursor}
} catch (${Exception} ${exception_variable_name}) {
    ${exception_variable_name}.printStackTrace();
}
finally
{
    // ${todo}: Clean up
}

There are many more examples you can think of by yourself. Explore the existing code completions that come with eclipse too. I hope that saves you as much time as it has for me. I will be writing 2 more follow up articles on eclipse. Subscribe to the RSS feed if you would like to keep up to date.




Categories: java Tags: , , ,
  1. Swati
    March 3rd, 2010 at 08:46 | #1

    Good article.:)

  2. June 16th, 2010 at 08:48 | #2

    I can’t get it working inside the JSP editor (WTP). Any idea whether this works too?

  3. June 16th, 2010 at 12:29 | #3

    hmm.. never tried it inside a JSP. Is the ‘context’ of the template under java ?

  4. June 16th, 2010 at 13:34 | #4

    @CertPal

    I tried “Java” and “Java statements”, unfortunately both with no effect. The standard auto completion works well, but a custom template doesn’t.

  5. June 17th, 2010 at 04:10 | #5

    @Drakanor

    Looks like it can be done through Web -> JSP Files -> Editor -> Templates

    I tried out a ‘Hello World’ template using ‘hw’ as the shortcut and it worked.

  1. September 21st, 2009 at 16:41 | #1
  2. June 16th, 2010 at 14:33 | #2
  3. June 22nd, 2010 at 05:46 | #3