Refine SingletonSupplier<T> static methods nullability

SingletonSupplier<T> supplier-based static methods nullability should
be refined to accept in a flexible way nullable or non-nullable T.

Closes gh-35559
This commit is contained in:
Sébastien Deleuze 2025-10-01 10:53:12 +02:00
parent a2c0e1cec8
commit 1e3f8651b8
5 changed files with 19 additions and 14 deletions

View File

@ -41,11 +41,12 @@ import org.springframework.context.annotation.Role;
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
public abstract class AbstractJCacheConfiguration extends AbstractCachingConfiguration { public abstract class AbstractJCacheConfiguration extends AbstractCachingConfiguration {
protected @Nullable Supplier<@Nullable CacheResolver> exceptionCacheResolver; @SuppressWarnings("NullAway.Init")
protected Supplier<@Nullable CacheResolver> exceptionCacheResolver;
@Override @Override
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1128 @SuppressWarnings("NullAway") // See https://github.com/uber/NullAway/issues/1290
protected void useCachingConfigurer(CachingConfigurerSupplier cachingConfigurerSupplier) { protected void useCachingConfigurer(CachingConfigurerSupplier cachingConfigurerSupplier) {
super.useCachingConfigurer(cachingConfigurerSupplier); super.useCachingConfigurer(cachingConfigurerSupplier);
this.exceptionCacheResolver = cachingConfigurerSupplier.adapt(config -> { this.exceptionCacheResolver = cachingConfigurerSupplier.adapt(config -> {

View File

@ -50,11 +50,11 @@ import org.springframework.util.function.SupplierUtils;
public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSource public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSource
implements BeanFactoryAware, SmartInitializingSingleton { implements BeanFactoryAware, SmartInitializingSingleton {
private @Nullable SingletonSupplier<CacheManager> cacheManager; private @Nullable SingletonSupplier<@Nullable CacheManager> cacheManager;
private @Nullable SingletonSupplier<CacheResolver> cacheResolver; private @Nullable SingletonSupplier<@Nullable CacheResolver> cacheResolver;
private @Nullable SingletonSupplier<CacheResolver> exceptionCacheResolver; private @Nullable SingletonSupplier<@Nullable CacheResolver> exceptionCacheResolver;
private SingletonSupplier<KeyGenerator> keyGenerator; private SingletonSupplier<KeyGenerator> keyGenerator;

View File

@ -50,13 +50,17 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
protected @Nullable AnnotationAttributes enableCaching; protected @Nullable AnnotationAttributes enableCaching;
protected @Nullable Supplier<@Nullable CacheManager> cacheManager; @SuppressWarnings("NullAway.Init")
protected Supplier<@Nullable CacheManager> cacheManager;
protected @Nullable Supplier<@Nullable CacheResolver> cacheResolver; @SuppressWarnings("NullAway.Init")
protected Supplier<@Nullable CacheResolver> cacheResolver;
protected @Nullable Supplier<@Nullable KeyGenerator> keyGenerator; @SuppressWarnings("NullAway.Init")
protected Supplier<@Nullable KeyGenerator> keyGenerator;
protected @Nullable Supplier<@Nullable CacheErrorHandler> errorHandler; @SuppressWarnings("NullAway.Init")
protected Supplier<@Nullable CacheErrorHandler> errorHandler;
@Override @Override
@ -101,7 +105,7 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
protected static class CachingConfigurerSupplier { protected static class CachingConfigurerSupplier {
private final SingletonSupplier<CachingConfigurer> supplier; private final SingletonSupplier<@Nullable CachingConfigurer> supplier;
public CachingConfigurerSupplier(Supplier<@Nullable CachingConfigurer> supplier) { public CachingConfigurerSupplier(Supplier<@Nullable CachingConfigurer> supplier) {
this.supplier = SingletonSupplier.ofNullable(supplier); this.supplier = SingletonSupplier.ofNullable(supplier);

View File

@ -143,7 +143,7 @@ public class SingletonSupplier<T extends @Nullable Object> implements Supplier<T
* @return the singleton supplier, or {@code null} if the instance was {@code null} * @return the singleton supplier, or {@code null} if the instance was {@code null}
*/ */
@Contract("null -> null; !null -> !null") @Contract("null -> null; !null -> !null")
public static <T> @Nullable SingletonSupplier<T> ofNullable(@Nullable T instance) { public static <T extends @Nullable Object> @Nullable SingletonSupplier<T> ofNullable(T instance) {
return (instance != null ? new SingletonSupplier<>(instance) : null); return (instance != null ? new SingletonSupplier<>(instance) : null);
} }
@ -152,7 +152,7 @@ public class SingletonSupplier<T extends @Nullable Object> implements Supplier<T
* @param supplier the instance supplier (never {@code null}) * @param supplier the instance supplier (never {@code null})
* @return the singleton supplier (never {@code null}) * @return the singleton supplier (never {@code null})
*/ */
public static <T> SingletonSupplier<T> of(Supplier<T> supplier) { public static <T extends @Nullable Object> SingletonSupplier<T> of(Supplier<T> supplier) {
return new SingletonSupplier<>(supplier); return new SingletonSupplier<>(supplier);
} }
@ -162,7 +162,7 @@ public class SingletonSupplier<T extends @Nullable Object> implements Supplier<T
* @return the singleton supplier, or {@code null} if the instance supplier was {@code null} * @return the singleton supplier, or {@code null} if the instance supplier was {@code null}
*/ */
@Contract("null -> null; !null -> !null") @Contract("null -> null; !null -> !null")
public static <T> @Nullable SingletonSupplier<T> ofNullable(@Nullable Supplier<@Nullable T> supplier) { public static <T extends @Nullable Object> @Nullable SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier) {
return (supplier != null ? new SingletonSupplier<>(supplier) : null); return (supplier != null ? new SingletonSupplier<>(supplier) : null);
} }

View File

@ -38,7 +38,7 @@ public abstract class SupplierUtils {
* @return the supplier's result, or {@code null} if none * @return the supplier's result, or {@code null} if none
*/ */
@Contract("null -> null") @Contract("null -> null")
public static <T> @Nullable T resolve(@Nullable Supplier<T> supplier) { public static <T extends @Nullable Object> @Nullable T resolve(@Nullable Supplier<T> supplier) {
return (supplier != null ? supplier.get() : null); return (supplier != null ? supplier.get() : null);
} }