CompatibleTypeUtils { @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object convert(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 Short.valueOf(string); } else if (type == Integer.class || type == int.class) { return Integer.valueOf(string); } else if (type == Long.class || type == long.class) { return Long.valueOf(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 Byte.valueOf(string); } else if (type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if (type == Date.class || type == java.sql.Date.class || type == java.sql.Time.class || type == java.sql.Timestamp.class) { try { if (type == Date.class) { return DateUtils.strToDate(string, DateUtils.DATE_FORMAT_TIME); } else if (type == java.sql.Date.class) { return new java.sql.Date(DateUtils.strToLong(string)); } else if (type == java.sql.Timestamp.class) { return new java.sql.Timestamp(DateUtils.strToLong(string)); } else { return new java.sql.Time(DateUtils.strToLong(string)); } } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DateUtils.DATE_FORMAT_TIME + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { return ClassTypeUtils.getClass((String) value); } } 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 (type == java.sql.Date.class) { return new java.sql.Date(number.longValue()); } else if (type == java.sql.Time.class) { return new java.sql.Time(number.longValue()); } else if (type == java.sql.Timestamp.class) { return new java.sql.Timestamp(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<Object>(collection); } else if (type == Set.class) { return new HashSet<Object>(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<Object>(); } } else if (type == Set.class) { collection = new HashSet<Object>(); } else { collection = new ArrayList<Object>(); } 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 convert(Object value, Class<?> type);  }
@Test public void convert() throws Exception { Assert.assertEquals('x', CompatibleTypeUtils.convert("x", char.class)); Assert.assertEquals('x', CompatibleTypeUtils.convert("x", Character.class)); boolean error = false; try { CompatibleTypeUtils.convert("xx", Character.class); } catch (Exception e) { error = true; } Assert.assertTrue(error); Assert.assertEquals(TestEnum.A, CompatibleTypeUtils.convert("A", TestEnum.class)); Assert.assertEquals(new BigInteger("123"), CompatibleTypeUtils.convert("123", BigInteger.class)); Assert.assertEquals(new BigDecimal("123.12"), CompatibleTypeUtils.convert("123.12", BigDecimal.class)); Assert.assertEquals(new Short("123"), CompatibleTypeUtils.convert("123", Short.class)); Assert.assertEquals(new Short("123"), CompatibleTypeUtils.convert("123", short.class)); Assert.assertEquals(new Integer("123"), CompatibleTypeUtils.convert("123", Integer.class)); Assert.assertEquals(new Integer("123"), CompatibleTypeUtils.convert("123", int.class)); Assert.assertEquals(new Long("123"), CompatibleTypeUtils.convert("123", Long.class)); Assert.assertEquals(new Long("123"), CompatibleTypeUtils.convert("123", long.class)); Assert.assertEquals(new Double("123.1"), CompatibleTypeUtils.convert("123.1", Double.class)); Assert.assertEquals(new Double("123.1"), CompatibleTypeUtils.convert("123.1", double.class)); Assert.assertEquals(new Byte("123"), CompatibleTypeUtils.convert("123", Byte.class)); Assert.assertEquals(new Byte("123"), CompatibleTypeUtils.convert("123", byte.class)); Assert.assertEquals(new Float("123.1"), CompatibleTypeUtils.convert("123.1", Float.class)); Assert.assertEquals(new Float("123.1"), CompatibleTypeUtils.convert("123.1", float.class)); Assert.assertEquals(Boolean.TRUE, CompatibleTypeUtils.convert("true", Boolean.class)); Assert.assertEquals(Boolean.TRUE, CompatibleTypeUtils.convert("true", boolean.class)); Date dataTime = DateUtils.strToDate("2018-1-1 11:22:33"); Assert.assertEquals(dataTime, CompatibleTypeUtils.convert("2018-1-1 11:22:33", Date.class)); Long timeLong = DateUtils.strToLong("2018-1-1 11:22:33"); java.sql.Date sqlDate = new java.sql.Date(timeLong); Object timeResult = CompatibleTypeUtils.convert("2018-1-1 11:22:33", java.sql.Date.class); Assert.assertEquals(sqlDate, timeResult); timeResult = CompatibleTypeUtils.convert(timeLong, java.sql.Date.class); Assert.assertEquals(sqlDate, timeResult); timeResult = CompatibleTypeUtils.convert("2018-1-1 11:22:33", java.sql.Timestamp.class); java.sql.Timestamp timestamp = new java.sql.Timestamp(timeLong); Assert.assertEquals(timestamp, timeResult); timeResult = CompatibleTypeUtils.convert("2018-1-1 11:22:33", java.sql.Time.class); java.sql.Time time = new java.sql.Time(timeLong); Assert.assertEquals(time, timeResult); Assert.assertEquals(new Short("123"), CompatibleTypeUtils.convert(123, Short.class)); Assert.assertEquals(new Short("123"), CompatibleTypeUtils.convert(123, short.class)); Assert.assertEquals(new Integer("123"), CompatibleTypeUtils.convert(123, Integer.class)); Assert.assertEquals(new Integer("123"), CompatibleTypeUtils.convert(123, int.class)); Assert.assertEquals(new Long("123"), CompatibleTypeUtils.convert(123, Long.class)); Assert.assertEquals(new Long("123"), CompatibleTypeUtils.convert(123, long.class)); Assert.assertEquals(new Double("123.1"), CompatibleTypeUtils.convert(123.1, Double.class)); Assert.assertEquals(new Double("123.1"), CompatibleTypeUtils.convert(123.1, double.class)); Assert.assertEquals(new Byte("123"), CompatibleTypeUtils.convert(123, Byte.class)); Assert.assertEquals(new Byte("123"), CompatibleTypeUtils.convert(123, byte.class)); Assert.assertEquals(new Float("123.1"), CompatibleTypeUtils.convert(123.1, Float.class)); Assert.assertEquals(new Float("123.1"), CompatibleTypeUtils.convert(123.1, float.class)); Assert.assertEquals(Boolean.TRUE, CompatibleTypeUtils.convert("true", Boolean.class)); Assert.assertEquals(Boolean.TRUE, CompatibleTypeUtils.convert("true", boolean.class)); String[] ss = (String[]) CompatibleTypeUtils.convert(Collections.singletonList("x"), String[].class); Assert.assertEquals("x", ss[0]); List list = (List) CompatibleTypeUtils.convert(Collections.singleton("x"), List.class); Assert.assertEquals("x", list.get(0)); list = (List) CompatibleTypeUtils.convert(Collections.singletonList("x"), ArrayList.class); Assert.assertEquals("x", list.get(0)); Set set = (Set) CompatibleTypeUtils.convert(Collections.singletonList("x"), Set.class); Assert.assertEquals("x", set.iterator().next()); list = (List) CompatibleTypeUtils.convert(new String[] { "x" }, List.class); Assert.assertEquals("x", list.get(0)); list = (List) CompatibleTypeUtils.convert(new String[] { "x" }, ArrayList.class); Assert.assertEquals("x", list.get(0)); set = (Set) CompatibleTypeUtils.convert(new String[] { "x" }, Set.class); Assert.assertEquals("x", set.iterator().next()); }