do it with a regular expression and verify it.

i'm assuming that each call to somemethod spans only one line. if not this method is still useful but slower.

copy the original file.

use ctrl+alt+h to show the callers of somemethod and get a count of them.

do regex search and replaces restricted to the proper area :
find : somemethod(somefield,([ ]*constant_[0-9]+)[ ]*,[ ]*true[ ]*)[ ]*;
replace : doitwith("$1");
find : somemethod(somefield,([ ]*constant_[0-9]+)[ ]*,[ ]*false[ ]*)[ ]*;
replace : doitnotwith("$1");

make a diff of the original file and the new file showing only the lines of the original file which have changed.

diff --changed-group-format='%<' --unchanged-group-format='' | wc

you should get the same number of lines as you got in the callers of somemethod.

if the calls to somemethod are multiline, or if you want greater verification, just drop | wc to see the lines which were modified in the original file to ensure that only the correct lines have been modified.


alas i know nothing in eclipse that allows to do this today. this is something i would like to achieve one day in autorefactor:

however the road to get there is quite long.

the only ways i know today are to extract local variables then extract method (as you suggested) or use regexes (as somebody else suggested).


i don't know of any utility that would do this directly.

i think using a regular expression is the only to go. first, you will need to create the two target methods doitwith and doitnotwith. then, you can highlight the contents of the method contentofsomemethods, hit ctrl+f, and use the following regular expressions:

find: somemethod\(somefield, (\w*), true\);
replace with: doitwith(\1);

and then

find: somemethod\(somefield, (\w*), false\);
replace with: doitnotwith(\1);

be sure to check "regular expressions" and "selected lines". here's a picture of it:

enter image description here

the regular expressions match the constant that is used inside the function call with (\w*) and then it is used during the replacement with \1. using this regular expression only on the selected lines minimizes the chance of breaking unrelated code.

Related Query

More Query from same tag