/** * Return the default converter if no converter is found for the given sourceType/targetType pair. * <p>Returns a NO_OP Converter if the source type is assignable to the target type. * Returns {@code null} otherwise, indicating no suitable converter could be found. * @param sourceType the source type to convert from * @param targetType the target type to convert to * @return the default generic converter that will perform the conversion */ @Nullable protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { return (sourceType.isAssignableTo(targetType) ? NO_OP_CONVERTER : null); }
private boolean isNestedAssignable(@Nullable TypeDescriptor nestedTypeDescriptor, @Nullable TypeDescriptor otherNestedTypeDescriptor) { return (nestedTypeDescriptor == null || otherNestedTypeDescriptor == null || nestedTypeDescriptor.isAssignableTo(otherNestedTypeDescriptor)); }
/** * Return the default converter if no converter is found for the given sourceType/targetType pair. * <p>Returns a NO_OP Converter if the source type is assignable to the target type. * Returns {@code null} otherwise, indicating no suitable converter could be found. * @param sourceType the source type to convert from * @param targetType the target type to convert to * @return the default generic converter that will perform the conversion */ @Nullable protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { return (sourceType.isAssignableTo(targetType) ? NO_OP_CONVERTER : null); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.isAssignableTo(STREAM_TYPE)) { return convertFromStream((Stream<?>) source, sourceType, targetType); } if (targetType.isAssignableTo(STREAM_TYPE)) { return convertToStream(source, sourceType, targetType); } // Should not happen throw new IllegalStateException("Unexpected source/target types"); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE); if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) { return (byteBufferTarget || matchesFromByteBuffer(targetType)); } return (byteBufferTarget && matchesToByteBuffer(sourceType)); }
@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); }
private boolean matchesFromByteBuffer(TypeDescriptor targetType) { return (targetType.isAssignableTo(BYTE_ARRAY_TYPE) || this.conversionService.canConvert(BYTE_ARRAY_TYPE, targetType)); }
private boolean matchesToByteBuffer(TypeDescriptor sourceType) { return (sourceType.isAssignableTo(BYTE_ARRAY_TYPE) || this.conversionService.canConvert(sourceType, BYTE_ARRAY_TYPE)); }
private boolean isNestedAssignable(@Nullable TypeDescriptor nestedTypeDescriptor, @Nullable TypeDescriptor otherNestedTypeDescriptor) { return (nestedTypeDescriptor == null || otherNestedTypeDescriptor == null || nestedTypeDescriptor.isAssignableTo(otherNestedTypeDescriptor)); }
@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) { boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE); if (source instanceof ByteBuffer) { ByteBuffer buffer = (ByteBuffer) source; return (byteBufferTarget ? buffer.duplicate() : convertFromByteBuffer(buffer, targetType)); } if (byteBufferTarget) { return convertToByteBuffer(source, sourceType); } // Should not happen throw new IllegalStateException("Unexpected source/target types"); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } if (sourceType.isAssignableTo(targetType)) { return source; } if (Array.getLength(source) == 0) { return null; } Object firstElement = Array.get(source, 0); return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } if (sourceType.isAssignableTo(targetType)) { return source; } Collection<?> sourceCollection = (Collection<?>) source; if (sourceCollection.isEmpty()) { return null; } Object firstElement = sourceCollection.iterator().next(); return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); }
@Nullable private Object handleConverterNotFound( @Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { assertNotPrimitiveTargetType(sourceType, targetType); return null; } if ((sourceType == null || sourceType.isAssignableTo(targetType)) && targetType.getObjectType().isInstance(source)) { return source; } throw new ConverterNotFoundException(sourceType, targetType); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.isAssignableTo(STREAM_TYPE)) { return matchesFromStream(sourceType.getElementTypeDescriptor(), targetType); } if (targetType.isAssignableTo(STREAM_TYPE)) { return matchesToStream(targetType.getElementTypeDescriptor(), sourceType); } return false; }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { String text = (String) source; if (!StringUtils.hasText(text)) { return null; } Object result; try { result = this.parser.parse(text, LocaleContextHolder.getLocale()); } catch (IllegalArgumentException ex) { throw ex; } catch (Throwable ex) { throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex); } TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass()); if (!resultType.isAssignableTo(targetType)) { result = this.conversionService.convert(result, resultType, targetType); } return result; }
@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()); }
@Test public void isAssignableElementTypes() throws Exception { assertTrue(new TypeDescriptor(getClass().getField("listField")).isAssignableTo(new TypeDescriptor(getClass().getField("listField")))); assertTrue(new TypeDescriptor(getClass().getField("notGenericList")).isAssignableTo(new TypeDescriptor(getClass().getField("listField")))); assertTrue(new TypeDescriptor(getClass().getField("listField")).isAssignableTo(new TypeDescriptor(getClass().getField("notGenericList")))); assertFalse(new TypeDescriptor(getClass().getField("isAssignableElementTypes")).isAssignableTo(new TypeDescriptor(getClass().getField("listField")))); assertTrue(TypeDescriptor.valueOf(List.class).isAssignableTo(new TypeDescriptor(getClass().getField("listField")))); }
@Test public void isAssignableMapKeyValueTypes() throws Exception { assertTrue(new TypeDescriptor(getClass().getField("mapField")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField")))); assertTrue(new TypeDescriptor(getClass().getField("notGenericMap")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField")))); assertTrue(new TypeDescriptor(getClass().getField("mapField")).isAssignableTo(new TypeDescriptor(getClass().getField("notGenericMap")))); assertFalse(new TypeDescriptor(getClass().getField("isAssignableMapKeyValueTypes")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField")))); assertTrue(TypeDescriptor.valueOf(Map.class).isAssignableTo(new TypeDescriptor(getClass().getField("mapField")))); }
@Test public void isAssignableTypes() { assertTrue(TypeDescriptor.valueOf(Integer.class).isAssignableTo(TypeDescriptor.valueOf(Number.class))); assertFalse(TypeDescriptor.valueOf(Number.class).isAssignableTo(TypeDescriptor.valueOf(Integer.class))); assertFalse(TypeDescriptor.valueOf(String.class).isAssignableTo(TypeDescriptor.valueOf(String[].class))); }