diff --git a/disconf-client/src/main/java/com/baidu/disconf/client/support/utils/ClassUtils.java b/disconf-client/src/main/java/com/baidu/disconf/client/support/utils/ClassUtils.java index 33ef9e5e7..e3b3b4586 100644 --- a/disconf-client/src/main/java/com/baidu/disconf/client/support/utils/ClassUtils.java +++ b/disconf-client/src/main/java/com/baidu/disconf/client/support/utils/ClassUtils.java @@ -5,6 +5,8 @@ import java.util.HashSet; import java.util.Set; +import com.google.gson.Gson; + /** * 类工具 * @@ -114,7 +116,7 @@ public static Object getValeByType(Class type, Object value) } // trim - String dataValue = (String) value; + String dataValue = String.valueOf(value); dataValue = dataValue.trim(); // process @@ -124,7 +126,7 @@ public static Object getValeByType(Class type, Object value) if (typeName.equals("int") || typeName.equals("java.lang.integer")) { if (value.equals("")) { - value = "0"; + return 0; } return Integer.valueOf(dataValue); @@ -132,7 +134,7 @@ public static Object getValeByType(Class type, Object value) } else if (typeName.equals("long") || typeName.equals("java.lang.long")) { if (value.equals("")) { - value = "0"; + return 0L; } return Long.valueOf(dataValue); @@ -141,7 +143,7 @@ public static Object getValeByType(Class type, Object value) || typeName.equals("java.lang.boolean")) { if (value.equals("")) { - value = "false"; + return false; } return Boolean.valueOf(dataValue); @@ -150,14 +152,26 @@ public static Object getValeByType(Class type, Object value) || typeName.equals("java.lang.double")) { if (value.equals("")) { - value = "0.0"; + return 0.0D; } return Double.valueOf(dataValue); - } else { - - return value; + } else if (typeName.equals("string") + || typeName.equals("java.lang.string")) { + + return dataValue; + } + else { + try { + // 其他类型尝试JSON解析 + Gson gson = new Gson(); + return gson.fromJson(value.toString(), type); + } catch (Exception ex) { + ex.printStackTrace(); + } + // JSON解析异常的情况下,返回null,防止待会转换继续抛异常 + return null; } } diff --git a/disconf-client/src/test/java/com/baidu/disconf/client/test/support/utils/ClassUtilsTest.java b/disconf-client/src/test/java/com/baidu/disconf/client/test/support/utils/ClassUtilsTest.java new file mode 100644 index 000000000..225af3cec --- /dev/null +++ b/disconf-client/src/test/java/com/baidu/disconf/client/test/support/utils/ClassUtilsTest.java @@ -0,0 +1,42 @@ +package com.baidu.disconf.client.test.support.utils; + +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import com.baidu.disconf.client.support.utils.ClassUtils; + +public class ClassUtilsTest { + + @SuppressWarnings("unchecked") + @Test + public void testGetValeByType() throws Exception { + assertTrue(ClassUtils.getValeByType(Integer.class, "10086").equals(new Integer(10086))); + assertTrue(ClassUtils.getValeByType(Integer.class, "").equals(new Integer(0))); + assertTrue(ClassUtils.getValeByType(Integer.class, 0).equals(new Integer(0))); + assertTrue(ClassUtils.getValeByType(Long.class, "1688").equals(new Long(1688))); + assertTrue(ClassUtils.getValeByType(Long.class, "").equals(new Long(0))); + assertTrue(ClassUtils.getValeByType(Boolean.class, "true").equals(new Boolean(true))); + assertTrue(ClassUtils.getValeByType(Boolean.class, "").equals(new Boolean(false))); + assertTrue(ClassUtils.getValeByType(Double.class, "234.4").equals(new Double(234.4))); + assertTrue(ClassUtils.getValeByType(Double.class, "").equals(new Double(0.0))); + assertTrue(ClassUtils.getValeByType(String.class, "234.4").equals(String.valueOf(234.4))); + List list = (List) ClassUtils.getValeByType(List.class, "[100,200,300]"); + assertTrue(list.contains(new Double(100))); + Map map = (Map) ClassUtils.getValeByType(Map.class, "{\"name\":\"zxh\",\"city\":\"shenzhen\"}"); + assertTrue(map.get("name").equals("zxh")); + assertTrue(ClassUtils.getValeByType(List.class, "234.4,123") == null); + } + + @Test + public void testGetFieldNameByGetMethodName() { + assertTrue(ClassUtils.getFieldNameByGetMethodName("getOrderNo").equals("orderNo")); + assertTrue(ClassUtils.getFieldNameByGetMethodName("isSended").equals("sended")); + assertTrue(ClassUtils.getFieldNameByGetMethodName("isA").equals("a")); + assertTrue(ClassUtils.getFieldNameByGetMethodName("someMethod") == null); + } + +}