Eclipse 3.4 - Templates and Save Actions

My save actions settings in Eclipse 3.4My save action configuration in EclipseSave actions are actions which are performed when a file is saved, duh?. The configuration can be found by pressing ctrl+3 and typing 'save actions' (Java > Editor > Save Actions).

The very first time I used Emacs I remember thinking, why would anyone use this? But the more I learned about how to configure Emacs the more i liked it. Nowadays it's on my top 10 list of things i would bring to a deserted island.
The point is, the less time you spend struggling with the editor the more time you can spend on developing. Having a properly configured IDE will speed up your development significantly.
I'm not going to list a bunch of hotkeys in this post. Instead I'm going to show the settings for 'code templates' and 'save actions' that I use.

Code templates

Both code templates and regular templates can be exported after they have been configured, which makes them easy to apply to new workspaces. The code template configuration can be found by pressing ctrl+3 and then typing 'code templates' (Java > Code Style > Code Templates).

Method body

It's so easy to use the 'add unimplemented methods'-feature in Eclipse that you sometimes forget that you also need to implement the methods. To make sure unimplemented methods aren't executed replace the existing template with one that thrown an exception when it's called.
throw new UnsupportedOperationException("The method ${enclosing_type}.${enclosing_method} is not yet implemented")

Catch block body / Code in new catch blocks

A better default solution for the catch blocks is to wrap and rethrow the caught exception in a RuntimeException. Don't forget to edit both templates.

throw new RuntimeException(${exception_var});


These templates are generic templates and can be invoked by pressing ctrl+space in the editor. They are found by pressing ctrl+3 and typing 'templates - java' (Java > Editor > Templates).


The only templates i use are logging templates because log-lines are so tedious to write. The first template creates a static final log object with commons logging:

Name: log
${:import(org.apache.commons.logging.Log, org.apache.commons.logging.LogFactory)}
private static final Log log = LogFactory.getLog(${enclosing_type}.class); 

The second template creates a one line log that can be used inside a catch block:

Name: logerr
${:import(org.apache.commons.logging.Log, org.apache.commons.logging.LogFactory)}
LogFactory.getLog(${enclosing_type}.class).error("${cursor}", ${exception_variable_name});

Save Actions

These are my settings right now. The @Override option is really useful for detecting inheritance problems and the rest of them basically just improves readability. I don't think they need any further explanation than that.
  • Convert control statement bodies to block
  • Add final modifier to private fields
  • Add missing '@Override' annotations
  • Add missing '@Deprecated' annotations
  • Remove unnecessary casts
  • Remove trailing white spaces on all lines

Read more

Eclipse documentation Google on code templates

2 comments:» Blog Archive » Eclipse 3.4 - Essential hotkeys said...

[...] Code templates and save actions [...]

123 said...