代码之家  ›  专栏  ›  技术社区  ›  Bob

Eclipse:Java类模板

  •  17
  • Bob  · 技术社区  · 15 年前

    在Eclipse3.5中,在Windows下->偏好->爪哇>编辑->模板,我可以添加代码模板。但是,这些模板只能包含我可以插入到现有Java类中的片段。

    4 回复  |  直到 15 年前
        1
  •  6
  •   Kothar    15 年前

    您可以添加' new file wizards write a new plugin 去做吧。我不知道有什么简单的方法可以在运行时以MS Office模板的方式实现这一点,我想这正是您正在尝试的。

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

        2
  •  22
  •   Jason Axelson Gazler    13 年前

    您可以做的是添加一个普通的代码捷径(java-->编辑-->模板),

    然后以正常方式创建新的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);
        }
    }
    
        3
  •  5
  •   nanda    15 年前

    窗口->偏好->Java->代码样式->代码模板

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

        4
  •  1
  •   fastcodejava    13 年前

    plug-in 这将允许您创建一个具有许多可配置参数的Java类,例如注释或XML配置。

        5
  •  1
  •   samuel segal    5 年前

    我想做一些类似的事情,结果却采取了类似的方法 Michael Wiles

    1) 首先,我们必须创建一个Java编辑器模板 Preferences->Java->Editor->Template
    *输入createspringservice作为name
    *为上下文字段保留选中的Java
    *在模式字段中输入以下模板。

    package ${enclosing_package};
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    
    @Slf4j
    @Service
    public class ${primary_type_name} {
        ${cursor}
    }
    

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

    查看预定义模板变量的列表。

    Online list of available template variables