@Test(expected = IllegalArgumentException.class) public void testConvertToStringWithWrongInstanceArg() throws Exception { new TypeConverter<Integer>(Integer.class).convertToString("hello"); }
@SuppressWarnings("unchecked") public String convertToString(Object obj) throws IOException { if(obj == null) { return null; } if(!clazz.isInstance(obj)) { throw new IllegalArgumentException(String.format("obj argument is not of type '%s' was '%s'.", clazz.getName(), obj.getClass().getName())); } return mapper.writeValueAsString(obj); }
TypeConverter implements ObjectConverter { @SuppressWarnings("unchecked") public String convertToString(Object obj) throws IOException { if(obj == null) { return null; } if(!clazz.isInstance(obj)) { throw new IllegalArgumentException(String.format("obj argument is not of type '%s' was '%s'.", clazz.getName(), obj.getClass().getName())); } return mapper.writeValueAsString(obj); } }
TypeConverter implements ObjectConverter { @SuppressWarnings("unchecked") public String convertToString(Object obj) throws IOException { if(obj == null) { return null; } if(!clazz.isInstance(obj)) { throw new IllegalArgumentException(String.format("obj argument is not of type '%s' was '%s'.", clazz.getName(), obj.getClass().getName())); } return mapper.writeValueAsString(obj); } TypeConverter(Class<T> clazz); }
TypeConverter implements ObjectConverter { @SuppressWarnings("unchecked") public String convertToString(Object obj) throws IOException { if(obj == null) { return null; } if(!clazz.isInstance(obj)) { throw new IllegalArgumentException(String.format("obj argument is not of type '%s' was '%s'.", clazz.getName(), obj.getClass().getName())); } return mapper.writeValueAsString(obj); } TypeConverter(Class<T> clazz); @SuppressWarnings("unchecked") String convertToString(Object obj); Object convertToObject(String str); }
TypeConverter implements ObjectConverter { @SuppressWarnings("unchecked") public String convertToString(Object obj) throws IOException { if(obj == null) { return null; } if(!clazz.isInstance(obj)) { throw new IllegalArgumentException(String.format("obj argument is not of type '%s' was '%s'.", clazz.getName(), obj.getClass().getName())); } return mapper.writeValueAsString(obj); } TypeConverter(Class<T> clazz); @SuppressWarnings("unchecked") String convertToString(Object obj); Object convertToObject(String str);  }