From f135a2d6ca8df4e6370f9733408e4e68d6c4967a Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 29 Jul 2025 17:11:37 +0200 Subject: [PATCH] Add more nullability annotations to core/spring-boot See gh-46587 --- .../springframework/boot/json/JsonValueWriter.java | 3 ++- .../org/springframework/boot/util/LambdaSafe.java | 14 +++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/core/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java b/core/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java index b2446e8812f..ed26a424a63 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java @@ -346,7 +346,8 @@ class JsonValueWriter { return value; } - @SuppressWarnings({ "unchecked" }) + // Lambda isn't detected with the correct nullability + @SuppressWarnings({ "unchecked", "NullAway" }) private @Nullable V processValue(@Nullable V value, ValueProcessor valueProcessor) { return (V) LambdaSafe.callback(ValueProcessor.class, valueProcessor, this.path, new Object[] { value }) .invokeAnd((call) -> call.processValue(this.path, value)) diff --git a/core/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java b/core/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java index 5a6b7724d02..ca1df772205 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java @@ -155,7 +155,7 @@ public final class LambdaSafe { return self(); } - protected final InvocationResult invoke(C callbackInstance, Supplier supplier) { + protected final InvocationResult invoke(C callbackInstance, Supplier<@Nullable R> supplier) { if (this.filter.match(this.callbackType, callbackInstance, this.argument, this.additionalArguments)) { try { return InvocationResult.of(supplier.get()); @@ -245,6 +245,8 @@ public final class LambdaSafe { * Invoke the callback instance where the callback method returns void. * @param invoker the invoker used to invoke the callback */ + // Lambda isn't detected with the correct nullability + @SuppressWarnings("NullAway") public void invoke(Consumer invoker) { invoke(this.callbackInstance, () -> { invoker.accept(this.callbackInstance); @@ -259,7 +261,9 @@ public final class LambdaSafe { * @return the result of the invocation (may be {@link InvocationResult#noResult} * if the callback was not invoked) */ - public InvocationResult invokeAnd(Function invoker) { + // Lambda isn't detected with the correct nullability + @SuppressWarnings("NullAway") + public InvocationResult invokeAnd(Function invoker) { return invoke(this.callbackInstance, () -> invoker.apply(this.callbackInstance)); } @@ -285,6 +289,8 @@ public final class LambdaSafe { * Invoke the callback instances where the callback method returns void. * @param invoker the invoker used to invoke the callback */ + // Lambda isn't detected with the correct nullability + @SuppressWarnings("NullAway") public void invoke(Consumer invoker) { this.callbackInstances.forEach((callbackInstance) -> invoke(callbackInstance, () -> { invoker.accept(callbackInstance); @@ -299,7 +305,9 @@ public final class LambdaSafe { * @return the results of the invocation (may be an empty stream if no callbacks * could be called) */ - public Stream invokeAnd(Function invoker) { + // Lambda isn't detected with the correct nullability + @SuppressWarnings("NullAway") + public Stream invokeAnd(Function invoker) { Function> mapper = (callbackInstance) -> invoke(callbackInstance, () -> invoker.apply(callbackInstance)); return this.callbackInstances.stream()