JMarques
JMarques

Reputation: 3064

Generate "special" getters and setters using Eclipse Templates

I´m trying to do a template that given some attributes generate "special" getters and setters for all the fields.

For example,

public class MyBean {

    private int numeric;
}

Generate this:

  public int getNumeric() {
    return numeric;
  }

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

I check that the default Eclipse template use the variable ${body_statement} to generate the setters and the getters, I think that I need to change this variable but I didn´t find the expression of this variable.

I try to use the following template but it doesn´t work correctly (I had to manually write the type and the field).

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

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

Anyone have an idea?

Thanks.

Upvotes: 1

Views: 5627

Answers (2)

nayakam
nayakam

Reputation: 4249

You could edit the template in Java > Code Style > Code Templates or May be export the code template from Java > Code Style > Code Templates in eclipse and edit it the xml then could import back in.

Extract from code 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 Editor Template Variables details are in following link http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-template-variables.htm

Upvotes: 2

Francis Upton IV
Francis Upton IV

Reputation: 19443

I think looking in Preferences -> Java -> Code Style -> Code Templates you will find in the Code section the getter body and setter body. Edit them there to include only the body you want (I assume you want to only customize the body).

Upvotes: 0

Related Questions