Class StringConverter

  • All Implemented Interfaces:
    Converter

    public final class StringConverter
    extends AbstractConverter
    Converter implementation that converts an incoming object into a java.lang.String object.

    Note that ConvertUtils really is designed to do string->object conversions, and offers very little support for object->string conversions. The ConvertUtils/ConvertUtilsBean methods only select a converter to apply based upon the target type being converted to, and generally assume that the input is a string (by calling its toString method if needed).

    This class is therefore just a dummy converter that converts its input into a string by calling the input object's toString method and returning that value.

    It is possible to replace this converter with something that has a big if/else statement that selects behaviour based on the real type of the object being converted (or possibly has a map of converters, and looks them up based on the class of the input object). However this is not part of the existing ConvertUtils framework.

    Since:
    1.3
    Version:
    $Id: StringConverter.java 1540518 2013-11-10 19:04:04Z oheger $
    • Constructor Detail

      • StringConverter

        public StringConverter()
        Construct a java.lang.String Converter that throws a ConversionException if an error occurs.
      • StringConverter

        public StringConverter​(java.lang.Object defaultValue)
        Construct a java.lang.String Converter that returns a default value if an error occurs.
        Parameters:
        defaultValue - The default value to be returned if the value to be converted is missing or an error occurs converting the value.
    • Method Detail

      • getDefaultType

        protected java.lang.Class<?> getDefaultType()
        Return the default type this Converter handles.
        Specified by:
        getDefaultType in class AbstractConverter
        Returns:
        The default type this Converter handles.
        Since:
        1.8.0
      • convertToType

        protected <T> T convertToType​(java.lang.Class<T> type,
                                      java.lang.Object value)
                               throws java.lang.Throwable
        Convert the specified input object into an output object of the specified type.
        Specified by:
        convertToType in class AbstractConverter
        Type Parameters:
        T - Target type of the conversion.
        Parameters:
        type - Data type to which this value should be converted.
        value - The input value to be converted.
        Returns:
        The converted value.
        Throws:
        java.lang.Throwable - if an error occurs converting to the specified type
        Since:
        1.8.0