Saturday, November 22, 2008

Eclipse Code Templates

This post is out-of-date! For the latest version, go here: "Useful Eclipse Templates for Faster Coding".

In this post, I will share some of my Eclipse Java Code Templates.

Templates are simply "magic words" or shortcuts to standard blocks of code or text. They are very handy because once you have them setup you don't have to waste time writing boilerplate code any more! An example of a pre-defined template in Eclipse is sysout which expands to System.out.println();. All you have to do is type sysout followed by Ctrl+Space to insert the statement in your java source file.

To see what templates are defined in Eclipse:

  • Open your Preferences dialog by going to Windows > Preferences
  • On the navigation tree on the left, go to Java > Editor > Templates
  • You will see a list of pre-defined templates
  • You can add new ones by pressing the "New..." button
Here is a list of templates I have created:

1. logger

Name: logger
Context: Java statements
Description: create new Logger
Pattern:

${:import(org.apache.log4j.Logger)}
private static final Logger logger =
       Logger.getLogger(${enclosing_type}.class);
2. logd

Name: logd
Context: Java statements
Description: logger debug
Pattern:

if(logger.isDebugEnabled())
     logger.debug(${word_selection}${});${cursor}
3. logi

Name: logi
Context: Java statements
Description: logger info
Pattern:

logger.info(${word_selection}${});${cursor}
4. logerr

Name: logerr
Context: Java statements
Description: logger error
Pattern:

logger.error(${errorMessage}, ${e});
5. readfile

Name: readfile
Context: Java
Description: read text from file
Pattern:

${:import(java.io.BufferedReader,
          java.io.FileNotFoundException,
          java.io.FileReader,
          java.io.IOException)}
BufferedReader in = null;
try {
   in = new BufferedReader(new FileReader(${fileName}));
   String line;
   while ((line = in.readLine()) != null) {
      ${process}
   }
}
catch (FileNotFoundException e) {
   logger.error(e) ;
}
catch (IOException e) {
   logger.error(e) ;
} finally {
   if(in != null) in.close();
}
${cursor}
6. for (iterate over map)

Name: for
Context: Java statements
Description: iterate over map
Pattern:

for(Map.Entry<${key:argType(map,0)},${value:argType(map,1)}> entry :
                    ${map:var(java.util.Map)}.entrySet()) {
    ${key} key = entry.getKey();
    ${value} value = entry.getValue();
    ${cursor}
}
Do YOU have any useful templates? If so, share them in the comments!

9 comments:

  1. Anonymous2:28 PM

    Neat, but where's that Java projectile game you promised?

    ReplyDelete
  2. Anonymous9:33 AM

    I like play online game, I also buy FFXI Gil and FFXI Gil, the FFXI gold is very cheap, and use the Final Fantasy XI gold can buy many things, I like cheap Final Fantasy XI Gold, thanks, it is very good.

    I like play online game, I also buy flyff penya and flyff penya, the flyff money is very cheap, and use the flyff gold can buy many things, I like cheap penya, thanks, it is very good.

    ReplyDelete
  3. Matthias11:48 AM

    Name: Singleton
    Context: Java type members
    Description: Singleton design pattern
    Pattern:

    private static ${enclosing_type} INSTANCE;

    private ${enclosing_type}(){
    // TODO Auto-generated constructor stub
    }

    /**
    * Retrieves the single instance of ${enclosing_type}.
    * @return The instance of ${enclosing_type}.
    */
    public static ${enclosing_type} getInstance(){
    if (INSTANCE == null)
    INSTANCE = new ${enclosing_type}();
    return INSTANCE;
    }

    ${cursor}

    ReplyDelete
  4. Anonymous10:54 PM

    This singleton isn't thread-safe.

    ReplyDelete
  5. The readfile template is cool.

    ReplyDelete
  6. Anonymous8:11 AM

    Good Contents!

    ReplyDelete
  7. Gerald Fnord7:45 PM

    Any way of doing string manipulation on the variables?

    ReplyDelete
  8. Gerald Fnord9:28 PM

    I want to have a private member mFoo back [gs]etters with signature source like
    public void setFoo(${enclosing_type} pFoo);
    public ${enclosing_type} getFoo() ;
    ...so I'm looking for a way of going from 'mFoo' to 'Foo'.

    ReplyDelete
  9. Anonymous7:35 PM

    seems in.close throws an exception

    ReplyDelete