16

Is there an annotation or another method to turn the non-exhaustive switch statement warning into an error? I want a certain method or class to produce an error if not all values have been handled in the switch properly.

Example:

public enum E {
  A,
  B
}

and somewhere else in the code there is a switch on that enum like so

switch (enumValue) {
case A: /* do something */ break;
}

Java will give you a warning that this switch does not handle all the enum values. I want to turn this warning into an error (permanently, regardless of individual IDE settings).

Please keep in mind that I cannot change the original enum in this case, so I want the compiler to enforce it.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • 2
    A short coding example would be very useful here. By the way, what's wrong with handing it in the `default` case during runtime? – barak manos Nov 28 '14 at 07:35
  • 1
    I suppose everyone knows how to write a switch statement ;) –  Nov 28 '14 at 07:35
  • 1
    How should this be possible if you switch over an int for example? – Smutje Nov 28 '14 at 07:36
  • Yes, but not everybody understands what "non-exhaustive switch" means! Also, you probably refer only to `enum`s, since it's not really possibly for "large domain" types. – barak manos Nov 28 '14 at 07:36
  • Then maybe you want to use the Strategy Pattern, or the State Pattern (which can be easily used with Enums). That way you can avoid using a switch statement (and also makes your code more easily extensible). ;) – Rodrigo Quesada Nov 28 '14 at 07:49
  • Or maybe you just focus on unit testing: Throw from the default case, and write a test that iterates over the enum values such that control enters the switch with each of them. – Chris Martin Nov 28 '14 at 07:52
  • Yeah, in that case I forgot to mention i cannot change the enum. That's why I wanted the compiler to enforce this. –  Nov 28 '14 at 07:52
  • @ChrisMartin that is what I actually planned do now, since there seems to be no viable alternative –  Nov 28 '14 at 07:54
  • 1
    Possible duplicate of [How to ensure completeness in an enum switch at compile time?](http://stackoverflow.com/questions/16797529/how-to-ensure-completeness-in-an-enum-switch-at-compile-time) – Vadzim Jan 19 '17 at 18:10
  • @Smutje If we consider how other language handle exhaustive pattern matching, then they use the default case to handle the rest of the domain. What ErikAigner wants is that the compiler complains if the default case was not added and some of the cases were not added either. – Little Helper Feb 27 '18 at 09:25

4 Answers4

14

I know this is an old thread but there is a new answer in the latest JDKs:

Switch Expressions must be exhaustive and are available as a preview language feature in JDK 12 and 13.

https://openjdk.java.net/jeps/354

This means you can modify switch statements that require validation to be switch expressions while other switch statements will continue to work as intended.

imoverclocked
  • 178
  • 1
  • 4
  • this feature is now in the java SE12, see here: https://docs.oracle.com/en/java/javase/13/language/switch-expressions.html – julaine Nov 11 '22 at 14:22
7

Since this is an enum, instead of a switch statement, you can use an abstract method; all enum values will have to implement it. For instance:

public enum MyEnum
{
    A {
        @Override public void foo() { /*whatever*/ }
    }
    // etc

    public abstract void foo();
}

Then call yourEnum.foo() when you need it instead of using a switch statement like you currently do.

And not implementing the method is not an option... Compilation will fail.

fge
  • 119,121
  • 33
  • 254
  • 329
  • 1
    As the OP stated, they don't have access to the original enum. Plus, even if they did, this abstract approach is on the enum itself, not where it may be used. Swift has this capability, and actually enforces it. You *have* to be exhaustive, or at a minimum, specify a default. – Mark A. Donohoe Jul 26 '17 at 17:33
5

Well, you can probably change a setting in your IDE to turn the warning into an error.

In Eclipse, for example, under Window->Preferences->Java->Compiler->Errors/Warnings, you can decide whether Incomplete 'switch' cases on enum should be ignored or produce a warning or an error.

If your switch statement is not on an enum, it doesn't make sense you force all the cases to be specified, as there would be a huge number of cases.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Linters like Checkstyle will flag this, so if you make a linter part of your build, you'll get a "compile-time" (I get that it's a phase after compilation, but it's fundamentally the same) warning. There's even Checkstyle (and probably other linters) integration in Eclipse and IntelliJ so that this is flagged in your IDE.

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61