1 /*
   2  * @test /nodynamiccopyright/
   3  * @bug 8206986
   4  * @summary Check expression switch works.
   5  * @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
   6  * @compile --enable-preview -source 12 ExpressionSwitch.java
   7  * @run main/othervm --enable-preview ExpressionSwitch
   8  */
   9 
  10 import java.util.Objects;
  11 
  12 public class ExpressionSwitch {
  13     public static void main(String... args) {
  14         new ExpressionSwitch().run();
  15     }
  16 
  17     private void run() {
  18         check(T.A, "A");
  19         check(T.B, "B");
  20         check(T.C, "other");
  21         exhaustive1(T.C);
  22         localClass(T.A);
  23     }
  24 
  25     private String print(T t) {
  26         return switch (t) {
  27             case A -> "A";
  28             case B -> { break "B"; }
  29             default -> { break "other"; }
  30         };
  31     }
  32 
  33     private String exhaustive1(T t) {
  34         return switch (t) {
  35             case A -> "A";
  36             case B -> { break "B"; }
  37             case C -> "C";
  38             case D -> "D";
  39         };
  40     }
  41 
  42     private String exhaustive2(T t) {
  43         return switch (t) {
  44             case A -> "A";
  45             case B -> "B";
  46             case C -> "C";
  47             case D -> "D";
  48         };
  49     }
  50 
  51     private void localClass(T t) {
  52         String good = "good";
  53         class L {
  54             public String c() {
  55                 STOP: switch (t) {
  56                     default: break STOP;
  57                 }
  58                 return switch (t) {
  59                     default: break good;
  60                 };
  61             }
  62         }
  63         String result = new L().c();
  64         if (!Objects.equals(result, good)) {
  65             throw new AssertionError("Unexpected result: " + result);
  66         }
  67     }
  68 
  69     private void check(T t, String expected) {
  70         String result = print(t);
  71         if (!Objects.equals(result, expected)) {
  72             throw new AssertionError("Unexpected result: " + result);
  73         }
  74     }
  75 
  76     enum T {
  77         A, B, C, D;
  78     }
  79     void t() {
  80         Runnable r = () -> {};
  81         r.run();
  82     }
  83 }