@Test public void testParseArgument() throws Exception { Object value = WXReflectionUtils.parseArgument(String.class,"dkdkdkdk"); assertTrue(value instanceof String); value = WXReflectionUtils.parseArgument(long.class,"123444"); assertTrue(value instanceof Long); value = WXReflectionUtils.parseArgument(int.class,"123444"); assertTrue(value instanceof Integer); value = WXReflectionUtils.parseArgument(Integer.class,"123444"); assertTrue(value instanceof Integer); value = WXReflectionUtils.parseArgument(int.class,123444); assertTrue(value instanceof Integer); value = WXReflectionUtils.parseArgument(double.class, Double.toString(123.444d)); assertTrue(value instanceof Double); JSONObject j = new JSONObject(); j.put("a","b"); j.put("c",23); value = WXReflectionUtils.parseArgument(String.class,j); assertTrue(value instanceof String); value = WXReflectionUtils.parseArgument(Map.class,j); assertTrue(value instanceof Map); assertEquals(((Map)value).get("a"),"b"); value = WXReflectionUtils.parseArgument(JSONObject.class,j); assertTrue(value instanceof JSONObject); assertEquals(((JSONObject)value).get("a"),"b"); assertEquals(((JSONObject)value).get("c"),23); value = WXReflectionUtils.parseArgument(JSONObject.class, JSON.toJSONString(j)); assertTrue(value instanceof JSONObject); assertEquals(((JSONObject)value).get("a"),"b"); assertEquals(((JSONObject)value).get("c"),23); JSONArray k = new JSONArray(); k.add("b"); k.add(23); value = WXReflectionUtils.parseArgument(String[].class, k); assertTrue(value instanceof String[]); assertEquals(((String[])value)[0],"b"); assertEquals(((String[])value)[1],"23"); value = WXReflectionUtils.parseArgument(String[].class, JSON.toJSONString(k)); assertTrue(value instanceof String[]); assertEquals(((String[])value)[0],"b"); assertEquals(((String[])value)[1],"23"); value = WXReflectionUtils.parseArgument(List.class, JSON.toJSONString(k)); assertTrue(value instanceof List); assertEquals(((List)value).get(0),"b"); assertEquals(((List)value).get(1),23); k = new JSONArray(); k.add(1); k.add(23); value = WXReflectionUtils.parseArgument(int[].class, JSON.toJSONString(k)); assertTrue(value instanceof int[]); assertEquals(((int[])value)[0],1); assertEquals(((int[])value)[1],23); }
public static Object parseArgument(Type paramClazz, Object value) { if(value != null){ if(value.getClass() == paramClazz){ return value; } if(paramClazz instanceof Class){ if( ((Class<?>) paramClazz).isAssignableFrom(value.getClass())) { return value; } } } if (paramClazz == String.class) { return value instanceof String ? value : JSON.toJSONString(value); } else if (paramClazz == int.class) { return value.getClass().isAssignableFrom(int.class) ? value : WXUtils.getInt(value); } else if (paramClazz == long.class) { return value.getClass().isAssignableFrom(long.class) ? value : WXUtils.getLong(value); } else if (paramClazz == double.class) { return value.getClass().isAssignableFrom(double.class) ? value : WXUtils.getDouble(value); } else if (paramClazz == float.class) { return value.getClass().isAssignableFrom(float.class) ? value : WXUtils.getFloat(value); } else if (paramClazz == JSONArray.class && value != null && value.getClass() == JSONArray.class) { return value; } else if (paramClazz == JSONObject.class && value != null && value.getClass() == JSONObject.class) { return value; } else { return JSON.parseObject(value instanceof String ? (String) value : JSON.toJSONString(value), paramClazz); } }
WXReflectionUtils { public static Object parseArgument(Type paramClazz, Object value) { if(value != null){ if(value.getClass() == paramClazz){ return value; } if(paramClazz instanceof Class){ if( ((Class<?>) paramClazz).isAssignableFrom(value.getClass())) { return value; } } } if (paramClazz == String.class) { return value instanceof String ? value : JSON.toJSONString(value); } else if (paramClazz == int.class) { return value.getClass().isAssignableFrom(int.class) ? value : WXUtils.getInt(value); } else if (paramClazz == long.class) { return value.getClass().isAssignableFrom(long.class) ? value : WXUtils.getLong(value); } else if (paramClazz == double.class) { return value.getClass().isAssignableFrom(double.class) ? value : WXUtils.getDouble(value); } else if (paramClazz == float.class) { return value.getClass().isAssignableFrom(float.class) ? value : WXUtils.getFloat(value); } else if (paramClazz == JSONArray.class && value != null && value.getClass() == JSONArray.class) { return value; } else if (paramClazz == JSONObject.class && value != null && value.getClass() == JSONObject.class) { return value; } else { return JSON.parseObject(value instanceof String ? (String) value : JSON.toJSONString(value), paramClazz); } } }
WXReflectionUtils { public static Object parseArgument(Type paramClazz, Object value) { if(value != null){ if(value.getClass() == paramClazz){ return value; } if(paramClazz instanceof Class){ if( ((Class<?>) paramClazz).isAssignableFrom(value.getClass())) { return value; } } } if (paramClazz == String.class) { return value instanceof String ? value : JSON.toJSONString(value); } else if (paramClazz == int.class) { return value.getClass().isAssignableFrom(int.class) ? value : WXUtils.getInt(value); } else if (paramClazz == long.class) { return value.getClass().isAssignableFrom(long.class) ? value : WXUtils.getLong(value); } else if (paramClazz == double.class) { return value.getClass().isAssignableFrom(double.class) ? value : WXUtils.getDouble(value); } else if (paramClazz == float.class) { return value.getClass().isAssignableFrom(float.class) ? value : WXUtils.getFloat(value); } else if (paramClazz == JSONArray.class && value != null && value.getClass() == JSONArray.class) { return value; } else if (paramClazz == JSONObject.class && value != null && value.getClass() == JSONObject.class) { return value; } else { return JSON.parseObject(value instanceof String ? (String) value : JSON.toJSONString(value), paramClazz); } }  }
WXReflectionUtils { public static Object parseArgument(Type paramClazz, Object value) { if(value != null){ if(value.getClass() == paramClazz){ return value; } if(paramClazz instanceof Class){ if( ((Class<?>) paramClazz).isAssignableFrom(value.getClass())) { return value; } } } if (paramClazz == String.class) { return value instanceof String ? value : JSON.toJSONString(value); } else if (paramClazz == int.class) { return value.getClass().isAssignableFrom(int.class) ? value : WXUtils.getInt(value); } else if (paramClazz == long.class) { return value.getClass().isAssignableFrom(long.class) ? value : WXUtils.getLong(value); } else if (paramClazz == double.class) { return value.getClass().isAssignableFrom(double.class) ? value : WXUtils.getDouble(value); } else if (paramClazz == float.class) { return value.getClass().isAssignableFrom(float.class) ? value : WXUtils.getFloat(value); } else if (paramClazz == JSONArray.class && value != null && value.getClass() == JSONArray.class) { return value; } else if (paramClazz == JSONObject.class && value != null && value.getClass() == JSONObject.class) { return value; } else { return JSON.parseObject(value instanceof String ? (String) value : JSON.toJSONString(value), paramClazz); } }  static Object parseArgument(Type paramClazz, Object value); static void setValue(Object obj, String fieldName, Object value); static Field getDeclaredField(Object object, String fieldName); static void setProperty(Object bean, Field field, Object value); }
WXReflectionUtils { public static Object parseArgument(Type paramClazz, Object value) { if(value != null){ if(value.getClass() == paramClazz){ return value; } if(paramClazz instanceof Class){ if( ((Class<?>) paramClazz).isAssignableFrom(value.getClass())) { return value; } } } if (paramClazz == String.class) { return value instanceof String ? value : JSON.toJSONString(value); } else if (paramClazz == int.class) { return value.getClass().isAssignableFrom(int.class) ? value : WXUtils.getInt(value); } else if (paramClazz == long.class) { return value.getClass().isAssignableFrom(long.class) ? value : WXUtils.getLong(value); } else if (paramClazz == double.class) { return value.getClass().isAssignableFrom(double.class) ? value : WXUtils.getDouble(value); } else if (paramClazz == float.class) { return value.getClass().isAssignableFrom(float.class) ? value : WXUtils.getFloat(value); } else if (paramClazz == JSONArray.class && value != null && value.getClass() == JSONArray.class) { return value; } else if (paramClazz == JSONObject.class && value != null && value.getClass() == JSONObject.class) { return value; } else { return JSON.parseObject(value instanceof String ? (String) value : JSON.toJSONString(value), paramClazz); } }  static Object parseArgument(Type paramClazz, Object value); static void setValue(Object obj, String fieldName, Object value); static Field getDeclaredField(Object object, String fieldName); static void setProperty(Object bean, Field field, Object value);  }