diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java index 5578bb91f0..81ed04f5dd 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java @@ -25,7 +25,7 @@ import org.springframework.cache.interceptor.CacheOperationInvoker; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.jcache.model.CacheResultOperation; import org.springframework.util.SerializationUtils; -import org.springframework.util.filter.ExceptionTypeFilter; +import org.springframework.util.ExceptionTypeFilter; /** * Intercept methods annotated with {@link CacheResult}. @@ -65,10 +65,10 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor context) { CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver(); if (exceptionCacheResolver != null) { - return extractFrom(context.getOperation() - .getExceptionCacheResolver().resolveCaches(context)); + return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context)); } return null; } @@ -121,8 +119,9 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor implements JCache protected final List allParameterDetails; + /** * Create a new instance. * @param methodDetails the {@link CacheMethodDetails} related to the cached method @@ -62,30 +62,32 @@ public abstract class BaseCacheOperation implements JCache this.allParameterDetails = initializeAllParameterDetails(methodDetails.getMethod()); } + /** * Return the {@link ExceptionTypeFilter} to use to filter exceptions thrown while * invoking the method. */ public abstract ExceptionTypeFilter getExceptionTypeFilter(); + @Override public Method getMethod() { - return methodDetails.getMethod(); + return this.methodDetails.getMethod(); } @Override public Set getAnnotations() { - return methodDetails.getAnnotations(); + return this.methodDetails.getAnnotations(); } @Override public A getCacheAnnotation() { - return methodDetails.getCacheAnnotation(); + return this.methodDetails.getCacheAnnotation(); } @Override public String getCacheName() { - return methodDetails.getCacheName(); + return this.methodDetails.getCacheName(); } @Override @@ -95,37 +97,28 @@ public abstract class BaseCacheOperation implements JCache @Override public CacheResolver getCacheResolver() { - return cacheResolver; + return this.cacheResolver; } @Override public CacheInvocationParameter[] getAllParameters(Object... values) { - if (allParameterDetails.size() != values.length) { - throw new IllegalStateException("Values mismatch, operation has " - + allParameterDetails.size() + " parameter(s) but got " + values.length + " value(s)"); + if (this.allParameterDetails.size() != values.length) { + throw new IllegalStateException("Values mismatch, operation has " + + this.allParameterDetails.size() + " parameter(s) but got " + values.length + " value(s)"); } List result = new ArrayList(); - for (int i = 0; i < allParameterDetails.size(); i++) { - result.add(allParameterDetails.get(i).toCacheInvocationParameter(values[i])); + for (int i = 0; i < this.allParameterDetails.size(); i++) { + result.add(this.allParameterDetails.get(i).toCacheInvocationParameter(values[i])); } return result.toArray(new CacheInvocationParameter[result.size()]); } - protected ExceptionTypeFilter createExceptionTypeFiler(Class[] includes, - Class[] excludes) { + protected ExceptionTypeFilter createExceptionTypeFilter( + Class[] includes, Class[] excludes) { + return new ExceptionTypeFilter(asList(includes), asList(excludes), true); } - - private static List initializeAllParameterDetails(Method method) { - List result = new ArrayList(); - for (int i = 0; i < method.getParameterTypes().length; i++) { - CacheParameterDetail detail = new CacheParameterDetail(method, i); - result.add(detail); - } - return result; - } - @Override public String toString() { return getOperationDescription().append("]").toString(); @@ -144,6 +137,16 @@ public abstract class BaseCacheOperation implements JCache } + private static List initializeAllParameterDetails(Method method) { + List result = new ArrayList(); + for (int i = 0; i < method.getParameterTypes().length; i++) { + CacheParameterDetail detail = new CacheParameterDetail(method, i); + result.add(detail); + } + return result; + } + + protected static class CacheParameterDetail { private final Class rawType; @@ -156,7 +159,7 @@ public abstract class BaseCacheOperation implements JCache private final boolean isValue; - private CacheParameterDetail(Method m, int parameterPosition) { + public CacheParameterDetail(Method m, int parameterPosition) { this.rawType = m.getParameterTypes()[parameterPosition]; this.annotations = new LinkedHashSet(); boolean foundKeyAnnotation = false; @@ -176,15 +179,15 @@ public abstract class BaseCacheOperation implements JCache } public int getParameterPosition() { - return parameterPosition; + return this.parameterPosition; } protected boolean isKey() { - return isKey; + return this.isKey; } protected boolean isValue() { - return isValue; + return this.isValue; } public CacheInvocationParameter toCacheInvocationParameter(Object value) { @@ -192,35 +195,36 @@ public abstract class BaseCacheOperation implements JCache } } + protected static class CacheInvocationParameterImpl implements CacheInvocationParameter { private final CacheParameterDetail detail; private final Object value; - private CacheInvocationParameterImpl(CacheParameterDetail detail, Object value) { + public CacheInvocationParameterImpl(CacheParameterDetail detail, Object value) { this.detail = detail; this.value = value; } @Override public Class getRawType() { - return detail.rawType; + return this.detail.rawType; } @Override public Object getValue() { - return value; + return this.value; } @Override public Set getAnnotations() { - return detail.annotations; + return this.detail.annotations; } @Override public int getParameterPosition() { - return detail.parameterPosition; + return this.detail.parameterPosition; } } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CachePutOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CachePutOperation.java index 35e958f46f..71a40e6421 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CachePutOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CachePutOperation.java @@ -25,7 +25,7 @@ import javax.cache.annotation.CachePut; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; -import org.springframework.util.filter.ExceptionTypeFilter; +import org.springframework.util.ExceptionTypeFilter; /** * The {@link JCacheOperation} implementation for a {@link CachePut} operation. @@ -40,11 +40,13 @@ public class CachePutOperation extends BaseKeyCacheOperation { private final CacheParameterDetail valueParameterDetail; - public CachePutOperation(CacheMethodDetails methodDetails, - CacheResolver cacheResolver, KeyGenerator keyGenerator) { + + public CachePutOperation( + CacheMethodDetails methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) { + super(methodDetails, cacheResolver, keyGenerator); CachePut ann = methodDetails.getCacheAnnotation(); - this.exceptionTypeFilter = createExceptionTypeFiler(ann.cacheFor(), ann.noCacheFor()); + this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor()); this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), allParameterDetails); if (valueParameterDetail == null) { throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " + @@ -52,9 +54,10 @@ public class CachePutOperation extends BaseKeyCacheOperation { } } + @Override public ExceptionTypeFilter getExceptionTypeFilter() { - return exceptionTypeFilter; + return this.exceptionTypeFilter; } /** @@ -74,17 +77,18 @@ public class CachePutOperation extends BaseKeyCacheOperation { * @return the {@link CacheInvocationParameter} instance for the value parameter */ public CacheInvocationParameter getValueParameter(Object... values) { - int parameterPosition = valueParameterDetail.getParameterPosition(); + int parameterPosition = this.valueParameterDetail.getParameterPosition(); if (parameterPosition >= values.length) { - throw new IllegalStateException("Values mismatch, value parameter at position " - + parameterPosition + " cannot be matched against " + values.length + " value(s)"); + throw new IllegalStateException("Values mismatch, value parameter at position " + + parameterPosition + " cannot be matched against " + values.length + " value(s)"); } - return valueParameterDetail.toCacheInvocationParameter(values[parameterPosition]); + return this.valueParameterDetail.toCacheInvocationParameter(values[parameterPosition]); } - private static CacheParameterDetail initializeValueParameterDetail(Method method, - List allParameters) { + private static CacheParameterDetail initializeValueParameterDetail( + Method method, List allParameters) { + CacheParameterDetail result = null; for (CacheParameterDetail parameter : allParameters) { if (parameter.isValue()) { diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveAllOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveAllOperation.java index dc524197e1..b3c24152a1 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveAllOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveAllOperation.java @@ -20,7 +20,7 @@ import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheRemoveAll; import org.springframework.cache.interceptor.CacheResolver; -import org.springframework.util.filter.ExceptionTypeFilter; +import org.springframework.util.ExceptionTypeFilter; /** * The {@link JCacheOperation} implementation for a {@link CacheRemoveAll} operation. @@ -33,15 +33,17 @@ public class CacheRemoveAllOperation extends BaseCacheOperation private final ExceptionTypeFilter exceptionTypeFilter; + public CacheRemoveAllOperation(CacheMethodDetails methodDetails, CacheResolver cacheResolver) { super(methodDetails, cacheResolver); CacheRemoveAll ann = methodDetails.getCacheAnnotation(); - this.exceptionTypeFilter = createExceptionTypeFiler(ann.evictFor(), ann.noEvictFor()); + this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor()); } + @Override public ExceptionTypeFilter getExceptionTypeFilter() { - return exceptionTypeFilter; + return this.exceptionTypeFilter; } /** diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveOperation.java index 45280d113a..d05335d7b8 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheRemoveOperation.java @@ -21,7 +21,7 @@ import javax.cache.annotation.CacheRemove; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; -import org.springframework.util.filter.ExceptionTypeFilter; +import org.springframework.util.ExceptionTypeFilter; /** * The {@link JCacheOperation} implementation for a {@link CacheRemove} operation. @@ -34,16 +34,19 @@ public class CacheRemoveOperation extends BaseKeyCacheOperation { private final ExceptionTypeFilter exceptionTypeFilter; - public CacheRemoveOperation(CacheMethodDetails methodDetails, - CacheResolver cacheResolver, KeyGenerator keyGenerator) { + + public CacheRemoveOperation( + CacheMethodDetails methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) { + super(methodDetails, cacheResolver, keyGenerator); CacheRemove ann = methodDetails.getCacheAnnotation(); - this.exceptionTypeFilter = createExceptionTypeFiler(ann.evictFor(), ann.noEvictFor()); + this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor()); } + @Override public ExceptionTypeFilter getExceptionTypeFilter() { - return exceptionTypeFilter; + return this.exceptionTypeFilter; } /** diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheResultOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheResultOperation.java index 0ced866948..fbc2e654fc 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheResultOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/CacheResultOperation.java @@ -22,7 +22,7 @@ import javax.cache.annotation.CacheResult; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.util.StringUtils; -import org.springframework.util.filter.ExceptionTypeFilter; +import org.springframework.util.ExceptionTypeFilter; /** * The {@link JCacheOperation} implementation for a {@link CacheResult} operation. @@ -39,20 +39,22 @@ public class CacheResultOperation extends BaseKeyCacheOperation { private final String exceptionCacheName; - public CacheResultOperation(CacheMethodDetails methodDetails, - CacheResolver cacheResolver, KeyGenerator keyGenerator, - CacheResolver exceptionCacheResolver) { + + public CacheResultOperation(CacheMethodDetails methodDetails, CacheResolver cacheResolver, + KeyGenerator keyGenerator, CacheResolver exceptionCacheResolver) { + super(methodDetails, cacheResolver, keyGenerator); CacheResult ann = methodDetails.getCacheAnnotation(); - this.exceptionTypeFilter = createExceptionTypeFiler(ann.cachedExceptions(), ann.nonCachedExceptions()); + this.exceptionTypeFilter = createExceptionTypeFilter(ann.cachedExceptions(), ann.nonCachedExceptions()); this.exceptionCacheResolver = exceptionCacheResolver; String exceptionCacheNameCandidate = ann.exceptionCacheName(); this.exceptionCacheName = StringUtils.hasText(exceptionCacheNameCandidate) ? exceptionCacheNameCandidate : null; } + @Override public ExceptionTypeFilter getExceptionTypeFilter() { - return exceptionTypeFilter; + return this.exceptionTypeFilter; } /** @@ -69,7 +71,7 @@ public class CacheResultOperation extends BaseKeyCacheOperation { * use for matching exceptions thrown by this operation. */ public CacheResolver getExceptionCacheResolver() { - return exceptionCacheResolver; + return this.exceptionCacheResolver; } /** @@ -78,7 +80,7 @@ public class CacheResultOperation extends BaseKeyCacheOperation { * @see javax.cache.annotation.CacheResult#exceptionCacheName() */ public String getExceptionCacheName() { - return exceptionCacheName; + return this.exceptionCacheName; } } diff --git a/spring-core/src/main/java/org/springframework/util/filter/ExceptionTypeFilter.java b/spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java similarity index 97% rename from spring-core/src/main/java/org/springframework/util/filter/ExceptionTypeFilter.java rename to spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java index cd005e426d..2ce7d42f95 100644 --- a/spring-core/src/main/java/org/springframework/util/filter/ExceptionTypeFilter.java +++ b/spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.util.filter; +package org.springframework.util; import java.util.Collection; @@ -29,6 +29,7 @@ public class ExceptionTypeFilter extends InstanceFilter> includes, Collection> excludes, boolean matchIfEmpty) { + super(includes, excludes, matchIfEmpty); } diff --git a/spring-core/src/main/java/org/springframework/util/filter/InstanceFilter.java b/spring-core/src/main/java/org/springframework/util/InstanceFilter.java similarity index 85% rename from spring-core/src/main/java/org/springframework/util/filter/InstanceFilter.java rename to spring-core/src/main/java/org/springframework/util/InstanceFilter.java index 99f96000ce..6aa8461238 100644 --- a/spring-core/src/main/java/org/springframework/util/filter/InstanceFilter.java +++ b/spring-core/src/main/java/org/springframework/util/InstanceFilter.java @@ -14,13 +14,11 @@ * limitations under the License. */ -package org.springframework.util.filter; +package org.springframework.util; import java.util.Collection; import java.util.Collections; -import org.springframework.util.Assert; - /** * A simple instance filter that checks if a given instance match based on * a collection of includes and excludes element. @@ -39,6 +37,7 @@ public class InstanceFilter { private final boolean matchIfEmpty; + /** * Create a new instance based on includes/excludes collections. *

A particular element will match if it "matches" the one of the element in the @@ -47,7 +46,6 @@ public class InstanceFilter { * another if it is equals according to {@link Object#equals(Object)} *

If both collections are empty, {@code matchIfEmpty} defines if * an element matches or not. - * * @param includes the collection of includes * @param excludes the collection of excludes * @param matchIfEmpty the matching result if both the includes and the excludes @@ -61,20 +59,21 @@ public class InstanceFilter { this.matchIfEmpty = matchIfEmpty; } + /** * Determine if the specified {code instance} matches this filter. */ public boolean match(T instance) { - Assert.notNull(instance, "The instance to match is mandatory."); + Assert.notNull(instance, "The instance to match is mandatory"); - boolean includesSet = !includes.isEmpty(); - boolean excludesSet = !excludes.isEmpty(); + boolean includesSet = !this.includes.isEmpty(); + boolean excludesSet = !this.excludes.isEmpty(); if (!includesSet && !excludesSet) { - return matchIfEmpty; + return this.matchIfEmpty; } - boolean matchIncludes = match(instance, includes); - boolean matchExcludes = match(instance, excludes); + boolean matchIncludes = match(instance, this.includes); + boolean matchExcludes = match(instance, this.excludes); if (!includesSet) { return !matchExcludes; @@ -89,7 +88,6 @@ public class InstanceFilter { /** * Determine if the specified {@code instance} is equal to the * specified {@code candidate}. - * * @param instance the instance to handle * @param candidate a candidate defined by this filter * @return {@code true} if the instance matches the candidate @@ -101,7 +99,6 @@ public class InstanceFilter { /** * Determine if the specified {@code instance} matches one of the candidates. *

If the candidates collection is {@code null}, returns {@code false}. - * * @param instance the instance to check * @param candidates a list of candidates * @return {@code true} if the instance match or the candidates collection is null @@ -117,7 +114,11 @@ public class InstanceFilter { @Override public String toString() { - return "includes=" + includes + ", excludes=" + excludes + ", matchIfEmpty=" + matchIfEmpty; + StringBuilder sb = new StringBuilder(getClass().getSimpleName()); + sb.append(": includes=").append(this.includes); + sb.append(", excludes=").append(this.excludes); + sb.append(", matchIfEmpty=").append(this.matchIfEmpty); + return sb.toString(); } } diff --git a/spring-core/src/test/java/org/springframework/util/filter/ExceptionTypeFilterTests.java b/spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java similarity index 92% rename from spring-core/src/test/java/org/springframework/util/filter/ExceptionTypeFilterTests.java rename to spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java index d8a05481ff..f98957d61c 100644 --- a/spring-core/src/test/java/org/springframework/util/filter/ExceptionTypeFilterTests.java +++ b/spring-core/src/test/java/org/springframework/util/ExceptionTypeFilterTests.java @@ -14,13 +14,15 @@ * limitations under the License. */ -package org.springframework.util.filter; +package org.springframework.util; import static java.util.Arrays.*; import static org.junit.Assert.*; import org.junit.Test; +import org.springframework.util.ExceptionTypeFilter; + /** * @author Stephane Nicoll */ diff --git a/spring-core/src/test/java/org/springframework/util/filter/InstanceFilterTests.java b/spring-core/src/test/java/org/springframework/util/InstanceFilterTests.java similarity index 96% rename from spring-core/src/test/java/org/springframework/util/filter/InstanceFilterTests.java rename to spring-core/src/test/java/org/springframework/util/InstanceFilterTests.java index 48c12156b3..2e7f5da16d 100644 --- a/spring-core/src/test/java/org/springframework/util/filter/InstanceFilterTests.java +++ b/spring-core/src/test/java/org/springframework/util/InstanceFilterTests.java @@ -14,13 +14,15 @@ * limitations under the License. */ -package org.springframework.util.filter; +package org.springframework.util; import static java.util.Arrays.*; import static org.junit.Assert.*; import org.junit.Test; +import org.springframework.util.InstanceFilter; + /** * @author Stephane Nicoll */