bolbat
bolbat

Reputation:

How can I customize Eclipse's getter and setter generation?

How can I configure Eclipse to generate getters and setters with some prefix for argument variable?

Example:

private String someVariable;

public void setSomeVariable(String aSomeVariable) {
    this.someVariable = aSomeVariable;
}

Upvotes: 16

Views: 10967

Answers (4)

Caffeinated Coder
Caffeinated Coder

Reputation: 680

Right click -> Source -> Generate Getters and Setters -> Click on the Code Template link just above the OK button -> Code

I usually write my setter like: if (${param}!=null) ${field} = ${param}; else return new String();

Upvotes: 2

akf
akf

Reputation: 39485

there are two thing that you need to do to effect the setter in your example.
first, as others have mentioned, you will need to go to the Preferences dialog and go to Java/Code Style/Code Templates/Code/Setter body. in the Pattern box, you should make the value to look like this:

this.${field} = ${param};

second, still in the Preferences dialog, go up a level to Code Style. Here you will see a table 'Conventions for variable names.' Select the Parameters row and select edit. add the letter a to the prefix field.

Once all of that is saved, you should be able to automatically generate setters as you have defined above, using the Generate Getter and Setter" command.

Upvotes: 5

Rich Kroll
Rich Kroll

Reputation: 4005

You can update all generated variables with a prefix by going to preferences > java > Code Style and selecting Parameters then edit. You can add a prefix/suffix for all generated variable names.

Upvotes: 21

Ann Addicks
Ann Addicks

Reputation: 925

You can select the variable(s) and right click > source > Generate getters and setters. If you only want setters uncheck the getVariable() option.

Upvotes: 0

Related Questions