Idea logo

The IntelliJ IDEA Blog

The Capable & Ergonomic Java IDE by JetBrains

Tips & Tricks

Type Migration Refactoring

If you care about performance of your project and development productivity, there is no any way to overestimate automated refactorings. They are in fact a powerful tool that allows improving your code without spending plenty of time.

In addition to IntelliJ IDEA rich set of refactorings, one of the latest EAPs offers a new one, called Type Migration. Let’s take a look on it.

Consider we have some code like the following one:
Before Type Migration
Here, myID is declared as String. You might encounter such situation while working on some legacy projects, for example. As our application apparently uses a database, we need to make myID int instead of String. Formerly, this could be a real headache, if we work on a quite large and complex project. Although, we can simply search and replace the type, we’ll definitely end up with a project full of red-underlined code such way. We need more intelligent and easy way to change the type, and now we’ve got it – type migration refactoring.

Just place the caret at the type to be refactored and press Ctrl+Shift+F6. Alternatively, select Refactor | Type Migration on the main menu.
In the Type Migration dialog specify the new type and the scope to be refactored, then click Preview to investigate the usages to be affected and conflicts, if found. In the preview you can also exclude some nodes from the refactoring, for instance, if they affect your API.
Type Migration Preview
In our example, there is a conflict, since getID() will return int, and equals() is not applicable to primitives. We can easily fix this part manually in the editor. Then, the only thing we need to do is rerun the refactoring from the preview, and voila, myID is now int.
After Type Migration

Technorati tags: , ,
Comments below can no longer be edited.

5 Responses to Type Migration Refactoring

  1. Avatar

    Alexey says:

    May 19, 2014

    Images are broken, please, fix.

    • Avatar

      Eugene Toporov says:

      May 22, 2014

      Thanks for noticing Alexey, we’re working on it.

  2. Avatar

    Eric Hubbard says:

    August 19, 2014

    hmm… having some problems with this feature. This is a sample snippet.. if I move the caret to “fan” then Type migration is greyed out. If I move the caret to “BaseThingy” on line A… it is usable.. but then it ends up changings all List .. even the one on line B…. hmmm

    a) List fan;
    b) Listcontroller;

  3. Avatar

    Strappanai says:

    March 10, 2019

    I can’t find this. Neither ctrl+shift+f6 nor a search in the menus work. And crtl-shift-a for the global search can’t find it either.

    • Avatar

      Fabio Spelta says:

      April 9, 2019

      So? Has this feature been removed?

Discover more