From 6ee7e20128f1152e53e101ee210854a56a37d133 Mon Sep 17 00:00:00 2001 From: Chris Povirk Date: Thu, 5 Dec 2024 10:31:49 -0500 Subject: [PATCH] Address nullness diagnostic. See https://github.com/ben-manes/caffeine/actions/runs/12181931315/job/33979716621?pr=1806 ``` Warning: /home/runner/work/caffeine/caffeine/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java:233: warning: [NullAway] method returns @Nullable, but superclass method com.github.benmanes.caffeine.cache.CacheLoader.load(K) returns @NonNull @Override public @Nullable V load(K key) { ^ (see http://t.uber.com/nullaway ) Did you mean '@SuppressWarnings("NullAway") @Override public @Nullable V load(K key) {'? error: warnings found and -Werror specified ``` --- .../benmanes/caffeine/cache/CacheLoader.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java index a8dff17fb4..b470946604 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java @@ -226,14 +226,20 @@ default CompletableFuture asyncReload( * @throws NullPointerException if the mappingFunction is null */ @SuppressWarnings("FunctionalInterfaceClash") - static CacheLoader bulk(Function, - ? extends Map> mappingFunction) { + static CacheLoader bulk(Function, + ? extends Map> mappingFunction) { requireNonNull(mappingFunction); return new CacheLoader<>() { - @Override public @Nullable V load(K key) { + /* + * If the caller passes a mapping function that may ever return partial results, then calls to + * load() may return null. In that case, the caller should type the return value of bulk(...) + * as a CacheLoader, rather than a CacheLoader. + */ + @SuppressWarnings("NullAway") + @Override public V load(K key) { return loadAll(Set.of(key)).get(key); } - @Override public Map loadAll(Set keys) { + @Override public Map loadAll(Set keys) { return mappingFunction.apply(keys); } };