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