Skip to content
Permalink
Browse files
8272776: NullPointerException not reported
Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed Sep 3, 2021
1 parent 7b023a3 commit 93eec9a103de7f4d9a87eac5b295c9a50702ee94
Showing 2 changed files with 17 additions and 4 deletions.
@@ -340,9 +340,8 @@ private void handleSwitch(JCTree tree,
JCCase lastCase = cases.last();

if (hasTotalPattern && !hasNullCase) {
JCCase last = lastCase;
if (last.labels.stream().noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
last.labels = last.labels.prepend(makeLit(syms.botType, null));
if (cases.stream().flatMap(c -> c.labels.stream()).noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
lastCase.labels = lastCase.labels.prepend(makeLit(syms.botType, null));
hasNullCase = true;
}
}
@@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8262891
* @bug 8262891 8272776
* @summary Check null handling for non-pattern switches.
* @compile --enable-preview -source ${jdk.version} NullSwitch.java
* @run main/othervm --enable-preview NullSwitch
@@ -57,6 +57,9 @@ void switchTest() {
assertEquals(0, matchingSwitch12(""));
assertEquals(2, matchingSwitch12(null));
assertEquals(1, matchingSwitch12(0.0));
assertEquals(0, matchingSwitch13(""));
assertEquals(1, matchingSwitch13(0.0));
assertEquals(2, matchingSwitch13(null));
}

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

private int matchingSwitch13(Object obj) {
try {
switch (obj) {
default: return 1;
case String s: return 0;
}
} catch (NullPointerException ex) {
return 2;
}
}

static void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("Expected: " + expected + ", actual: " + actual);

1 comment on commit 93eec9a

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 93eec9a Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.