Eclipse:Java类模板

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

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

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

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

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

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

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

这是一个简单exception类的示例:

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

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

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

您可以尝试使用这个eclipse 插件,它可以创建一个包含许多可配置参数的Java类,例如注释或XML配置。