@Override @Nullable public T convert(S source) { return this.conversionService.convert(source, this.targetType); } }
@Nullable protected String formatUriValue(@Nullable ConversionService cs, @Nullable TypeDescriptor sourceType, Object value) { if (value instanceof String) { return (String) value; } else if (cs != null) { return (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR); } else { return value.toString(); } }
@Override protected Long getContentLength(Object obj, @Nullable MediaType contentType) { String value = this.conversionService.convert(obj, String.class); if (value == null) { return 0L; } return this.stringHttpMessageConverter.getContentLength(value, contentType); }
@Nullable private Object convertKey(Object sourceKey, TypeDescriptor sourceType, @Nullable TypeDescriptor targetType) { if (targetType == null) { return sourceKey; } return this.conversionService.convert(sourceKey, sourceType.getMapKeyTypeDescriptor(sourceKey), targetType); }
@Nullable private Object convertValue(Object sourceValue, TypeDescriptor sourceType, @Nullable TypeDescriptor targetType) { if (targetType == null) { return sourceValue; } return this.conversionService.convert(sourceValue, sourceType.getMapValueTypeDescriptor(sourceValue), targetType); }
@Override public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) throws EvaluationException { return this.service.convert(value, sourceType, targetType); } }
@Nullable private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) { byte[] bytes = new byte[source.remaining()]; source.get(bytes); if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) { return bytes; } return this.conversionService.convert(bytes, BYTE_ARRAY_TYPE, targetType); }
@Override protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException { String value = this.conversionService.convert(obj, String.class); if (value != null) { this.stringHttpMessageConverter.writeInternal(value, outputMessage); } }
private Object convertToByteBuffer(@Nullable Object source, TypeDescriptor sourceType) { byte[] bytes = (byte[]) (source instanceof byte[] ? source : this.conversionService.convert(source, sourceType, BYTE_ARRAY_TYPE)); if (bytes == null) { return ByteBuffer.wrap(new byte[0]); } ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length); byteBuffer.put(bytes); // Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since // otherwise the overridden ByteBuffer-returning rewind method would be chosen // which isn't available on JDK 8. return ((Buffer) byteBuffer).rewind(); }
@Nullable private Object convertFromStream(@Nullable Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) { List<Object> content = (source != null ? source.collect(Collectors.<Object>toList()) : Collections.emptyList()); TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor()); return this.conversionService.convert(content, listType, targetType); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); Assert.state(targetElementType != null, "No target element type"); Object target = Array.newInstance(targetElementType.getType(), 1); Object targetElement = this.conversionService.convert(source, sourceType, targetElementType); Array.set(target, 0, targetElement); return target; }
private Object convertToStream(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor streamType) { TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor()); List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection); if (target == null) { target = Collections.emptyList(); } return target.stream(); }
@Override @SuppressWarnings("unchecked") public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (!sourceType.isAssignableTo(this.printerObjectType)) { source = this.conversionService.convert(source, sourceType, this.printerObjectType); } if (source == null) { return ""; } return this.printer.print(source, LocaleContextHolder.getLocale()); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } Method finder = getFinder(targetType.getType()); Assert.state(finder != null, "No finder method"); Object id = this.conversionService.convert( source, sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0])); return ReflectionUtils.invokeMethod(finder, source, id); }
@Override @Nullable public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { try { return this.conversionService.convert(value, sourceType, targetType); } catch (ConversionException ex) { throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR, (sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")), targetType.toString()); } }
@Override @Nullable public String getAsText() { if (this.canConvertToString) { return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.valueOf(String.class)); } else { return null; } }
@Override public void setAsText(@Nullable String text) throws IllegalArgumentException { setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor)); }
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { String value = this.stringHttpMessageConverter.readInternal(String.class, inputMessage); Object result = this.conversionService.convert(value, clazz); if (result == null) { throw new HttpMessageNotReadableException( "Unexpected null conversion result for '" + value + "' to " + clazz, inputMessage); } return result; }
@Test public void withConversionService() { ConversionService conversionService = new DefaultConversionService(); assertTrue(conversionService.canConvert(String.class, MimeType.class)); MimeType mimeType = MimeType.valueOf("application/xml"); assertEquals(mimeType, conversionService.convert("application/xml", MimeType.class)); }
@Test public void testWithConversionService() { ConversionService conversionService = new DefaultConversionService(); assertTrue(conversionService.canConvert(String.class, MediaType.class)); MediaType mediaType = MediaType.parseMediaType("application/xml"); assertEquals(mediaType, conversionService.convert("application/xml", MediaType.class)); }