SPR-8082
+ support generic discovery of multiple annotations of the same type (such as stereotypes)
This commit is contained in:
parent
5fe0804017
commit
735ba9dcde
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.cache.annotation;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
@ -25,7 +26,6 @@ import org.springframework.cache.interceptor.CacheEvictOperation;
|
|||
import org.springframework.cache.interceptor.CacheOperation;
|
||||
import org.springframework.cache.interceptor.CachePutOperation;
|
||||
import org.springframework.cache.interceptor.CacheableOperation;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
|
@ -43,31 +43,39 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
|||
public Collection<CacheOperation> parseCacheAnnotations(AnnotatedElement ae) {
|
||||
Collection<CacheOperation> ops = null;
|
||||
|
||||
Cacheable cache = AnnotationUtils.getAnnotation(ae, Cacheable.class);
|
||||
if (cache != null) {
|
||||
Collection<Cacheable> cacheables = getAnnotations(ae, Cacheable.class);
|
||||
if (cacheables != null) {
|
||||
ops = lazyInit(ops);
|
||||
ops.add(parseCacheableAnnotation(ae, cache));
|
||||
for (Cacheable cacheable : cacheables) {
|
||||
ops.add(parseCacheableAnnotation(ae, cacheable));
|
||||
}
|
||||
}
|
||||
CacheEvict evict = AnnotationUtils.getAnnotation(ae, CacheEvict.class);
|
||||
if (evict != null) {
|
||||
Collection<CacheEvict> evicts = getAnnotations(ae, CacheEvict.class);
|
||||
if (evicts != null) {
|
||||
ops = lazyInit(ops);
|
||||
ops.add(parseEvictAnnotation(ae, evict));
|
||||
for (CacheEvict e : evicts) {
|
||||
ops.add(parseEvictAnnotation(ae, e));
|
||||
}
|
||||
}
|
||||
CachePut update = AnnotationUtils.getAnnotation(ae, CachePut.class);
|
||||
if (update != null) {
|
||||
Collection<CachePut> updates = getAnnotations(ae, CachePut.class);
|
||||
if (updates != null) {
|
||||
ops = lazyInit(ops);
|
||||
ops.add(parseUpdateAnnotation(ae, update));
|
||||
for (CachePut p : updates) {
|
||||
ops.add(parseUpdateAnnotation(ae, p));
|
||||
}
|
||||
}
|
||||
Caching caching = AnnotationUtils.getAnnotation(ae, Caching.class);
|
||||
Collection<Caching> caching = getAnnotations(ae, Caching.class);
|
||||
if (caching != null) {
|
||||
ops = lazyInit(ops);
|
||||
ops.addAll(parseCachingAnnotation(ae, caching));
|
||||
for (Caching c : caching) {
|
||||
ops.addAll(parseCachingAnnotation(ae, c));
|
||||
}
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
private Collection<CacheOperation> lazyInit(Collection<CacheOperation> ops) {
|
||||
return (ops != null ? ops : new ArrayList<CacheOperation>(2));
|
||||
private <T extends Annotation> Collection<CacheOperation> lazyInit(Collection<CacheOperation> ops) {
|
||||
return (ops != null ? ops : new ArrayList<CacheOperation>(1));
|
||||
}
|
||||
|
||||
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, Cacheable caching) {
|
||||
|
@ -126,4 +134,24 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
|||
|
||||
return ops;
|
||||
}
|
||||
}
|
||||
|
||||
private static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement ae, Class<T> annotationType) {
|
||||
Collection<T> anns = new ArrayList<T>(2);
|
||||
|
||||
// look at raw annotation
|
||||
T ann = ae.getAnnotation(annotationType);
|
||||
if (ann != null) {
|
||||
anns.add(ann);
|
||||
}
|
||||
|
||||
// scan meta-annotations
|
||||
for (Annotation metaAnn : ae.getAnnotations()) {
|
||||
ann = metaAnn.annotationType().getAnnotation(annotationType);
|
||||
if (ann != null) {
|
||||
anns.add(ann);
|
||||
}
|
||||
}
|
||||
|
||||
return anns;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* Copyright 2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.annotation;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cache.interceptor.CacheEvictOperation;
|
||||
import org.springframework.cache.interceptor.CacheOperation;
|
||||
import org.springframework.cache.interceptor.CacheableOperation;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class AnnotationCacheOperationSourceTest {
|
||||
|
||||
private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource();
|
||||
|
||||
private Collection<CacheOperation> getOps(String name) {
|
||||
Method method = ReflectionUtils.findMethod(AnnotatedClass.class, name);
|
||||
return source.getCacheOperations(method, AnnotatedClass.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingularAnnotation() throws Exception {
|
||||
Collection<CacheOperation> ops = getOps("singular");
|
||||
assertEquals(1, ops.size());
|
||||
assertTrue(ops.iterator().next() instanceof CacheableOperation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleAnnotation() throws Exception {
|
||||
Collection<CacheOperation> ops = getOps("multiple");
|
||||
assertEquals(2, ops.size());
|
||||
Iterator<CacheOperation> it = ops.iterator();
|
||||
assertTrue(it.next() instanceof CacheableOperation);
|
||||
assertTrue(it.next() instanceof CacheEvictOperation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCaching() throws Exception {
|
||||
Collection<CacheOperation> ops = getOps("caching");
|
||||
assertEquals(2, ops.size());
|
||||
Iterator<CacheOperation> it = ops.iterator();
|
||||
assertTrue(it.next() instanceof CacheableOperation);
|
||||
assertTrue(it.next() instanceof CacheEvictOperation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingularStereotype() throws Exception {
|
||||
Collection<CacheOperation> ops = getOps("singleStereotype");
|
||||
assertEquals(1, ops.size());
|
||||
assertTrue(ops.iterator().next() instanceof CacheEvictOperation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleStereotypes() throws Exception {
|
||||
Collection<CacheOperation> ops = getOps("multipleStereotype");
|
||||
assertEquals(3, ops.size());
|
||||
Iterator<CacheOperation> it = ops.iterator();
|
||||
assertTrue(it.next() instanceof CacheableOperation);
|
||||
CacheOperation next = it.next();
|
||||
assertTrue(next instanceof CacheEvictOperation);
|
||||
assertTrue(next.getCacheNames().contains("foo"));
|
||||
next = it.next();
|
||||
assertTrue(next instanceof CacheEvictOperation);
|
||||
assertTrue(next.getCacheNames().contains("bar"));
|
||||
}
|
||||
|
||||
private static class AnnotatedClass {
|
||||
@Cacheable("test")
|
||||
public void singular() {
|
||||
}
|
||||
|
||||
@CacheEvict("test")
|
||||
@Cacheable("test")
|
||||
public void multiple() {
|
||||
}
|
||||
|
||||
@Caching(cacheable = { @Cacheable("test") }, evict = { @CacheEvict("test") })
|
||||
public void caching() {
|
||||
}
|
||||
|
||||
@EvictFoo
|
||||
public void singleStereotype() {
|
||||
|
||||
}
|
||||
|
||||
@EvictFoo
|
||||
@CacheableFoo
|
||||
@EvictBar
|
||||
public void multipleStereotype() {
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Cacheable("foo")
|
||||
public @interface CacheableFoo {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@CacheEvict(value = "foo")
|
||||
public @interface EvictFoo {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@CacheEvict(value = "bar")
|
||||
public @interface EvictBar {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue