1 /* 2 * Copyright (c) 2018, 2019, 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 8214114 8214529 27 * @summary Verify various corner cases with nested switch expressions. 28 * @compile ExpressionSwitchBugs.java 29 * @run main ExpressionSwitchBugs 30 */ 31 32 public class ExpressionSwitchBugs { 33 public static void main(String... args) { 34 new ExpressionSwitchBugs().testNested(); 35 new ExpressionSwitchBugs().testAnonymousClasses(); 36 new ExpressionSwitchBugs().testFields(); 37 check(3, new C(-1, 3).test(false)); 38 check(3, new C(3, -1).test(true)); 39 } 40 41 private void testNested() { 42 int i = 0; 43 check(42, id(switch (42) { 44 default: i++; yield 42; 45 })); 46 i = 0; 47 check(43, id(switch (42) { 48 case 42: while (i == 0) { 49 i++; 50 } 51 yield 42 + i; 52 default: i++; yield 42; 53 })); 54 i = 0; 55 check(42, id(switch (42) { 56 case 42: if (i == 0) { 57 yield 42; 58 } 59 default: i++; yield 43; 60 })); 61 i = 0; 62 check(42, id(switch (42) { 63 case 42: if (i == 0) { 64 yield 41 + switch (0) { 65 case 0 -> 1; 66 default -> -1; 67 }; 68 } 69 default: i++; yield 43; 70 })); 71 } 72 73 private void testAnonymousClasses() { 74 for (int i : new int[] {1, 2}) { 75 check(3, id((switch (i) { 76 case 1: yield new I() { 77 public int g() { return 3; } 78 }; 79 default: yield (I) () -> { return 3; }; 80 }).g())); 81 check(3, id((switch (i) { 82 case 1 -> new I() { 83 public int g() { return 3; } 84 }; 85 default -> (I) () -> { return 3; }; 86 }).g())); 87 } 88 } 89 90 private void testFields() { 91 check(3, field); 92 check(3, ExpressionSwitchBugs.staticField); 93 } 94 95 private final int value = 2; 96 private final int field = id(switch(value) { 97 case 0 -> -1; 98 case 2 -> { 99 int temp = 0; 100 temp += 3; 101 yield temp; 102 } 103 default -> throw new IllegalStateException(); 104 }); 105 106 private static final int staticValue = 2; 107 private static final int staticField = new ExpressionSwitchBugs().id(switch(staticValue) { 108 case 0 -> -1; 109 case 2 -> { 110 int temp = 0; 111 temp += 3; 112 yield temp; 113 } 114 default -> throw new IllegalStateException(); 115 }); 116 117 private int id(int i) { 118 return i; 119 } 120 121 private int id(Object o) { 122 return -1; 123 } 124 125 private static void check(int actual, int expected) { 126 if (actual != expected) { 127 throw new AssertionError("Unexpected result: " + actual); 128 } 129 } 130 131 public interface I { 132 public int g(); 133 } 134 135 static class Super { 136 public final int i; 137 138 public Super(int i) { 139 this.i = i; 140 } 141 142 } 143 static class C extends Super { 144 public final int i; 145 146 public C(int superI, int i) { 147 super(superI); 148 this.i = i; 149 } 150 151 public int test(boolean fromSuper) { 152 return switch (fromSuper ? 0 : 1) { 153 case 0 -> { 154 yield super.i; 155 } 156 default -> { 157 yield this.i; 158 } 159 }; 160 } 161 } 162 }