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

This entry was posted in Tips & Tricks. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>