How do I perform a Boolean To Enum refactor in IntelliJ IDEA?
Asked Answered
D

1

6

How do I perform a Boolean To Enum refactor in IntelliJ IDEA?

For example, convert this:

void changeLights(boolean isOn) {
    this.isOn = isOn;
    }

changeLights(true);
changeLights(false);

Into this:

enum LightState { ON, OFF }

void changeLights(LightState lightState) {
    this.lightState = lightState;
    }

changeLights(LightState.ON);
changeLights(LightState.OFF);
Drub answered 6/1, 2017 at 15:28 Comment(3)
I have opened this issue: youtrack.jetbrains.com/issue/IDEA-166277Drub
youtrack.jetbrains.com/issue/IDEA-112022Drub
For a list of manual steps you can look at #13247668Rollie
E
3

I would add a method

@Deprecated
void changeLights(boolean isOn) {
    changeLights(isOn ? LightState.ON : LightState.OFF);
}

Then you can inline this method. Lastly you can "simplify" using the inspection analyse tool to simplify

changeLights(true ? LightState.ON : LightState.OFF); // use IDEA to simplify

to

changeLights(LightState.ON);

similar for false -> changeLights(LightState.OFF);

Electrician answered 6/1, 2017 at 15:31 Comment(2)
I was hoping there was some specialized Boolean to Enum refactoring to do just that in Intellij. So it seems it really does not exist? :(Drub
I believe you made a small mistake there? You meant to add void changeLights(LightState lightState) { this.lightState = lightState; } then change the original changeLights method to void changeLights(boolean isOn) { changeLights(isOn ? LightState.ON : LightState.OFF); }, and then inline this last changed method and simplify it.Drub

© 2022 - 2024 — McMap. All rights reserved.