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

Vicente Romero vromero at openjdk.org
Fri Jul 8 16:27:27 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.

looks sensible

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

Marked as reviewed by vromero (Reviewer).

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


More information about the compiler-dev mailing list