1 /* 2 * Copyright (c) 2018, 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 * @summary Check definite (un)assignment for in switch expressions. 28 * @compile --enable-preview -source 12 ExpressionSwitchDA.java 29 * @run main/othervm --enable-preview ExpressionSwitchDA 30 */ 31 32 public class ExpressionSwitchDA { 33 public static void test1() { 34 int i; 35 int j = 0; 36 switch (j) { 37 case 0 : i=42; break; 38 default: i=42; 39 } 40 System.out.println(i); 41 } 42 public static void test2(){ 43 int i; 44 int j = 0; 45 switch (j) { 46 case 0 -> i=42; 47 default -> i=42; 48 } 49 System.out.println(i); 50 } 51 public static void test3(){ 52 int i; 53 int j = 0; 54 switch (j) { 55 case 0 -> { i=42; } 56 default -> { i=42; } 57 } 58 System.out.println(i); 59 } 60 public static void test4(){ 61 int i; 62 int j = 0; 63 int k = switch (j) { 64 case 0 -> i=42; 65 default -> i=42; 66 }; 67 System.out.println(i); 68 } 69 public static void test5(){ 70 int i; 71 int j = 0; 72 int k = switch (j) { 73 case 0 -> { i=42; break 42; } 74 default -> i=42; 75 }; 76 System.out.println(i); 77 } 78 public static void test6(){ 79 int i; 80 int j = 0; 81 int k = switch (j) { 82 case 0 -> i=42; 83 default -> { i=42; break 42; } 84 }; 85 System.out.println(i); 86 } 87 public static void test7(){ 88 int i; 89 int j = 0; 90 int k = switch (j) { 91 case 0 -> { i=42; break 42; } 92 default -> { i=42; break 42; } 93 }; 94 System.out.println(i); 95 } 96 public static void test8() { 97 int i; 98 int j = 0; 99 switch (j) { 100 case 0 : i=42; break; 101 default: throw new NullPointerException(); 102 } 103 System.out.println(i); 104 } 105 public static void test9() { 106 int i; 107 int j = 0; 108 switch (j) { 109 case 0 -> i=42; 110 default -> throw new NullPointerException(); 111 } 112 System.out.println(i); 113 } 114 public static void test10() { 115 int i; 116 int j = 0; 117 switch (j) { 118 case 0 -> { i=42; System.out.print(i);} 119 default -> throw new NullPointerException(); 120 } 121 System.out.println(i); 122 } 123 public static void test11() { 124 int i; 125 int j = 0; 126 switch (j) { 127 case 0 -> { i=42; System.out.print(i);} 128 default -> { throw new NullPointerException(); } 129 } 130 System.out.println(i); 131 } 132 public static void test12() { 133 int i; 134 int j = 0; 135 switch (j) { 136 case 0 : i=42; break; 137 default: return; 138 } 139 System.out.println(i); 140 } 141 public static void test13() { 142 int i; 143 int j = 0; 144 switch (j) { 145 case 0 -> i=42; 146 default -> { return; } 147 } 148 System.out.println(i); 149 } 150 public static void test14() { 151 int i; 152 int j = 0; 153 switch (j) { 154 case 0 -> { i=42; } 155 default -> { return; } 156 } 157 System.out.println(i); 158 } 159 public static void test15() { 160 final int i; 161 int j = 0; 162 switch (j) { 163 case 0 -> { i=42; } 164 default -> { i=42; } 165 } 166 System.out.println(i); 167 } 168 public static void main(String[] args) { 169 test1(); 170 test2(); 171 test3(); 172 test4(); 173 test5(); 174 test6(); 175 test7(); 176 test8(); 177 test9(); 178 test10(); 179 test11(); 180 test12(); 181 test13(); 182 test14(); 183 test15(); 184 } 185 }