Skip to content

Commit 6f6b072

Browse files
Alexey PavlyutkinYuri Nesterenko
authored andcommitted
8272776: NullPointerException not reported
Backport-of: 93eec9a103de7f4d9a87eac5b295c9a50702ee94
1 parent a18e936 commit 6f6b072

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,8 @@ private void handleSwitch(JCTree tree,
341341
JCCase lastCase = cases.last();
342342

343343
if (hasTotalPattern && !hasNullCase) {
344-
JCCase last = lastCase;
345-
if (last.labels.stream().noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
346-
last.labels = last.labels.prepend(makeLit(syms.botType, null));
344+
if (cases.stream().flatMap(c -> c.labels.stream()).noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
345+
lastCase.labels = lastCase.labels.prepend(makeLit(syms.botType, null));
347346
hasNullCase = true;
348347
}
349348
}

test/langtools/tools/javac/patterns/NullSwitch.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8262891
3+
* @bug 8262891 8272776
44
* @summary Check null handling for non-pattern switches.
55
* @compile --enable-preview -source ${jdk.version} NullSwitch.java
66
* @run main/othervm --enable-preview NullSwitch
@@ -57,6 +57,9 @@ void switchTest() {
5757
assertEquals(0, matchingSwitch12(""));
5858
assertEquals(2, matchingSwitch12(null));
5959
assertEquals(1, matchingSwitch12(0.0));
60+
assertEquals(0, matchingSwitch13(""));
61+
assertEquals(1, matchingSwitch13(0.0));
62+
assertEquals(2, matchingSwitch13(null));
6063
}
6164

6265
private int matchingSwitch1(Object obj) {
@@ -159,6 +162,17 @@ private int matchingSwitch12(Object obj) {
159162
}
160163
}
161164

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+
162176
static void assertEquals(int expected, int actual) {
163177
if (expected != actual) {
164178
throw new AssertionError("Expected: " + expected + ", actual: " + actual);

0 commit comments

Comments
 (0)