How to detect a new value has been added to the enumeration and is not processed in the switch

From time to time I have to add a new value to the 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 that I have that does not handle the new value, for example:

 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 value: what throwing some kind of exception is not good enough, I would like it to have compilation time.

I don't think this is possible, but maybe someone has a neat trick ...

I thought Findbugs had a rule to find those, but I only saw this: Eq: the covariant equals () method defined for listing (EQ_DONT_DEFINE_EQUALS_FOR_ENUM)

EDIT: I choose Mark's answer, I use Eclipse and it sounds just like what I need! I'm not a specialist in findbugs at all, so I could have missed that functionality, although I don't think so.

+6
java enums compilation findbugs
Oct 22 2018-10-18
source share
3 answers

There is a compile-time warning / error in Eclipse that you can enable: The Enum constant does not apply to the "switch".

In the project properties (or general settings) go to Java Compiler-> Errors / Warnings, check the Enable specific project settings box. You will find a warning under Potential Programming Problems. It is set to Ignore by default, but you can increase it to Warning or Error .

Edit: I thought that goes without saying, but I suppose I will say it anyway: this only applies if you work in Eclipse or use it to control the layout. Obviously, Findbugs or a similar equivalent will be the "real" answer, as it goes beyond the IDE and can be integrated into the build process.

+7
Oct 22 2018-10-18
source share

This can be done by adding a default sentence and logging when visiting it:

 switch (color) { default: log.error("Unknown color in switch: " + color); break case MONDAY: /*FALLTHROUGH*/ case TUESDAY: 

(adding failed comments will later help the companions decide whether you forgot the code or not :-))

Edit Clarification copied from comments on Mark's answer:

Signaling cases IDE functions like this are great for the developer at the time of the change, but he doesn’t catch the changes in other parts of the code that your code depends on.

This does not make cient code containing switches on enums reliable against changes unless they are recompiled with respect to the new version.

This helps when client code logs raw cases; Deployed code does not always have full control over its class path or library versions on it.

+2
Oct 22 2018-10-10
source share

IntelliJ was a check where not all cases were installed. It has auto-fix to fill in the missing cases.

0
Oct 22 '10 at 20:39
source share



All Articles