In Eclipse 3.5, under Windows -> Preferences -> Java > Editor -> Templates, I can add code templates. However, these templates can only contain snippets which I can insert into an existing Java class.
Is it possible to create templates for whole Java classes, which I can add for example using File -> New -> My-Java-Class?
Templates are shortcuts used to insert a pre-defined framework of code. The purpose is to save time and reduce the potential for errors in standard, repetitive code units. Once a template is inserted, you can complete the code quickly using manual and automated code entry methods.
What you could do is add a normal code short cut (java --> editor --> templates),
i.e. make an editor template "newcustomclass" be the contents of the class you're talking about.
Then create the new java class in the normal way, delete all the content and then use the "newcustomclass" code template to create the new auto java class.
Here's an example for a simple exception class:
public class ${enclosing_type} extends Exception {
/**
* Constructs with the given throwable
* @param t the throwable to throw
*/
public ${enclosing_type}(Throwable t) {
super(t);
}
/**
* Constructs with the given message
* @param message the message of the exception
*/
public ${enclosing_type}(String message) {
super(message);
}
/**
* Constructs with the given message and the original throwable cause
* @param message the message of the exception
* @param t the original throwable
*/
public ${enclosing_type}(String message, Throwable t) {
super(message, t);
}
}
You can add 'new file wizards' to eclipse, but you'll need to write a new plugin to do it. I don't know of an easy way to do this at runtime, in the style of MS Office templates, which I think is what you're trying to do.
A new mechanism for templates might be a useful plugin, but I can't find anything that does that already.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With