0

Any IDE wizards hanging out?

I want to do something seemingly simple, which is turning out to be fairly difficult: I want to replace all variable declarations of one type with variable declarations of another type

Ex:

Foo x; ==> Bar x;
protected final Foo x; ==> protected final Bar x;
public abstract Foo = Foo() + 7; ==> public abstract Bar = Foo() + 7;

I figure that this must be possible but I cannot, for the life of me, figure out how. I can get close, but I stall on the following issues:

  1. How can I either not modify the "modifiers" like public, final, and so on, preceding the Foo variable declaration, or capture them so that I can make sure they don't get lost?
  2. How can I not replace the right-hand side of the equals sign in the 3rd example?
phoniel
  • 1
  • 3
  • Are you committed to using Structural Search and Replace, or would you consider a regex-based solution? – Hank D May 04 '16 at 03:27
  • Which version of IntelliJ IDEA are you using? – Bas Leijdekkers May 04 '16 at 07:17
  • Doesn't Type migration refactoring do the job? – Argb32 May 04 '16 at 09:44
  • @Argb32 No, alas, it doesn't. That would work if I needed to refactor a particular method, or a particular variable. Unfortunately, I need to refactor _every instance of Foo in an enormous project._ (In case you're interested why, the reason is that I have to replace every instance of Foo with an instance of Bar -- a superclass of Foo, and then cast Bar to Foo every time Foo is used. In case you're interested why... you couldn't be. This is a hell of Java refactoring :) ). – phoniel May 04 '16 at 18:52
  • @HankD, I'm not. I'd do whatever worked. – phoniel May 04 '16 at 18:53
  • Is there some relationship between Foo and Bar? Like splitting an interface or abstract class from an implementation? – Hank D May 04 '16 at 20:39

1 Answers1

0

Structural Search & Replace should work.

Search Pattern:

Foo $a$ = $b$;

Replacement Pattern:

Bar $a$ = $b$;

Click Edit Variables and set the Minimum count of b to 0 and Maximum count to 1. Modifiers should get saved automatically.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68