@Test public void convertListOfListToString() { List<String> list1 = Arrays.asList("Foo", "Bar"); List<String> list2 = Arrays.asList("Baz", "Boop"); List<List<String>> list = Arrays.asList(list1, list2); assertTrue(conversionService.canConvert(list.getClass(), String.class)); String result = conversionService.convert(list, String.class); assertEquals("Foo,Bar,Baz,Boop", result); }
@Test public void convertObjectToStringNotSupported() { assertFalse(conversionService.canConvert(TestEntity.class, String.class)); }
@Test public void convertListOfStringToString() { List<String> list = Arrays.asList("Foo", "Bar"); assertTrue(conversionService.canConvert(list.getClass(), String.class)); String result = conversionService.convert(list, String.class); assertEquals("Foo,Bar", result); }
@Test public void convertListOfNonStringifiable() { List<Object> list = Arrays.asList(new TestEntity(1L), new TestEntity(2L)); assertTrue(conversionService.canConvert(list.getClass(), String.class)); try { conversionService.convert(list, String.class); } catch (ConversionFailedException ex) { assertTrue(ex.getMessage().contains(list.getClass().getName())); assertTrue(ex.getCause() instanceof ConverterNotFoundException); assertTrue(ex.getCause().getMessage().contains(TestEntity.class.getName())); } }
/** * Return the configuration map as a {@link Properties}. * @return the properties. */ public Properties asProperties() { if (this.properties == null) { Properties props = new Properties(); this.configs.forEach((k, v) -> { String value; if (this.conversionService.canConvert(v.getClass(), String.class)) { value = this.conversionService.convert(v, String.class); } else { value = v.toString(); } props.setProperty(k, value); }); this.properties = props; } return this.properties; }
private Object suggestPrameterValue(Type type, Class<?> cls, String parameter) throws JsonParseException, JsonMappingException, IOException { PrimitiveType fromType = PrimitiveType.fromType(type); if (null != fromType) { DefaultConversionService service = new DefaultConversionService(); boolean actual = service.canConvert(String.class, cls); if (actual) { return service.convert(parameter, cls); } } else { if (null == parameter) return null; try { return Json.mapper().readValue(parameter, cls); } catch (Exception e) { throw new IllegalArgumentException("The parameter value [" + parameter + "] should be json of [" + cls.getName() + "] Type.", e); } } try { return Class.forName(cls.getName()).newInstance(); } catch (Exception e) { e.printStackTrace(); } return null; }
@Override public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { // fix EnumToString conversions for Enums implemented in Groovy // see org.springframework.core.convert.support.EnumToStringConverter.match method if (targetType != null && targetType.getType() == String.class && sourceType != null && (sourceType.getType() == GroovyObject.class || sourceType.getType() == Comparable.class || sourceType.getType() == Serializable.class)) { return false; } boolean reply = super.canConvert(sourceType, targetType); if(!reply && sourceType != null && CharSequence.class.isAssignableFrom(sourceType.getType())) { reply = super.canConvert(TypeDescriptor.valueOf(String.class), targetType); } return reply; } }