Skip to content

Commit

Permalink
DUBBO-627 重构
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi committed Dec 26, 2012
1 parent 570a062 commit 737dc24
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entr

public static final int TYPE_BEAN = 7;

public static final String ENUM_PROPERTY_NAME = "name";
private static final String ENUM_PROPERTY_NAME = "name";

public static final String CLASS_PROPERTY_NAME = "name";
private static final String CLASS_PROPERTY_NAME = "name";

public static final String PRIMITIVE_PROPERTY_VALUE = "value";
private static final String PRIMITIVE_PROPERTY_VALUE = "value";

/**
* Used to define a type is valid.
Expand Down Expand Up @@ -122,14 +122,46 @@ public Object setProperty(Object propertyName, Object propertyValue) {
return oldValue;
}

public Object setPrimitive(Object primitiveValue) {
public String setEnumNameProperty(String name) {
if (isEnumType()) {
Object result = setProperty(ENUM_PROPERTY_NAME, name);
return result == null ? null : result.toString();
}
throw new IllegalStateException("The instance is not a enum wrapper");
}

public String getEnumPropertyName() {
if (isEnumType()) {
Object result = getProperty(ENUM_PROPERTY_NAME).toString();
return result == null ? null : result.toString();
}
throw new IllegalStateException("The instance is not a enum wrapper");
}

public String setClassNameProperty(String name) {
if (isClassType()) {
Object result = setProperty(CLASS_PROPERTY_NAME, name);
return result == null ? null : result.toString();
}
throw new IllegalStateException("The instance is not a class wrapper");
}

public String getClassNameProperty() {
if (isClassType()) {
Object result = getProperty(CLASS_PROPERTY_NAME);
return result == null ? null : result.toString();
}
throw new IllegalStateException("The instance is not a class wrapper");
}

public Object setPrimitiveProperty(Object primitiveValue) {
if (isPrimitiveType()) {
return setProperty(PRIMITIVE_PROPERTY_VALUE, primitiveValue);
}
throw new IllegalStateException("The instance is not a primitive type wrapper");
}

public Object getPrimitive() {
public Object getPrimitiveProperty() {
if (isPrimitiveType()) {
return getProperty(PRIMITIVE_PROPERTY_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ private static void serializeInternal(JavaBeanDescriptor descriptor, Object obj,
}

if (obj.getClass().isEnum()) {
descriptor.setProperty(JavaBeanDescriptor.ENUM_PROPERTY_NAME, ((Enum<?>) obj).name());
descriptor.setEnumNameProperty(((Enum<?>) obj).name());
} else if (ReflectUtils.isPrimitive(obj.getClass())) {
descriptor.setProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE, obj);
descriptor.setPrimitiveProperty(obj);
} else if (Class.class.equals(obj.getClass())) {
descriptor.setProperty(JavaBeanDescriptor.CLASS_PROPERTY_NAME, ((Class<?>) obj).getName());
descriptor.setClassNameProperty(((Class<?>) obj).getName());
} else if (obj.getClass().isArray()) {
int len = Array.getLength(obj);
for (int i = 0; i < len; i++) {
Expand Down Expand Up @@ -344,8 +344,7 @@ private static Object instantiateForDeserialize(JavaBeanDescriptor beanDescripto
Object result = null;
if (beanDescriptor.isClassType()) {
try {
result = name2Class(loader,
beanDescriptor.getProperty(JavaBeanDescriptor.CLASS_PROPERTY_NAME).toString());
result = name2Class(loader, beanDescriptor.getClassNameProperty());
return result;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
Expand All @@ -354,22 +353,22 @@ private static Object instantiateForDeserialize(JavaBeanDescriptor beanDescripto
try {
Class<?> enumType = name2Class(loader, beanDescriptor.getClassName());
Method method = getEnumValueOfMethod(enumType);
result = method.invoke(null, enumType, beanDescriptor.getProperty(JavaBeanDescriptor.ENUM_PROPERTY_NAME));
result = method.invoke(null, enumType, beanDescriptor.getEnumPropertyName());
return result;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
} else if (beanDescriptor.isPrimitiveType()) {
result = beanDescriptor.getProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE);
result = beanDescriptor.getPrimitiveProperty();
return result;
} else if (beanDescriptor.isArrayType()) {
Class<?> conponentType = null;
Class<?> componentType;
try {
conponentType = name2Class(loader, beanDescriptor.getClassName());
componentType = name2Class(loader, beanDescriptor.getClassName());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
result = Array.newInstance(conponentType, beanDescriptor.propertySize());
result = Array.newInstance(componentType, beanDescriptor.propertySize());
cache.put(beanDescriptor, result);
} else try {
Class<?> cl = name2Class(loader, beanDescriptor.getClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ public void testSerialize_Primitive() throws Exception {
JavaBeanDescriptor descriptor;
descriptor = JavaBeanSerializeUtil.serialize(Integer.MAX_VALUE);
Assert.assertTrue(descriptor.isPrimitiveType());
Assert.assertEquals(Integer.MAX_VALUE, descriptor.getProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE));
Assert.assertEquals(Integer.MAX_VALUE, descriptor.getPrimitiveProperty());

Date now = new Date();
descriptor = JavaBeanSerializeUtil.serialize(now);
Assert.assertTrue(descriptor.isPrimitiveType());
Assert.assertEquals(now, descriptor.getProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE));
Assert.assertEquals(now, descriptor.getPrimitiveProperty());
}

@Test
public void testDeserialize_Primitive() throws Exception {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE);
descriptor.setProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE, Long.MAX_VALUE);
descriptor.setPrimitiveProperty(Long.MAX_VALUE);
Assert.assertEquals(Long.MAX_VALUE, JavaBeanSerializeUtil.deserialize(descriptor));

BigDecimal decimal = BigDecimal.TEN;
Assert.assertEquals(Long.MAX_VALUE, descriptor.setProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE, decimal));
Assert.assertEquals(Long.MAX_VALUE, descriptor.setPrimitiveProperty(decimal));
Assert.assertEquals(decimal, JavaBeanSerializeUtil.deserialize(descriptor));

String string = UUID.randomUUID().toString();
Assert.assertEquals(decimal, descriptor.setProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE, string));
Assert.assertEquals(decimal, descriptor.setPrimitiveProperty(string));
Assert.assertEquals(string, JavaBeanSerializeUtil.deserialize(descriptor));
}

Expand All @@ -73,7 +73,7 @@ public void testSerialize_Array() throws Exception {
Assert.assertEquals(int.class.getName(), descriptor.getClassName());
for (int i = 0; i < array.length; i++) {
Assert.assertEquals(array[i],
((JavaBeanDescriptor) descriptor.getProperty(i)).getProperty(JavaBeanDescriptor.PRIMITIVE_PROPERTY_VALUE));
((JavaBeanDescriptor) descriptor.getProperty(i)).getPrimitiveProperty());
}

int[][] second = {{1, 2}, {3, 4}};
Expand All @@ -85,7 +85,7 @@ public void testSerialize_Array() throws Exception {
JavaBeanDescriptor item = (((JavaBeanDescriptor)descriptor.getProperty(i)));
Assert.assertTrue(item.isArrayType());
Assert.assertEquals(int.class.getName(), item.getClassName());
Assert.assertEquals(second[i][j], ((JavaBeanDescriptor)item.getProperty(j)).getPrimitive());
Assert.assertEquals(second[i][j], ((JavaBeanDescriptor)item.getProperty(j)).getPrimitiveProperty());
}
}

Expand Down Expand Up @@ -179,14 +179,14 @@ static void assertEqualsEnum(Enum<?> expected, Object obj) {
JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj;
Assert.assertTrue(descriptor.isEnumType());
Assert.assertEquals(expected.getClass().getName(), descriptor.getClassName());
Assert.assertEquals(expected.name(), descriptor.getProperty(JavaBeanDescriptor.ENUM_PROPERTY_NAME));
Assert.assertEquals(expected.name(), descriptor.getEnumPropertyName());
}

static void assertEqualsPrimitive(Object expected, Object obj) {
if (expected == null) { return; }
JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj;
Assert.assertTrue(descriptor.isPrimitiveType());
Assert.assertEquals(expected, descriptor.getPrimitive());
Assert.assertEquals(expected, descriptor.getPrimitiveProperty());
}

static void assertEqualsBigPerson(BigPerson person, Object obj) {
Expand Down

0 comments on commit 737dc24

Please sign in to comment.