使用Eclipse模板生成“特殊”getter和setter

时间:2011-12-21 00:56:06

标签: eclipse templates

我正在尝试做一个模板,给出一些属性为所有字段生成“特殊”的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};}

有人有想法吗?

感谢。

2 个答案:

答案 0 :(得分:2)

您可以在Java中编辑模板>代码风格>代码模板或可以从Java导出代码模板>代码风格> eclipse中的代码模板并将其编辑为xml,然后可以重新导入。

从代码tempalte中提取

<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

答案 1 :(得分:0)

我认为在偏好中查看 - &gt; Java - &gt;代码风格 - &gt;您可以在代码部分找到代码模板getter body和setter body。在那里编辑它们只包括你想要的身体(我假设你只想自定义身体)。