Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: Java class templates

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?

like image 253
Bob Avatar asked Jan 21 '10 13:01

Bob


People also ask

What is name template in eclipse?

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.


2 Answers

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);
    }
}
like image 81
Michael Wiles Avatar answered Sep 27 '22 20:09

Michael Wiles


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.

like image 29
Kothar Avatar answered Sep 27 '22 19:09

Kothar