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