Eclipse:Java 类模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:19:58  来源:igfitidea点击:

Eclipse: Java class templates

javaeclipsetemplates

提问by Bob

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.

在 Eclipse 3.5 中,在 Windows -> Preferences -> Java > Editor -> Templates 下,我可以添加代码模板。但是,这些模板只能包含我可以插入到现有 Java 类中的片段。

Is it possible to create templates for whole Java classes, which I can add for example using File -> New -> My-Java-Class?

是否可以为整个 Java 类创建模板,例如我可以使用 File -> New -> My-Java-Class 添加模板?

采纳答案by Mike Houston

You can add 'new file wizards' to eclipse, but you'll need to write a new pluginto 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.

您可以将“新文件向导”添加到 eclipse,但您需要编写一个新插件来执行此操作。我不知道有什么简单的方法可以在运行时以 MS Office 模板的样式执行此操作,我认为这就是您要尝试执行的操作。

A new mechanism for templates might be a useful plugin, but I can't find anything that does that already.

模板的新机制可能是一个有用的插件,但我找不到任何已经这样做的东西。

回答by Michael Wiles

What you could do is add a normal code short cut (java --> editor --> templates),

你可以做的是添加一个普通的代码快捷方式(java --> 编辑器 --> 模板),

i.e. make an editor template "newcustomclass" be the contents of the class you're talking about.

即使编辑器模板“newcustomclass”成为您正在谈论的类的内容。

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.

然后按照正常方式创建新的java类,删除所有内容,然后使用“newcustomclass”代码模板创建新的auto java类。

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

回答by nanda

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

是的!窗口 -> 首选项 -> Java -> 代码样式 -> 代码模板

Select Code in the tree panel and new Java files.

在树面板中选择代码和新的 Java 文件。

回答by fastcodejava

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

您可以试试这个 Eclipse插件,它可以让您创建一个带有大量可配置参数的 Java 类,例如注释或 XML 配置。

回答by samuel segal

Wanting to do something similar I ended up taking similar approach to Michael Wilesanswer 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

想要做类似的事情,我最终使用 Java 编辑器模板对Michael Wiles 的回答采取了类似的方法。但是我不得不使用 ${primary_type_name} 而不是 ${enclosureType} 来填充类名。根据我的经验,在输入模板命令之前删除所有内容后,${enclosureType} 将丢失类名。这是eclipse版本: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.

例如,以下是创建模板命令以创建启用 Lombok 日志记录的新 Spring 服务的步骤。

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.

1) 首先我们必须创建一个 Java 编辑器模板Preferences->Java->Editor->Template
* 输入 create-spring-service 作为名称
* 为 Context 字段选择 Java
* 在 Pattern 字段中输入以下模板。

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 -> spaceand begin typing the template name create-spring-service

2) 创建一个新类。
3) 打开新类并全选ctl -> a
4) 然后通过点击调用模板ctl -> space并开始输入模板名称create-spring-service

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

注意:在 Pattern 字段中键入模板时,您可以键入 $ 或点击ctl -> space查看预定义模板变量的列表。

Online list of available template variables

可用模板变量的在线列表