Eclipse Tip: Add imports with Code Templates

Code templates have been in Eclipse for a long time. I have blogged about it a long time ago. I have been using it for inserting commonly used statements like logging statements…

When I am inserting a logger declaration I always end up with this:

Eclipse does not know which logger to import. In my case I want it to import the apache logger. Now there is a way you can tell Eclipse to import this automatically. Just edit the code template and add the following:

${:import(org.apache.log4j.Logger)}

This is cool and saves a bit of time. :-) For more information on template variables look here.

Vasanth Dharmaraj is a Java Architect currently working for Western Union living in the Bay Area. He is interested in programming, gadgets, formula one... You can follow him on Twitter.

Posted in Eclipse, Java
2 comments on “Eclipse Tip: Add imports with Code Templates
  1. Omar says:

    Hi Vasanth,

    Do I need some aditional steps? Like restart, a particular version? Because I already have a getlogger template without the import enhacement and still needs to organize the imports after the template call.

    This is the template code:
    ${:import(org.apache.log4j.Logger)}
    private final static Logger logger = Logger.getLogger( ${enclosing_type}.class );

    I am currently using Eclipse SDK 3.3

  2. I should have mentioned this is a new variable in Eclipse 3.4.

Ads By Google