1 /* 2 * @test /nodymaticcopyright/ 3 * @bug 8206986 4 * @summary Verify rule cases work properly. 5 * @compile/fail/ref=SwitchStatementArrow-old.out -source 9 -Xlint:-options -XDrawDiagnostics SwitchStatementArrow.java 6 * @compile SwitchStatementArrow.java 7 * @run main SwitchStatementArrow 8 */ 9 10 import java.util.Objects; 11 import java.util.function.Function; 12 13 public class SwitchStatementArrow { 14 public static void main(String... args) { 15 new SwitchStatementArrow().run(); 16 } 17 18 private void run() { 19 runTest(this::statement1); 20 runTest(this::scope); 21 } 22 23 private void runTest(Function<T, String> print) { 24 check(T.A, print, "A"); 25 check(T.B, print, "B-C"); 26 check(T.C, print, "B-C"); 27 try { 28 print.apply(T.D); 29 throw new AssertionError(); 30 } catch (IllegalStateException ex) { 31 if (!Objects.equals("D", ex.getMessage())) 32 throw new AssertionError(ex); 33 } 34 check(T.E, print, "other"); 35 } 36 37 private String statement1(T t) { 38 String res; 39 40 switch (t) { 41 case A -> { res = "A"; } 42 case B, C -> res = "B-C"; 43 case D -> throw new IllegalStateException("D"); 44 default -> { res = "other"; break; } 45 } 46 47 return res; 48 } 49 50 private String scope(T t) { 51 String res; 52 53 switch (t) { 54 case A -> { String r = "A"; res = r; } 55 case B, C -> {String r = "B-C"; res = r; } 56 case D -> throw new IllegalStateException("D"); 57 default -> { String r = "other"; res = r; break; } 58 } 59 60 return res; 61 } 62 63 private int r; 64 65 private void check(T t, Function<T, String> print, String expected) { 66 String result = print.apply(t); 67 if (!Objects.equals(result, expected)) { 68 throw new AssertionError("Unexpected result: " + result); 69 } 70 } 71 72 enum T { 73 A, B, C, D, E; 74 } 75 }