Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a new value was added to an enum and is not handled in a switch

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.

like image 207
Persimmonium Avatar asked Oct 22 '10 18:10

Persimmonium


People also ask

Does enum valueOf throw exception?

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.

Can we use enum in switch case?

We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive.

Can we add constants to enum without breaking existing code?

4) Adding new constants on Enum in Java is easy and you can add new constants without breaking the existing code.


2 Answers

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.

like image 99
Mark Peters Avatar answered Sep 21 '22 10:09

Mark Peters


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.

like image 22
rsp Avatar answered Sep 20 '22 10:09

rsp