43
43
*/
44
44
public abstract class AnnotationJCacheOperationSource extends AbstractFallbackJCacheOperationSource {
45
45
46
- /**
47
- * Locate or create an instance of the specified {@code type}.
48
- * @param type the type of the bean to manage
49
- * @return the required bean
50
- */
51
- protected abstract <T > T getBean (Class <T > type );
52
-
53
- /**
54
- * Return the default {@link CacheResolver} if none is set.
55
- */
56
- protected abstract CacheResolver getDefaultCacheResolver ();
57
-
58
- /**
59
- * Return the default exception {@link CacheResolver} if none is set.
60
- */
61
- protected abstract CacheResolver getDefaultExceptionCacheResolver ();
62
-
63
- /**
64
- * Return the default {@link KeyGenerator} if none is set.
65
- */
66
- protected abstract KeyGenerator getDefaultKeyGenerator ();
67
-
68
-
69
46
@ Override
70
47
protected JCacheOperation <?> findCacheOperation (Method method , Class <?> targetType ) {
71
48
CacheResult cacheResult = method .getAnnotation (CacheResult .class );
72
49
CachePut cachePut = method .getAnnotation (CachePut .class );
73
50
CacheRemove cacheRemove = method .getAnnotation (CacheRemove .class );
74
51
CacheRemoveAll cacheRemoveAll = method .getAnnotation (CacheRemoveAll .class );
52
+
75
53
int found = countNonNull (cacheResult , cachePut , cacheRemove , cacheRemoveAll );
76
54
if (found == 0 ) {
77
55
return null ;
78
56
}
79
57
if (found > 1 ) {
80
58
throw new IllegalStateException ("More than one cache annotation found on '" + method + "'" );
81
59
}
82
- CacheDefaults defaults = getCacheDefaults (method , targetType );
83
60
61
+ CacheDefaults defaults = getCacheDefaults (method , targetType );
84
62
if (cacheResult != null ) {
85
63
return createCacheResultOperation (method , defaults , cacheResult );
86
64
}
@@ -103,9 +81,7 @@ protected CacheDefaults getCacheDefaults(Method method, Class<?> targetType) {
103
81
return targetType .getAnnotation (CacheDefaults .class );
104
82
}
105
83
106
-
107
- protected CacheResultOperation createCacheResultOperation (Method method , CacheDefaults defaults ,
108
- CacheResult ann ) {
84
+ protected CacheResultOperation createCacheResultOperation (Method method , CacheDefaults defaults , CacheResult ann ) {
109
85
String cacheName = determineCacheName (method , defaults , ann .cacheName ());
110
86
CacheResolverFactory cacheResolverFactory =
111
87
determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
@@ -123,49 +99,39 @@ protected CacheResultOperation createCacheResultOperation(Method method, CacheDe
123
99
return new CacheResultOperation (methodDetails , cacheResolver , keyGenerator , exceptionCacheResolver );
124
100
}
125
101
126
- protected CachePutOperation createCachePutOperation (Method method , CacheDefaults defaults ,
127
- CachePut ann ) {
102
+ protected CachePutOperation createCachePutOperation (Method method , CacheDefaults defaults , CachePut ann ) {
128
103
String cacheName = determineCacheName (method , defaults , ann .cacheName ());
129
104
CacheResolverFactory cacheResolverFactory =
130
105
determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
131
106
KeyGenerator keyGenerator = determineKeyGenerator (defaults , ann .cacheKeyGenerator ());
132
107
133
108
CacheMethodDetails <CachePut > methodDetails = createMethodDetails (method , ann , cacheName );
134
-
135
109
CacheResolver cacheResolver = getCacheResolver (cacheResolverFactory , methodDetails );
136
-
137
110
return new CachePutOperation (methodDetails , cacheResolver , keyGenerator );
138
111
}
139
112
140
- protected CacheRemoveOperation createCacheRemoveOperation (Method method , CacheDefaults defaults ,
141
- CacheRemove ann ) {
113
+ protected CacheRemoveOperation createCacheRemoveOperation (Method method , CacheDefaults defaults , CacheRemove ann ) {
142
114
String cacheName = determineCacheName (method , defaults , ann .cacheName ());
143
115
CacheResolverFactory cacheResolverFactory =
144
116
determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
145
117
KeyGenerator keyGenerator = determineKeyGenerator (defaults , ann .cacheKeyGenerator ());
146
118
147
119
CacheMethodDetails <CacheRemove > methodDetails = createMethodDetails (method , ann , cacheName );
148
-
149
120
CacheResolver cacheResolver = getCacheResolver (cacheResolverFactory , methodDetails );
150
-
151
121
return new CacheRemoveOperation (methodDetails , cacheResolver , keyGenerator );
152
122
}
153
123
154
- protected CacheRemoveAllOperation createCacheRemoveAllOperation (Method method , CacheDefaults defaults ,
155
- CacheRemoveAll ann ) {
124
+ protected CacheRemoveAllOperation createCacheRemoveAllOperation (Method method , CacheDefaults defaults , CacheRemoveAll ann ) {
156
125
String cacheName = determineCacheName (method , defaults , ann .cacheName ());
157
126
CacheResolverFactory cacheResolverFactory =
158
127
determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
159
128
160
129
CacheMethodDetails <CacheRemoveAll > methodDetails = createMethodDetails (method , ann , cacheName );
161
-
162
130
CacheResolver cacheResolver = getCacheResolver (cacheResolverFactory , methodDetails );
163
-
164
131
return new CacheRemoveAllOperation (methodDetails , cacheResolver );
165
132
}
166
133
167
- private <A extends Annotation > CacheMethodDetails <A > createMethodDetails (
168
- Method method , A annotation , String cacheName ) {
134
+ private <A extends Annotation > CacheMethodDetails <A > createMethodDetails (Method method , A annotation , String cacheName ) {
169
135
return new DefaultCacheMethodDetails <A >(method , annotation , cacheName );
170
136
}
171
137
@@ -181,6 +147,7 @@ protected CacheResolver getCacheResolver(CacheResolverFactory factory, CacheMeth
181
147
182
148
protected CacheResolver getExceptionCacheResolver (CacheResolverFactory factory ,
183
149
CacheMethodDetails <CacheResult > details ) {
150
+
184
151
if (factory != null ) {
185
152
javax .cache .annotation .CacheResolver cacheResolver = factory .getExceptionCacheResolver (details );
186
153
return new CacheResolverAdapter (cacheResolver );
@@ -192,6 +159,7 @@ protected CacheResolver getExceptionCacheResolver(CacheResolverFactory factory,
192
159
193
160
protected CacheResolverFactory determineCacheResolverFactory (CacheDefaults defaults ,
194
161
Class <? extends CacheResolverFactory > candidate ) {
162
+
195
163
if (!CacheResolverFactory .class .equals (candidate )) {
196
164
return getBean (candidate );
197
165
}
@@ -203,8 +171,7 @@ else if (defaults != null && !CacheResolverFactory.class.equals(defaults.cacheRe
203
171
}
204
172
}
205
173
206
- protected KeyGenerator determineKeyGenerator (CacheDefaults defaults ,
207
- Class <? extends CacheKeyGenerator > candidate ) {
174
+ protected KeyGenerator determineKeyGenerator (CacheDefaults defaults , Class <? extends CacheKeyGenerator > candidate ) {
208
175
if (!CacheKeyGenerator .class .equals (candidate )) {
209
176
return new KeyGeneratorAdapter (this , getBean (candidate ));
210
177
}
@@ -233,28 +200,48 @@ protected String determineCacheName(Method method, CacheDefaults defaults, Strin
233
200
*/
234
201
protected String generateDefaultCacheName (Method method ) {
235
202
Class <?>[] parameterTypes = method .getParameterTypes ();
236
- List <String > parameters = new ArrayList <String >();
203
+ List <String > parameters = new ArrayList <String >(parameterTypes . length );
237
204
for (Class <?> parameterType : parameterTypes ) {
238
205
parameters .add (parameterType .getName ());
239
206
}
240
207
241
- StringBuilder sb = new StringBuilder ();
242
- sb .append (method .getDeclaringClass ().getName ())
243
- .append ("." )
244
- .append (method .getName ())
245
- .append ("(" )
246
- .append (StringUtils .collectionToCommaDelimitedString (parameters ))
247
- .append (")" );
208
+ StringBuilder sb = new StringBuilder (method .getDeclaringClass ().getName ());
209
+ sb .append ("." ).append (method .getName ());
210
+ sb .append ("(" ).append (StringUtils .collectionToCommaDelimitedString (parameters )).append (")" );
248
211
return sb .toString ();
249
212
}
250
213
251
214
private int countNonNull (Object ... instances ) {
252
215
int result = 0 ;
253
- for (Object o : instances ) {
254
- if (o != null ) {
216
+ for (Object instance : instances ) {
217
+ if (instance != null ) {
255
218
result += 1 ;
256
219
}
257
220
}
258
221
return result ;
259
222
}
223
+
224
+
225
+ /**
226
+ * Locate or create an instance of the specified cache strategy {@code type}.
227
+ * @param type the type of the bean to manage
228
+ * @return the required bean
229
+ */
230
+ protected abstract <T > T getBean (Class <T > type );
231
+
232
+ /**
233
+ * Return the default {@link CacheResolver} if none is set.
234
+ */
235
+ protected abstract CacheResolver getDefaultCacheResolver ();
236
+
237
+ /**
238
+ * Return the default exception {@link CacheResolver} if none is set.
239
+ */
240
+ protected abstract CacheResolver getDefaultExceptionCacheResolver ();
241
+
242
+ /**
243
+ * Return the default {@link KeyGenerator} if none is set.
244
+ */
245
+ protected abstract KeyGenerator getDefaultKeyGenerator ();
246
+
260
247
}
0 commit comments