Eclipse: Java class templates

20,176

Solution 1

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.

Solution 2

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);
    }
}

Solution 3

Yes! Window -> Preferences -> Java -> Code Style -> Code Templates

Select Code in the tree panel and new Java files.

Solution 4

Wanting to do something similar I ended up taking similar approach to Michael Wiles answer using Java editor template. However I had to use ${primary_type_name} rather than ${enclosingType} to populate the class name. In my experience ${enclosingType} would lose the class name once all content was deleted before entering the template command. This is with eclipse Version: 2.2.500.v20190307-0500

As an example, the following are steps to create template command to create a new Spring Service with Lombok logging enabled.

1) First we must create a Java Editor template Preferences->Java->Editor->Template.
* Enter create-spring-service for name
* Leave Java selected for Context field
* Enter the following template in the Pattern field.

package ${enclosing_package};

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ${primary_type_name} {
    ${cursor}
}

2) Create a new class.
3) Open the new class and Select all ctl -> a
4) Then invoke the template by hitting ctl -> space and begin typing the template name create-spring-service

Note: When typing template in the Pattern field you can type $ or hit ctl -> space to view list of predefined template variables.

Online list of available template variables

Solution 5

You can try this eclipse plug-in which will let you create a Java class with lot of configurable parameters e.g. annotations or XML configurations.

Share:
20,176
Bob
Author by

Bob

Updated on July 09, 2022

Comments

  • Bob
    Bob almost 2 years

    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?