4343 */
4444public abstract class AnnotationJCacheOperationSource extends AbstractFallbackJCacheOperationSource {
4545
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-
6946 @ Override
7047 protected JCacheOperation <?> findCacheOperation (Method method , Class <?> targetType ) {
7148 CacheResult cacheResult = method .getAnnotation (CacheResult .class );
7249 CachePut cachePut = method .getAnnotation (CachePut .class );
7350 CacheRemove cacheRemove = method .getAnnotation (CacheRemove .class );
7451 CacheRemoveAll cacheRemoveAll = method .getAnnotation (CacheRemoveAll .class );
52+
7553 int found = countNonNull (cacheResult , cachePut , cacheRemove , cacheRemoveAll );
7654 if (found == 0 ) {
7755 return null ;
7856 }
7957 if (found > 1 ) {
8058 throw new IllegalStateException ("More than one cache annotation found on '" + method + "'" );
8159 }
82- CacheDefaults defaults = getCacheDefaults (method , targetType );
8360
61+ CacheDefaults defaults = getCacheDefaults (method , targetType );
8462 if (cacheResult != null ) {
8563 return createCacheResultOperation (method , defaults , cacheResult );
8664 }
@@ -103,9 +81,7 @@ protected CacheDefaults getCacheDefaults(Method method, Class<?> targetType) {
10381 return targetType .getAnnotation (CacheDefaults .class );
10482 }
10583
106-
107- protected CacheResultOperation createCacheResultOperation (Method method , CacheDefaults defaults ,
108- CacheResult ann ) {
84+ protected CacheResultOperation createCacheResultOperation (Method method , CacheDefaults defaults , CacheResult ann ) {
10985 String cacheName = determineCacheName (method , defaults , ann .cacheName ());
11086 CacheResolverFactory cacheResolverFactory =
11187 determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
@@ -123,49 +99,39 @@ protected CacheResultOperation createCacheResultOperation(Method method, CacheDe
12399 return new CacheResultOperation (methodDetails , cacheResolver , keyGenerator , exceptionCacheResolver );
124100 }
125101
126- protected CachePutOperation createCachePutOperation (Method method , CacheDefaults defaults ,
127- CachePut ann ) {
102+ protected CachePutOperation createCachePutOperation (Method method , CacheDefaults defaults , CachePut ann ) {
128103 String cacheName = determineCacheName (method , defaults , ann .cacheName ());
129104 CacheResolverFactory cacheResolverFactory =
130105 determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
131106 KeyGenerator keyGenerator = determineKeyGenerator (defaults , ann .cacheKeyGenerator ());
132107
133108 CacheMethodDetails <CachePut > methodDetails = createMethodDetails (method , ann , cacheName );
134-
135109 CacheResolver cacheResolver = getCacheResolver (cacheResolverFactory , methodDetails );
136-
137110 return new CachePutOperation (methodDetails , cacheResolver , keyGenerator );
138111 }
139112
140- protected CacheRemoveOperation createCacheRemoveOperation (Method method , CacheDefaults defaults ,
141- CacheRemove ann ) {
113+ protected CacheRemoveOperation createCacheRemoveOperation (Method method , CacheDefaults defaults , CacheRemove ann ) {
142114 String cacheName = determineCacheName (method , defaults , ann .cacheName ());
143115 CacheResolverFactory cacheResolverFactory =
144116 determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
145117 KeyGenerator keyGenerator = determineKeyGenerator (defaults , ann .cacheKeyGenerator ());
146118
147119 CacheMethodDetails <CacheRemove > methodDetails = createMethodDetails (method , ann , cacheName );
148-
149120 CacheResolver cacheResolver = getCacheResolver (cacheResolverFactory , methodDetails );
150-
151121 return new CacheRemoveOperation (methodDetails , cacheResolver , keyGenerator );
152122 }
153123
154- protected CacheRemoveAllOperation createCacheRemoveAllOperation (Method method , CacheDefaults defaults ,
155- CacheRemoveAll ann ) {
124+ protected CacheRemoveAllOperation createCacheRemoveAllOperation (Method method , CacheDefaults defaults , CacheRemoveAll ann ) {
156125 String cacheName = determineCacheName (method , defaults , ann .cacheName ());
157126 CacheResolverFactory cacheResolverFactory =
158127 determineCacheResolverFactory (defaults , ann .cacheResolverFactory ());
159128
160129 CacheMethodDetails <CacheRemoveAll > methodDetails = createMethodDetails (method , ann , cacheName );
161-
162130 CacheResolver cacheResolver = getCacheResolver (cacheResolverFactory , methodDetails );
163-
164131 return new CacheRemoveAllOperation (methodDetails , cacheResolver );
165132 }
166133
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 ) {
169135 return new DefaultCacheMethodDetails <A >(method , annotation , cacheName );
170136 }
171137
@@ -181,6 +147,7 @@ protected CacheResolver getCacheResolver(CacheResolverFactory factory, CacheMeth
181147
182148 protected CacheResolver getExceptionCacheResolver (CacheResolverFactory factory ,
183149 CacheMethodDetails <CacheResult > details ) {
150+
184151 if (factory != null ) {
185152 javax .cache .annotation .CacheResolver cacheResolver = factory .getExceptionCacheResolver (details );
186153 return new CacheResolverAdapter (cacheResolver );
@@ -192,6 +159,7 @@ protected CacheResolver getExceptionCacheResolver(CacheResolverFactory factory,
192159
193160 protected CacheResolverFactory determineCacheResolverFactory (CacheDefaults defaults ,
194161 Class <? extends CacheResolverFactory > candidate ) {
162+
195163 if (!CacheResolverFactory .class .equals (candidate )) {
196164 return getBean (candidate );
197165 }
@@ -203,8 +171,7 @@ else if (defaults != null && !CacheResolverFactory.class.equals(defaults.cacheRe
203171 }
204172 }
205173
206- protected KeyGenerator determineKeyGenerator (CacheDefaults defaults ,
207- Class <? extends CacheKeyGenerator > candidate ) {
174+ protected KeyGenerator determineKeyGenerator (CacheDefaults defaults , Class <? extends CacheKeyGenerator > candidate ) {
208175 if (!CacheKeyGenerator .class .equals (candidate )) {
209176 return new KeyGeneratorAdapter (this , getBean (candidate ));
210177 }
@@ -233,28 +200,48 @@ protected String determineCacheName(Method method, CacheDefaults defaults, Strin
233200 */
234201 protected String generateDefaultCacheName (Method method ) {
235202 Class <?>[] parameterTypes = method .getParameterTypes ();
236- List <String > parameters = new ArrayList <String >();
203+ List <String > parameters = new ArrayList <String >(parameterTypes . length );
237204 for (Class <?> parameterType : parameterTypes ) {
238205 parameters .add (parameterType .getName ());
239206 }
240207
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 (")" );
248211 return sb .toString ();
249212 }
250213
251214 private int countNonNull (Object ... instances ) {
252215 int result = 0 ;
253- for (Object o : instances ) {
254- if (o != null ) {
216+ for (Object instance : instances ) {
217+ if (instance != null ) {
255218 result += 1 ;
256219 }
257220 }
258221 return result ;
259222 }
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+
260247}
0 commit comments