1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8206986 27 * @compile --enable-preview -source 12 ExpressionSwitchBreaks1.java 28 * @run main/othervm --enable-preview ExpressionSwitchBreaks1 29 */ 30 31 import java.util.Objects; 32 import java.util.function.Supplier; 33 34 public class ExpressionSwitchBreaks1 { 35 public static void main(String... args) { 36 new ExpressionSwitchBreaks1().run(); 37 } 38 39 private void run() { 40 check(print1(0, 0), "0-0"); 41 check(print1(0, 1), "0-1"); 42 check(print1(0, -1), "0-X"); 43 check(print1(-1, -1), "X"); 44 check(print2(0, 0, 0), "0-0-0"); 45 check(print2(0, 0, 1), "0-0-1"); 46 check(print2(0, 0, 2), "0-0-2"); 47 check(print2(0, 0, -1), "0-0-X"); 48 check(print2(0, 1, -1), "0-1"); 49 check(print2(0, -1, -1), "0-X"); 50 check(print2(1, -1, -1), "1"); 51 check(print2(2, 5, 5), "2-X-5"); 52 check(print2(-11, -1, -1), "X"); 53 } 54 55 private String print1(int i, int j) { 56 switch (i) { 57 case 0: 58 return switch (j) { 59 case 0: 60 if (true) break "0-0"; 61 case 1: 62 break "0-1"; 63 default: 64 break "0-X"; 65 }; 66 default: return "X"; 67 } 68 } 69 70 private String print2(int i, int j, int k) { 71 return switch (i) { 72 case 0: 73 String r; 74 OUTER: switch (j) { 75 case 0: 76 String res; 77 INNER: switch (k) { 78 case 0: res = "0-0-0"; break; 79 case 1: res = "0-0-1"; break; 80 case 2: res = "0-0-2"; break INNER; 81 default: r = "0-0-X"; break OUTER; 82 } 83 r = res; 84 break; 85 case 1: 86 r = "0-1"; 87 break; 88 default: 89 r = "0-X"; 90 break; 91 } 92 break r; 93 case 1: 94 break "1"; 95 case 2: 96 LOP: while (j-- > 0) { 97 if (k == 5) { 98 k--; 99 continue; 100 } 101 break LOP; 102 } 103 Supplier<String> getter = () -> { return "2-X-5"; }; 104 break getter.get(); 105 default: 106 break "X"; 107 }; 108 } 109 110 private void check(String result, String expected) { 111 if (!Objects.equals(result, expected)) { 112 throw new AssertionError("Unexpected result: " + result); 113 } 114 } 115 116 enum T { 117 A, B; 118 } 119 }