Skip to content

Commit 42dbc39

Browse files
committed
Polishing
(cherry picked from commit 8e571de)
1 parent 1d45e32 commit 42dbc39

File tree

6 files changed

+65
-67
lines changed

6 files changed

+65
-67
lines changed
Lines changed: 17 additions & 18 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-2018 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.
@@ -24,41 +24,40 @@
2424

2525
/**
2626
* Strategy interface for parsing known caching annotation types.
27-
* {@link AnnotationCacheOperationSource} delegates to such
28-
* parsers for supporting specific annotation types such as Spring's own
29-
* {@link Cacheable}, {@link CachePut} or {@link CacheEvict}.
27+
* {@link AnnotationCacheOperationSource} delegates to such parsers
28+
* for supporting specific annotation types such as Spring's own
29+
* {@link Cacheable}, {@link CachePut} and{@link CacheEvict}.
3030
*
3131
* @author Costin Leau
3232
* @author Stephane Nicoll
3333
* @since 3.1
34+
* @see AnnotationCacheOperationSource
35+
* @see SpringCacheAnnotationParser
3436
*/
3537
public interface CacheAnnotationParser {
3638

3739
/**
38-
* Parses the cache definition for the given class,
39-
* based on a known annotation type.
40-
* <p>This essentially parses a known cache annotation into Spring's
41-
* metadata attribute class. Returns {@code null} if the class
42-
* is not cacheable.
40+
* Parse the cache definition for the given class,
41+
* based on an annotation type understood by this parser.
42+
* <p>This essentially parses a known cache annotation into Spring's metadata
43+
* attribute class. Returns {@code null} if the class is not cacheable.
4344
* @param type the annotated class
44-
* @return CacheOperation the configured caching operation,
45-
* or {@code null} if none was found
45+
* @return the configured caching operation, or {@code null} if none found
4646
* @see AnnotationCacheOperationSource#findCacheOperations(Class)
4747
*/
4848
@Nullable
4949
Collection<CacheOperation> parseCacheAnnotations(Class<?> type);
5050

5151
/**
52-
* Parses the cache definition for the given method,
53-
* based on a known annotation type.
54-
* <p>This essentially parses a known cache annotation into Spring's
55-
* metadata attribute class. Returns {@code null} if the method
56-
* is not cacheable.
52+
* Parse the cache definition for the given method,
53+
* based on an annotation type understood by this parser.
54+
* <p>This essentially parses a known cache annotation into Spring's metadata
55+
* attribute class. Returns {@code null} if the method is not cacheable.
5756
* @param method the annotated method
58-
* @return CacheOperation the configured caching operation,
59-
* or {@code null} if none was found
57+
* @return the configured caching operation, or {@code null} if none found
6058
* @see AnnotationCacheOperationSource#findCacheOperations(Method)
6159
*/
6260
@Nullable
6361
Collection<CacheOperation> parseCacheAnnotations(Method method);
62+
6463
}

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,22 @@ private Collection<CacheOperation> computeCacheOperations(Method method, @Nullab
163163

164164

165165
/**
166-
* Subclasses need to implement this to return the caching attribute
167-
* for the given method, if any.
168-
* @param method the method to retrieve the attribute for
169-
* @return all caching attribute associated with this method
170-
* (or {@code null} if none)
166+
* Subclasses need to implement this to return the caching attribute for the
167+
* given class, if any.
168+
* @param clazz the class to retrieve the attribute for
169+
* @return all caching attribute associated with this class, or {@code null} if none
171170
*/
172171
@Nullable
173-
protected abstract Collection<CacheOperation> findCacheOperations(Method method);
172+
protected abstract Collection<CacheOperation> findCacheOperations(Class<?> clazz);
174173

175174
/**
176-
* Subclasses need to implement this to return the caching attribute
177-
* for the given class, if any.
178-
* @param clazz the class to retrieve the attribute for
179-
* @return all caching attribute associated with this class
180-
* (or {@code null} if none)
175+
* Subclasses need to implement this to return the caching attribute for the
176+
* given method, if any.
177+
* @param method the method to retrieve the attribute for
178+
* @return all caching attribute associated with this method, or {@code null} if none
181179
*/
182180
@Nullable
183-
protected abstract Collection<CacheOperation> findCacheOperations(Class<?> clazz);
181+
protected abstract Collection<CacheOperation> findCacheOperations(Method method);
184182

185183
/**
186184
* Should only public methods be allowed to have caching semantics?

spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -96,7 +96,6 @@ public void properExceptionForExistingProxyDependencyMismatch() {
9696
fail("Should have thrown UnsatisfiedDependencyException");
9797
}
9898
catch (UnsatisfiedDependencyException ex) {
99-
ex.printStackTrace();
10099
assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException);
101100
}
102101
}
@@ -111,7 +110,6 @@ public void properExceptionForResolvedProxyDependencyMismatch() {
111110
fail("Should have thrown UnsatisfiedDependencyException");
112111
}
113112
catch (UnsatisfiedDependencyException ex) {
114-
ex.printStackTrace();
115113
assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException);
116114
}
117115
}
@@ -218,8 +216,8 @@ public void customExecutorConfig() throws InterruptedException {
218216
ctx.close();
219217
}
220218

221-
@Test
222-
public void spr14949FindsOnInterfaceWithInterfaceProxy() throws InterruptedException {
219+
@Test // SPR-14949
220+
public void findOnInterfaceWithInterfaceProxy() throws InterruptedException {
223221
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigA.class);
224222

225223
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);
@@ -230,8 +228,8 @@ public void spr14949FindsOnInterfaceWithInterfaceProxy() throws InterruptedExcep
230228
ctx.close();
231229
}
232230

233-
@Test
234-
public void spr14949FindsOnInterfaceWithCglibProxy() throws InterruptedException {
231+
@Test // SPR-14949
232+
public void findOnInterfaceWithCglibProxy() throws InterruptedException {
235233
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class);
236234

237235
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);

spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ public AnnotationTransactionAttributeSource(Set<TransactionAnnotationParser> ann
135135

136136
@Override
137137
@Nullable
138-
protected TransactionAttribute findTransactionAttribute(Method method) {
139-
return determineTransactionAttribute(method);
138+
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
139+
return determineTransactionAttribute(clazz);
140140
}
141141

142142
@Override
143143
@Nullable
144-
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
145-
return determineTransactionAttribute(clazz);
144+
protected TransactionAttribute findTransactionAttribute(Method method) {
145+
return determineTransactionAttribute(method);
146146
}
147147

148148
/**

spring-tx/src/main/java/org/springframework/transaction/interceptor/AbstractFallbackTransactionAttributeSource.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
5555
* Canonical value held in cache to indicate no transaction attribute was
5656
* found for this method, and we don't need to look again.
5757
*/
58-
private static final TransactionAttribute NULL_TRANSACTION_ATTRIBUTE = new DefaultTransactionAttribute();
58+
@SuppressWarnings("serial")
59+
private static final TransactionAttribute NULL_TRANSACTION_ATTRIBUTE = new DefaultTransactionAttribute() {
60+
@Override
61+
public String toString() {
62+
return "null";
63+
}
64+
};
5965

6066

6167
/**
@@ -78,7 +84,7 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
7884
* <p>Defaults to the class's transaction attribute if no method attribute is found.
7985
* @param method the method for the current invocation (never {@code null})
8086
* @param targetClass the target class for this invocation (may be {@code null})
81-
* @return TransactionAttribute for this method, or {@code null} if the method
87+
* @return a TransactionAttribute for this method, or {@code null} if the method
8288
* is not transactional
8389
*/
8490
@Override
@@ -90,15 +96,15 @@ public TransactionAttribute getTransactionAttribute(Method method, @Nullable Cla
9096

9197
// First, see if we have a cached value.
9298
Object cacheKey = getCacheKey(method, targetClass);
93-
Object cached = this.attributeCache.get(cacheKey);
99+
TransactionAttribute cached = this.attributeCache.get(cacheKey);
94100
if (cached != null) {
95101
// Value will either be canonical value indicating there is no transaction attribute,
96102
// or an actual transaction attribute.
97103
if (cached == NULL_TRANSACTION_ATTRIBUTE) {
98104
return null;
99105
}
100106
else {
101-
return (TransactionAttribute) cached;
107+
return cached;
102108
}
103109
}
104110
else {
@@ -182,25 +188,22 @@ protected TransactionAttribute computeTransactionAttribute(Method method, @Nulla
182188

183189

184190
/**
185-
* Subclasses need to implement this to return the transaction attribute
186-
* for the given method, if any.
187-
* @param method the method to retrieve the attribute for
188-
* @return all transaction attribute associated with this method
189-
* (or {@code null} if none)
191+
* Subclasses need to implement this to return the transaction attribute for the
192+
* given class, if any.
193+
* @param clazz the class to retrieve the attribute for
194+
* @return all transaction attribute associated with this class, or {@code null} if none
190195
*/
191196
@Nullable
192-
protected abstract TransactionAttribute findTransactionAttribute(Method method);
197+
protected abstract TransactionAttribute findTransactionAttribute(Class<?> clazz);
193198

194199
/**
195-
* Subclasses need to implement this to return the transaction attribute
196-
* for the given class, if any.
197-
* @param clazz the class to retrieve the attribute for
198-
* @return all transaction attribute associated with this class
199-
* (or {@code null} if none)
200+
* Subclasses need to implement this to return the transaction attribute for the
201+
* given method, if any.
202+
* @param method the method to retrieve the attribute for
203+
* @return all transaction attribute associated with this method, or {@code null} if none
200204
*/
201205
@Nullable
202-
protected abstract TransactionAttribute findTransactionAttribute(Class<?> clazz);
203-
206+
protected abstract TransactionAttribute findTransactionAttribute(Method method);
204207

205208
/**
206209
* Should only public methods be allowed to have transactional semantics?

spring-tx/src/test/java/org/springframework/transaction/interceptor/MapTransactionAttributeSource.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -31,23 +31,23 @@ public class MapTransactionAttributeSource extends AbstractFallbackTransactionAt
3131
private final Map<Object, TransactionAttribute> attributeMap = new HashMap<>();
3232

3333

34-
public void register(Method method, TransactionAttribute txAttr) {
35-
this.attributeMap.put(method, txAttr);
36-
}
37-
3834
public void register(Class<?> clazz, TransactionAttribute txAttr) {
3935
this.attributeMap.put(clazz, txAttr);
4036
}
4137

42-
43-
@Override
44-
protected TransactionAttribute findTransactionAttribute(Method method) {
45-
return this.attributeMap.get(method);
38+
public void register(Method method, TransactionAttribute txAttr) {
39+
this.attributeMap.put(method, txAttr);
4640
}
4741

42+
4843
@Override
4944
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
5045
return this.attributeMap.get(clazz);
5146
}
5247

48+
@Override
49+
protected TransactionAttribute findTransactionAttribute(Method method) {
50+
return this.attributeMap.get(method);
51+
}
52+
5353
}

0 commit comments

Comments
 (0)