-2

I have a whole load of method calls like this:

thing.doOneThing(a);
thing.doOneThing(b);
thing.doOneThing(c);
thing.doOneThing(d);
// ... etc

But the Thing class has an equivalent doManyThings method, so this could be rewritten as:

thing.doManyThings(a, b, c, d /* etc */);

What's the right way to do this refactoring in intellij?

I've been using structural replacements to do:

  • Replace $t$.doOneThing($a$); $t$.doOneThing($b$); with $t$.doManyThings($a$, $b$);
  • Replace $t$.doManyThings($a$); $t$.doOneThing($b$); ($a$ having infinite multiplicity) with $t$.doManyThings($a$, $b$);
  • Replace $t$.doManyThings($a$); $t$.doManyThings($b$); ($a$ and $b$ having infinite multiplicities) with $t$.doManyThings($a$, $b$);

I have to keep on doing the last step over and over, because it merges pairs of matching statements, so this just halves the number of calls to doManyThings each time, and it needs to be done a few times to merge them all.

This feels harder than it ought to be - please can you point me to the super-obvious better way to achieve this? :)

Andy Turner
  • 137,514
  • 11
  • 162
  • 243

1 Answers1

0

Personally I'd write a regex to replace all strings "thing.doOneThing(" with a comma (,) and another one to replace all strings ");\n" to nothing (or the other way round, you can also do this over a selected area in case you've got more code in the file that you don't want modifying). You'll have an extra leading/trailing comma depending on what approach you take, but after this you'll have a nice comma separated list of all of your arguments. You could then replace all those calls with a single call to your doManyThings method where you'll paste the comma separated list of arguments.

edu
  • 428
  • 2
  • 10