25

I want to create an object of a class in and then set all the properties in it using setter methods. There are more than 150 setter methods and I do want to type each one of them or even type in the object instance name Object instance type in dot . and then hit the spacebar for Eclipse to give me suggestions and then go and select the setter method. I do not want to do that 150 times.

Thus, I was looking for some sort of shortcut in Eclipse that allows you to call all the setters on the method. So like you type in the instance name and Eclipse calls all the setter methods e.g.

  • instanceName.setterOne("valOne");
  • instanceName.setterTwo("valOne");
  • instanceName.setterThree("valOne");

I cannot create another constructor in the the class, I am not allowed to do so

simranNarula
  • 641
  • 3
  • 9
  • 14
  • 4
    Your question needs more detail about the larger-picture, because the exact thing you are talking about doing sounds pretty pointless to me. Is this a class assignment that's actually designed to force you to learn Reflection? – Darien Aug 03 '11 at 01:16
  • I want a reason to flag this just because of the "do not close the question for a stupid reason" bit. Challenge accepted! – Coeffect Aug 03 '11 at 01:20
  • Please show us the design that led to this situation. From what I can see, a List or Map may be able to take the place of these 150 fields. – Paul Bellora Aug 03 '11 at 02:09
  • Mannimarco... ""Power does not corrupt men; fools, however, if they get into a position of power, corrupt power" – simranNarula Aug 03 '11 at 06:40
  • 1
    @simranNarula: I'll match your quote with a more relevant one: don't bite the hand that feeds you. Insulting the community you're asking help of is not a wise move. – Coeffect Aug 03 '11 at 15:16
  • Arrived here while googling for a tool to help quickly generate skeletons for Unit Tests. This would be a useful feature indeed. – Alex R Jan 21 '21 at 02:00

8 Answers8

39

From my experience last time , I cannot find eclipse has such feature .The most that I can do is open the Type Hierarchy View (by pressing F4 when viewing that class ), and then sort by the method name of that class and copy all the setters for further edit.

Or , you can use reflection to find out all the methods of this class , and print out the setter calls . Suppose this class is called Foo , you can have something like this:

for (Method m : Foo.class.getMethods()) {
        if (m.getName().startsWith("set")) {
            System.out.println(String.format("instanceName.%s(\"valOne\");", m.getName()));
        }
}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • 6
    thanks a lot.. this is what I was looking for... I have not studied reflections... but unfortunately some people do not understand that there are people who are learning and this is what this place is for...and they also have forgotten question that they asked when learning.... you ask a question here and people come around and vote that question down... just because they can do it. most of the people here lack common sense .. – simranNarula Aug 03 '11 at 06:38
  • 2
    "Power does not corrupt men; fools, however, if they get into a position of power, corrupt power" – simranNarula Aug 03 '11 at 06:40
  • The F4 thing was invaluable to me :) It's only 10 setters in doing a mapping from FBO to DTO but its still tedious :) (Yes i could of used XSTREAM) – VeenarM Sep 16 '14 at 03:54
11

See this question: Generate all setXXX calls of a POJO in Eclipse?

It has a great and simple way of doing what you want to do.

Oh and try to ignore the haters!

Community
  • 1
  • 1
Grouchal
  • 9,756
  • 6
  • 34
  • 46
3

If you're using IntelliJ, check out this plugin:

https://plugins.jetbrains.com/plugin/9360-generateallsetter

cwash
  • 4,185
  • 5
  • 43
  • 53
2

I do not want to do that 150 times

I'm not aware of any Eclipse feature that will help you here.

You could fall back to using something clunky like emacs keyboard macros or perl scripting to massage the source code outside of Eclipse.


Your other option (though it probably won't be productive ...) is to request that the cumbersome API be revised; e.g. to support the "builder" pattern.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

I have Eclipse 4.6.0 - which has an option to auto-generate getters and setters (not sure about earlier versions of Eclipse).

(a) Select your class (and right click) (b) Select Source (Shift+Alt+S) (c) Select Getters and Setters The resultant pop-up window (Generate getters & setters) allows you to select ALL or Individual fields.

Hope this helps (if still relevant).

1

Here are some tips I used -

Ctrl+Shift+A - > eclipse block selection (this is the magic tool 01)

Ctrl + F -> eclipse find/ find and replace ( magic tool 02)

Ctrl +Shift + X -> to upper case (tool 03)

Here how I did

  1. block selection -> select all field names (tool 01) Ex : user

  2. select all first letters and make the first letter uppercase (tool 01/03)
    Ex : User

  3. select all and put the word set (tool 01) Ex: setUser

  4. Align all fields nicely in line. So you can use the block selector straight

  5. select all and set "();" Ex: setUser();

now you have all the setters ready

When you want to insert a value inside parentheses you can use magic tool 02. Find and replace ( think a little and you will see an option )

Finally, save your time of boredom from getters/setters settings.

cheers !

shanika yrs
  • 303
  • 1
  • 13
1

I would seriously consider redesigning your class, given that you have reached this situation. Without knowing much about this class or your goals, I would venture that there is almost no reason to have 150 individual fields, especially if they are of the same type. Think about it - if you are already in this predicament, how easy will it be to maintain this code in the future?

If the fields are all of the same type, consider using an array, a List or a Map (property->value). If they are many diverse types, think about how you might break them up into component classes that could be more easily managed.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • 1
    thanks for your help, but that class is not written by me and I cannot modify it :( – simranNarula Aug 03 '11 at 06:43
  • 1
    In big organisations (e.g. production, telecom, banking companies) entities just have many fields. Does that mean that the bean has to be redesigned ? -- As long as the code remains readable, I don't see an issue. Do you think the number of fields can be decreased by a redesign ? --> you'll just spread it accross multiple classes. That's not a solution. – bvdb Dec 15 '15 at 09:52
  • 1
    PS: putting the fields in a map will result in a list of 150 `public static final String` constants for the keys. Which actually makes things worse. It will make later refacturing more difficult, as you also have to worry about the constant value of the key itself now. – bvdb Dec 15 '15 at 09:53
0

I created my own more general solution to this problem based on @Ken Chan anwser, which set not only basic types. It is far from perfect, still to some it might be some starting point.

Code is here

Community
  • 1
  • 1
SkorpEN
  • 2,491
  • 1
  • 22
  • 28