Skip to content

Commit 3d6b0ca

Browse files
committed
SpringCacheAnnotationParser properly accepts empty @caching annotation
Issue: SPR-14162
1 parent a8418d3 commit 3d6b0ca

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,10 @@ public Collection<CacheOperation> parseCacheAnnotations(AnnotatedElement ae) {
7070
if (cachings != null) {
7171
ops = lazyInit(ops);
7272
for (Caching caching : cachings) {
73-
ops.addAll(parseCachingAnnotation(ae, caching));
73+
Collection<CacheOperation> cachingOps = parseCachingAnnotation(ae, caching);
74+
if (cachingOps != null) {
75+
ops.addAll(cachingOps);
76+
}
7477
}
7578
}
7679

spring-context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTests.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.cache.annotation;
1818

19-
import static org.junit.Assert.*;
20-
2119
import java.lang.annotation.ElementType;
2220
import java.lang.annotation.Retention;
2321
import java.lang.annotation.RetentionPolicy;
@@ -27,22 +25,20 @@
2725
import java.util.Iterator;
2826

2927
import org.junit.Test;
28+
3029
import org.springframework.cache.interceptor.CacheEvictOperation;
3130
import org.springframework.cache.interceptor.CacheOperation;
3231
import org.springframework.cache.interceptor.CacheableOperation;
33-
import org.springframework.util.ReflectionUtils;
32+
33+
import static org.junit.Assert.*;
3434

3535
/**
3636
* @author Costin Leau
3737
*/
3838
public class AnnotationCacheOperationSourceTests {
3939

40-
private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource();
40+
private final AnnotationCacheOperationSource source = new AnnotationCacheOperationSource();
4141

42-
private Collection<CacheOperation> getOps(String name) {
43-
Method method = ReflectionUtils.findMethod(AnnotatedClass.class, name);
44-
return source.getCacheOperations(method, AnnotatedClass.class);
45-
}
4642

4743
@Test
4844
public void testSingularAnnotation() throws Exception {
@@ -69,6 +65,12 @@ public void testCaching() throws Exception {
6965
assertTrue(it.next() instanceof CacheEvictOperation);
7066
}
7167

68+
@Test
69+
public void testEmptyCaching() throws Exception {
70+
Collection<CacheOperation> ops = getOps("emptyCaching");
71+
assertTrue(ops.isEmpty());
72+
}
73+
7274
@Test
7375
public void testSingularStereotype() throws Exception {
7476
Collection<CacheOperation> ops = getOps("singleStereotype");
@@ -90,7 +92,15 @@ public void testMultipleStereotypes() throws Exception {
9092
assertTrue(next.getCacheNames().contains("bar"));
9193
}
9294

95+
96+
private Collection<CacheOperation> getOps(String name) throws Exception {
97+
Method method = AnnotatedClass.class.getMethod(name);
98+
return source.getCacheOperations(method, AnnotatedClass.class);
99+
}
100+
101+
93102
private static class AnnotatedClass {
103+
94104
@Cacheable("test")
95105
public void singular() {
96106
}
@@ -100,13 +110,16 @@ public void singular() {
100110
public void multiple() {
101111
}
102112

103-
@Caching(cacheable = { @Cacheable("test") }, evict = { @CacheEvict("test") })
113+
@Caching(cacheable = @Cacheable("test"), evict = @CacheEvict("test"))
104114
public void caching() {
105115
}
106116

117+
@Caching
118+
public void emptyCaching() {
119+
}
120+
107121
@EvictFoo
108122
public void singleStereotype() {
109-
110123
}
111124

112125
@EvictFoo
@@ -120,21 +133,25 @@ public void multipleCaching() {
120133
}
121134
}
122135

136+
123137
@Retention(RetentionPolicy.RUNTIME)
124138
@Target(ElementType.METHOD)
125139
@Cacheable("foo")
126140
public @interface CacheableFoo {
127141
}
128142

143+
129144
@Retention(RetentionPolicy.RUNTIME)
130145
@Target(ElementType.METHOD)
131-
@CacheEvict(value = "foo")
146+
@CacheEvict("foo")
132147
public @interface EvictFoo {
133148
}
134149

150+
135151
@Retention(RetentionPolicy.RUNTIME)
136152
@Target(ElementType.METHOD)
137-
@CacheEvict(value = "bar")
153+
@CacheEvict("bar")
138154
public @interface EvictBar {
139155
}
140-
}
156+
157+
}

0 commit comments

Comments
 (0)