/* * @test /nodynamiccopyright/ * @bug 8206986 * @summary Check expression switch works. * @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java * @compile --enable-preview -source 12 ExpressionSwitch.java * @run main/othervm --enable-preview ExpressionSwitch */ import java.util.Objects; public class ExpressionSwitch { public static void main(String... args) { new ExpressionSwitch().run(); } private void run() { check(T.A, "A"); check(T.B, "B"); check(T.C, "other"); exhaustive1(T.C); localClass(T.A); } private String print(T t) { return switch (t) { case A -> "A"; case B -> { break "B"; } default -> { break "other"; } }; } private String exhaustive1(T t) { return switch (t) { case A -> "A"; case B -> { break "B"; } case C -> "C"; case D -> "D"; }; } private String exhaustive2(T t) { return switch (t) { case A -> "A"; case B -> "B"; case C -> "C"; case D -> "D"; }; } private void localClass(T t) { String good = "good"; class L { public String c() { STOP: switch (t) { default: break STOP; } return switch (t) { default: break good; }; } } String result = new L().c(); if (!Objects.equals(result, good)) { throw new AssertionError("Unexpected result: " + result); } } private void check(T t, String expected) { String result = print(t); if (!Objects.equals(result, expected)) { throw new AssertionError("Unexpected result: " + result); } } enum T { A, B, C, D; } void t() { Runnable r = () -> {}; r.run(); } }