-1

I have a complex case in a switch statement that (as it happens) always abruptly terminates (with a return, or break or ...). I'd like to put something like lint's /*UNREACHABLE*/ declaration so as to indicate to anyone reading the code that it doesn't fall through to the next case (a common bug in Java). I'm looking for a more formal way to indicate this, ideally something that would cause a compiler warning or error if it was not unreachable. Any ideas?

    switch (x) {
        case 1:
           if (...) {
             switch (.) {
                ...
             }
           } else {
             ...
           }
           /*UNREACHABLE*/
        case 2:
           ...
    }

The code is truly unreachable and the Java compiler knows it is, so I can't put any (normal) code there because it would be marked as unreachable code by the compiler.

  • If it's not supposed to be reached, you could just throw an exception. You could make your own `Exception` and explain why the code shouldn't have reached that point in the stacktrace. – Paul Lemarchand May 27 '19 at 17:17
  • Maybe[this](https://stackoverflow.com/questions/1752607/how-to-intentionally-cause-a-custom-java-compiler-warning-message) will help – Erik May 27 '19 at 17:19
  • @Paul Lemarchande and @Erik: If I put a in `throw` statement there or any other statement, it gets a compiler error (as I clarified after your comments). – John Tang Boyland May 27 '19 at 17:20
  • 2
    @JohnTangBoyland Not if you make it a [`RuntimeException`](https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/RuntimeException.html). This is (IMO) a bad idea; your code shouldn't be so obfuscated that you need a "UNREACHABLE" marker. Seeing nested `switch`(es) and `if`-`else` blocks suggests [Strategy Pattern](https://en.wikipedia.org/wiki/Strategy_pattern) to me... which should make it more readable (and that should be your goal). – Elliott Frisch May 27 '19 at 17:21
  • @Elliot Frisch: I don't think you understand the compiler error: even adding "int x = 3;" will get an error. The Java compiler doesn't permit code in unreachable situations, e.g. after 'return' statements. – John Tang Boyland May 27 '19 at 17:23
  • @JohnTangBoyland You think I don't understand what compiler error where exactly? This question is entirely around introducing a compiler error with a marker; there is no such mechanism (Java doesn't have a precompiler, and I don't think an annotation can do what you desire). My comment was a follow-up to Paul's, you ***could*** add a `RuntimeException` at the section that *should* be unreachable; it should never be reached, and thus if it is - you would get a runtime error. Best of luck in your programming journey! – Elliott Frisch May 27 '19 at 17:30
  • What you should really do, is put the thing in case 1 into it's own function, so all your case 1 does is call that function and then break – MTilsted May 27 '19 at 17:30
  • You can not do that with Java itself, however if you search for ***java lint*** with popular search engines, you will find linters. And then you may find some metasyntax for this purpose. – tevemadar May 27 '19 at 17:31
  • @MTilsted: the code in the case sometimes returns a value, sometimes does a 'continue', sometimes does 'break'. Yes, the code is complex; it's doing a lexer style task. – John Tang Boyland May 27 '19 at 17:32

1 Answers1

1

If you are open to wild hacks, you can insert unused cases between the valid ones:

switch (x) {
    case 1:
       if (...) {
         switch (.) {
            ...
         }
       } else {
         ...
       }
    case -1:
       /*UNREACHABLE*/
       throw new RuntimeException("nope");

    case 2:
       ...
}

Personally I do not think I would use this, but it should work.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Truly answered the question. – John Tang Boyland May 27 '19 at 17:42
  • @JohnTangBoyland: as I mentioned above, I would rather search for linters. For example I just encountered Lint4j, and it says it checks for fall-through `case` statements: http://www.jutils.com/checks/language.html – tevemadar May 27 '19 at 17:48