@SuppressWarnings("unchecked") @Test public void testCompatibleTypeConvert() throws Exception { Object result; { Object input = new Object(); result = CompatibleTypeUtils.compatibleTypeConvert(input, Date.class); assertSame(input, result); result = CompatibleTypeUtils.compatibleTypeConvert(input, null); assertSame(input, result); result = CompatibleTypeUtils.compatibleTypeConvert(null, Date.class); assertNull(result); } { result = CompatibleTypeUtils.compatibleTypeConvert("a", char.class); assertEquals(Character.valueOf('a'), (Character) result); result = CompatibleTypeUtils.compatibleTypeConvert("A", MyEnum.class); assertEquals(MyEnum.A, (MyEnum) result); result = CompatibleTypeUtils.compatibleTypeConvert("3", BigInteger.class); assertEquals(new BigInteger("3"), (BigInteger) result); result = CompatibleTypeUtils.compatibleTypeConvert("3", BigDecimal.class); assertEquals(new BigDecimal("3"), (BigDecimal) result); result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", Date.class); assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-11 12:24:12"), (Date) result); } { result = CompatibleTypeUtils.compatibleTypeConvert(3, byte.class); assertEquals(Byte.valueOf((byte)3), (Byte) result); result = CompatibleTypeUtils.compatibleTypeConvert((byte)3, int.class); assertEquals(Integer.valueOf(3), (Integer) result); result = CompatibleTypeUtils.compatibleTypeConvert(3, short.class); assertEquals(Short.valueOf((short)3), (Short) result); result = CompatibleTypeUtils.compatibleTypeConvert((short)3, int.class); assertEquals(Integer.valueOf(3), (Integer) result); result = CompatibleTypeUtils.compatibleTypeConvert(3, int.class); assertEquals(Integer.valueOf(3), (Integer) result); result = CompatibleTypeUtils.compatibleTypeConvert(3, long.class); assertEquals(Long.valueOf(3), (Long) result); result = CompatibleTypeUtils.compatibleTypeConvert(3L, int.class); assertEquals(Integer.valueOf(3), (Integer) result); result = CompatibleTypeUtils.compatibleTypeConvert(3L, BigInteger.class); assertEquals(BigInteger.valueOf(3L), (BigInteger) result); result = CompatibleTypeUtils.compatibleTypeConvert(BigInteger.valueOf(3L), int.class); assertEquals(Integer.valueOf(3), (Integer) result); } { result = CompatibleTypeUtils.compatibleTypeConvert(3D, float.class); assertEquals(Float.valueOf(3), (Float) result); result = CompatibleTypeUtils.compatibleTypeConvert(3F, double.class); assertEquals(Double.valueOf(3), (Double) result); result = CompatibleTypeUtils.compatibleTypeConvert(3D, double.class); assertEquals(Double.valueOf(3), (Double) result); result = CompatibleTypeUtils.compatibleTypeConvert(3D, BigDecimal.class); assertEquals(BigDecimal.valueOf(3D), (BigDecimal) result); result = CompatibleTypeUtils.compatibleTypeConvert(BigDecimal.valueOf(3D), double.class); assertEquals(Double.valueOf(3), (Double) result); } { List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); Set<String> set = new HashSet<String>(); set.add("a"); set.add("b"); String[] array = new String[] {"a", "b"}; result = CompatibleTypeUtils.compatibleTypeConvert(array, List.class); assertEquals(ArrayList.class, result.getClass()); assertEquals(2, ((List<String>)result).size()); assertTrue(((List<String>)result).contains("a")); assertTrue(((List<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(set, List.class); assertEquals(ArrayList.class, result.getClass()); assertEquals(2, ((List<String>)result).size()); assertTrue(((List<String>)result).contains("a")); assertTrue(((List<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(array, CopyOnWriteArrayList.class); assertEquals(CopyOnWriteArrayList.class, result.getClass()); assertEquals(2, ((List<String>)result).size()); assertTrue(((List<String>)result).contains("a")); assertTrue(((List<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(set, CopyOnWriteArrayList.class); assertEquals(CopyOnWriteArrayList.class, result.getClass()); assertEquals(2, ((List<String>)result).size()); assertTrue(((List<String>)result).contains("a")); assertTrue(((List<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(set, String[].class); assertEquals(String[].class, result.getClass()); assertEquals(2, ((String[])result).length); assertTrue(((String[])result)[0].equals("a") || ((String[])result)[0].equals("b")); assertTrue(((String[])result)[1].equals("a") || ((String[])result)[1].equals("b")); result = CompatibleTypeUtils.compatibleTypeConvert(array, Set.class); assertEquals(HashSet.class, result.getClass()); assertEquals(2, ((Set<String>)result).size()); assertTrue(((Set<String>)result).contains("a")); assertTrue(((Set<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(list, Set.class); assertEquals(HashSet.class, result.getClass()); assertEquals(2, ((Set<String>)result).size()); assertTrue(((Set<String>)result).contains("a")); assertTrue(((Set<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(array, ConcurrentHashSet.class); assertEquals(ConcurrentHashSet.class, result.getClass()); assertEquals(2, ((Set<String>)result).size()); assertTrue(((Set<String>)result).contains("a")); assertTrue(((Set<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(list, ConcurrentHashSet.class); assertEquals(ConcurrentHashSet.class, result.getClass()); assertEquals(2, ((Set<String>)result).size()); assertTrue(((Set<String>)result).contains("a")); assertTrue(((Set<String>)result).contains("b")); result = CompatibleTypeUtils.compatibleTypeConvert(list, String[].class); assertEquals(String[].class, result.getClass()); assertEquals(2, ((String[])result).length); assertTrue(((String[])result)[0].equals("a")); assertTrue(((String[])result)[1].equals("b")); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Object compatibleTypeConvert(Object value, Class<?> type) { if(value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if(value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if(string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if(type.isEnum()) { return Enum.valueOf((Class<Enum>)type, string); } else if(type == BigInteger.class) { return new BigInteger(string); } else if(type == BigDecimal.class) { return new BigDecimal(string); } else if(type == Short.class || type == short.class) { return new Short(string); } else if(type == Integer.class || type == int.class) { return new Integer(string); } else if(type == Long.class || type == long.class) { return new Long(string); } else if(type == Double.class || type == double.class) { return new Double(string); } else if(type == Float.class || type == float.class) { return new Float(string); } else if(type == Byte.class || type == byte.class) { return new Byte(string); } else if(type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if(type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { try { return ReflectUtils.name2class((String)value); } catch (ClassNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } } } else if(value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if(value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i ++, item); } return array; } else if (! type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable ignore) { } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if(value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (! type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i ++) { collection.add(Array.get(value, i)); } return collection; } return value; }
CompatibleTypeUtils { @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object compatibleTypeConvert(Object value, Class<?> type) { if(value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if(value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if(string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if(type.isEnum()) { return Enum.valueOf((Class<Enum>)type, string); } else if(type == BigInteger.class) { return new BigInteger(string); } else if(type == BigDecimal.class) { return new BigDecimal(string); } else if(type == Short.class || type == short.class) { return new Short(string); } else if(type == Integer.class || type == int.class) { return new Integer(string); } else if(type == Long.class || type == long.class) { return new Long(string); } else if(type == Double.class || type == double.class) { return new Double(string); } else if(type == Float.class || type == float.class) { return new Float(string); } else if(type == Byte.class || type == byte.class) { return new Byte(string); } else if(type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if(type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { try { return ReflectUtils.name2class((String)value); } catch (ClassNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } } } else if(value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if(value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i ++, item); } return array; } else if (! type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable ignore) { } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if(value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (! type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i ++) { collection.add(Array.get(value, i)); } return collection; } return value; } }
CompatibleTypeUtils { @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object compatibleTypeConvert(Object value, Class<?> type) { if(value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if(value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if(string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if(type.isEnum()) { return Enum.valueOf((Class<Enum>)type, string); } else if(type == BigInteger.class) { return new BigInteger(string); } else if(type == BigDecimal.class) { return new BigDecimal(string); } else if(type == Short.class || type == short.class) { return new Short(string); } else if(type == Integer.class || type == int.class) { return new Integer(string); } else if(type == Long.class || type == long.class) { return new Long(string); } else if(type == Double.class || type == double.class) { return new Double(string); } else if(type == Float.class || type == float.class) { return new Float(string); } else if(type == Byte.class || type == byte.class) { return new Byte(string); } else if(type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if(type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { try { return ReflectUtils.name2class((String)value); } catch (ClassNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } } } else if(value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if(value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i ++, item); } return array; } else if (! type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable ignore) { } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if(value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (! type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i ++) { collection.add(Array.get(value, i)); } return collection; } return value; } private  CompatibleTypeUtils(); }
CompatibleTypeUtils { @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object compatibleTypeConvert(Object value, Class<?> type) { if(value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if(value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if(string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if(type.isEnum()) { return Enum.valueOf((Class<Enum>)type, string); } else if(type == BigInteger.class) { return new BigInteger(string); } else if(type == BigDecimal.class) { return new BigDecimal(string); } else if(type == Short.class || type == short.class) { return new Short(string); } else if(type == Integer.class || type == int.class) { return new Integer(string); } else if(type == Long.class || type == long.class) { return new Long(string); } else if(type == Double.class || type == double.class) { return new Double(string); } else if(type == Float.class || type == float.class) { return new Float(string); } else if(type == Byte.class || type == byte.class) { return new Byte(string); } else if(type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if(type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { try { return ReflectUtils.name2class((String)value); } catch (ClassNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } } } else if(value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if(value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i ++, item); } return array; } else if (! type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable ignore) { } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if(value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (! type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i ++) { collection.add(Array.get(value, i)); } return collection; } return value; } private  CompatibleTypeUtils(); @SuppressWarnings({ "unchecked", "rawtypes" }) static Object compatibleTypeConvert(Object value, Class<?> type); }
CompatibleTypeUtils { @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object compatibleTypeConvert(Object value, Class<?> type) { if(value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if(value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if(string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if(type.isEnum()) { return Enum.valueOf((Class<Enum>)type, string); } else if(type == BigInteger.class) { return new BigInteger(string); } else if(type == BigDecimal.class) { return new BigDecimal(string); } else if(type == Short.class || type == short.class) { return new Short(string); } else if(type == Integer.class || type == int.class) { return new Integer(string); } else if(type == Long.class || type == long.class) { return new Long(string); } else if(type == Double.class || type == double.class) { return new Double(string); } else if(type == Float.class || type == float.class) { return new Float(string); } else if(type == Byte.class || type == byte.class) { return new Byte(string); } else if(type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if(type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { try { return ReflectUtils.name2class((String)value); } catch (ClassNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } } } else if(value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if(value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i ++, item); } return array; } else if (! type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable ignore) { } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if(value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (! type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i ++) { collection.add(Array.get(value, i)); } return collection; } return value; } private  CompatibleTypeUtils(); @SuppressWarnings({ "unchecked", "rawtypes" }) static Object compatibleTypeConvert(Object value, Class<?> type);  }