Skip to content

Commit

Permalink
DUBBO-627 序列化时,如果为 JavaBeanDescriptor 则直接返回
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi committed Jan 8, 2013
1 parent 4f8ed01 commit f489972
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static JavaBeanDescriptor serialize(Object obj, JavaBeanAccessor accessor
if (obj == null) {
return null;
}
if (obj instanceof JavaBeanDescriptor) {
return (JavaBeanDescriptor)obj;
}
IdentityHashMap<Object, JavaBeanDescriptor> cache = new IdentityHashMap<Object, JavaBeanDescriptor>();
JavaBeanDescriptor result = createDescriptorIfAbsent(obj, accessor, cache);
return result;
Expand All @@ -72,6 +75,8 @@ private static JavaBeanDescriptor createDescriptorForSerialize(Class<?> cl) {
private static JavaBeanDescriptor createDescriptorIfAbsent(Object obj, JavaBeanAccessor accessor, IdentityHashMap<Object, JavaBeanDescriptor> cache) {
if (cache.containsKey(obj)) {
return cache.get(obj);
} else if (obj instanceof JavaBeanDescriptor) {
return (JavaBeanDescriptor)obj;
} else {
JavaBeanDescriptor result = createDescriptorForSerialize(obj.getClass());
cache.put(obj, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,25 @@ public void testDeserializeBean() throws Exception {
deserBean.getAddresses().entrySet().iterator().next().getValue().getClass());
}

@Test
@SuppressWarnings("unchecked")
public void testSerializeJavaBeanDescriptor() throws Exception {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor();
JavaBeanDescriptor result = JavaBeanSerializeUtil.serialize(descriptor);
Assert.assertTrue(descriptor == result);

Map map = new HashMap();
map.put("first", descriptor);
result = JavaBeanSerializeUtil.serialize(map);
Assert.assertTrue(result.isMapType());
Assert.assertEquals(HashMap.class.getName(), result.getClassName());
Assert.assertEquals(map.size(), result.propertySize());
Object object = result.iterator().next().getValue();
Assert.assertTrue(object instanceof JavaBeanDescriptor);
JavaBeanDescriptor actual = (JavaBeanDescriptor)object;
Assert.assertEquals(map.get("first"), actual);
}

static void assertEqualsEnum(Enum<?> expected, Object obj) {
JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj;
Assert.assertTrue(descriptor.isEnumType());
Expand Down

0 comments on commit f489972

Please sign in to comment.