StringToNumberConverter extends AbstractConverter { @Override public Object convert(Object source, TypeToken<?> targetTypeToken) { if (targetTypeToken.isPrimitive()) { targetTypeToken = PrimitiveTypeUtils.toWrapperTypeToken(targetTypeToken); } if (canHandle(source, targetTypeToken)) { return NumberUtils.parseNumber((String) source, (Class) targetTypeToken.getType()); } else { throw new ConverterException("can not convert '" + source + "' to " + targetTypeToken.getRawType()); } }  @Override Object convert(Object source, TypeToken<?> targetTypeToken); @Override boolean canHandle(Object source, TypeToken<?> targetTypeToken);  }
@Test public void testConvert() { assertEquals(1, converter.convert("1", TypeToken.of(Integer.class))); assertEquals(1.1d, converter.convert("1.1", TypeToken.of(Double.class))); }
@Test(expected = ConverterException.class) public void testRaiseError() { converter.convert("abc", TypeToken.of(Integer.class)); }