From 6c138d3981ff2c3d2635908ceae9c8a1992544a9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 14 Oct 2015 14:00:02 +0200 Subject: [PATCH] Reliable null value handling in ConcurrentMapCache, GuavaCache, JCacheCache Issue: SPR-13553 (cherry picked from commit de93290) --- .../cache/jcache/JCacheCache.java | 6 ++++- .../cache/concurrent/ConcurrentMapCache.java | 24 ++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java index d47e3dcb92..7c5ede5ca8 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,6 +126,10 @@ public class JCacheCache implements Cache { @SuppressWarnings("serial") private static class NullHolder implements Serializable { + + private Object readResolve() { + return NULL_HOLDER; + } } } diff --git a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java index 54b9b6ef14..5ba4e2c2aa 100644 --- a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java +++ b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +16,17 @@ package org.springframework.cache.concurrent; -import org.springframework.cache.Cache; -import org.springframework.cache.support.SimpleValueWrapper; - import java.io.Serializable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import org.springframework.cache.Cache; +import org.springframework.cache.support.SimpleValueWrapper; +import org.springframework.util.Assert; + /** - * Simple {@link Cache} implementation based on the core JDK - * {@code java.util.concurrent} package. + * Simple {@link org.springframework.cache.Cache} implementation based on the + * core JDK {@code java.util.concurrent} package. * *

Useful for testing or simple caching scenarios, typically in combination * with {@link org.springframework.cache.support.SimpleCacheManager} or @@ -62,7 +63,8 @@ public class ConcurrentMapCache implements Cache { /** * Create a new ConcurrentMapCache with the specified name. * @param name the name of the cache - * @param allowNullValues whether to accept and convert null values for this cache + * @param allowNullValues whether to accept and convert {@code null} + * values for this cache */ public ConcurrentMapCache(String name, boolean allowNullValues) { this(name, new ConcurrentHashMap(256), allowNullValues); @@ -70,13 +72,15 @@ public class ConcurrentMapCache implements Cache { /** * Create a new ConcurrentMapCache with the specified name and the - * given internal ConcurrentMap to use. + * given internal {@link ConcurrentMap} to use. * @param name the name of the cache * @param store the ConcurrentMap to use as an internal store * @param allowNullValues whether to allow {@code null} values * (adapting them to an internal null holder value) */ public ConcurrentMapCache(String name, ConcurrentMap store, boolean allowNullValues) { + Assert.notNull(name, "Name must not be null"); + Assert.notNull(store, "Store must not be null"); this.name = name; this.store = store; this.allowNullValues = allowNullValues; @@ -142,6 +146,10 @@ public class ConcurrentMapCache implements Cache { @SuppressWarnings("serial") private static class NullHolder implements Serializable { + + private Object readResolve() { + return NULL_HOLDER; + } } }