diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java b/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java index d3c15b4d7c6..c90292763ee 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java @@ -32,6 +32,7 @@ import org.springframework.util.Assert; * invocation context. * * @author Stephane Nicoll + * @author Juergen Hoeller * @since 4.1 */ public abstract class AbstractCacheResolver implements CacheResolver, InitializingBean { @@ -83,18 +84,16 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi if (cacheNames == null) { return Collections.emptyList(); } - else { - Collection result = new ArrayList<>(); - for (String cacheName : cacheNames) { - Cache cache = getCacheManager().getCache(cacheName); - if (cache == null) { - throw new IllegalArgumentException("Cannot find cache named '" + - cacheName + "' for " + context.getOperation()); - } - result.add(cache); + Collection result = new ArrayList<>(cacheNames.size()); + for (String cacheName : cacheNames) { + Cache cache = getCacheManager().getCache(cacheName); + if (cache == null) { + throw new IllegalArgumentException("Cannot find cache named '" + + cacheName + "' for " + context.getOperation()); } - return result; + result.add(cache); } + return result; } /** diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index cd6c84ab9f5..47c4694857c 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -586,16 +586,16 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker private class CacheOperationContexts { - private final MultiValueMap, CacheOperationContext> contexts = - new LinkedMultiValueMap<>(); + private final MultiValueMap, CacheOperationContext> contexts; private final boolean sync; public CacheOperationContexts(Collection operations, Method method, Object[] args, Object target, Class targetClass) { - for (CacheOperation operation : operations) { - this.contexts.add(operation.getClass(), getOperationContext(operation, method, args, target, targetClass)); + this.contexts = new LinkedMultiValueMap<>(operations.size()); + for (CacheOperation op : operations) { + this.contexts.add(op.getClass(), getOperationContext(op, method, args, target, targetClass)); } this.sync = determineSyncFlag(method); } @@ -623,18 +623,22 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker } if (syncEnabled) { if (this.contexts.size() > 1) { - throw new IllegalStateException("@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'"); + throw new IllegalStateException( + "@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'"); } if (cacheOperationContexts.size() > 1) { - throw new IllegalStateException("Only one @Cacheable(sync=true) entry is allowed on '" + method + "'"); + throw new IllegalStateException( + "Only one @Cacheable(sync=true) entry is allowed on '" + method + "'"); } CacheOperationContext cacheOperationContext = cacheOperationContexts.iterator().next(); CacheableOperation operation = (CacheableOperation) cacheOperationContext.getOperation(); if (cacheOperationContext.getCaches().size() > 1) { - throw new IllegalStateException("@Cacheable(sync=true) only allows a single cache on '" + operation + "'"); + throw new IllegalStateException( + "@Cacheable(sync=true) only allows a single cache on '" + operation + "'"); } if (StringUtils.hasText(operation.getUnless())) { - throw new IllegalStateException("@Cacheable(sync=true) does not support unless attribute on '" + operation + "'"); + throw new IllegalStateException( + "@Cacheable(sync=true) does not support unless attribute on '" + operation + "'"); } return true; }