File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/patterns Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -340,9 +340,8 @@ private void handleSwitch(JCTree tree,
340
340
JCCase lastCase = cases .last ();
341
341
342
342
if (hasTotalPattern && !hasNullCase ) {
343
- JCCase last = lastCase ;
344
- if (last .labels .stream ().noneMatch (l -> l .hasTag (Tag .DEFAULTCASELABEL ))) {
345
- last .labels = last .labels .prepend (makeLit (syms .botType , null ));
343
+ if (cases .stream ().flatMap (c -> c .labels .stream ()).noneMatch (l -> l .hasTag (Tag .DEFAULTCASELABEL ))) {
344
+ lastCase .labels = lastCase .labels .prepend (makeLit (syms .botType , null ));
346
345
hasNullCase = true ;
347
346
}
348
347
}
Original file line number Diff line number Diff line change 1
1
/*
2
2
* @test /nodynamiccopyright/
3
- * @bug 8262891
3
+ * @bug 8262891 8272776
4
4
* @summary Check null handling for non-pattern switches.
5
5
* @compile --enable-preview -source ${jdk.version} NullSwitch.java
6
6
* @run main/othervm --enable-preview NullSwitch
@@ -57,6 +57,9 @@ void switchTest() {
57
57
assertEquals (0 , matchingSwitch12 ("" ));
58
58
assertEquals (2 , matchingSwitch12 (null ));
59
59
assertEquals (1 , matchingSwitch12 (0.0 ));
60
+ assertEquals (0 , matchingSwitch13 ("" ));
61
+ assertEquals (1 , matchingSwitch13 (0.0 ));
62
+ assertEquals (2 , matchingSwitch13 (null ));
60
63
}
61
64
62
65
private int matchingSwitch1 (Object obj ) {
@@ -159,6 +162,17 @@ private int matchingSwitch12(Object obj) {
159
162
}
160
163
}
161
164
165
+ private int matchingSwitch13 (Object obj ) {
166
+ try {
167
+ switch (obj ) {
168
+ default : return 1 ;
169
+ case String s : return 0 ;
170
+ }
171
+ } catch (NullPointerException ex ) {
172
+ return 2 ;
173
+ }
174
+ }
175
+
162
176
static void assertEquals (int expected , int actual ) {
163
177
if (expected != actual ) {
164
178
throw new AssertionError ("Expected: " + expected + ", actual: " + actual );
You can’t perform that action at this time.
0 commit comments