Given I have code like the following:
void foo() {
String str = "hello";
a(str, 1);
b(str, true);
a(str, 2);
b(str, false);
}
I would like to extract a new method c like:
void foo() {
String str = "hello";
c(str, 1, true);
c(str, 2, false);
}
However, the automated Extract Method refactoring will only extract one of the a/b pairs. My guess is that it dislikes the differing constants. I can work around this by extracting a local variable first, then extracting the method, then inlining the previously extracted variable, but I still have to find all the instances by hand. With that amount of work, I might as well just make the full change myself when I am looking at each part.
Is there a trick I am missing to let Eclipse know to search a little harder to extract this type of code?
Yes, you need to extract your constants into variable that you can put in the top to tell your tool to pass them as arguments to the extracted method.
Doing an extract method should give the following: