author | jlahoda |
Tue, 12 Nov 2019 06:32:13 +0000 | |
changeset 59021 | cfc7bb9a5a92 |
parent 55306 | ea43db53de91 |
permissions | -rw-r--r-- |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
1 |
/* |
53878
4584d0331318
8219254: Update explicit uses of latest source/target in langtools tests to a property
darcy
parents:
53023
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
4 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
8 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
13 |
* accompanied this code). |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
14 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
18 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
21 |
* questions. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
22 |
*/ |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
23 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
24 |
/* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
25 |
* @test |
52936
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
26 |
* @bug 8206986 8214114 8214529 |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
27 |
* @summary Verify various corner cases with nested switch expressions. |
59021 | 28 |
* @compile ExpressionSwitchBugs.java |
29 |
* @run main ExpressionSwitchBugs |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
30 |
*/ |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
31 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
32 |
public class ExpressionSwitchBugs { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
33 |
public static void main(String... args) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
34 |
new ExpressionSwitchBugs().testNested(); |
52796
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
35 |
new ExpressionSwitchBugs().testAnonymousClasses(); |
52936
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
36 |
new ExpressionSwitchBugs().testFields(); |
55306 | 37 |
check(3, new C(-1, 3).test(false)); |
38 |
check(3, new C(3, -1).test(true)); |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
39 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
40 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
41 |
private void testNested() { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
42 |
int i = 0; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
43 |
check(42, id(switch (42) { |
55306 | 44 |
default: i++; yield 42; |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
45 |
})); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
46 |
i = 0; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
47 |
check(43, id(switch (42) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
48 |
case 42: while (i == 0) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
49 |
i++; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
50 |
} |
55306 | 51 |
yield 42 + i; |
52 |
default: i++; yield 42; |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
53 |
})); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
54 |
i = 0; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
55 |
check(42, id(switch (42) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
56 |
case 42: if (i == 0) { |
55306 | 57 |
yield 42; |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
58 |
} |
55306 | 59 |
default: i++; yield 43; |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
60 |
})); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
61 |
i = 0; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
62 |
check(42, id(switch (42) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
63 |
case 42: if (i == 0) { |
55306 | 64 |
yield 41 + switch (0) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
65 |
case 0 -> 1; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
66 |
default -> -1; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
67 |
}; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
68 |
} |
55306 | 69 |
default: i++; yield 43; |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
70 |
})); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
71 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
72 |
|
52796
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
73 |
private void testAnonymousClasses() { |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
74 |
for (int i : new int[] {1, 2}) { |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
75 |
check(3, id((switch (i) { |
55306 | 76 |
case 1: yield new I() { |
52796
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
77 |
public int g() { return 3; } |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
78 |
}; |
55306 | 79 |
default: yield (I) () -> { return 3; }; |
52796
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
80 |
}).g())); |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
81 |
check(3, id((switch (i) { |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
82 |
case 1 -> new I() { |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
83 |
public int g() { return 3; } |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
84 |
}; |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
85 |
default -> (I) () -> { return 3; }; |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
86 |
}).g())); |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
87 |
} |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
88 |
} |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
89 |
|
52936
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
90 |
private void testFields() { |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
91 |
check(3, field); |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
92 |
check(3, ExpressionSwitchBugs.staticField); |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
93 |
} |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
94 |
|
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
95 |
private final int value = 2; |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
96 |
private final int field = id(switch(value) { |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
97 |
case 0 -> -1; |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
98 |
case 2 -> { |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
99 |
int temp = 0; |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
100 |
temp += 3; |
55306 | 101 |
yield temp; |
52936
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
102 |
} |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
103 |
default -> throw new IllegalStateException(); |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
104 |
}); |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
105 |
|
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
106 |
private static final int staticValue = 2; |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
107 |
private static final int staticField = new ExpressionSwitchBugs().id(switch(staticValue) { |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
108 |
case 0 -> -1; |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
109 |
case 2 -> { |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
110 |
int temp = 0; |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
111 |
temp += 3; |
55306 | 112 |
yield temp; |
52936
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
113 |
} |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
114 |
default -> throw new IllegalStateException(); |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
115 |
}); |
9745e4e36dd1
8214114: Switch expressions with try-catch statements
jlahoda
parents:
52796
diff
changeset
|
116 |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
117 |
private int id(int i) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
118 |
return i; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
119 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
120 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
121 |
private int id(Object o) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
122 |
return -1; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
123 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
124 |
|
55306 | 125 |
private static void check(int actual, int expected) { |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
126 |
if (actual != expected) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
127 |
throw new AssertionError("Unexpected result: " + actual); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
128 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
129 |
} |
52796
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
130 |
|
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
131 |
public interface I { |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
132 |
public int g(); |
2c8e6decb1c3
8214529: Exception while using Anonymous class in switch expression
jlahoda
parents:
51563
diff
changeset
|
133 |
} |
55306 | 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 |
} |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
162 |
} |