Blog

IntelliJ 8: Type Migration

07 Nov, 2008
Xebia Background Header Wave

Coding is refactoring. What starts with a great idea, might prove incorrect in the future. A simple example is the following code:

public class Address {
    private String streetName;
    private Integer houseNumber;
    // getters and setters
}

While initialy, a housenumber represented as an Integer might sound like a valid thing to do, chances are that this might change in the future when you need to handle
housenumbers like ’21A’, or ’19 4th story’. However, your UI expects this to be a Integer. Your domain expects this to be an integer. Your service layer expects this to be an integer.
I guess you catch my drift. Until now, it took quite some effort to refactor this, since you cannot just rename an Integer to a String. Now, with the help of Type Migration, this is possible!
By selecting the Integer type in the Address, and pressing Ctrl+Shift+F6 (Refactor -> Type Migration), you can change the type of the housenumber to be a String. The
great thing about this is that the getters and setters automatically change, but also methods which call this method. For example, if I have this code in the Person class:

    public Integer getPersonHouseNumber() {
        return address.getHouseNumber();
    }

Changing the type of the house number property in the address class will have the following result:

    public String getPersonHouseNumber() {
        return address.getHouseNumber();
    }

So, the type did not only change in the Address class, but in the whole chain of method calls! This saves a lot of time in refactoring, and means that when changing
types at the DAO level, this change will automatically affect the whole call chain, for example up to the level of the UI.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts