开发者

Shortcut for calling all the setter methods on an object in Eclipse?

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 开发者_如何学Cin the the class, I am not allowed to do so


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()));
        }
}


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!


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

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


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.


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.


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).


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 !


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜