StringToNumberConverter extends AbstractConverter { @Override public boolean canHandle(Object source, TypeToken<?> targetTypeToken) { if (targetTypeToken.isPrimitive()) { targetTypeToken = PrimitiveTypeUtils.toWrapperTypeToken(targetTypeToken); } if (!targetTypeToken.isSubtypeOf(Number.class)) return false; if (!(source instanceof String)) return false; try { NumberUtils.parseNumber((String) source, (Class) targetTypeToken.getType()); return true; } catch (IllegalArgumentException e){ return false; } }  @Override Object convert(Object source, TypeToken<?> targetTypeToken); @Override boolean canHandle(Object source, TypeToken<?> targetTypeToken);  }
@Test public void testCanHandle() { assertTrue(converter.canHandle("1", TypeToken.of(Integer.class))); assertTrue(converter.canHandle("1", TypeToken.of(Float.class))); assertTrue(converter.canHandle("1", TypeToken.of(Short.class))); assertTrue(converter.canHandle("1", TypeToken.of(Double.class))); assertTrue(converter.canHandle("1", TypeToken.of(Long.class))); assertTrue(converter.canHandle("1", TypeToken.of(Integer.class))); assertTrue(converter.canHandle("1", TypeToken.of(int.class))); assertTrue(converter.canHandle("1", TypeToken.of(short.class))); assertTrue(converter.canHandle("1", TypeToken.of(double.class))); assertTrue(converter.canHandle("1", TypeToken.of(long.class))); assertFalse(converter.canHandle("abc", TypeToken.of(Integer.class))); assertFalse(converter.canHandle("1", TypeToken.of(String.class))); }