【问题标题】:Generate "special" getters and setters using Eclipse Templates使用 Eclipse 模板生成“特殊”getter 和 setter
【发布时间】:2011-12-21 00:56:06
【问题描述】:

我正在尝试做一个模板,给定一些属性,为所有字段生成“特殊”的 getter 和 setter。

例如,

public class MyBean {

    private int numeric;
}

生成这个:

  public int getNumeric() {
    return numeric;
  }

  public void setNumeric(final int newNumeric) {
    this.numeric = newNumeric;
  }

我检查了默认的 Eclipse 模板是否使用变量 ${body_statement} 来生成 setter 和 getter,我认为我需要更改这个变量但我没有找到这个变量的表达式。

我尝试使用以下模板,但它不能正常工作(我必须手动编写类型和字段)。

private ${type} get${field}() {return ${field};}

private void set${field}(final ${type} ${field}) {this.${field} = ${field};}

有人有想法吗?

谢谢。

【问题讨论】:

    标签: eclipse templates


    【解决方案1】:

    您可以在 Java > Code Style > Code Templates 中编辑模板,也可以在 eclipse 中从 Java > Code Style > Code Templates 中导出代码模板,然后将其编辑为 xml,然后可以重新导入。

    从代码模板中提取

    <template autoinsert="true" context="setterbody_context"
            deleted="false" description="Code in created setters" enabled="true"        id="org.eclipse.jdt.ui.text.codetemplates.setterbody"
     name="setterbody">${field} =       ${param};</template>
    

    Java 编辑器模板变量详细信息在以下链接中 http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-template-variables.htm

    【讨论】:

    • 不幸的是,它不起作用,因为我必须更改 eclipse 源来做我的“特殊”吸气剂......也许我会做的。 tks
    【解决方案2】:

    我认为查看 Preferences -> Java -> Code Style -> Code Templates 你会在 Code 部分找到 getter body 和 setter body。在那里编辑它们以仅包含您想要的正文(我假设您只想自定义正文)。

    【讨论】:

    • 是的,我不想只改变身体。我想要一个具有这方面的吸气剂(例如):------------------------------------------------ ---------- public void setNumeric(final int newNumeric) { this.numeric = newNumeric; } ----------------------------------------------
    猜你喜欢
    • 2012-06-12
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多