Is there an IntelliJ refactoring allowing to automatically turn an enum into a class, thus transforming its enum values into static final
fields?
I can't seem to find anything about it, I only find stuff about extracting constants but not what I'm looking for.
For instance, I'd like to turn an enum like this one:
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
// getters here
}
Into a class like that:
public class Planet {
public static final Planet MERCURY = new Planet(3.303e+23, 2.4397e6);
public static final Planet VENUS = new Planet(4.869e+24, 6.0518e6);
public static final Planet EARTH = new Planet(5.976e+24, 6.37814e6);
public static final Planet MARS = new Planet(6.421e+23, 3.3972e6);
public static final Planet JUPITER = new Planet(1.9e+27, 7.1492e7);
public static final Planet SATURN = new Planet(5.688e+26, 6.0268e7);
public static final Planet URANUS = new Planet(8.686e+25, 2.5559e7);
public static final Planet NEPTUNE = new Planet(1.024e+26, 2.4746e7);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
// getters here
}
enum
keyword into class
, no need for such a complex refactoring)Note: the reason why someone would make the change is off-topic of course, please avoid discussing about it in this thread.
I can't find a link to it. But there is/has been talk of Java 9 or 10 having enum classes not inherit from the Enum
class(Which all 'enums' are "just classes that extend Enum automatically hiddenly"(not a quote, but an easy way to think of them.)). Thereby allowing extends
to work properly. I think it was even discussed for Java 8, but put on the back burner over finding more rock solid impl. of lambda, etc.
So I wouldn't be surprised if in the future it happens that you can extend enums.
Edit:
(Thought this was in here, but wasn't paying attention to my last previous edit.)
I've done this exact thing, and just used the "Replace" functionality within Notepad++, Kate, Eclipse (whatever editor I'm using at the time) or I used gnu's awk command to do string manipulations. (I've done operations like this a lot. With a variety of means) Once you learn some basic Regular Expression, it is quite easy to manipulate string files like this.
For example you'd replace (within 'selection only' of top lines, if within an IDE/editor)
1) <tab or 3/4 spaces>
with public static final Planet
2) (
with = new Planet(
3) change the word enum
to class
And you're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With