Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: How to automatically generate getter when adding a field?

Tags:

java

eclipse

I am using TDD, and have a very typical coding pattern, using Eclipse to autocreate methods and fields as I code the unit test. For example:

  1. type name of method that doesn't exist, e.g:

    myObj.setValue(someValue);

  2. Click on the little red error mark in the IDE to create the "setValue" method.

  3. Type inside of setValue method:

    public void setValue(String value) { this.value = value; }

  4. Click the red error mark to auto-create a private field (called "value in this case");

So at this point, I would like Eclipse to auto-create the getter method, without having to do it using the source -> generate getters and setters menu.

I saw this question: How do you force Eclipse to prompt you to create a getter and setter when it doesn't do it automatically which seems to imply that Eclipse does this, but I couldn't find where to configure that.

Is there some way to configure Eclipse to automatically add setter/getters when adding new private variable?

UPDATE: To clarify further, I'm looking for something like I saw in the Spring Roo documentation. If you take a look at the "How It Works" section, it describes how the framework automatically adds additional methods to your class, whenever you add a private variable. (My preference is not to run another process like that, however, and also not to get all the cruft that it seems to add). I was hoping Eclipse had something similar.

like image 807
Sam Goldberg Avatar asked Jun 04 '12 16:06

Sam Goldberg


People also ask

Can Eclipse generate getters and setters?

To generate getters and setters, do the following: Create the fields you want in the class then press Alt+Shift+S, R. A dialog will pop up allowing you to choose the fields you want to generate getters and setters for. Click Select All to create getters/setters for all fields.

What is the difference between getter and setter?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

How do I change my getters and setters in Eclipse?

Select "Javadoc -> Generate Getter/Setter Javadocs from Field Javadocs..." in the main menu of Eclipse or the context menu of the editor, and a dialog opens. The shortcut key is Ctrl+Shift+D. In the dialog select getters and setters for which you want to generate Javadocs and click "OK" button.


1 Answers

When you move your mouse above the name of the variable, you can select "Create getter and setter for varname" in the popup menu.

Or you can create yourself a shortcut to do so. Preferences -> General -> Keys. In the search box you enter "getter" and then you´ll find it.

like image 162
CAA Avatar answered Oct 06 '22 01:10

CAA