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: IntelliJ IDEA, IntelliJ, refactoring
image description