[jdk19] Integrated: JDK-8289894: A NullPointerException thrown from guard expression

Jan Lahoda jlahoda at openjdk.org
Mon Jul 11 09:03:41 UTC 2022


On Thu, 7 Jul 2022 14:48:09 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider the following code:
> 
> public class SwitchGuard {
>     public static void main(String... args) {
>         test(null);
>     }
>     private static void test(Object o) {
>         switch (o) {
>             case null, String s when s.isEmpty() -> System.err.println("OK.");
>             default -> {}
>         }
>     }
> }
> 
> 
> In the current specification, the guard should not be invoked when `o` is `null`, but javac will invoke it, for historical reasons.
> 
> Also, as opposed to JDK 18/JEP 420, `case null, <pattern>` is allowed for parenthesized pattern when the parenthesized pattern encloses (directly or via other parenthesized patterns) a type pattern.
> 
> The patch proposed here strives to fix both of these problems.

This pull request has now been integrated.

Changeset: 25f4b043
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.org/jdk19/commit/25f4b04365e40a91ba7a06f6f9fe99e1785ce4f4
Stats:     69 lines in 6 files changed: 55 ins; 0 del; 14 mod

8289894: A NullPointerException thrown from guard expression

Reviewed-by: vromero

-------------

PR: https://git.openjdk.org/jdk19/pull/120


More information about the compiler-dev mailing list