Skip to content

Commit e05fb49

Browse files
committed
Polishing
1 parent 91b473f commit e05fb49

File tree

6 files changed

+41
-46
lines changed

6 files changed

+41
-46
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,54 +29,50 @@
2929
import org.springframework.util.ClassUtils;
3030

3131
/**
32-
* Abstract implementation of {@link JCacheOperationSource} that caches
33-
* attributes for methods and implements a fallback policy: 1. specific
34-
* target method; 2. declaring method.
32+
* Abstract implementation of {@link JCacheOperationSource} that caches attributes
33+
* for methods and implements a fallback policy: 1. specific target method;
34+
* 2. declaring method.
3535
*
36-
* <p>This implementation caches attributes by method after they are
37-
* first used.
36+
* <p>This implementation caches attributes by method after they are first used.
3837
*
3938
* @author Stephane Nicoll
39+
* @author Juergen Hoeller
4040
* @since 4.1
4141
* @see org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource
4242
*/
43-
public abstract class AbstractFallbackJCacheOperationSource
44-
implements JCacheOperationSource {
43+
public abstract class AbstractFallbackJCacheOperationSource implements JCacheOperationSource {
4544

4645
/**
4746
* Canonical value held in cache to indicate no caching attribute was
4847
* found for this method and we don't need to look again.
4948
*/
5049
private final static Object NULL_CACHING_ATTRIBUTE = new Object();
5150

51+
5252
protected final Log logger = LogFactory.getLog(getClass());
5353

54-
private final Map<Object, Object> cache =
55-
new ConcurrentHashMap<Object, Object>(1024);
54+
private final Map<Object, Object> cache = new ConcurrentHashMap<Object, Object>(1024);
55+
5656

5757
@Override
5858
public JCacheOperation<?> getCacheOperation(Method method, Class<?> targetClass) {
59-
// First, see if we have a cached value.
6059
Object cacheKey = new AnnotatedElementKey(method, targetClass);
6160
Object cached = this.cache.get(cacheKey);
61+
6262
if (cached != null) {
63-
if (cached == NULL_CACHING_ATTRIBUTE) {
64-
return null;
65-
}
66-
return (JCacheOperation<?>) cached;
63+
return (cached != NULL_CACHING_ATTRIBUTE ? (JCacheOperation<?>) cached : null);
6764
}
6865
else {
6966
JCacheOperation<?> operation = computeCacheOperation(method, targetClass);
70-
if (operation == null) {
71-
this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
72-
}
73-
else {
67+
if (operation != null) {
7468
if (logger.isDebugEnabled()) {
75-
logger.debug("Adding cacheable method '" + method.getName()
76-
+ "' with operation: " + operation);
69+
logger.debug("Adding cacheable method '" + method.getName() + "' with operation: " + operation);
7770
}
7871
this.cache.put(cacheKey, operation);
7972
}
73+
else {
74+
this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
75+
}
8076
return operation;
8177
}
8278
}
@@ -108,6 +104,7 @@ private JCacheOperation<?> computeCacheOperation(Method method, Class<?> targetC
108104
return null;
109105
}
110106

107+
111108
/**
112109
* Subclasses need to implement this to return the caching operation
113110
* for the given method, if any.

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,10 @@ protected KeyGenerator getDefaultKeyGenerator() {
196196

197197
/**
198198
* Only resolve the default exception cache resolver when an exception needs to be handled.
199-
* <p>
200-
* A non-JSR-107 setup requires either a {@link CacheManager} or a {@link CacheResolver}. If only
199+
* <p>A non-JSR-107 setup requires either a {@link CacheManager} or a {@link CacheResolver}. If only
201200
* the latter is specified, it is not possible to extract a default exception {@code CacheResolver}
202201
* from a custom {@code CacheResolver} implementation so we have to fallback on the {@code CacheManager}.
203-
* <p>
204-
* This gives this weird situation of a perfectly valid configuration that breaks all the sudden
202+
* <p>This gives this weird situation of a perfectly valid configuration that breaks all the sudden
205203
* because the JCache support is enabled. To avoid this we resolve the default exception {@code CacheResolver}
206204
* as late as possible to avoid such hard requirement in other cases.
207205
*/

spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheJavaConfigTests.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ protected ApplicationContext getApplicationContext() {
6161
return new AnnotationConfigApplicationContext(EnableCachingConfig.class);
6262
}
6363

64+
6465
@Test
6566
public void fullCachingConfig() throws Exception {
6667
AnnotationConfigApplicationContext context =
6768
new AnnotationConfigApplicationContext(FullCachingConfig.class);
69+
6870
DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
6971
assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator());
7072
assertSame(context.getBean("cacheResolver", CacheResolver.class),
@@ -103,14 +105,14 @@ public void bothSetOnlyResolverIsUsed() {
103105

104106
@Test
105107
public void exceptionCacheResolverLazilyRequired() {
106-
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
107-
NoExceptionCacheResolverConfig.class);
108+
ConfigurableApplicationContext context =
109+
new AnnotationConfigApplicationContext(NoExceptionCacheResolverConfig.class);
110+
108111
try {
109112
DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class);
110113
assertSame(context.getBean("cacheResolver"), cos.getCacheResolver());
111114

112115
JCacheableService<?> service = context.getBean(JCacheableService.class);
113-
114116
service.cache("id");
115117

116118
// This call requires the cache manager to be set
@@ -149,11 +151,11 @@ public Cache defaultCache() {
149151
}
150152
}
151153

154+
152155
@Configuration
153156
@EnableCaching
154157
public static class FullCachingConfig implements JCacheConfigurer {
155158

156-
157159
@Override
158160
@Bean
159161
public CacheManager cacheManager() {
@@ -185,6 +187,7 @@ public CacheResolver exceptionCacheResolver() {
185187
}
186188
}
187189

190+
188191
@Configuration
189192
@EnableCaching
190193
public static class EmptyConfigSupportConfig extends JCacheConfigurerSupport {
@@ -194,6 +197,7 @@ public CacheManager cm() {
194197
}
195198
}
196199

200+
197201
@Configuration
198202
@EnableCaching
199203
static class FullCachingConfigSupport extends JCacheConfigurerSupport {
@@ -223,6 +227,7 @@ public CacheResolver exceptionCacheResolver() {
223227
}
224228
}
225229

230+
226231
@Configuration
227232
@EnableCaching
228233
static class NoExceptionCacheResolverConfig extends JCacheConfigurerSupport {

spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera
6060
*/
6161
private final static Collection<CacheOperation> NULL_CACHING_ATTRIBUTE = Collections.emptyList();
6262

63+
6364
/**
6465
* Logger available to subclasses.
6566
* <p>As this base class is not marked Serializable, the logger will be recreated
@@ -68,13 +69,14 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera
6869
protected final Log logger = LogFactory.getLog(getClass());
6970

7071
/**
71-
* Cache of CacheOperations, keyed by {@link MethodCacheKey} (Method + target Class).
72+
* Cache of CacheOperations, keyed by {@link AnnotatedElementKey}.
7273
* <p>As this base class is not marked Serializable, the cache will be recreated
7374
* after serialization - provided that the concrete subclass is Serializable.
7475
*/
75-
final Map<Object, Collection<CacheOperation>> attributeCache =
76+
private final Map<Object, Collection<CacheOperation>> attributeCache =
7677
new ConcurrentHashMap<Object, Collection<CacheOperation>>(1024);
7778

79+
7880
/**
7981
* Determine the caching attribute for this method invocation.
8082
* <p>Defaults to the class's caching attribute if no method attribute is found.
@@ -85,30 +87,23 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera
8587
*/
8688
@Override
8789
public Collection<CacheOperation> getCacheOperations(Method method, Class<?> targetClass) {
88-
// First, see if we have a cached value.
8990
Object cacheKey = getCacheKey(method, targetClass);
9091
Collection<CacheOperation> cached = this.attributeCache.get(cacheKey);
92+
9193
if (cached != null) {
92-
if (cached == NULL_CACHING_ATTRIBUTE) {
93-
return null;
94-
}
95-
// Value will either be canonical value indicating there is no caching attribute,
96-
// or an actual caching attribute.
97-
return cached;
94+
return (cached != NULL_CACHING_ATTRIBUTE ? cached : null);
9895
}
9996
else {
100-
// We need to work it out.
10197
Collection<CacheOperation> cacheOps = computeCacheOperations(method, targetClass);
102-
// Put it in the cache.
103-
if (cacheOps == null) {
104-
this.attributeCache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
105-
}
106-
else {
98+
if (cacheOps != null) {
10799
if (logger.isDebugEnabled()) {
108100
logger.debug("Adding cacheable method '" + method.getName() + "' with attribute: " + cacheOps);
109101
}
110102
this.attributeCache.put(cacheKey, cacheOps);
111103
}
104+
else {
105+
this.attributeCache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
106+
}
112107
return cacheOps;
113108
}
114109
}
@@ -187,4 +182,5 @@ private Collection<CacheOperation> computeCacheOperations(Method method, Class<?
187182
protected boolean allowPublicMethodsOnly() {
188183
return false;
189184
}
185+
190186
}

spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public AnnotationAttributes getAnnotationAttributes(String annotationName) {
122122
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
123123
AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(
124124
this.attributesMap, this.metaAnnotationMap, annotationName);
125-
return (AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString));
125+
return AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString);
126126
}
127127

128128
@Override

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/UndertowXhrTransport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public UndertowXhrTransport() throws IOException {
105105

106106
public UndertowXhrTransport(OptionMap optionMap) throws IOException {
107107
Assert.notNull(optionMap, "OptionMap is required");
108-
this.httpClient = UndertowClient.getInstance();
109108
this.optionMap = optionMap;
109+
this.httpClient = UndertowClient.getInstance();
110110
this.worker = Xnio.getInstance().createWorker(optionMap);
111111
this.bufferPool = new ByteBufferSlicePool(1048, 1048);
112112
}
@@ -370,7 +370,6 @@ private class SockJsResponseListener implements ChannelListener<StreamSourceChan
370370

371371
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
372372

373-
374373
public SockJsResponseListener(TransportRequest request, ClientConnection connection, URI url,
375374
HttpHeaders headers, XhrClientSockJsSession sockJsSession,
376375
SettableListenableFuture<WebSocketSession> connectFuture) {

0 commit comments

Comments
 (0)