Eclipse:Java类模板

Bob*_*Bob 17 java eclipse templates

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

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

Mic*_*les 21

你能做的是添加一个普通的代码快捷方式(java - > editor - > templates),

即使编辑器模板"newcustomclass"成为您正在讨论的类的内容.

然后以正常方式创建新的java类,删除所有内容,然后使用"newcustomclass"代码模板创建新的自动java类.

这是一个简单异常类的示例:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*ton 6

您可以在eclipse中添加" 新文件向导 ",但是您需要编写一个新插件来执行此操作.我不知道在运行时以MS Office模板的方式执行此操作的简单方法,我认为这是您正在尝试执行的操作.

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


nan*_*nda 5

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

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

  • 好吧,如果我改变那些我在Eclipse中创建的_all_ Java文件看起来会有所不同.这不是我正在寻找的...... (3认同)