Source:
public static <T> T stringToPrimitive(String value, Class<T> targetType) { assert Primitives.allPrimitiveTypes().contains(targetType) || Primitives.allWrapperTypes().contains(targetType) : "targetType="+targetType; value = value.trim(); if (targetType == Character.class || targetType == char.class) { if (value.length() == 1) { return (T) (Character) value.charAt(0); } else if (value.length() != 1) { throw new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); } } if (targetType == Boolean.class || targetType == boolean.class) { if ("true".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("false".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("yes".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("no".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("t".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("f".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("y".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("n".equalsIgnoreCase(value)) return (T) Boolean.FALSE; throw new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); } Class<?> wrappedType; if (Primitives.allPrimitiveTypes().contains(targetType)) { wrappedType = Primitives.wrap(targetType); } else { wrappedType = targetType; } try { return (T) wrappedType.getMethod("valueOf", String.class).invoke(null, value); } catch (Exception e) { ClassCoercionException tothrow = new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); tothrow.initCause(e); throw tothrow; } }

Target:
public static <T> T stringToPrimitive(String value, Class<T> targetType) { assert Primitives.allPrimitiveTypes().contains(targetType) || Primitives.allWrapperTypes().contains(targetType) : "targetType="+targetType; if (targetType == Character.class || targetType == char.class) { if (value.length() == 1) { return (T) (Character) value.charAt(0); } else if (value.length() != 1) { throw new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); } } value = value.trim(); if (targetType == Boolean.class || targetType == boolean.class) { if ("true".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("false".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("yes".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("no".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("t".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("f".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("y".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("n".equalsIgnoreCase(value)) return (T) Boolean.FALSE; throw new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); } Class<?> wrappedType; if (Primitives.allPrimitiveTypes().contains(targetType)) { wrappedType = Primitives.wrap(targetType); } else { wrappedType = targetType; } try { return (T) wrappedType.getMethod("valueOf", String.class).invoke(null, value); } catch (Exception e) { ClassCoercionException tothrow = new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+" ("+value+"): adapting failed"); tothrow.initCause(e); throw tothrow; } }