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 import java.util.function.Supplier;
  12 
  13 public class ExpressionSwitch {
  14     public static void main(String... args) {
  15         new ExpressionSwitch().run();
  16     }
  17 
  18     private void run() {
  19         check(T.A, "A");
  20         check(T.B, "B");
  21         check(T.C, "other");
  22         assertEquals(exhaustive1(T.C), "C");
  23         assertEquals(scopesIsolated(T.B), "B");
  24         assertEquals(lambdas1(T.B).get(), "B");
  25         assertEquals(lambdas2(T.B).get(), "B");
  26         localClass(T.A);
  27     }
  28 
  29     private String print(T t) {
  30         return switch (t) {
  31             case A -> "A";
  32             case B -> { break "B"; }
  33             default -> { break "other"; }
  34         };
  35     }
  36 
  37     private String exhaustive1(T t) {
  38         return switch (t) {
  39             case A -> "A";
  40             case B -> { break "B"; }
  41             case C -> "C";
  42             case D -> "D";
  43         };
  44     }
  45 
  46     private String exhaustive2(T t) {
  47         return switch (t) {
  48             case A -> "A";
  49             case B -> "B";
  50             case C -> "C";
  51             case D -> "D";
  52         };
  53     }
  54 
  55     private String scopesIsolated(T t) {
  56         return switch (t) {
  57             case A -> { String res = "A"; break res;}
  58             case B -> { String res = "B"; break res;}
  59             default -> { String res = "default"; break res;}
  60         };
  61     }
  62 
  63     private Supplier<String> lambdas1(T t) {
  64         return switch (t) {
  65             case A -> () -> "A";
  66             case B -> { break () -> "B"; }
  67             default -> () -> "default";
  68         };
  69     }
  70 
  71     private Supplier<String> lambdas2(T t) {
  72         return switch (t) {
  73             case A: break () -> "A";
  74             case B: { break () -> "B"; }
  75             default: break () -> "default";
  76         };
  77     }
  78 
  79     private void localClass(T t) {
  80         String good = "good";
  81         class L {
  82             public String c() {
  83                 STOP: switch (t) {
  84                     default: break STOP;
  85                 }
  86                 return switch (t) {
  87                     default: break good;
  88                 };
  89             }
  90         }
  91         String result = new L().c();
  92         if (!Objects.equals(result, good)) {
  93             throw new AssertionError("Unexpected result: " + result);
  94         }
  95     }
  96 
  97     private void check(T t, String expected) {
  98         String result = print(t);
  99         assertEquals(result, expected);
 100     }
 101 
 102     private void assertEquals(Object result, Object expected) {
 103         if (!Objects.equals(result, expected)) {
 104             throw new AssertionError("Unexpected result: " + result);
 105         }
 106     }
 107 
 108     enum T {
 109         A, B, C, D;
 110     }
 111     void t() {
 112         Runnable r = () -> {};
 113         r.run();
 114     }
 115 }