Eclipse Tool Tip #5: Templates
Continuing with the Eclipse Tool Tip Series try this out. Inside a method type ‘ins’ and then hit the ctrl+space keys. You should see the instanceof template in the code assist popup box. Select it and you get the following bit of code generated for you:
if (name instanceof type) {
type new_name = (type) name;
}
The sample code above does not do the Eclipse template functionality any service because I can’t show you how if you update the type in the if condition it will update it in the cast and declaration for you. You have to try it out yourself or take me word for it.
By default you will have templates for try/catch, for/in, private static method, several while loops, etc. And of course you can add your custom templates. For example, I created a try catch template that automatically has our applications exception already filled. So when I type ‘try’ and hit the ctrl+space keys my try/catch template generates code like this:
try {
// TODO: Fill in exception block
}catch(MyApplicationException ex) {
ex.printStackTrace();
logger.error(ex.getMessage());
NotificationService.error(ex.getMessage());
}
If you want to add your own code template you need to open up the preferences (Window > Preferences) and click on Java > Editor > Templates to add a new template. Yes, I know, some where there is some ol’ timer muttering under his breath “Emacs had templates back in ’72.” Hey, I wasn’t there then so I wouldn’t know but templates are a common functionality in NetBeans, IntelliJ, and since Microsoft ‘tailgates’ other technologies I am sure Visual Studio has it as well.