tags:

views:

109

answers:

3

I need to do lot of refactoring (actually need to change field names) in eclipse. It is changing all occurrences, references of fields. But one thing is it is not changing getter/setter names which have been generated based on previous name.

Do we have any plugin to do this or any configuration change needed to be done?

+4  A: 

Yes, in Galileo the "rename" dialog has an option for changing the setters and getters of a field - see here. (Alternatively, you can use CTRL + SHIFT + R, and open the context menu from the down arrow that appears after pointing the selection.)

Before Galileo, you'd have to do 3 actions instead of one (not that bad)

Bozho
Thanks a lot Bozho :) It is a lot better for me now.Though there is a small problem still, it is not considering setter's parameter name.
Reddy
hm, yes, that's a fair point. You can leave it, or you can manually change it.
Bozho
+1 to include link to my site :D
nanda
A: 

ALT+SHIFT+R is the default keyboard shortcut for renaming ... it's awesome!

LES2
yes, but it won't rename setters and getters
Bozho
it in fact *does* give you a dialog with options for renaming the getter and setter methods.
LES2
Hi LES2, yes.. that is the keyboard shortcut but in Galileo you have to press it two times to get the dialog box.
Reddy
+1  A: 

There is no real necessity for 'getters' and 'setters' to have the same name as the internal field. In fact many would argue that linking the two together is a violation of the data hiding principles of object oriented programming. Part of the point of having getters and setters is to protect the rest of the code from changes that are internal to the class, like renaming.

Having said that, if you absolutely decide that you need to rename the methods as well as the fields, then Eclipse will do it for you. You have to do them separately, but the same 'rename' that you are using for renaming fields works for methods. Just select the method and do what you did before.

DJClayworth
Thanks DJClayworth. But, how a private variable having same name as its getters/setters would violate data hiding principles?Since it is private, no body would be able to access it directly..right?
Reddy
You are right. But if you FORCED a change of the getters and setters when you changed the variable name, that would violate data hiding, because a change to the internal structure of the class would force a change to the classes interacting with it.You might want to think about what you would do if you restructured the class so the internal data was stored in a different format, so the field no longer existed. Good design says you need to keep the getters and setters: what do you call them then?
DJClayworth
Understood nowIn my case it is new code and I have had to just change the name 2, 3 times before committing it.Though it would be violating OOPS principles, I think, if I can change all the calls to getters/setters as well (since all calls happening from the code under my control) Shouldn't we be changing them to keep code more readable?
Reddy
Yes I agree you should, provided the codebase is still small enough to fit in a single Eclipse project (so that automatic rename can do the job) and that no code outside that Eclipse project is using the methods.
DJClayworth