From time to time I have to add a new value to a enum type in my project.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,
FILENOTFOUND //this one is new one
}
What I would like is to have a compile time error for every switch I have that is not treating the new value, like this one:
switch (color) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY: System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY: System.out.println("Weekends are best.");
break;
}
Having a default: that throws some exception is not good enough, I would like it to be compile time.
I don't think this is possible but maybe someone has a neat trick...
I thought Findbugs would have a rule to find those but I only saw this: Eq: Covariant equals() method defined for enum (EQ_DONT_DEFINE_EQUALS_FOR_ENUM)
EDIT: I'm choosing Mark's reply, I do use Eclipse and that sounds just like what I needed! I am not an expert in findbugs at all so I might have missed such functionality, though I don't think so.
The valueOf() enum method converts a specified string to an enum constant value. An exception is thrown if the input string doesn't match an enum value.
We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive.
4) Adding new constants on Enum in Java is easy and you can add new constants without breaking the existing code.
Eclipse has a compile-time warning/error you can enable: Enum constant not covered on "switch".
From your Project properties (or general preferences), go to Java Compiler->Errors/Warnings , check Enable project specific settings. You'll find the warning under Potential programming problems. It's set to Ignore by default but you can bump it up to Warning or Error.
Edit: I thought this goes without saying but I suppose I'll say it anyway: this is only applicable if you're developing in Eclipse or using it for your build management. Obviously a Findbugs or similar equivalent would be the "real" answer since it transcends the IDE and can be integrated into the build process.
You could do that by adding a default clause and logging when it is visited:
switch (color) {
default:
log.error("Unknown color in switch: " + color);
break
case MONDAY: /*FALLTHROUGH*/
case TUESDAY:
(adding fallthrough comments help later maintainers to decide whether you forgot code or not :-))
Edit Clarification copied from comments on Mark's answer:
An IDE feature signalling cases like this is fine for the developer at the moment of the change but it does not catch changes in other parts of code that your code depends on.
It does not make the cient code containing switches on enums robust against changes unless they are recompiled against the new version.
It does help when the client code logs unhandled cases; deployed code does not always have full control over its classpath or the versions of libraries on it.
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