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.
java enums compilation findbugs
Persimmonium Oct 22 2018-10-18 18:47
source share