Sam Goldberg
Sam Goldberg

Reputation: 6801

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

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.

Upvotes: 8

Views: 46725

Answers (5)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Try this, It works like butter

Go to Source --> Generate Getter and Setter Methods

Either select one instance variable, or all ---> Ok

Upvotes: 10

KrishPrabakar
KrishPrabakar

Reputation: 2842

I do somewhat differently.

  • First create the variable: (say private int threadsInPool = 3).
  • Place the cursor on the variable name (say threadsInPool)
  • Apply this shortcut: Alt + Shift + s, r (Press the last r after a slight delay)
  • Just press Enter when Generate Getters and Setters dialog shows up and Voila!

EDIT : You can also try using Lombok annotations:

@Getter @Setter private String value;

Upvotes: 5

Dave Newton
Dave Newton

Reputation: 160181

I don't think that question implies that, since all answers gave a manual solution.

AFAIK it's not possible to do automatically without a plugin, and I don't know of a plugin that does it.

I use a template to create artifacts like that all at once, but I don't follow the same path--I'm not sure a template solution would work if a getter or setter already existed, since I'm not sure you can check for the presence of a method and make template decisions based on that. Maybe you can.

Upvotes: 1

Tudor
Tudor

Reputation: 62439

Hmm... dunno if this is what you are looking for, but if I create a field in a class there is a warning that the field is unused. If I click on the warning sign the option to generate getter and setter for the field appears:

enter image description here

Upvotes: 3

CAA
CAA

Reputation: 970

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.

Upvotes: 16

Related Questions