Polishing
This commit is contained in:
@@ -57,8 +57,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
return parseCacheAnnotations(defaultConfig, method);
|
||||
}
|
||||
|
||||
protected Collection<CacheOperation> parseCacheAnnotations(DefaultCacheConfig cachingConfig,
|
||||
AnnotatedElement ae) {
|
||||
protected Collection<CacheOperation> parseCacheAnnotations(DefaultCacheConfig cachingConfig, AnnotatedElement ae) {
|
||||
Collection<CacheOperation> ops = null;
|
||||
|
||||
Collection<Cacheable> cacheables = getAnnotations(ae, Cacheable.class);
|
||||
@@ -71,24 +70,25 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
Collection<CacheEvict> evicts = getAnnotations(ae, CacheEvict.class);
|
||||
if (evicts != null) {
|
||||
ops = lazyInit(ops);
|
||||
for (CacheEvict e : evicts) {
|
||||
ops.add(parseEvictAnnotation(ae, cachingConfig, e));
|
||||
for (CacheEvict evict : evicts) {
|
||||
ops.add(parseEvictAnnotation(ae, cachingConfig, evict));
|
||||
}
|
||||
}
|
||||
Collection<CachePut> updates = getAnnotations(ae, CachePut.class);
|
||||
if (updates != null) {
|
||||
Collection<CachePut> puts = getAnnotations(ae, CachePut.class);
|
||||
if (puts != null) {
|
||||
ops = lazyInit(ops);
|
||||
for (CachePut p : updates) {
|
||||
ops.add(parseUpdateAnnotation(ae, cachingConfig, p));
|
||||
for (CachePut put : puts) {
|
||||
ops.add(parsePutAnnotation(ae, cachingConfig, put));
|
||||
}
|
||||
}
|
||||
Collection<Caching> caching = getAnnotations(ae, Caching.class);
|
||||
if (caching != null) {
|
||||
Collection<Caching> cachings = getAnnotations(ae, Caching.class);
|
||||
if (cachings != null) {
|
||||
ops = lazyInit(ops);
|
||||
for (Caching c : caching) {
|
||||
ops.addAll(parseCachingAnnotation(ae, cachingConfig, c));
|
||||
for (Caching caching : cachings) {
|
||||
ops.addAll(parseCachingAnnotation(ae, cachingConfig, caching));
|
||||
}
|
||||
}
|
||||
|
||||
return ops;
|
||||
}
|
||||
|
||||
@@ -96,63 +96,62 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
return (ops != null ? ops : new ArrayList<CacheOperation>(1));
|
||||
}
|
||||
|
||||
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae,
|
||||
DefaultCacheConfig defaultConfig, Cacheable caching) {
|
||||
CacheableOperation cuo = new CacheableOperation();
|
||||
cuo.setCacheNames(caching.value());
|
||||
cuo.setCondition(caching.condition());
|
||||
cuo.setUnless(caching.unless());
|
||||
cuo.setKey(caching.key());
|
||||
cuo.setKeyGenerator(caching.keyGenerator());
|
||||
cuo.setCacheManager(caching.cacheManager());
|
||||
cuo.setCacheResolver(caching.cacheResolver());
|
||||
cuo.setName(ae.toString());
|
||||
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable caching) {
|
||||
CacheableOperation op = new CacheableOperation();
|
||||
|
||||
defaultConfig.applyDefault(cuo);
|
||||
op.setCacheNames(caching.value());
|
||||
op.setCondition(caching.condition());
|
||||
op.setUnless(caching.unless());
|
||||
op.setKey(caching.key());
|
||||
op.setKeyGenerator(caching.keyGenerator());
|
||||
op.setCacheManager(caching.cacheManager());
|
||||
op.setCacheResolver(caching.cacheResolver());
|
||||
op.setName(ae.toString());
|
||||
|
||||
validateCacheOperation(ae, cuo);
|
||||
return cuo;
|
||||
defaultConfig.applyDefault(op);
|
||||
validateCacheOperation(ae, op);
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae,
|
||||
DefaultCacheConfig defaultConfig, CacheEvict caching) {
|
||||
CacheEvictOperation ceo = new CacheEvictOperation();
|
||||
ceo.setCacheNames(caching.value());
|
||||
ceo.setCondition(caching.condition());
|
||||
ceo.setKey(caching.key());
|
||||
ceo.setKeyGenerator(caching.keyGenerator());
|
||||
ceo.setCacheManager(caching.cacheManager());
|
||||
ceo.setCacheResolver(caching.cacheResolver());
|
||||
ceo.setCacheWide(caching.allEntries());
|
||||
ceo.setBeforeInvocation(caching.beforeInvocation());
|
||||
ceo.setName(ae.toString());
|
||||
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict caching) {
|
||||
CacheEvictOperation op = new CacheEvictOperation();
|
||||
|
||||
defaultConfig.applyDefault(ceo);
|
||||
op.setCacheNames(caching.value());
|
||||
op.setCondition(caching.condition());
|
||||
op.setKey(caching.key());
|
||||
op.setKeyGenerator(caching.keyGenerator());
|
||||
op.setCacheManager(caching.cacheManager());
|
||||
op.setCacheResolver(caching.cacheResolver());
|
||||
op.setCacheWide(caching.allEntries());
|
||||
op.setBeforeInvocation(caching.beforeInvocation());
|
||||
op.setName(ae.toString());
|
||||
|
||||
validateCacheOperation(ae, ceo);
|
||||
return ceo;
|
||||
defaultConfig.applyDefault(op);
|
||||
validateCacheOperation(ae, op);
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
CacheOperation parseUpdateAnnotation(AnnotatedElement ae,
|
||||
DefaultCacheConfig defaultConfig, CachePut caching) {
|
||||
CachePutOperation cuo = new CachePutOperation();
|
||||
cuo.setCacheNames(caching.value());
|
||||
cuo.setCondition(caching.condition());
|
||||
cuo.setUnless(caching.unless());
|
||||
cuo.setKey(caching.key());
|
||||
cuo.setKeyGenerator(caching.keyGenerator());
|
||||
cuo.setCacheManager(caching.cacheManager());
|
||||
cuo.setCacheResolver(caching.cacheResolver());
|
||||
cuo.setName(ae.toString());
|
||||
CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut caching) {
|
||||
CachePutOperation op = new CachePutOperation();
|
||||
|
||||
defaultConfig.applyDefault(cuo);
|
||||
op.setCacheNames(caching.value());
|
||||
op.setCondition(caching.condition());
|
||||
op.setUnless(caching.unless());
|
||||
op.setKey(caching.key());
|
||||
op.setKeyGenerator(caching.keyGenerator());
|
||||
op.setCacheManager(caching.cacheManager());
|
||||
op.setCacheResolver(caching.cacheResolver());
|
||||
op.setName(ae.toString());
|
||||
|
||||
validateCacheOperation(ae, cuo);
|
||||
return cuo;
|
||||
defaultConfig.applyDefault(op);
|
||||
validateCacheOperation(ae, op);
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
Collection<CacheOperation> parseCachingAnnotation(AnnotatedElement ae,
|
||||
DefaultCacheConfig defaultConfig, Caching caching) {
|
||||
Collection<CacheOperation> parseCachingAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Caching caching) {
|
||||
Collection<CacheOperation> ops = null;
|
||||
|
||||
Cacheable[] cacheables = caching.cacheable();
|
||||
@@ -173,7 +172,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
if (!ObjectUtils.isEmpty(updates)) {
|
||||
ops = lazyInit(ops);
|
||||
for (CachePut update : updates) {
|
||||
ops.add(parseUpdateAnnotation(ae, defaultConfig, update));
|
||||
ops.add(parsePutAnnotation(ae, defaultConfig, update));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,12 +181,11 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
|
||||
/**
|
||||
* Provides the {@link DefaultCacheConfig} instance for the specified {@link Class}.
|
||||
*
|
||||
* @param target the class-level to handle
|
||||
* @return the default config (never {@code null})
|
||||
*/
|
||||
DefaultCacheConfig getDefaultCacheConfig(Class<?> target) {
|
||||
final CacheConfig annotation = AnnotationUtils.getAnnotation(target, CacheConfig.class);
|
||||
CacheConfig annotation = AnnotationUtils.getAnnotation(target, CacheConfig.class);
|
||||
if (annotation != null) {
|
||||
return new DefaultCacheConfig(annotation.cacheNames(), annotation.keyGenerator(),
|
||||
annotation.cacheManager(), annotation.cacheResolver());
|
||||
@@ -220,27 +218,26 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
* <p>Throws an {@link IllegalStateException} if the state of the operation is
|
||||
* invalid. As there might be multiple sources for default values, this ensure
|
||||
* that the operation is in a proper state before being returned.
|
||||
*
|
||||
* @param ae the annotated element of the cache operation
|
||||
* @param operation the {@link CacheOperation} to validate
|
||||
*/
|
||||
private void validateCacheOperation(AnnotatedElement ae, CacheOperation operation) {
|
||||
if (StringUtils.hasText(operation.getKey()) && StringUtils.hasText(operation.getKeyGenerator())) {
|
||||
throw new IllegalStateException("Invalid cache annotation configuration on '"
|
||||
+ ae.toString() + "'. Both 'key' and 'keyGenerator' attributes have been set. " +
|
||||
throw new IllegalStateException("Invalid cache annotation configuration on '" +
|
||||
ae.toString() + "'. Both 'key' and 'keyGenerator' attributes have been set. " +
|
||||
"These attributes are mutually exclusive: either set the SpEL expression used to" +
|
||||
"compute the key at runtime or set the name of the KeyGenerator bean to use.");
|
||||
}
|
||||
if (StringUtils.hasText(operation.getCacheManager()) && StringUtils.hasText(operation.getCacheResolver())) {
|
||||
throw new IllegalStateException("Invalid cache annotation configuration on '"
|
||||
+ ae.toString() + "'. Both 'cacheManager' and 'cacheResolver' attributes have been set. " +
|
||||
throw new IllegalStateException("Invalid cache annotation configuration on '" +
|
||||
ae.toString() + "'. Both 'cacheManager' and 'cacheResolver' attributes have been set. " +
|
||||
"These attributes are mutually exclusive: the cache manager is used to configure a" +
|
||||
"default cache resolver if none is set. If a cache resolver is set, the cache manager" +
|
||||
"won't be used.");
|
||||
}
|
||||
if (operation.getCacheNames().isEmpty()) {
|
||||
throw new IllegalStateException("No cache names could be detected on '"
|
||||
+ ae.toString() + "'. Make sure to set the value parameter on the annotation or " +
|
||||
throw new IllegalStateException("No cache names could be detected on '" +
|
||||
ae.toString() + "'. Make sure to set the value parameter on the annotation or " +
|
||||
"declare a @CacheConfig at the class-level with the default cache name(s) to use.");
|
||||
}
|
||||
}
|
||||
@@ -260,6 +257,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
* Provides default settings for a given set of cache operations.
|
||||
*/
|
||||
static class DefaultCacheConfig {
|
||||
|
||||
private final String[] cacheNames;
|
||||
|
||||
private final String keyGenerator;
|
||||
@@ -268,47 +266,41 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
|
||||
private final String cacheResolver;
|
||||
|
||||
private DefaultCacheConfig(String[] cacheNames, String keyGenerator,
|
||||
String cacheManager, String cacheResolver) {
|
||||
public DefaultCacheConfig() {
|
||||
this(null, null, null, null);
|
||||
}
|
||||
|
||||
private DefaultCacheConfig(String[] cacheNames, String keyGenerator, String cacheManager, String cacheResolver) {
|
||||
this.cacheNames = cacheNames;
|
||||
this.keyGenerator = keyGenerator;
|
||||
this.cacheManager = cacheManager;
|
||||
this.cacheResolver = cacheResolver;
|
||||
}
|
||||
|
||||
public DefaultCacheConfig() {
|
||||
this(null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the defaults to the specified {@link CacheOperation}.
|
||||
*
|
||||
* @param operation the operation to update
|
||||
*/
|
||||
public void applyDefault(CacheOperation operation) {
|
||||
if (operation.getCacheNames().isEmpty() && cacheNames != null) {
|
||||
operation.setCacheNames(cacheNames);
|
||||
if (operation.getCacheNames().isEmpty() && this.cacheNames != null) {
|
||||
operation.setCacheNames(this.cacheNames);
|
||||
}
|
||||
if (!StringUtils.hasText(operation.getKey()) && !StringUtils.hasText(operation.getKeyGenerator())
|
||||
&& StringUtils.hasText(keyGenerator)) {
|
||||
operation.setKeyGenerator(keyGenerator);
|
||||
if (!StringUtils.hasText(operation.getKey()) && !StringUtils.hasText(operation.getKeyGenerator()) &&
|
||||
StringUtils.hasText(this.keyGenerator)) {
|
||||
operation.setKeyGenerator(this.keyGenerator);
|
||||
}
|
||||
|
||||
if (isSet(operation.getCacheManager()) || isSet(operation.getCacheResolver())) {
|
||||
if (StringUtils.hasText(operation.getCacheManager()) || StringUtils.hasText(operation.getCacheResolver())) {
|
||||
// One of these is set so we should not inherit anything
|
||||
}
|
||||
else if (isSet(cacheResolver)) {
|
||||
operation.setCacheResolver(cacheResolver);
|
||||
else if (StringUtils.hasText(this.cacheResolver)) {
|
||||
operation.setCacheResolver(this.cacheResolver);
|
||||
}
|
||||
else if (isSet(cacheManager)) {
|
||||
operation.setCacheManager(cacheManager);
|
||||
else if (StringUtils.hasText(this.cacheManager)) {
|
||||
operation.setCacheManager(this.cacheManager);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSet(String s) {
|
||||
return StringUtils.hasText(s);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user