TypeUtils { @SuppressWarnings("unchecked") public static <T> T fromString(Class<T> type, String value) { if (type == null || !types.containsValue(type)) { throw new IllegalArgumentException("Cannot convert to type argument : " + type); } if (value == null || value.length() < 1) { throw new IllegalArgumentException("Argument value cannot be null or empty"); } T result = null; try { if (type.equals(String.class)) { result = (T) value; } else if (type.equals(Character.class) || type.equals(Character.TYPE)) { result = (T) Character.valueOf(value.charAt(0)); } else if (wrapperTypes.containsValue(type) || mathTypes.containsValue(type)) { Constructor<? extends Object> constructor = type.getConstructor(String.class); result = (T) constructor.newInstance(value); } else if (primitiveToWrapper.containsKey(type)) { Class<? extends Object> promotedType = primitiveToWrapper.get(type); char[] simpleTypeName = type.getName().toCharArray(); simpleTypeName[0] = Character.toUpperCase(simpleTypeName[0]); String parseMethodName = "parse" + new String(simpleTypeName); Method parseMethod = promotedType.getDeclaredMethod(parseMethodName, String.class); result = (T) parseMethod.invoke(null, value); } } catch (SecurityException e) { throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e); } catch (InstantiationException e) { throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e); } return result; } private  TypeUtils(); static Map<String, String> fromDictionary(Dictionary<String, String> dictionary); static Long[] toLong(long[] array); static long[] toPrimitive(Long[] array); @SuppressWarnings("unchecked") static T fromString(Class<T> type, String value); static Map<String, Class<? extends Object>> primitiveTypes; static Map<String, Class<? extends Object>> wrapperTypes; static Map<String, Class<? extends Object>> mathTypes; static Map<Class<? extends Object>, Class<? extends Object>> primitiveToWrapper; static Map<String, Class<? extends Object>> types; }
@Test public void testFromString() throws Exception { String value; value = "1"; Integer integerValue = fromString(Integer.class, value); assertEquals(new Integer(1), integerValue); int intValue = fromString(Integer.TYPE, value); assertEquals(1, intValue); Long wrappedLongValue = fromString(Long.class, value); assertEquals(Long.valueOf(1), wrappedLongValue); long longValue = fromString(Long.TYPE, value); assertEquals(1, longValue); Double wrappedDoubleValue = fromString(Double.class, value); assertEquals(Double.valueOf(1), wrappedDoubleValue); double doubleValue = fromString(Double.TYPE, value); assertEquals(1, doubleValue, 0); Float wrappedFloatValue = fromString(Float.class, value); assertEquals(Float.valueOf(1), wrappedFloatValue); float floatValue = fromString(Float.TYPE, value); assertEquals(1, floatValue, 0); Short shortValue = fromString(Short.class, value); assertEquals(Short.valueOf(value), shortValue); Byte byteValue = fromString(Byte.class, value); assertEquals(Byte.valueOf(value), byteValue); value = "true"; assertTrue(fromString(Boolean.class, value)); assertTrue(fromString(Boolean.TYPE, value)); char charValue = fromString(Character.TYPE, "a"); assertEquals('a', charValue); Character characterValue = fromString(Character.class, "a"); assertEquals(Character.valueOf('a'), characterValue); BigDecimal bigDecimal = fromString(BigDecimal.class, "2"); assertEquals(new BigDecimal("2"), bigDecimal); BigInteger bigInteger = fromString(BigInteger.class, "2"); assertEquals(new BigInteger("2"), bigInteger); String stringValue = fromString(String.class, value); assertEquals(stringValue, value); }