From c99cfb98b94a2fd2dd1862c0018c1a461d1fe1cd Mon Sep 17 00:00:00 2001 From: qiujiayu <153163285@qq.com> Date: Sat, 13 Jul 2019 00:56:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E8=BF=94=E5=9B=9E=E5=80=BC=E4=B8=BA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=97=B6=EF=BC=8C=E4=BD=BF=E7=94=A8Array.new?= =?UTF-8?q?Instance=E5=88=9B=E5=BB=BA=E6=95=B0=E7=BB=84=EF=BC=8Cmagic?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=EF=BC=8C=E7=94=9F=E6=88=90=E7=BC=93?= =?UTF-8?q?=E5=AD=98key=E4=B8=AA=E6=95=B0=E4=B8=BA0=E6=97=B6=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=A9=BA=E7=9A=84=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jarvis/cache/MagicHandler.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/jarvis/cache/MagicHandler.java b/src/main/java/com/jarvis/cache/MagicHandler.java index c2f9d7b2..d67cb3ea 100644 --- a/src/main/java/com/jarvis/cache/MagicHandler.java +++ b/src/main/java/com/jarvis/cache/MagicHandler.java @@ -125,12 +125,24 @@ public static boolean isMagic(Cache cache, Method method) throws Exception { private Collection newCollection(Class collectionType, int resSize) throws Exception { Collection res; if (LinkedList.class.isAssignableFrom(collectionType)) { + if (resSize == 0) { + return Collections.emptyList(); + } res = new LinkedList<>(); } else if (List.class.isAssignableFrom(collectionType)) { + if (resSize == 0) { + return Collections.emptyList(); + } res = new ArrayList<>(resSize); } else if (LinkedHashSet.class.isAssignableFrom(collectionType)) { + if (resSize == 0) { + return Collections.emptySet(); + } res = new LinkedHashSet<>(resSize); } else if (Set.class.isAssignableFrom(collectionType)) { + if (resSize == 0) { + return Collections.emptySet(); + } res = new HashSet<>(resSize); } else { throw new Exception("Unsupported type:" + collectionType.getName()); @@ -154,6 +166,12 @@ public Object magic() throws Throwable { return newValue; } Map keyArgMap = getCacheKeyForMagic(); + if (null == keyArgMap || keyArgMap.isEmpty()) { + if (returnType.isArray()) { + return Array.newInstance(returnType.getComponentType(), 0); + } + return newCollection(returnType, 0); + } Type returnItemType = getRealReturnType(); Map> cacheValues = this.cacheHandler.mget(method, returnItemType, keyArgMap.keySet()); // 如果所有key都已经命中 @@ -276,14 +294,14 @@ private Object convertToReturnObject(Map> cache Object[] newValues = (Object[]) newValue; resSize = cacheValues.size() + (null == newValues ? 0 : newValues.length); } - Object[] res = new Object[resSize]; + Object res = Array.newInstance(returnType.getComponentType(), resSize); int ind = 0; for (CacheKeyTO cacheKeyTO : cacheKeys) { Object val = getValueFormCacheOrDatasource(cacheKeyTO, cacheValues, unmatchCache); if (!magic.returnNullValue() && null == val) { continue; } - res[ind] = val; + Array.set(res, ind, val); ind++; } return res; @@ -363,9 +381,6 @@ private Map getCacheKeyForMagic() { cacheKeys[ind] = cacheKeyTO; } } - if (null == keyArgMap || keyArgMap.isEmpty()) { - throw new IllegalArgumentException("the 'keyArgMap' is empty"); - } return keyArgMap; }