org.springframework.core.convert

Interface ConversionService

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method and Description
      boolean canConvert(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
      Return true if objects of sourceType can be converted to the targetType.
      boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
      Return true if objects of sourceType can be converted to the targetType.
      <T> T convert(java.lang.Object source, java.lang.Class<T> targetType)
      Convert the given source to the specified targetType.
      java.lang.Object convert(java.lang.Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
      Convert the given source to the specified targetType.
    • Method Detail

      • canConvert

        boolean canConvert(@Nullable
                           java.lang.Class<?> sourceType,
                           java.lang.Class<?> targetType)
        Return true if objects of sourceType can be converted to the targetType.

        If this method returns true, it means convert(Object, Class) is capable of converting an instance of sourceType to targetType.

        Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return true even though a convert invocation may still generate a ConversionException if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.

        Parameters:
        sourceType - the source type to convert from (may be null if source is null)
        targetType - the target type to convert to (required)
        Returns:
        true if a conversion can be performed, false if not
        Throws:
        java.lang.IllegalArgumentException - if targetType is null
      • canConvert

        boolean canConvert(@Nullable
                           TypeDescriptor sourceType,
                           TypeDescriptor targetType)
        Return true if objects of sourceType can be converted to the targetType. The TypeDescriptors provide additional context about the source and target locations where conversion would occur, often object fields or property locations.

        If this method returns true, it means convert(Object, TypeDescriptor, TypeDescriptor) is capable of converting an instance of sourceType to targetType.

        Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return true even though a convert invocation may still generate a ConversionException if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.

        Parameters:
        sourceType - context about the source type to convert from (may be null if source is null)
        targetType - context about the target type to convert to (required)
        Returns:
        true if a conversion can be performed between the source and target types, false if not
        Throws:
        java.lang.IllegalArgumentException - if targetType is null
      • convert

        @Nullable
        <T> T convert(@Nullable
                                java.lang.Object source,
                                java.lang.Class<T> targetType)
        Convert the given source to the specified targetType.
        Parameters:
        source - the source object to convert (may be null)
        targetType - the target type to convert to (required)
        Returns:
        the converted object, an instance of targetType
        Throws:
        ConversionException - if a conversion exception occurred
        java.lang.IllegalArgumentException - if targetType is null
      • convert

        @Nullable
        java.lang.Object convert(@Nullable
                                           java.lang.Object source,
                                           @Nullable
                                           TypeDescriptor sourceType,
                                           TypeDescriptor targetType)
        Convert the given source to the specified targetType. The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations.
        Parameters:
        source - the source object to convert (may be null)
        sourceType - context about the source type to convert from (may be null if source is null)
        targetType - context about the target type to convert to (required)
        Returns:
        the converted object, an instance of targetType
        Throws:
        ConversionException - if a conversion exception occurred
        java.lang.IllegalArgumentException - if targetType is null, or sourceType is null but source is not null