开发者

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

开发者 https://www.devze.com 2023-01-21 04:46 出处:网络
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,

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开发者_C百科 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.


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.


IntelliJ was a check where not all case have been set. It has an auto-fix to fill in the missing cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消