@@ -125,12 +125,24 @@ public static boolean isMagic(Cache cache, Method method) throws Exception {
125125 private Collection <Object > newCollection (Class <?> collectionType , int resSize ) throws Exception {
126126 Collection <Object > res ;
127127 if (LinkedList .class .isAssignableFrom (collectionType )) {
128+ if (resSize == 0 ) {
129+ return Collections .emptyList ();
130+ }
128131 res = new LinkedList <>();
129132 } else if (List .class .isAssignableFrom (collectionType )) {
133+ if (resSize == 0 ) {
134+ return Collections .emptyList ();
135+ }
130136 res = new ArrayList <>(resSize );
131137 } else if (LinkedHashSet .class .isAssignableFrom (collectionType )) {
138+ if (resSize == 0 ) {
139+ return Collections .emptySet ();
140+ }
132141 res = new LinkedHashSet <>(resSize );
133142 } else if (Set .class .isAssignableFrom (collectionType )) {
143+ if (resSize == 0 ) {
144+ return Collections .emptySet ();
145+ }
134146 res = new HashSet <>(resSize );
135147 } else {
136148 throw new Exception ("Unsupported type:" + collectionType .getName ());
@@ -154,6 +166,12 @@ public Object magic() throws Throwable {
154166 return newValue ;
155167 }
156168 Map <CacheKeyTO , Object > keyArgMap = getCacheKeyForMagic ();
169+ if (null == keyArgMap || keyArgMap .isEmpty ()) {
170+ if (returnType .isArray ()) {
171+ return Array .newInstance (returnType .getComponentType (), 0 );
172+ }
173+ return newCollection (returnType , 0 );
174+ }
157175 Type returnItemType = getRealReturnType ();
158176 Map <CacheKeyTO , CacheWrapper <Object >> cacheValues = this .cacheHandler .mget (method , returnItemType , keyArgMap .keySet ());
159177 // 如果所有key都已经命中
@@ -276,14 +294,14 @@ private Object convertToReturnObject(Map<CacheKeyTO, CacheWrapper<Object>> cache
276294 Object [] newValues = (Object []) newValue ;
277295 resSize = cacheValues .size () + (null == newValues ? 0 : newValues .length );
278296 }
279- Object [] res = new Object [ resSize ] ;
297+ Object res = Array . newInstance ( returnType . getComponentType (), resSize ) ;
280298 int ind = 0 ;
281299 for (CacheKeyTO cacheKeyTO : cacheKeys ) {
282300 Object val = getValueFormCacheOrDatasource (cacheKeyTO , cacheValues , unmatchCache );
283301 if (!magic .returnNullValue () && null == val ) {
284302 continue ;
285303 }
286- res [ ind ] = val ;
304+ Array . set ( res , ind , val ) ;
287305 ind ++;
288306 }
289307 return res ;
@@ -363,9 +381,6 @@ private Map<CacheKeyTO, Object> getCacheKeyForMagic() {
363381 cacheKeys [ind ] = cacheKeyTO ;
364382 }
365383 }
366- if (null == keyArgMap || keyArgMap .isEmpty ()) {
367- throw new IllegalArgumentException ("the 'keyArgMap' is empty" );
368- }
369384 return keyArgMap ;
370385 }
371386
0 commit comments