From 5260018868e090493884efec06289d01b4e29293 Mon Sep 17 00:00:00 2001 From: kimi Date: Fri, 20 Apr 2012 02:50:34 +0000 Subject: [PATCH] DUBBO-342 git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@1589 1a56cb94-b969-4eaa-88fa-be21384802f2 --- .../alibaba/dubbo/common/utils/PojoUtils.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java index 13dfc2114..034b35b26 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/PojoUtils.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -79,11 +80,11 @@ public class PojoUtils { } public static Object generalize(Object pojo) { - return generalize(pojo, new HashMap()); + return generalize(pojo, new IdentityHashMap()); } @SuppressWarnings("unchecked") - private static Object generalize(Object pojo, Map history) { + private static Object generalize(Object pojo, Map history) { if (pojo == null) { return null; } @@ -110,11 +111,10 @@ public class PojoUtils { return ((Class)pojo).getName(); } - Integer id = hashCode(pojo); - if (history.containsKey(id)) { - return history.get(id); + if (history.containsKey(pojo)) { + return history.get(pojo); } - history.put(id, pojo); + history.put(pojo, pojo); if (pojo.getClass().isArray()) { int len = Array.getLength(pojo); @@ -144,7 +144,7 @@ public class PojoUtils { return src; } Map map = new HashMap(); - history.put(id, map); + history.put(pojo, map); map.put("class", pojo.getClass().getName()); for (Method method : pojo.getClass().getMethods()) { if (ReflectUtils.isBeanPropertyReadMethod(method)) { @@ -160,11 +160,11 @@ public class PojoUtils { } public static Object realize(Object pojo, Class type) { - return realize0(pojo, type, null , new HashMap()); + return realize0(pojo, type, null , new IdentityHashMap()); } public static Object realize(Object pojo, Class type, Type genericType) { - return realize0(pojo, type, genericType, new HashMap()); + return realize0(pojo, type, genericType, new IdentityHashMap()); } private static class PojoInvocationHandler implements InvocationHandler { @@ -190,7 +190,7 @@ public class PojoUtils { value = map.get(methodName.substring(0, 1).toLowerCase() + methodName.substring(1)); } if (value instanceof Map && ! Map.class.isAssignableFrom(method.getReturnType())) { - value = realize0((Map) value, method.getReturnType(), null, new HashMap()); + value = realize0((Map) value, method.getReturnType(), null, new IdentityHashMap()); } return value; } @@ -215,7 +215,7 @@ public class PojoUtils { } @SuppressWarnings({ "unchecked", "rawtypes" }) - private static Object realize0(Object pojo, Class type, Type genericType, final Map history) { + private static Object realize0(Object pojo, Class type, Type genericType, final Map history) { if (pojo == null) { return null; } @@ -232,11 +232,10 @@ public class PojoUtils { return CompatibleTypeUtils.compatibleTypeConvert(pojo, type); } - Integer id = System.identityHashCode(pojo); - if (history.containsKey(id)) { - return history.get(id); + if (history.containsKey(pojo)) { + return history.get(pojo); } - history.put(id, pojo); + history.put(pojo, pojo); if (pojo.getClass().isArray()) { if (Collection.class.isAssignableFrom(type)) { @@ -341,11 +340,11 @@ public class PojoUtils { return map; } else if (type.isInterface()) { Object dest = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{type}, new PojoInvocationHandler(map)); - history.put(id, dest); + history.put(pojo, dest); return dest; } else { Object dest = newInstance(type); - history.put(id, dest); + history.put(pojo, dest); for (Map.Entry entry : map.entrySet()) { Object key = entry.getKey(); if (key instanceof String) { @@ -457,7 +456,4 @@ public class PojoUtils { && ! Map.class.isAssignableFrom(cls); } - private static int hashCode(Object object) { - return 31 * (object.getClass().getName().hashCode() ^ System.identityHashCode(object)); - } } \ No newline at end of file -- GitLab