Friday, February 24, 2012

Templating in Eclipse

A great but underused feature in Eclipse (and most other IDE:s) is code templates. I am often declaring a logger member variable which looks like this:
private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
To make this easier, I can create a template, by going to Window->Preferences, Java->Editor->Templates->New...
I fill in a shortcut, for example log, choose "Java Type Members" in the context select box, a short description and then in the "pattern" textarea I write:
private static final Logger LOG = LoggerFactory.getLogger(${enclosing_type}.class);

Now when I write log followed by Ctrl+Enter, I get the whole thing written for me.

No comments:

Post a Comment